Skip to content

Commit 588917a

Browse files
committed
start afresh on new audio in worklet
1 parent 92efac4 commit 588917a

File tree

2 files changed

+24
-32
lines changed

2 files changed

+24
-32
lines changed

packages/react/src/lib/useSoundPlayer.ts

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -87,40 +87,28 @@ export const useSoundPlayer = (props: {
8787
}
8888
}, []);
8989

90-
const addToQueue = useCallback(
91-
async (message: AudioOutputMessage) => {
92-
if (!isInitialized.current || !audioContext.current) {
93-
onError.current('Audio player has not been initialized');
94-
return;
95-
}
90+
const addToQueue = useCallback(async (message: AudioOutputMessage) => {
91+
if (!isInitialized.current || !audioContext.current) {
92+
onError.current('Audio player has not been initialized');
93+
return;
94+
}
9695

97-
try {
98-
const blob = convertBase64ToBlob(message.data);
99-
const arrayBuffer = await blob.arrayBuffer();
100-
const audioBuffer =
101-
await audioContext.current.decodeAudioData(arrayBuffer);
102-
103-
const pcmData = audioBuffer.getChannelData(0);
104-
workletNode.current?.port.postMessage({ type: 'audio', data: pcmData });
105-
106-
if (gainNode.current) {
107-
const now = audioContext.current.currentTime;
108-
gainNode.current.gain.cancelScheduledValues(now);
109-
gainNode.current.gain.setValueAtTime(
110-
isAudioMuted ? 0 : volume,
111-
audioContext.current.currentTime,
112-
);
113-
}
96+
try {
97+
const blob = convertBase64ToBlob(message.data);
98+
const arrayBuffer = await blob.arrayBuffer();
99+
const audioBuffer =
100+
await audioContext.current.decodeAudioData(arrayBuffer);
114101

115-
setIsPlaying(true);
116-
onPlayAudio.current(message.id);
117-
} catch (e) {
118-
const eMessage = e instanceof Error ? e.message : 'Unknown error';
119-
onError.current(`Failed to add clip to queue: ${eMessage}`);
120-
}
121-
},
122-
[isAudioMuted, volume],
123-
);
102+
const pcmData = audioBuffer.getChannelData(0);
103+
workletNode.current?.port.postMessage({ type: 'audio', data: pcmData });
104+
105+
setIsPlaying(true);
106+
onPlayAudio.current(message.id);
107+
} catch (e) {
108+
const eMessage = e instanceof Error ? e.message : 'Unknown error';
109+
onError.current(`Failed to add clip to queue: ${eMessage}`);
110+
}
111+
}, []);
124112

125113
const stopAll = useCallback(() => {
126114
isInitialized.current = false;

packages/react/src/worklets/audio-worklet.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ class AudioStreamProcessor extends AudioWorkletProcessor {
8181
switch (e.data?.type) {
8282
case 'audio':
8383
this._bq.push(new Float32Array(e.data.data));
84+
if (this._fadeOutActive) {
85+
this._fadeOutActive = false;
86+
this._fadeOutCounter = 0;
87+
}
8488
break;
8589
case 'end':
8690
this._shouldStop = true;

0 commit comments

Comments
 (0)