2 var X_Audio_WebAudio_context = !X_UA[ 'iPhone_4s' ] && !X_UA[ 'iPad_2Mini1' ] && !X_UA[ 'iPod_4' ] &&
3 !( X_UA[ 'Gecko' ] && X_UA[ 'Android' ] ) &&
4 ( window.AudioContext || window.webkitAudioContext ),
5 X_Audio_BUFFER_LIST = [],
6 X_Audio_WebAudioWrapper,
10 * iPhone 4s 以下、iPad2以下、iPad mini 1以下, iPod touch 4G 以下は不可
12 if( X_Audio_WebAudio_context ){
14 X_Audio_WebAudio_context = new X_Audio_WebAudio_context;
16 X_Audio_BufferLoader = X_EventDispatcher[ 'inherits' ](
17 'X.AV.WebAudioBufferLoader',
22 onDecodeSuccess : null,
29 Constructor : function( webAudio, url ){
30 this.webAudioList = [ webAudio ];
32 this.xhr = X.Net( { 'xhr' : url, 'dataType' : 'arraybuffer' } )
33 [ 'listen' ]( X_EVENT_PROGRESS, this )
34 [ 'listenOnce' ]( [ X_EVENT_SUCCESS, X_EVENT_COMPLETE ], this );
37 handleEvent : function( e ){
39 case X_EVENT_PROGRESS :
40 this[ 'dispatch' ]( { type : 'progress', 'percent' : e[ 'percent' ] } );
43 case X_EVENT_SUCCESS :
45 // https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API/Porting_webkitAudioContext_code_to_standards_based_AudioContext
47 // http://qiita.com/sou/items/5688d4e7d3a37b4e2ff1
48 // iOS 7.1 で decodeAudioData に処理が入った瞬間にスクリーンを長押しする(スクロールを繰り返す)と
49 // decoeAudioData の処理がキャンセルされることがある(エラーやコールバックの発火もなく、ただ処理が消滅する)。
50 // ただし iOS 8.1.2 では エラーになる
51 if( X_Audio_WebAudio_context.createBuffer && X_UA[ 'iOS' ] < 8 ){
52 this._onDecodeSuccess( X_Audio_WebAudio_context.createBuffer( e.response, false ) );
54 if( X_Audio_WebAudio_context.decodeAudioData ){
55 X_Audio_WebAudio_context.decodeAudioData( e.response,
56 this.onDecodeSuccess = X_Callback_create( this, this._onDecodeSuccess ),
57 this.onDecodeError = X_Callback_create( this, this._onDecodeError ) );
59 this._onDecodeSuccess( X_Audio_WebAudio_context.createBuffer( e.response, false ) );
63 case X_EVENT_COMPLETE :
65 this[ 'asyncDispatch' ]( X_EVENT_COMPLETE );
68 this.xhr[ 'unlisten' ]( [ X_EVENT_PROGRESS, X_EVENT_SUCCESS, X_EVENT_COMPLETE ], this );
72 _onDecodeSuccess : function( buffer ){
73 console.log( 'WebAudio decode success!' );
75 this.onDecodeSuccess && this._onDecodeComplete();
79 this[ 'asyncDispatch' ]( X_EVENT_COMPLETE );
85 this[ 'asyncDispatch' ]( X_EVENT_COMPLETE );
87 console.log( 'WebAudio decoded!' );
90 _onDecodeError : function(){
91 console.log( 'WebAudio decode error!' );
92 this._onDecodeComplete();
94 this[ 'asyncDispatch' ]( X_EVENT_COMPLETE );
97 _onDecodeComplete : function(){
98 X_Callback_correct( this.onDecodeSuccess );
99 delete this.onDecodeSuccess;
100 X_Callback_correct( this.onDecodeError );
101 delete this.onDecodeError;
104 unregister : function( webAudio ){
105 var list = this.webAudioList,
106 i = list.indexOf( webAudio );
110 this.xhr && this.xhr[ 'kill' ]();
120 X_Audio_WebAudioWrapper = X_Audio_AbstractAudioBackend[ 'inherits' ](
121 'X.AV.WebAudioWrapper',
137 Constructor : function( target, url, option ){
139 l = X_Audio_BUFFER_LIST.length,
143 loader = X_Audio_BUFFER_LIST[ i ];
144 if( loader.url === url ){
145 this.loader = loader;
146 loader.webAudioList.push( this );
152 this.loader = loader = new X_Audio_BufferLoader( this, url );
155 this.target = target || this;
157 this.setState( option );
159 this[ 'listenOnce' ]( X_EVENT_KILL_INSTANCE, this.onKill );
161 if( loader.buffer || loader.error ){
162 this._onLoadBufferComplete();
164 loader[ 'listenOnce' ]( X_EVENT_COMPLETE, this, this._onLoadBufferComplete );
169 this.loader[ 'unlisten' ]( X_EVENT_COMPLETE, this, this._onLoadBufferComplete )
174 this.playing && this.actualPause();
175 this.source && this._sourceDispose();
177 this._onended && X_Callback_correct( this._onended );
179 this.gainNode && this.gainNode.disconnect();
181 _onLoadBufferComplete : function( e ){
182 var loader = this.loader,
183 buffer = loader.buffer;
185 e && loader[ 'unlisten' ]( X_EVENT_COMPLETE, this, this._onLoadBufferComplete );
188 this.error = loader.error;
190 this.target[ 'dispatch' ]({
191 type : X_EVENT_ERROR,
192 error : loader.error,
193 message : loader.error === 1 ?
194 'load buffer network error' :
195 'buffer decode error'
201 this.buffer = buffer;
202 this.duration = buffer.duration * 1000;
204 this.target[ 'asyncDispatch' ]( X_EVENT_READY );
206 this.autoplay && X_Timer_once( 16, this, this.play );
210 actualPlay : function(){
214 this.autoplay = true;
218 end = X_AudioWrapper_getEndTime( this );
219 begin = X_AudioWrapper_getStartTime( this, end, true );
221 console.log( '[WebAudio] play ' + begin + ' -> ' + end );
223 if( this.source ) this._sourceDispose();
224 if( !this.gainNode ){
225 this.gainNode = X_Audio_WebAudio_context.createGain ? X_Audio_WebAudio_context.createGain() : X_Audio_WebAudio_context.createGainNode();
226 this.gainNode.connect( X_Audio_WebAudio_context.destination );
228 this.source = X_Audio_WebAudio_context.createBufferSource();
229 this.source.buffer = this.buffer;
230 this.source.connect( this.gainNode );
232 this.gainNode.gain.value = this.gain;
234 // おかしい、stop 前に外していても呼ばれる、、、@Firefox33.1
235 // 破棄された X.Callback が呼ばれて、obj.proxy() でエラーになる。Firefox では、onended は使わない
236 if( false && this.source.onended !== undefined ){
237 //console.log( '> use onended' );
238 this.source.onended = this._onended || ( this._onended = X_Callback_create( this, this._onEnded ) );
240 this._timerID && X_Timer_remove( this._timerID );
241 this._timerID = X_Timer_once( end - begin, this, this._onEnded );
244 if( this.source.start ){
245 this.source.start( 0, begin / 1000, end / 1000 );
247 this.source.noteGrainOn( 0, begin / 1000, end / 1000 );
251 this._startPos = begin;
252 this._endPosition = end;
253 this._startTime = X_Audio_WebAudio_context.currentTime * 1000;
254 this._interval = this._interval || X_Timer_add( 1000, 0, this, this._onInterval );
257 _sourceDispose : function(){
258 this.source.disconnect();
259 delete this.source.onended;
263 _onInterval : function(){
265 delete this._interval;
266 return X_Callback_UN_LISTEN;
268 this.target[ 'dispatch' ]( X_EVENT_MEDIA_PLAYING );
271 _onEnded : function(){
273 delete this._timerID;
276 time = X_Audio_WebAudio_context.currentTime * 1000 - this._startTime - this._endPosition + this._startPos | 0;
277 //console.log( '> onEnd ' + ( this.playing && ( X_Audio_WebAudio_context.currentTime * 1000 - this._startTime ) ) + ' < ' + ( this._endPosition - this._startPos ) );
280 if( time < 0 ) return;
283 console.log( '> onEnd crt:' + ( X_Audio_WebAudio_context.currentTime * 1000 ) + ' startTime:' + this._startTime +
284 ' from:' + this._startPos + ' to:' + this._endPosition );
285 this._timerID = X_Timer_once( -time, this, this._onEnded );
291 if( !( this.target[ 'dispatch' ]( X_EVENT_MEDIA_BEFORE_LOOP ) & X_Callback_PREVENT_DEFAULT ) ){
293 this.target[ 'dispatch' ]( X_EVENT_MEDIA_LOOPED );
298 this.target[ 'dispatch' ]( X_EVENT_MEDIA_ENDED );
303 actualPause : function(){
304 if( !this.playing ) return this;
306 console.log( '[WebAudio] pause' );
308 this.seekTime = this.getActualCurrentTime();
310 this._timerID && X_Timer_remove( this._timerID );
311 delete this._timerID;
315 if( this.source.onended ) delete this.source.onended;
318 this.source.stop( 0 ) : this.source.noteOff( 0 );
322 getActualCurrentTime : function(){
323 return X_Audio_WebAudio_context.currentTime * 1000 - this._startTime + this._startPos | 0;
326 afterUpdateState : function( result ){
327 if( result & 2 || result & 1 ){ // seek
331 this.gainNode.gain.value = this.gain;
339 * http://qiita.com/sou/items/5688d4e7d3a37b4e2ff1
340 * L-01F 等の一部端末で Web Audio API の再生結果に特定条件下でノイズが混ざることがある。
341 * 描画レート(描画 FPS)が下がるとノイズが混ざり始め、レートを上げると再生結果が正常になるというもので、オーディオ処理が描画スレッドに巻き込まれているような動作を見せる。
343 if( X_UA[ 'Android' ] && X_UA[ 'Chrome' ] ){
344 X_Node_systemNode.create( 'div', { id : 'fps-slowdown-make-sound-noisy' } );
347 X_Audio_BACKENDS.push(
349 backendName : 'Web Audio',
352 detect : function( proxy, source, ext ){
353 proxy[ 'asyncDispatch' ]( { type : X_EVENT_COMPLETE, canPlay : X_Audio_codecs[ ext ] } );
356 klass : X_Audio_WebAudioWrapper