Skip to content

Commit 97ecda2

Browse files
committed
nits
1 parent c9ebbab commit 97ecda2

File tree

3 files changed

+37
-58
lines changed

3 files changed

+37
-58
lines changed

packages/react/src/lib/VoiceProvider.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,7 @@ export const VoiceProvider: FC<VoiceProviderProps> = ({
232232
if (player.isPlaying) {
233233
onInterruption.current(message);
234234
}
235-
const shouldFadeOut = message.type === 'user_interruption';
236-
player.clearQueue({
237-
fadeOut: shouldFadeOut,
238-
});
235+
player.clearQueue();
239236
}
240237

241238
if (

packages/react/src/lib/useSoundPlayer.ts

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -87,40 +87,29 @@ 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+
104+
workletNode.current?.port.postMessage({ type: 'audio', data: pcmData });
105+
106+
setIsPlaying(true);
107+
onPlayAudio.current(message.id);
108+
} catch (e) {
109+
const eMessage = e instanceof Error ? e.message : 'Unknown error';
110+
onError.current(`Failed to add clip to queue: ${eMessage}`);
111+
}
112+
}, []);
124113

125114
const stopAll = useCallback(() => {
126115
isInitialized.current = false;
@@ -133,7 +122,7 @@ export const useSoundPlayer = (props: {
133122
window.clearInterval(frequencyDataIntervalId.current);
134123
}
135124

136-
workletNode.current?.port.postMessage({ type: 'fade' });
125+
workletNode.current?.port.postMessage({ type: 'fadeAndClear' });
137126
workletNode.current?.port.postMessage({ type: 'end' });
138127

139128
if (analyserNode.current) {
@@ -164,22 +153,15 @@ export const useSoundPlayer = (props: {
164153
setFft(generateEmptyFft());
165154
}, []);
166155

167-
const clearQueue = useCallback(
168-
({
169-
fadeOut,
170-
}: {
171-
fadeOut?: boolean;
172-
} = {}) => {
173-
workletNode.current?.port.postMessage({
174-
type: fadeOut ? 'fade' : 'clear',
175-
});
176-
177-
isProcessing.current = false;
178-
setIsPlaying(false);
179-
setFft(generateEmptyFft());
180-
},
181-
[],
182-
);
156+
const clearQueue = useCallback(() => {
157+
workletNode.current?.port.postMessage({
158+
type: 'fadeAndClear',
159+
});
160+
161+
isProcessing.current = false;
162+
setIsPlaying(false);
163+
setFft(generateEmptyFft());
164+
}, []);
183165

184166
const setVolume = useCallback(
185167
(newLevel: number) => {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class AudioStreamProcessor extends AudioWorkletProcessor {
8585
case 'end':
8686
this._shouldStop = true;
8787
break;
88-
case 'fade':
88+
case 'fadeAndClear':
8989
this._fadeOutActive = true;
9090
this._fadeOutCounter = 0;
9191
break;
@@ -111,15 +111,15 @@ class AudioStreamProcessor extends AudioWorkletProcessor {
111111
process(inputs, outputs) {
112112
const output = outputs[0];
113113
const frames = output[0].length;
114-
const chans = output.length;
114+
const channels = output.length;
115115

116116
const block = this._bq.read();
117117

118118
if (block) {
119-
for (let ch = 0; ch < chans; ch++) {
119+
for (let ch = 0; ch < channels; ch++) {
120120
const out = output[ch];
121121
for (let i = 0; i < frames; i++) {
122-
let sample = block[i * chans + ch] ?? 0;
122+
let sample = block[i * channels + ch] ?? 0;
123123

124124
// Apply fade out if active
125125
if (this._fadeOutActive) {
@@ -154,7 +154,7 @@ class AudioStreamProcessor extends AudioWorkletProcessor {
154154
return false;
155155
}
156156

157-
for (let ch = 0; ch < chans; ch++) {
157+
for (let ch = 0; ch < channels; ch++) {
158158
output[ch].fill(0);
159159
}
160160

0 commit comments

Comments
 (0)