Skip to content

Commit

Permalink
fix: more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsek committed Nov 20, 2024
1 parent 90ac5ad commit fb4c245
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 39 deletions.
4 changes: 2 additions & 2 deletions apps/common-app/src/examples/DrumMachine/DrumMachine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const defaultPreset = 'Empty';

function setupPlayer(audioCtx: AudioContext) {
const kick = new Kick(audioCtx);
const clap = new Clap(audioCtx);
// const clap = new Clap(audioCtx);
const hiHat = new HiHat(audioCtx);

const playNote = (name: InstrumentName, time: number) => {
Expand All @@ -31,7 +31,7 @@ function setupPlayer(audioCtx: AudioContext) {
kick.play(time);
break;
case 'clap':
clap.play(time);
// clap.play(time);
break;
case 'hi-hat':
hiHat.play(time);
Expand Down
8 changes: 4 additions & 4 deletions apps/common-app/src/examples/DrumMachine/usePlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ export default function usePlayer(options: PlayerOptions) {
playingInstruments.value = getPlayingInstruments();
}

return () => {
console.log('Closing audio context');
audioContext.close();
};
// return () => {
// console.log('Closing audio context');
// audioContext.close();
// };
// \/ Shared values are not necessary in deps array
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isPlaying, setup]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ AudioBufferSourceNode::AudioBufferSourceNode(BaseAudioContext *context)
: AudioScheduledSourceNode(context), loop_(false), bufferIndex_(0) {
numberOfInputs_ = 0;
buffer_ = std::shared_ptr<AudioBuffer>(nullptr);
debugName_ = "AudioBufferSourceNode";
isInitialized_ = true;
}

bool AudioBufferSourceNode::getLoop() const {
Expand Down
12 changes: 6 additions & 6 deletions packages/react-native-audio-api/common/cpp/core/AudioBus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ void AudioBus::copy(const AudioBus *source, int sourceStart, int destinationStar
return;
}

// if (source->getNumberOfChannels() == getNumberOfChannels()) {
// for (int i = 0; i < getNumberOfChannels(); i += 1) {
// getChannel(i)->copy(source->getChannel(i), sourceStart, destinationStart, length);
// }
if (source->getNumberOfChannels() == getNumberOfChannels()) {
for (int i = 0; i < getNumberOfChannels(); i += 1) {
getChannel(i)->copy(source->getChannel(i), sourceStart, destinationStart, length);
}

// return;
// }
return;
}

// zero + sum is equivalent to copy, but takes care of up/down-mixing.
zero(destinationStart, length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ AudioDestinationNode::AudioDestinationNode(BaseAudioContext *context)
numberOfOutputs_ = 0;
numberOfInputs_ = INT_MAX;
channelCountMode_ = ChannelCountMode::EXPLICIT;
debugName_ = "AudioDestinationNode";
isInitialized_ = true;
}

std::size_t AudioDestinationNode::getCurrentSampleFrame() const {
Expand All @@ -22,6 +24,10 @@ double AudioDestinationNode::getCurrentTime() const {
}

void AudioDestinationNode::renderAudio(AudioBus *destinationBus, int32_t numFrames) {
if (!isInitialized_) {
return;
}

destinationBus->zero();

if (!numFrames) {
Expand All @@ -30,7 +36,7 @@ void AudioDestinationNode::renderAudio(AudioBus *destinationBus, int32_t numFram

AudioBus* processedBus = processAudio(destinationBus, numFrames);

if (processedBus != destinationBus) {
if (processedBus && processedBus != destinationBus) {
destinationBus->copy(processedBus);
}

Expand Down
12 changes: 10 additions & 2 deletions packages/react-native-audio-api/common/cpp/core/AudioNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ AudioNode::AudioNode(BaseAudioContext *context) : context_(context) {
}

AudioNode::~AudioNode() {
isInitialized_ = false;
cleanup();
}

Expand Down Expand Up @@ -76,6 +77,11 @@ std::string AudioNode::toString(ChannelInterpretation interpretation) {
}

AudioBus* AudioNode::processAudio(AudioBus* outputBus, int framesToProcess) {
debugName_;
if (!isInitialized_) {
return outputBus;
}

std::size_t currentSampleFrame = context_->getCurrentSampleFrame();

// check if the node has already been processed for this rendering quantum
Expand Down Expand Up @@ -114,10 +120,12 @@ AudioBus* AudioNode::processAudio(AudioBus* outputBus, int framesToProcess) {
// resulting in one less summing operation.q
if (it == inputNodes_.begin()) {
it->get()->processAudio(processingBus, framesToProcess);
} else {
} else if (it->get()) {
// Enforce the summing to be done using the internal bus.
AudioBus* inputBus = it->get()->processAudio(0, framesToProcess);
processingBus->sum(inputBus);
if (inputBus) {
processingBus->sum(inputBus);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/react-native-audio-api/common/cpp/core/AudioNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class AudioNode : public std::enable_shared_from_this<AudioNode> {
int numberOfInputs_ = 1;
int numberOfOutputs_ = 1;
int channelCount_ = CHANNEL_COUNT;
std::string debugName_;
bool isInitialized_ = false;

std::size_t lastRenderedFrame_ { SIZE_MAX };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ BiquadFilterNode::BiquadFilterNode(BaseAudioContext *context)
gainParam_ = std::make_shared<AudioParam>(
context, 0.0, MIN_FILTER_GAIN, MAX_FILTER_GAIN);
type_ = FilterType::LOWPASS;
debugName_ = "BiquadFilterNode";
isInitialized_ = true;
}

std::string BiquadFilterNode::getType() const {
Expand Down Expand Up @@ -354,35 +356,34 @@ void BiquadFilterNode::applyFilter() {
}

void BiquadFilterNode::processNode(AudioBus* processingBus, int framesToProcess) {
printf("BiquadFilterNode::processNode\n");
resetCoefficients();
applyFilter();

float x1 = x1_;
float x2 = x2_;
float y1 = y1_;
float y2 = y2_;

float b0 = b0_;
float b1 = b1_;
float b2 = b2_;
float a1 = a1_;
float a2 = a2_;

for (int i = 0; i < processingBus->getNumberOfChannels(); i += 1) {
AudioArray* inputChannel = processingBus->getChannel(i);

// Reset the coefficients for each channel
float x1 = x1_;
float x2 = x2_;
float y1 = y1_;
float y2 = y2_;

float b0 = b0_;
float b1 = b1_;
float b2 = b2_;
float a1 = a1_;
float a2 = a2_;

for (int f = 0; i < framesToProcess; f += 1) {
float input = (*inputChannel)[f];
float output = b0 * input + b1 * x1 + b2 * x2 - a1 * y1 - a2 * y2;

(*inputChannel)[f] = output;

x2 = x1;
x1 = input;
y2 = y1;
y1 = output;
float input = (*processingBus->getChannel(0))[i];
float output = b0 * input + b1 * x1 + b2 * x2 - a1 * y1 - a2 * y2;

for (int j = 0; j < channelCount_; j += 1) {
(*processingBus->getChannel(j))[i] = output;
}


x2 = x1;
x1 = input;
y2 = y1;
y1 = output;
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/react-native-audio-api/common/cpp/core/GainNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace audioapi {

GainNode::GainNode(BaseAudioContext *context) : AudioNode(context) {
gainParam_ = std::make_shared<AudioParam>(context, 1.0, -MAX_GAIN, MAX_GAIN);
debugName_ = "GainNode";
isInitialized_ = true;
}

std::shared_ptr<AudioParam> GainNode::getGainParam() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ OscillatorNode::OscillatorNode(BaseAudioContext *context)
context, 444.0, -NYQUIST_FREQUENCY, NYQUIST_FREQUENCY);
detuneParam_ =
std::make_shared<AudioParam>(context, 0.0, -MAX_DETUNE, MAX_DETUNE);
debugName_ = "OscillatorNode";
isInitialized_ = true;
}

std::shared_ptr<AudioParam> OscillatorNode::getFrequencyParam() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ StereoPannerNode::StereoPannerNode(BaseAudioContext *context)
: AudioNode(context) {
channelCountMode_ = ChannelCountMode::CLAMPED_MAX;
panParam_ = std::make_shared<AudioParam>(context, 0.0, -MAX_PAN, MAX_PAN);
debugName_ = "StereoPannerNode";
isInitialized_ = true;
}

std::shared_ptr<AudioParam> StereoPannerNode::getPanParam() const {
Expand Down

0 comments on commit fb4c245

Please sign in to comment.