@@ -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 ) => {
0 commit comments