Skip to content

Commit c997218

Browse files
committed
fix: hi-hat sound
1 parent cc1ad11 commit c997218

File tree

11 files changed

+27
-49
lines changed

11 files changed

+27
-49
lines changed

apps/common-app/src/examples/DrumMachine/DrumMachine.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ const defaultPreset = 'Empty';
2222

2323
function setupPlayer(audioCtx: AudioContext) {
2424
const kick = new Kick(audioCtx);
25-
// const clap = new Clap(audioCtx);
25+
const clap = new Clap(audioCtx);
2626
const hiHat = new HiHat(audioCtx);
2727

2828
const playNote = (name: InstrumentName, time: number) => {
2929
switch (name) {
3030
case 'kick':
3131
kick.play(time);
3232
break;
33-
// case 'clap':
34-
// clap.play(time);
35-
// break;
33+
case 'clap':
34+
clap.play(time);
35+
break;
3636
case 'hi-hat':
3737
hiHat.play(time);
3838
break;

apps/common-app/src/examples/SharedUtils/soundEngines/Clap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Clap implements SoundEngine {
2020
}
2121

2222
createNoiseBuffer() {
23-
const bufferSize = this.audioContext.sampleRate / 10;
23+
const bufferSize = this.audioContext.sampleRate / 5;
2424
const buffer = this.audioContext.createBuffer(
2525
1,
2626
bufferSize,

apps/common-app/src/examples/SharedUtils/soundEngines/HiHat.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,25 @@ class HiHat implements SoundEngine {
2929

3030
const bandpassFilter = this.audioContext.createBiquadFilter();
3131
const highpassFilter = this.audioContext.createBiquadFilter();
32-
// const gain = this.audioContext.createGain();
32+
const gain = this.audioContext.createGain();
3333

3434
bandpassFilter.type = 'bandpass';
3535
bandpassFilter.frequency.value = this.bandpassFilterFrequency;
3636

3737
highpassFilter.type = 'highpass';
3838
highpassFilter.frequency.value = this.highpassFilterFrequency;
3939

40-
// gain.gain.setValueAtTime(0.0001, time);
41-
// gain.gain.exponentialRampToValueAtTime(this.volume, time + 0.02);
42-
// gain.gain.exponentialRampToValueAtTime(this.volume * 0.33, time + 0.03);
43-
// gain.gain.exponentialRampToValueAtTime(this.volume * 0.0001, time + 0.3);
44-
// gain.gain.setValueAtTime(0, time + 0.3 + 0.001);
40+
gain.gain.setValueAtTime(0.0001, time);
41+
gain.gain.exponentialRampToValueAtTime(this.volume, time + 0.02);
42+
gain.gain.exponentialRampToValueAtTime(this.volume * 0.33, time + 0.03);
43+
gain.gain.exponentialRampToValueAtTime(this.volume * 0.0001, time + 0.3);
44+
gain.gain.setValueAtTime(0, time + 0.3 + 0.001);
4545

4646
oscillator.connect(bandpassFilter);
47-
// bandpassFilter.connect(highpassFilter);
48-
// highpassFilter.connect(gain);
49-
// gain.connect(this.audioContext.destination!);
47+
bandpassFilter.connect(highpassFilter);
48+
highpassFilter.connect(gain);
49+
gain.connect(this.audioContext.destination!);
5050

51-
bandpassFilter.connect(this.audioContext.destination);
5251
oscillator.start(time);
5352
oscillator.stop(time + this.decay);
5453
});

packages/react-native-audio-api/common/cpp/core/AudioBufferSourceNode.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ AudioBufferSourceNode::AudioBufferSourceNode(BaseAudioContext *context)
99
: AudioScheduledSourceNode(context), loop_(false), bufferIndex_(0) {
1010
numberOfInputs_ = 0;
1111
buffer_ = std::shared_ptr<AudioBuffer>(nullptr);
12-
isInitialized_ = true;
1312
}
1413

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

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

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

72-
if (bufferIndex_ + framesToCopy == buffer_->getLength() - 1) {
73-
playbackState_ = PlaybackState::FINISHED;
74-
bufferIndex_ = 0;
73+
if (!loop_ && processingBufferPosition >= framesToCopy) {
74+
playbackState_ = PlaybackState::FINISHED;
75+
bufferIndex_ = 0;
76+
}
7577
}
7678

7779
return;

packages/react-native-audio-api/common/cpp/core/AudioDestinationNode.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ AudioDestinationNode::AudioDestinationNode(BaseAudioContext *context)
1212
numberOfOutputs_ = 0;
1313
numberOfInputs_ = INT_MAX;
1414
channelCountMode_ = ChannelCountMode::EXPLICIT;
15-
isInitialized_ = true;
1615
}
1716

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

2625
void AudioDestinationNode::renderAudio(AudioBus *destinationBus, int32_t numFrames) {
27-
printf("connected nodes: %d\n", inputNodes_.size());
28-
29-
if (!isInitialized_) {
30-
return;
31-
}
32-
3326
context_->getNodeManager()->preProcessGraph();
3427

3528
destinationBus->zero();

packages/react-native-audio-api/common/cpp/core/AudioNode.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ AudioNode::AudioNode(BaseAudioContext *context) : context_(context) {
1212
}
1313

1414
AudioNode::~AudioNode() {
15-
isInitialized_ = false;
1615
cleanup();
1716
}
1817

@@ -60,10 +59,6 @@ void AudioNode::disconnectNode(std::shared_ptr<AudioNode> &node) {
6059
}
6160
}
6261

63-
bool AudioNode::isInitialized() const {
64-
return isInitialized_;
65-
}
66-
6762
bool AudioNode::isEnabled() const {
6863
return isEnabled_;
6964
}
@@ -109,10 +104,6 @@ std::string AudioNode::toString(ChannelInterpretation interpretation) {
109104
}
110105

111106
AudioBus* AudioNode::processAudio(AudioBus* outputBus, int framesToProcess) {
112-
if (!isInitialized_) {
113-
return outputBus;
114-
}
115-
116107
std::size_t currentSampleFrame = context_->getCurrentSampleFrame();
117108

118109
// check if the node has already been processed for this rendering quantum

packages/react-native-audio-api/common/cpp/core/AudioNode.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ class AudioNode : public std::enable_shared_from_this<AudioNode> {
2525
void connect(const std::shared_ptr<AudioNode> &node);
2626
void disconnect(const std::shared_ptr<AudioNode> &node);
2727

28-
bool isInitialized() const;
29-
3028
bool isEnabled() const;
3129
void enable();
3230
void disable();
@@ -44,8 +42,6 @@ class AudioNode : public std::enable_shared_from_this<AudioNode> {
4442
int numberOfOutputs_ = 1;
4543
int numberOfEnabledInputNodes_ = 0;
4644

47-
48-
bool isInitialized_ = false;
4945
bool isEnabled_ = true;
5046

5147
std::size_t lastRenderedFrame_ { SIZE_MAX };

packages/react-native-audio-api/common/cpp/core/BiquadFilterNode.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ void BiquadFilterNode::processNode(AudioBus* processingBus, int framesToProcess)
355355
resetCoefficients();
356356
applyFilter();
357357

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

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

374374
(*processingBus->getChannel(c))[i] = output;

packages/react-native-audio-api/common/cpp/core/GainNode.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ namespace audioapi {
77

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

1312
std::shared_ptr<AudioParam> GainNode::getGainParam() const {

packages/react-native-audio-api/common/cpp/core/OscillatorNode.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ OscillatorNode::OscillatorNode(BaseAudioContext *context)
1111
context, 444.0, -NYQUIST_FREQUENCY, NYQUIST_FREQUENCY);
1212
detuneParam_ =
1313
std::make_shared<AudioParam>(context, 0.0, -MAX_DETUNE, MAX_DETUNE);
14-
isInitialized_ = true;
1514
type_ = OscillatorType::SINE;
1615
periodicWave_ = context_->getBasicWaveForm(type_);
1716
}

packages/react-native-audio-api/common/cpp/core/StereoPannerNode.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ StereoPannerNode::StereoPannerNode(BaseAudioContext *context)
1212
: AudioNode(context) {
1313
channelCountMode_ = ChannelCountMode::CLAMPED_MAX;
1414
panParam_ = std::make_shared<AudioParam>(context, 0.0, -MAX_PAN, MAX_PAN);
15-
isInitialized_ = true;
1615
}
1716

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

0 commit comments

Comments
 (0)