Skip to content

Commit

Permalink
Update RecordTemp data buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
FangCunWuChang committed Nov 25, 2024
1 parent 5a01dd7 commit 443303b
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/audioCore/graph/SourceRecordProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ SourceRecordProcessor::~SourceRecordProcessor() {}
void SourceRecordProcessor::prepareToPlay(
double sampleRate, int maximumExpectedSamplesPerBlock) {
this->setRateAndBufferSizeDetails(sampleRate, maximumExpectedSamplesPerBlock);
RecordTemp::getInstance()->setInputSampleRate(sampleRate);
}

void SourceRecordProcessor::processBlock(
Expand Down Expand Up @@ -43,3 +44,7 @@ double SourceRecordProcessor::getTailLengthSeconds() const {
}
return 0;
}

void SourceRecordProcessor::numChannelsChanged() {
RecordTemp::getInstance()->setInputChannelNum(this->getTotalNumInputChannels());
}
2 changes: 2 additions & 0 deletions src/audioCore/graph/SourceRecordProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class SourceRecordProcessor final : public juce::AudioProcessor {
MainGraph* const parent;
LimitedCall limitedCall;

void numChannelsChanged() override;

JUCE_DECLARE_WEAK_REFERENCEABLE(SourceRecordProcessor)
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SourceRecordProcessor)
};
88 changes: 87 additions & 1 deletion src/audioCore/misc/RecordTemp.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,94 @@
#include "RecordTemp.h"
#include "VMath.h"

#define AUDIO_BUFFER_MIN 48000 * 30

RecordTemp::RecordTemp() {
/** Init Audio Buffer */
this->clearAudio();
}

void RecordTemp::setInputSampleRate(double sampleRate) {
juce::GenericScopedLock locker(this->lock);
this->sampleRate = sampleRate;
this->clearAll();
}

void RecordTemp::setInputChannelNum(int channels) {
juce::GenericScopedLock locker(this->lock);
this->audioBuffer.setSize(channels,
this->audioBuffer.getNumSamples(), true, true, true);
}

void RecordTemp::recordData(double timeSec,
const juce::AudioBuffer<float>& buffer, const juce::MidiBuffer& midiMessages) {
/** TODO */
/** Lock */
juce::GenericScopedLock locker(this->lock);

/** Set Start Time */
if (timeSec < this->startTime) {
this->clearAll();
}
if (this->startTime < 0) {
this->startTime = timeSec;
}

/** Collect MIDI Message */
if (this->recordMIDI) {
for (auto i : midiMessages) {
auto message = i.getMessage();
message.setTimeStamp((timeSec - this->startTime) + message.getTimeStamp() / this->sampleRate);
this->midiCollector.addMessageToQueue(message);
}
}

/** Write Audio Buffer */
if (this->recordAudio) {
uint64_t startSample = (timeSec - this->startTime) * this->sampleRate;
uint64_t endSample = startSample + buffer.getNumSamples();
if (this->tryToEnsureAudioBufferSamplesAllocated(endSample)) {
int channelNum = std::min(buffer.getNumChannels(), this->audioBuffer.getNumChannels());
for (int i = 0; i < channelNum; i++) {
vMath::copyAudioData(this->audioBuffer, buffer,
(int)startSample, 0, i, i, buffer.getNumSamples());
}
}
}
}

void RecordTemp::clearAll() {
juce::GenericScopedLock locker(this->lock);
this->clearAudio();
this->clearMIDI();
this->startTime = -1;
}

void RecordTemp::clearMIDI() {
juce::GenericScopedLock locker(this->lock);
this->midiCollector.reset(sampleRate);
}

void RecordTemp::clearAudio() {
juce::GenericScopedLock locker(this->lock);
this->audioBuffer.setSize(
this->audioBuffer.getNumChannels(),
AUDIO_BUFFER_MIN, false, true, true);
}

bool RecordTemp::tryToEnsureAudioBufferSamplesAllocated(uint64_t sampleNum) {
while (this->audioBuffer.getNumSamples() < sampleNum) {
uint16_t num = (uint64_t)(this->audioBuffer.getNumSamples()) * 2;
if (num > INT_MAX) {
this->audioBuffer.setSize(this->audioBuffer.getNumChannels(),
INT_MAX, false, true, true);
break;
}

this->audioBuffer.setSize(this->audioBuffer.getNumChannels(),
(int)num, false, true, true);
}

return this->audioBuffer.getNumSamples() >= sampleNum;
}

RecordTemp* RecordTemp::getInstance() {
Expand Down
20 changes: 19 additions & 1 deletion src/audioCore/misc/RecordTemp.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,29 @@

class RecordTemp final : private juce::DeletedAtShutdown {
public:
RecordTemp() = default;
RecordTemp();

void setInputSampleRate(double sampleRate);
void setInputChannelNum(int channels);
void recordData(double timeSec,
const juce::AudioBuffer<float>& buffer, const juce::MidiBuffer& midiMessages);

void clearAll();
void clearMIDI();
void clearAudio();

private:
juce::CriticalSection lock;
double sampleRate = 0;

juce::MidiMessageCollector midiCollector;
juce::AudioSampleBuffer audioBuffer;
double startTime = -1;

bool recordMIDI = true, recordAudio = true;

bool tryToEnsureAudioBufferSamplesAllocated(uint64_t sampleNum);

public:
static RecordTemp* getInstance();
static void releaseInstance();
Expand Down

0 comments on commit 443303b

Please sign in to comment.