Skip to content

Commit 05fad42

Browse files
authored
fix: scheduled source node delayed start, better piano sounds, usePlayer timing (#249)
1 parent 4746fbf commit 05fad42

File tree

8 files changed

+16
-15
lines changed

8 files changed

+16
-15
lines changed

apps/common-app/src/examples/Piano/PianoNote.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class PianoNote {
1313

1414
private gain: GainNode | null = null;
1515
private bufferSource: AudioBufferSourceNode | null = null;
16+
private startedAt: number | null = null;
1617

1718
constructor(audioContext: AudioContext, key: Key) {
1819
this.audioContext = audioContext;
@@ -33,27 +34,27 @@ class PianoNote {
3334
this.bufferSource.playbackRate.value = playbackRate;
3435

3536
this.gain = this.audioContext.createGain();
36-
this.gain.gain.setValueAtTime(0.001, this.audioContext.currentTime);
37-
this.gain.gain.exponentialRampToValueAtTime(
38-
1,
39-
this.audioContext.currentTime + 0.01
40-
);
37+
this.gain.gain.setValueAtTime(1, this.audioContext.currentTime);
4138

4239
this.bufferSource.connect(this.gain);
4340
this.gain.connect(this.audioContext.destination);
4441

4542
this.bufferSource.start(tNow);
43+
this.startedAt = tNow;
4644
}
4745

4846
stop() {
4947
if (!this.bufferSource || !this.gain) {
5048
return;
5149
}
5250

53-
const tNow = this.audioContext.currentTime;
51+
const tNow = Math.max(
52+
this.audioContext.currentTime,
53+
(this.startedAt ?? 0) + 5.0
54+
);
5455

55-
this.gain.gain.exponentialRampToValueAtTime(0.0001, tNow);
56-
this.gain.gain.setValueAtTime(0, tNow + 0.01);
56+
this.gain.gain.exponentialRampToValueAtTime(0.0001, tNow + 0.08);
57+
this.gain.gain.setValueAtTime(0, tNow + 0.09);
5758
this.bufferSource.stop(tNow + 0.1);
5859

5960
this.bufferSource = null;

apps/common-app/src/utils/usePlayer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export default function usePlayer(options: PlayerOptions) {
7979
true
8080
);
8181

82-
playNote(r(patternsRef)[i].instrumentName, audioContext.currentTime);
82+
playNote(r(patternsRef)[i].instrumentName, nextNoteTime);
8383
}
8484
}
8585

packages/audiodocs/docs/fundamentals/making-a-piano-keyboard.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ const onKeyPressOut = (which: KeyName) => {
286286

287287
const { source, envelope, startedAt } = playingNote;
288288

289-
const tStop = Math.max(audioContext.currentTime, startedAt + 1);
289+
const tStop = Math.max(audioContext.currentTime, startedAt + 5);
290290

291291
envelope.gain.exponentialRampToValueAtTime(0.0001, tStop + 0.08);
292292
envelope.gain.setValueAtTime(0, tStop + 0.09);

packages/audiodocs/src/examples/SimplePiano/EnvelopesComponent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ const SimplePiano: FC = () => {
8686

8787
const { source, envelope, startedAt } = playingNote;
8888

89-
const tStop = Math.max(audioContext.currentTime, startedAt + 1);
89+
const tStop = Math.max(audioContext.currentTime, startedAt + 5);
9090

9191
envelope.gain.exponentialRampToValueAtTime(0.0001, tStop + 0.08);
9292
envelope.gain.setValueAtTime(0, tStop + 0.09);

packages/audiodocs/src/examples/SimplePiano/EnvelopesSource.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ const SimplePiano: FC = () => {
8787

8888
const { source, envelope, startedAt } = playingNote;
8989

90-
const tStop = Math.max(audioContext.currentTime, startedAt + 1);
90+
const tStop = Math.max(audioContext.currentTime, startedAt + 5);
9191

9292
envelope.gain.exponentialRampToValueAtTime(0.0001, tStop + 0.08);
9393
envelope.gain.setValueAtTime(0, tStop + 0.09);

packages/audiodocs/src/examples/SimplePiano/FinalComponent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ const SimplePiano: FC = () => {
116116
return;
117117
}
118118

119-
const tNow = Math.max(aCtx.currentTime, startedAt + 1);
119+
const tNow = Math.max(aCtx.currentTime, startedAt + 5.0);
120120

121121
envelope.gain.exponentialRampToValueAtTime(0.0001, tNow + 0.08);
122122
envelope.gain.setValueAtTime(0, tNow + 0.09);

packages/audiodocs/src/examples/SimplePiano/FinalSource.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ const SimplePiano: FC = () => {
117117
return;
118118
}
119119

120-
const tNow = Math.max(aCtx.currentTime, startedAt + 1);
120+
const tNow = Math.max(aCtx.currentTime, startedAt + 5);
121121

122122
envelope.gain.exponentialRampToValueAtTime(0.0001, tNow + 0.08);
123123
envelope.gain.setValueAtTime(0, tNow + 0.09);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void AudioScheduledSourceNode::updatePlaybackInfo(
8686
? std::max(startFrame, firstFrame) - firstFrame
8787
: 0;
8888
nonSilentFramesToProcess =
89-
std::min(lastFrame, stopFrame) - startFrame - startOffset;
89+
std::min(lastFrame, stopFrame) - startFrame;
9090
processingBus->zero(0, startOffset);
9191
return;
9292
}

0 commit comments

Comments
 (0)