11var LibraryHTML5Audio = {
22 $AUDIO : {
3- players : [ ] ,
4- lastSoundID : 0 ,
3+ player : [ ] ,
4+ lastPlayerID : 0 ,
55 } ,
66
7- html5audio_list_devices : function ( ) {
7+ html5audio_list_devices : function ( ) {
88 if ( ! navigator . mediaDevices . enumerateDevices ) {
99 console . log ( "enumerateDevices() not supported." ) ;
1010 } else {
@@ -23,7 +23,7 @@ var LibraryHTML5Audio = {
2323 } ) ;
2424 }
2525 } ,
26-
26+
2727 html5audio_context_create : function ( ) {
2828 try {
2929 // Fix up for prefixing
@@ -69,116 +69,139 @@ var LibraryHTML5Audio = {
6969 return AUDIO . context . sampleRate . value ;
7070 } ,
7171
72- html5audio_sound_load : function ( url ) {
72+ html5audio_player_create : function ( ) {
7373 var audio = document . createElement ( 'audio' ) ;
74- var sound_id = AUDIO . lastSoundID ++ ;
75- AUDIO . players [ sound_id ] = audio ;
76- AUDIO . players [ sound_id ] . src = UTF8ToString ( url ) ;
77- var source = AUDIO . context . createMediaElementSource ( AUDIO . players [ sound_id ] ) ;
78- AUDIO . players [ sound_id ] . soundPan = AUDIO . context . createStereoPanner ( ) ;
79- source . connect ( AUDIO . players [ sound_id ] . soundPan ) . connect ( AUDIO . fft ) ;
80- return sound_id ;
74+ var player_id = AUDIO . lastPlayerID ++ ;
75+ AUDIO . player [ player_id ] = audio ;
76+ var source = AUDIO . context . createMediaElementSource ( AUDIO . player [ player_id ] ) ;
77+ AUDIO . player [ player_id ] . soundPan = AUDIO . context . createStereoPanner ( ) ;
78+ source . connect ( AUDIO . player [ player_id ] . soundPan ) . connect ( AUDIO . fft ) ;
79+ return player_id ;
8180 } ,
82-
83- html5audio_sound_play : function ( sound_id , offset ) {
84- AUDIO . players [ sound_id ] . play ( offset ) ;
81+
82+ html5audio_sound_load : function ( player_id , url ) {
83+ AUDIO . player [ player_id ] . src = UTF8ToString ( url ) ;
84+ } ,
85+
86+ html5audio_sound_play : function ( player_id , multiplay , volume , speed , pan , offset ) {
87+ if ( AUDIO . player [ player_id ] . src != "" ) {
88+ if ( multiplay ) {
89+ const clone = AUDIO . player [ player_id ] . cloneNode ( ) ;
90+ clone . soundPan = AUDIO . context . createStereoPanner ( ) ;
91+ clone . volume = volume ;
92+ clone . playbackRate = speed ;
93+ clone . soundPan . pan . value = pan ;
94+ AUDIO . player [ player_id ] = clone ;
95+ }
96+ AUDIO . player [ player_id ] . play ( offset ) ;
97+ }
8598 } ,
8699
87- html5audio_sound_stop : function ( sound_id ) {
88- AUDIO . players [ sound_id ] . currentTime = 0 ;
89- AUDIO . players [ sound_id ] . pause ( ) ;
100+ html5audio_sound_stop : function ( player_id ) {
101+ AUDIO . player [ player_id ] . currentTime = 0 ;
102+ AUDIO . player [ player_id ] . pause ( ) ;
90103 } ,
91104
92- html5audio_sound_pause : function ( sound_id ) {
93- AUDIO . players [ sound_id ] . pause ( ) ;
105+ html5audio_sound_pause : function ( player_id ) {
106+ AUDIO . player [ player_id ] . pause ( ) ;
94107 } ,
95108
96- html5audio_sound_rate : function ( sound_id ) {
97- return AUDIO . players [ sound_id ] . playbackRate ;
109+ html5audio_sound_rate : function ( player_id ) {
110+ return AUDIO . player [ player_id ] . playbackRate ;
98111 } ,
99112
100- html5audio_sound_set_rate : function ( sound_id , rate ) {
101- AUDIO . players [ sound_id ] . playbackRate = rate ;
113+ html5audio_sound_set_rate : function ( player_id , rate ) {
114+ AUDIO . player [ player_id ] . playbackRate = rate ;
102115 } ,
103116
104- html5audio_sound_done : function ( sound_id ) {
105- return AUDIO . players [ sound_id ] . done ;
117+ html5audio_sound_done : function ( player_id ) {
118+ return AUDIO . player [ player_id ] . done ;
106119 } ,
107120
108- html5audio_sound_duration : function ( sound_id ) {
109- return AUDIO . players [ sound_id ] . duration ;
121+ html5audio_sound_duration : function ( player_id ) {
122+ if ( AUDIO . player [ player_id ] . src != "" ) {
123+ return AUDIO . player [ player_id ] . duration ;
124+ } else {
125+ return 0 ;
126+ }
110127 } ,
111128
112- html5audio_sound_position : function ( sound_id ) {
113- return AUDIO . players [ sound_id ] . currentTime ;
129+ html5audio_sound_position : function ( player_id ) {
130+ if ( AUDIO . player [ player_id ] . src != "" ) {
131+ return AUDIO . player [ player_id ] . currentTime ;
132+ } else {
133+ return 0 ;
134+ }
114135 } ,
115136
116- html5audio_sound_set_position : function ( sound_id , position ) {
117- AUDIO . players [ sound_id ] . currentTime = position * AUDIO . players [ sound_id ] . duration ;
137+ html5audio_sound_set_position : function ( player_id , position ) {
138+ if ( AUDIO . player [ player_id ] . src != "" ) {
139+ AUDIO . player [ player_id ] . currentTime = position * AUDIO . player [ player_id ] . duration ;
140+ }
118141 } ,
119142
120- html5audio_sound_set_loop : function ( sound_id , loop ) {
121- AUDIO . players [ sound_id ] . loop = true ;
143+ html5audio_sound_set_loop : function ( player_id , loop ) {
144+ AUDIO . player [ player_id ] . loop = true ;
122145 } ,
123146
124- html5audio_sound_set_volume : function ( sound_id , volume ) {
125- AUDIO . players [ sound_id ] . volume = volume ;
147+ html5audio_sound_set_volume : function ( player_id , volume ) {
148+ AUDIO . player [ player_id ] . volume = volume ;
126149 } ,
127150
128- html5audio_sound_volume : function ( sound_id ) {
129- return AUDIO . players [ sound_id ] . volume ;
151+ html5audio_sound_volume : function ( player_id ) {
152+ return AUDIO . player [ player_id ] . volume ;
130153 } ,
131154
132- html5audio_sound_set_pan : function ( sound_id , pan ) {
133- AUDIO . players [ sound_id ] . soundPan . pan . value = pan ;
155+ html5audio_sound_set_pan : function ( player_id , pan ) {
156+ AUDIO . player [ player_id ] . soundPan . pan . value = pan ;
134157 } ,
135158
136- html5audio_sound_pan : function ( sound_id ) {
137- return AUDIO . players [ sound_id ] . soundPan . pan . value ;
159+ html5audio_sound_pan : function ( player_id ) {
160+ return AUDIO . player [ player_id ] . soundPan . pan . value ;
138161 } ,
139162
140- html5audio_sound_free : function ( sound_id ) {
141- if ( AUDIO . players [ sound_id ] != undefined ) {
142- AUDIO . players [ sound_id ] . pause ( ) ;
143- URL . revokeObjectURL ( AUDIO . players [ sound_id ] . src ) ;
163+ html5audio_sound_free : function ( player_id ) {
164+ if ( AUDIO . player [ player_id ] . src != "" ) {
165+ AUDIO . player [ player_id ] . pause ( ) ;
166+ URL . revokeObjectURL ( AUDIO . player [ player_id ] . src ) ;
144167 }
145168 } ,
146169
147- html5audio_stream_create : function ( bufferSize , inputChannels , outputChannels , inbuffer , outbuffer , callback , userData ) {
148- var stream = AUDIO . context . createScriptProcessor ( bufferSize , inputChannels , outputChannels ) ;
149- var inbufferArray = Module . HEAPF32 . subarray ( inbuffer >> 2 , ( inbuffer >> 2 ) + bufferSize * inputChannels ) ;
150- var outbufferArray = Module . HEAPF32 . subarray ( outbuffer >> 2 , ( outbuffer >> 2 ) + bufferSize * outputChannels ) ;
170+ html5audio_stream_create : function ( bufferSize , inputChannels , outputChannels , inbuffer , outbuffer , callback , userData ) {
171+ var stream = AUDIO . context . createScriptProcessor ( bufferSize , inputChannels , outputChannels ) ;
172+ var inbufferArray = Module . HEAPF32 . subarray ( inbuffer >> 2 , ( inbuffer >> 2 ) + bufferSize * inputChannels ) ;
173+ var outbufferArray = Module . HEAPF32 . subarray ( outbuffer >> 2 , ( outbuffer >> 2 ) + bufferSize * outputChannels ) ;
151174
152- stream . onaudioprocess = function ( event ) {
175+ stream . onaudioprocess = function ( event ) {
153176 var i , j , c ;
154- if ( inputChannels > 0 ) {
155- for ( c = 0 ; c < inputChannels ; ++ c ) {
177+ if ( inputChannels > 0 ) {
178+ for ( c = 0 ; c < inputChannels ; ++ c ) {
156179 var inChannel = event . inputBuffer . getChannelData ( c ) ;
157- for ( i = 0 , j = c ; i < bufferSize ; ++ i , j += inputChannels ) {
180+ for ( i = 0 , j = c ; i < bufferSize ; ++ i , j += inputChannels ) {
158181 inbufferArray [ j ] = inChannel [ i ] ;
159182 }
160183 }
161184 }
162185
163186 { { { makeDynCall ( 'viiii' , 'callback' ) } } } ( bufferSize , inputChannels , outputChannels , userData ) ;
164187
165- if ( outputChannels > 0 ) {
166- for ( c = 0 ; c < outputChannels ; ++ c ) {
188+ if ( outputChannels > 0 ) {
189+ for ( c = 0 ; c < outputChannels ; ++ c ) {
167190 var outChannel = event . outputBuffer . getChannelData ( c ) ;
168- for ( i = 0 , j = c ; i < bufferSize ; ++ i , j += outputChannels ) {
191+ for ( i = 0 , j = c ; i < bufferSize ; ++ i , j += outputChannels ) {
169192 outChannel [ i ] = outbufferArray [ j ] ;
170193 }
171194 }
172195 }
173196 } ;
174197
175- if ( inputChannels > 0 ) {
198+ if ( inputChannels > 0 ) {
176199 navigator . getUserMedia = navigator . getUserMedia ||
177200 navigator . webkitGetUserMedia ||
178201 navigator . mozGetUserMedia ||
179202 navigator . msGetUserMedia ;
180203
181- if ( navigator . getUserMedia ) {
204+ if ( navigator . getUserMedia ) {
182205 navigator . getUserMedia (
183206 { audio : true } ,
184207 function ( audioIn ) {
@@ -193,16 +216,14 @@ var LibraryHTML5Audio = {
193216 }
194217
195218 stream . connect ( AUDIO . fft ) ;
196- AUDIO . stream = stream ;
197219 } ,
198220
199221 html5audio_stream_free : function ( ) {
200- return AUDIO . stream = null ;
201- return AUDIO . mediaElement = null ;
222+
202223 } ,
203224
204- html5audio_sound_is_loaded : function ( sound_id ) {
205- if ( AUDIO . players [ sound_id ] . src != undefined ) {
225+ html5audio_sound_is_loaded : function ( player_id ) {
226+ if ( AUDIO . player [ player_id ] . readyState > 0 ) {
206227 return true ;
207228 }
208229 return false ;
0 commit comments