Skip to content

Commit

Permalink
fix: hi-hat sound
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsek committed Nov 25, 2024
1 parent cc1ad11 commit c997218
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 49 deletions.
8 changes: 4 additions & 4 deletions apps/common-app/src/examples/DrumMachine/DrumMachine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ 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) => {
switch (name) {
case 'kick':
kick.play(time);
break;
// case 'clap':
// clap.play(time);
// break;
case 'clap':
clap.play(time);
break;
case 'hi-hat':
hiHat.play(time);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Clap implements SoundEngine {
}

createNoiseBuffer() {
const bufferSize = this.audioContext.sampleRate / 10;
const bufferSize = this.audioContext.sampleRate / 5;
const buffer = this.audioContext.createBuffer(
1,
bufferSize,
Expand Down
19 changes: 9 additions & 10 deletions apps/common-app/src/examples/SharedUtils/soundEngines/HiHat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,25 @@ class HiHat implements SoundEngine {

const bandpassFilter = this.audioContext.createBiquadFilter();
const highpassFilter = this.audioContext.createBiquadFilter();
// const gain = this.audioContext.createGain();
const gain = this.audioContext.createGain();

bandpassFilter.type = 'bandpass';
bandpassFilter.frequency.value = this.bandpassFilterFrequency;

highpassFilter.type = 'highpass';
highpassFilter.frequency.value = this.highpassFilterFrequency;

// gain.gain.setValueAtTime(0.0001, time);
// gain.gain.exponentialRampToValueAtTime(this.volume, time + 0.02);
// gain.gain.exponentialRampToValueAtTime(this.volume * 0.33, time + 0.03);
// gain.gain.exponentialRampToValueAtTime(this.volume * 0.0001, time + 0.3);
// gain.gain.setValueAtTime(0, time + 0.3 + 0.001);
gain.gain.setValueAtTime(0.0001, time);
gain.gain.exponentialRampToValueAtTime(this.volume, time + 0.02);
gain.gain.exponentialRampToValueAtTime(this.volume * 0.33, time + 0.03);
gain.gain.exponentialRampToValueAtTime(this.volume * 0.0001, time + 0.3);
gain.gain.setValueAtTime(0, time + 0.3 + 0.001);

oscillator.connect(bandpassFilter);
// bandpassFilter.connect(highpassFilter);
// highpassFilter.connect(gain);
// gain.connect(this.audioContext.destination!);
bandpassFilter.connect(highpassFilter);
highpassFilter.connect(gain);
gain.connect(this.audioContext.destination!);

bandpassFilter.connect(this.audioContext.destination);
oscillator.start(time);
oscillator.stop(time + this.decay);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ AudioBufferSourceNode::AudioBufferSourceNode(BaseAudioContext *context)
: AudioScheduledSourceNode(context), loop_(false), bufferIndex_(0) {
numberOfInputs_ = 0;
buffer_ = std::shared_ptr<AudioBuffer>(nullptr);
isInitialized_ = true;
}

bool AudioBufferSourceNode::getLoop() const {
Expand Down Expand Up @@ -60,18 +59,21 @@ void AudioBufferSourceNode::processNode(AudioBus* processingBus, int framesToPro
// The buffer is longer than the number of frames to process.
// We have to keep track of where we are in the buffer.
if (framesToProcess < buffer_->getLength()) {
int framesToCopy = std::min(framesToProcess, buffer_->getLength() - bufferIndex_);
int processingBufferPosition = 0;
int framesToCopy = 0;

processingBus->copy(buffer_->bus_.get(), bufferIndex_, framesToCopy);
while (processingBufferPosition < framesToProcess)
{
framesToCopy = std::min(framesToProcess, buffer_->getLength() - bufferIndex_);

if (loop_) {
processingBus->copy(buffer_->bus_.get(), processingBufferPosition, bufferIndex_, framesToCopy);
processingBufferPosition += framesToCopy;
bufferIndex_ = (bufferIndex_ + framesToCopy) % buffer_->getLength();
return;
}

if (bufferIndex_ + framesToCopy == buffer_->getLength() - 1) {
playbackState_ = PlaybackState::FINISHED;
bufferIndex_ = 0;
if (!loop_ && processingBufferPosition >= framesToCopy) {
playbackState_ = PlaybackState::FINISHED;
bufferIndex_ = 0;
}
}

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ AudioDestinationNode::AudioDestinationNode(BaseAudioContext *context)
numberOfOutputs_ = 0;
numberOfInputs_ = INT_MAX;
channelCountMode_ = ChannelCountMode::EXPLICIT;
isInitialized_ = true;
}

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

void AudioDestinationNode::renderAudio(AudioBus *destinationBus, int32_t numFrames) {
printf("connected nodes: %d\n", inputNodes_.size());

if (!isInitialized_) {
return;
}

context_->getNodeManager()->preProcessGraph();

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

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

Expand Down Expand Up @@ -60,10 +59,6 @@ void AudioNode::disconnectNode(std::shared_ptr<AudioNode> &node) {
}
}

bool AudioNode::isInitialized() const {
return isInitialized_;
}

bool AudioNode::isEnabled() const {
return isEnabled_;
}
Expand Down Expand Up @@ -109,10 +104,6 @@ std::string AudioNode::toString(ChannelInterpretation interpretation) {
}

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

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

// check if the node has already been processed for this rendering quantum
Expand Down
4 changes: 0 additions & 4 deletions packages/react-native-audio-api/common/cpp/core/AudioNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ class AudioNode : public std::enable_shared_from_this<AudioNode> {
void connect(const std::shared_ptr<AudioNode> &node);
void disconnect(const std::shared_ptr<AudioNode> &node);

bool isInitialized() const;

bool isEnabled() const;
void enable();
void disable();
Expand All @@ -44,8 +42,6 @@ class AudioNode : public std::enable_shared_from_this<AudioNode> {
int numberOfOutputs_ = 1;
int numberOfEnabledInputNodes_ = 0;


bool isInitialized_ = false;
bool isEnabled_ = true;

std::size_t lastRenderedFrame_ { SIZE_MAX };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ void BiquadFilterNode::processNode(AudioBus* processingBus, int framesToProcess)
resetCoefficients();
applyFilter();

for (int c = 0; c < framesToProcess; c += 1) {
for (int c = 0; c < processingBus->getNumberOfChannels(); c += 1) {
float x1 = x1_;
float x2 = x2_;
float y1 = y1_;
Expand All @@ -368,7 +368,7 @@ void BiquadFilterNode::processNode(AudioBus* processingBus, int framesToProcess)
float a2 = a2_;

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

(*processingBus->getChannel(c))[i] = output;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace audioapi {

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

std::shared_ptr<AudioParam> GainNode::getGainParam() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ OscillatorNode::OscillatorNode(BaseAudioContext *context)
context, 444.0, -NYQUIST_FREQUENCY, NYQUIST_FREQUENCY);
detuneParam_ =
std::make_shared<AudioParam>(context, 0.0, -MAX_DETUNE, MAX_DETUNE);
isInitialized_ = true;
type_ = OscillatorType::SINE;
periodicWave_ = context_->getBasicWaveForm(type_);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ StereoPannerNode::StereoPannerNode(BaseAudioContext *context)
: AudioNode(context) {
channelCountMode_ = ChannelCountMode::CLAMPED_MAX;
panParam_ = std::make_shared<AudioParam>(context, 0.0, -MAX_PAN, MAX_PAN);
isInitialized_ = true;
}

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

0 comments on commit c997218

Please sign in to comment.