2 var X_Audio_WebAudio_context = !X_UA[ 'iPhone_4s' ] && !X_UA[ 'iPad_2Mini1' ] && !X_UA[ 'iPod_4' ] &&
3 // TODO なんで fennec を禁止?
4 !( X_UA[ 'Gecko' ] && X_UA[ 'Android' ] ) &&
5 ( window[ 'AudioContext' ] || window[ 'webkitAudioContext' ] ),
6 X_Audio_BUFFER_LIST = [],
7 X_Audio_WebAudioWrapper,
11 * iPhone 4s 以下、iPad2以下、iPad mini 1以下, iPod touch 4G 以下は不可
13 if( X_Audio_WebAudio_context ){
15 X_Audio_WebAudio_context = new X_Audio_WebAudio_context;
17 X_Audio_BufferLoader = X_EventDispatcher[ 'inherits' ](
18 'X.WebAudio.BufferLoader',
23 onDecodeSuccess : null,
30 'Constructor' : function( webAudio, url ){
31 this.webAudioList = [ webAudio ];
33 this.xhr = X[ 'Net' ]( { 'xhr' : url, 'dataType' : 'arraybuffer' } )
34 [ 'listen' ]( X_EVENT_PROGRESS, this )
35 [ 'listenOnce' ]( [ X_EVENT_SUCCESS, X_EVENT_COMPLETE ], this );
38 handleEvent : function( e ){
40 case X_EVENT_PROGRESS :
41 this[ 'dispatch' ]( { type : 'progress', 'percent' : e[ 'percent' ] } );
44 case X_EVENT_SUCCESS :
46 // https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API/Porting_webkitAudioContext_code_to_standards_based_AudioContext
48 // http://qiita.com/sou/items/5688d4e7d3a37b4e2ff1
49 // iOS 7.1 で decodeAudioData に処理が入った瞬間にスクリーンを長押しする(スクロールを繰り返す)と
50 // decoeAudioData の処理がキャンセルされることがある(エラーやコールバックの発火もなく、ただ処理が消滅する)。
51 // ただし iOS 8.1.2 では エラーになる
52 if( X_UA[ 'iOS' ] < 8 || !X_Audio_WebAudio_context[ 'decodeAudioData' ] ){
53 this._onDecodeSuccess( X_Audio_WebAudio_context[ 'createBuffer' ]( e.response, false ) );
55 if( X_Audio_WebAudio_context[ 'decodeAudioData' ] ){
56 X_Audio_WebAudio_context[ 'decodeAudioData' ]( e.response,
57 this.onDecodeSuccess = X_Closure_create( this, this._onDecodeSuccess ),
58 this.onDecodeError = X_Closure_create( this, this._onDecodeError ) );
62 case X_EVENT_COMPLETE :
64 this[ 'asyncDispatch' ]( X_EVENT_COMPLETE );
67 this.xhr[ 'unlisten' ]( [ X_EVENT_PROGRESS, X_EVENT_SUCCESS, X_EVENT_COMPLETE ], this );
71 _onDecodeSuccess : function( buffer ){
72 console.log( 'WebAudio decode success!' );
74 this.onDecodeSuccess && this._onDecodeComplete();
78 this[ 'asyncDispatch' ]( X_EVENT_COMPLETE );
84 this[ 'asyncDispatch' ]( X_EVENT_COMPLETE );
86 console.log( 'WebAudio decoded!' );
89 _onDecodeError : function(){
90 console.log( 'WebAudio decode error!' );
91 this._onDecodeComplete();
93 this[ 'asyncDispatch' ]( X_EVENT_COMPLETE );
96 _onDecodeComplete : function(){
97 X_Closure_correct( this.onDecodeSuccess );
98 delete this.onDecodeSuccess;
99 X_Closure_correct( this.onDecodeError );
100 delete this.onDecodeError;
103 unregister : function( webAudio ){
104 var list = this.webAudioList,
105 i = list.indexOf( webAudio );
109 this.xhr && this.xhr[ 'kill' ]();
119 X_Audio_WebAudioWrapper = X_Audio_AbstractAudioBackend[ 'inherits' ](
136 'Constructor' : function( target, url, option ){
138 l = X_Audio_BUFFER_LIST.length,
142 loader = X_Audio_BUFFER_LIST[ i ];
143 if( loader.url === url ){
144 this.loader = loader;
145 loader.webAudioList.push( this );
151 this.loader = loader = new X_Audio_BufferLoader( this, url );
154 this.target = target || this;
156 this.setState( option );
158 this[ 'listenOnce' ]( X_EVENT_KILL_INSTANCE, this.onKill );
160 if( loader.buffer || loader.error ){
161 this._onLoadBufferComplete();
163 loader[ 'listenOnce' ]( X_EVENT_COMPLETE, this, this._onLoadBufferComplete );
168 this.loader[ 'unlisten' ]( X_EVENT_COMPLETE, this, this._onLoadBufferComplete )
173 this.playing && this.actualPause();
174 this.bufferSource && this._sourceDispose();
176 this._onended && X_Closure_correct( this._onended );
178 this.gainNode && this.gainNode.disconnect();
180 _onLoadBufferComplete : function( e ){
181 var loader = this.loader,
182 buffer = loader.buffer;
184 e && loader[ 'unlisten' ]( X_EVENT_COMPLETE, this, this._onLoadBufferComplete );
187 this.error = loader.error;
189 this.target[ 'dispatch' ]({
190 type : X_EVENT_ERROR,
191 error : loader.error,
192 message : loader.error === 1 ?
193 'load buffer network error' :
194 'buffer decode error'
200 this.buffer = buffer;
201 this.duration = buffer.duration * 1000;
203 this.target[ 'asyncDispatch' ]( X_EVENT_READY );
205 console.log( 'WebAudio buffer ready' );
207 this.autoplay && X_Timer_once( 16, this, this.play );
211 actualPlay : function(){
215 this.autoplay = true;
219 end = X_AudioWrapper_getEndTime( this );
220 begin = X_AudioWrapper_getStartTime( this, end, true );
222 console.log( '[WebAudio] play ' + begin + ' -> ' + end );
224 if( this.bufferSource ) this._sourceDispose();
225 if( !this.gainNode ){
226 this.gainNode = X_Audio_WebAudio_context[ 'createGain' ] ? X_Audio_WebAudio_context[ 'createGain' ]() : X_Audio_WebAudio_context[ 'createGainNode' ]();
227 this.gainNode[ 'connect' ]( X_Audio_WebAudio_context[ 'destination' ] );
229 this.bufferSource = X_Audio_WebAudio_context[ 'createBufferSource' ]();
230 this.bufferSource.buffer = this.buffer;
231 this.bufferSource[ 'connect' ]( this.gainNode );
233 this.gainNode[ 'gain' ].value = this.gain;
235 // おかしい、stop 前に外していても呼ばれる、、、@Firefox33.1
236 // 破棄された X.Callback が呼ばれて、obj.proxy() でエラーになる。Firefox では、onended は使わない
237 if( false && this.bufferSource.onended !== undefined ){
238 //console.log( '> use onended' );
239 this.bufferSource.onended = this._onended || ( this._onended = X_Closure_create( this, this._onEnded ) );
241 this._timerID && X_Timer_remove( this._timerID );
242 this._timerID = X_Timer_once( end - begin, this, this._onEnded );
245 if( this.bufferSource.start ){
246 this.bufferSource.start( 0, begin / 1000, end / 1000 );
248 this.bufferSource[ 'noteGrainOn' ]( 0, begin / 1000, end / 1000 );
252 this._startPos = begin;
253 this._endPosition = end;
254 this._startTime = X_Audio_WebAudio_context.currentTime * 1000;
255 this._interval = this._interval || X_Timer_add( 1000, 0, this, this._onInterval );
258 _sourceDispose : function(){
259 this.bufferSource.disconnect();
260 delete this.bufferSource.onended;
261 delete this.bufferSource;
264 _onInterval : function(){
266 delete this._interval;
267 return X_CALLBACK_UN_LISTEN;
269 this.target[ 'dispatch' ]( X_EVENT_MEDIA_PLAYING );
272 _onEnded : function(){
274 delete this._timerID;
277 time = X_Audio_WebAudio_context.currentTime * 1000 - this._startTime - this._endPosition + this._startPos | 0;
278 //console.log( '> onEnd ' + ( this.playing && ( X_Audio_WebAudio_context.currentTime * 1000 - this._startTime ) ) + ' < ' + ( this._endPosition - this._startPos ) );
281 if( time < 0 ) return;
284 console.log( '> onEnd crt:' + ( X_Audio_WebAudio_context.currentTime * 1000 ) + ' startTime:' + this._startTime +
285 ' from:' + this._startPos + ' to:' + this._endPosition );
286 this._timerID = X_Timer_once( -time, this, this._onEnded );
292 if( !( this.target[ 'dispatch' ]( X_EVENT_MEDIA_BEFORE_LOOP ) & X_CALLBACK_PREVENT_DEFAULT ) ){
294 this.target[ 'dispatch' ]( X_EVENT_MEDIA_LOOPED );
299 this.target[ 'dispatch' ]( X_EVENT_MEDIA_ENDED );
304 actualPause : function(){
305 if( !this.playing ) return this;
307 console.log( '[WebAudio] pause' );
309 this.seekTime = this.getActualCurrentTime();
311 this._timerID && X_Timer_remove( this._timerID );
312 delete this._timerID;
315 if( this.bufferSource ){
316 if( this.bufferSource.onended ) delete this.bufferSource.onended;
318 this.bufferSource.stop ?
319 this.bufferSource.stop( 0 ) : this.bufferSource[ 'noteOff' ]( 0 );
323 getActualCurrentTime : function(){
324 return X_Audio_WebAudio_context.currentTime * 1000 - this._startTime + this._startPos | 0;
327 afterUpdateState : function( result ){
328 if( result & 2 || result & 1 ){ // seek
332 this.gainNode[ 'gain' ].value = this.gain;
340 * http://qiita.com/sou/items/5688d4e7d3a37b4e2ff1
341 * L-01F 等の一部端末で Web Audio API の再生結果に特定条件下でノイズが混ざることがある。
342 * 描画レート(描画 FPS)が下がるとノイズが混ざり始め、レートを上げると再生結果が正常になるというもので、オーディオ処理が描画スレッドに巻き込まれているような動作を見せる。
344 if( X_UA[ 'Android' ] && X_UA[ 'Chrome' ] ){
345 X_Node_systemNode.create( 'div', { id : 'fps-slowdown-make-sound-noisy' } );
348 X_Audio_BACKENDS.push(
350 backendName : 'Web Audio',
352 canPlay : {}, // TODO HTMLAudio と同じ
355 detect : function( proxy, source, ext ){
356 proxy[ 'asyncDispatch' ]( { type : X_EVENT_COMPLETE, canPlay : X_Audio_codecs[ ext ] } );
359 klass : X_Audio_WebAudioWrapper