Skip to content

Commit

Permalink
fix: get back to previous scheduling impl
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsek committed Nov 19, 2024
1 parent fd4049e commit d4e948c
Show file tree
Hide file tree
Showing 18 changed files with 147 additions and 120 deletions.
1 change: 1 addition & 0 deletions apps/common-app/src/examples/DrumMachine/usePlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export default function usePlayer(options: PlayerOptions) {
}

return () => {
console.log('Closing audio context');
audioContext.close();
};
// \/ Shared values are not necessary in deps array
Expand Down
19 changes: 9 additions & 10 deletions apps/common-app/src/examples/Oscillator/Oscillator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ const Oscillator: FC = () => {
oscillatorRef.current.frequency.value = frequency;
oscillatorRef.current.detune.value = detune;
oscillatorRef.current.type = oscillatorType;

gainRef.current = audioContextRef.current.createGain();
gainRef.current.gain.value = gain;

panRef.current = audioContextRef.current.createStereoPanner();
panRef.current.pan.value = pan;

oscillatorRef.current.connect(gainRef.current);
gainRef.current.connect(panRef.current);
panRef.current.connect(audioContextRef.current.destination);
oscillatorRef.current.connect(audioContextRef.current.destination);

// gainRef.current = audioContextRef.current.createGain();
// gainRef.current.gain.value = gain;
// panRef.current = audioContextRef.current.createStereoPanner();
// panRef.current.pan.value = pan;
// oscillatorRef.current.connect(gainRef.current);
// gainRef.current.connect(panRef.current);
// panRef.current.connect(audioContextRef.current.destination);
};

const handleGainChange = (newValue: number) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ AudioPlayer::AudioPlayer(const std::function<void(AudioBus*, int)> &renderAudio)
->setDataCallback(this)
->openStream(mStream_);

mBus_ = std::make_unique<AudioBus>(getSampleRate(), getBufferSizeInFrames(), CHANNEL_COUNT);
mBus_ = std::make_shared<AudioBus>(getSampleRate(), getBufferSizeInFrames(), CHANNEL_COUNT);
}

int AudioPlayer::getSampleRate() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class AudioPlayer : public AudioStreamDataCallback {
private:
std::function<void(AudioBus*, int)> renderAudio_;
std::shared_ptr<AudioStream> mStream_;
std::unique_ptr<AudioBus> mBus_;
std::shared_ptr<AudioBus> mBus_;
};

} // namespace audioapi
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
namespace audioapi {

AudioArray::AudioArray(int size) : size_(size), data_(0) {
data_ = new float[size];
printf("AudioArray::AudioArray(%d)\n", size);
resize(size);
}

AudioArray::~AudioArray() {
Expand Down Expand Up @@ -48,15 +49,15 @@ void AudioArray::resize(int size) {
data_ = new float[size];
}

zero();
zero(0, size);
return;
}

delete[] data_;
size_ = size;
data_ = new float[size_];

zero();
zero(0, size_);
}

void AudioArray::scale(float value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
namespace audioapi {

AudioBuffer::AudioBuffer(int numberOfChannels, int length, int sampleRate) {
bus_ = std::make_unique<AudioBus>(sampleRate, length, numberOfChannels);
bus_->zero();
bus_ = std::make_shared<AudioBus>(sampleRate, length, numberOfChannels);
}

int AudioBuffer::getLength() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AudioBuffer : public std::enable_shared_from_this<AudioBuffer> {
int startInChannel);

private:
std::unique_ptr<AudioBus> bus_;
std::shared_ptr<AudioBus> bus_;

friend class AudioBufferSourceNode;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ void AudioBufferSourceNode::setBuffer(
// otherwise it will use the summing function taking care of up/down mixing.
void AudioBufferSourceNode::processNode(AudioBus* processingBus, int framesToProcess) {
double time = context_->getCurrentTime();
handlePlayback(time, framesToProcess);

// No audio data to fill, zero the output and return.
if (!isPlaying_ || !buffer_ || buffer_->getLength() == 0) {
Expand All @@ -48,7 +47,7 @@ void AudioBufferSourceNode::processNode(AudioBus* processingBus, int framesToPro

// Easiest case, the buffer is the same length as the number of frames to process, just copy the data.
if (framesToProcess == buffer_->getLength()) {
processingBus->copy(*buffer_->bus_.get());
processingBus->copy(buffer_->bus_.get());

if (!loop_) {
isPlaying_ = false;
Expand All @@ -62,7 +61,7 @@ void AudioBufferSourceNode::processNode(AudioBus* processingBus, int framesToPro
if (framesToProcess < buffer_->getLength()) {
int framesToCopy = std::min(framesToProcess, buffer_->getLength() - bufferIndex_);

processingBus->copy(*buffer_->bus_.get(), bufferIndex_, framesToCopy);
processingBus->copy(buffer_->bus_.get(), bufferIndex_, framesToCopy);

if (loop_) {
bufferIndex_ = (bufferIndex_ + framesToCopy) % buffer_->getLength();
Expand All @@ -80,7 +79,7 @@ void AudioBufferSourceNode::processNode(AudioBus* processingBus, int framesToPro
// processing bus is longer than the source buffer
if (!loop_) {
// If we don't loop the buffer, copy it once and zero the remaining processing bus frames.
processingBus->copy(*buffer_->bus_.get());
processingBus->copy(buffer_->bus_.get());
processingBus->zero(buffer_->getLength(), framesToProcess - buffer_->getLength());
isPlaying_ = false;
return;
Expand All @@ -96,20 +95,20 @@ void AudioBufferSourceNode::processNode(AudioBus* processingBus, int framesToPro
// Do we have some frames left in the buffer from the previous render quantum,
// if yes copy them over and reset the buffer position.
if (bufferIndex_ > 0) {
processingBus->copy(*buffer_->bus_.get(), 0, bufferIndex_);
processingBus->copy(buffer_->bus_.get(), 0, bufferIndex_);
processingBusPosition += bufferIndex_;
bufferIndex_ = 0;
}

// Copy the entire buffer n times to the processing bus.
while (processingBusPosition + bufferSize <= framesToProcess) {
processingBus->copy(*buffer_->bus_.get());
processingBus->copy(buffer_->bus_.get());
processingBusPosition += bufferSize;
}

// Fill in the remaining frames from the processing buffer and update buffer index for next render quantum.
if (remainingFrames > 0) {
processingBus->copy(*buffer_->bus_.get(), 0, processingBusPosition, remainingFrames);
processingBus->copy(buffer_->bus_.get(), 0, processingBusPosition, remainingFrames);
bufferIndex_ = remainingFrames;
}
}
Expand Down
Loading

0 comments on commit d4e948c

Please sign in to comment.