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,
12 * iPhone 4s 以下、iPad2以下、iPad mini 1以下, iPod touch 4G 以下は不可
14 if( X_Audio_WebAudio_context ){
16 X_Audio_WebAudio_context = new X_Audio_WebAudio_context;
18 X_Audio_BufferLoader = X_EventDispatcher[ 'inherits' ](
19 'X.WebAudio.BufferLoader',
24 onDecodeSuccess : null,
31 'Constructor' : function( webAudio, url ){
32 this.webAudioList = [ webAudio ];
34 this.xhr = X[ 'Net' ]( { 'xhr' : url, 'dataType' : 'arraybuffer' } )
35 [ 'listen' ]( X_EVENT_PROGRESS, this )
36 [ 'listenOnce' ]( [ X_EVENT_SUCCESS, X_EVENT_COMPLETE ], this );
39 handleEvent : function( e ){
41 case X_EVENT_PROGRESS :
42 this[ 'dispatch' ]( { type : 'progress', 'percent' : e[ 'percent' ] } );
45 case X_EVENT_SUCCESS :
47 // https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API/Porting_webkitAudioContext_code_to_standards_based_AudioContext
49 // http://qiita.com/sou/items/5688d4e7d3a37b4e2ff1
50 // iOS 7.1 で decodeAudioData に処理が入った瞬間にスクリーンを長押しする(スクロールを繰り返す)と
51 // decoeAudioData の処理がキャンセルされることがある(エラーやコールバックの発火もなく、ただ処理が消滅する)。
52 // ただし iOS 8.1.2 では エラーになる
53 if( X_UA[ 'iOS' ] < 8 || !X_Audio_WebAudio_context[ 'decodeAudioData' ] ){
54 this._onDecodeSuccess( X_Audio_WebAudio_context[ 'createBuffer' ]( e.response, false ) );
56 if( X_Audio_WebAudio_context[ 'decodeAudioData' ] ){
57 X_Audio_WebAudio_context[ 'decodeAudioData' ]( e.response,
58 this.onDecodeSuccess = X_Closure_create( this, this._onDecodeSuccess ),
59 this.onDecodeError = X_Closure_create( this, this._onDecodeError ) );
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_Closure_correct( this.onDecodeSuccess );
99 delete this.onDecodeSuccess;
100 X_Closure_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' ](
137 'Constructor' : function( target, url, option ){
139 l = X_Audio_BUFFER_LIST.length,
143 * http://qiita.com/sou/items/5688d4e7d3a37b4e2ff1
144 * L-01F 等の一部端末で Web Audio API の再生結果に特定条件下でノイズが混ざることがある。
145 * 描画レート(描画 FPS)が下がるとノイズが混ざり始め、レートを上げると再生結果が正常になるというもので、オーディオ処理が描画スレッドに巻き込まれているような動作を見せる。
147 if( X_UA[ 'Android' ] && X_UA[ 'Chrome' ] && !X_Audio_fpsFix ){
148 X_Node_systemNode.create( 'div', { id : 'fps-slowdown-make-sound-noisy' } );
149 X_Audio_fpsFix = true;
153 loader = X_Audio_BUFFER_LIST[ i ];
154 if( loader.url === url ){
155 this.loader = loader;
156 loader.webAudioList.push( this );
162 this.loader = loader = new X_Audio_BufferLoader( this, url );
165 this.target = target || this;
167 this.setState( option );
169 this[ 'listenOnce' ]( X_EVENT_KILL_INSTANCE, this.onKill );
171 if( loader.buffer || loader.error ){
172 this._onLoadBufferComplete();
174 loader[ 'listenOnce' ]( X_EVENT_COMPLETE, this, this._onLoadBufferComplete );
179 this.loader[ 'unlisten' ]( X_EVENT_COMPLETE, this, this._onLoadBufferComplete )
184 this.playing && this.actualPause();
185 this.bufferSource && this._sourceDispose();
187 this._onended && X_Closure_correct( this._onended );
189 this.gainNode && this.gainNode.disconnect();
191 _onLoadBufferComplete : function( e ){
192 var loader = this.loader,
193 buffer = loader.buffer;
195 e && loader[ 'unlisten' ]( X_EVENT_COMPLETE, this, this._onLoadBufferComplete );
198 this.error = loader.error;
200 this.target[ 'dispatch' ]({
201 type : X_EVENT_ERROR,
202 error : loader.error,
203 message : loader.error === 1 ?
204 'load buffer network error' :
205 'buffer decode error'
211 this.buffer = buffer;
212 this.duration = buffer.duration * 1000;
214 this.target[ 'asyncDispatch' ]( X_EVENT_READY );
216 console.log( 'WebAudio buffer ready' );
218 this.autoplay && X_Timer_once( 16, this, this.play );
222 actualPlay : function(){
226 this.autoplay = true;
230 end = X_AudioWrapper_getEndTime( this );
231 begin = X_AudioWrapper_getStartTime( this, end, true );
233 console.log( '[WebAudio] play ' + begin + ' -> ' + end );
235 if( this.bufferSource ) this._sourceDispose();
236 if( !this.gainNode ){
237 this.gainNode = X_Audio_WebAudio_context[ 'createGain' ] ? X_Audio_WebAudio_context[ 'createGain' ]() : X_Audio_WebAudio_context[ 'createGainNode' ]();
238 this.gainNode[ 'connect' ]( X_Audio_WebAudio_context[ 'destination' ] );
240 this.bufferSource = X_Audio_WebAudio_context[ 'createBufferSource' ]();
241 this.bufferSource.buffer = this.buffer;
242 this.bufferSource[ 'connect' ]( this.gainNode );
244 this.gainNode[ 'gain' ].value = this.gain;
246 // おかしい、stop 前に外していても呼ばれる、、、@Firefox33.1
247 // 破棄された X.Callback が呼ばれて、obj.proxy() でエラーになる。Firefox では、onended は使わない
248 if( false && this.bufferSource.onended !== undefined ){
249 //console.log( '> use onended' );
250 this.bufferSource.onended = this._onended || ( this._onended = X_Closure_create( this, this._onEnded ) );
252 this._timerID && X_Timer_remove( this._timerID );
253 this._timerID = X_Timer_once( end - begin, this, this._onEnded );
256 if( this.bufferSource.start ){
257 this.bufferSource.start( 0, begin / 1000, end / 1000 );
259 this.bufferSource[ 'noteGrainOn' ]( 0, begin / 1000, end / 1000 );
263 this._startPos = begin;
264 this._endPosition = end;
265 this._startTime = X_Audio_WebAudio_context.currentTime * 1000;
266 this._interval = this._interval || X_Timer_add( 1000, 0, this, this._onInterval );
269 _sourceDispose : function(){
270 this.bufferSource.disconnect();
271 delete this.bufferSource.onended;
272 delete this.bufferSource;
275 _onInterval : function(){
277 delete this._interval;
278 return X_CALLBACK_UN_LISTEN;
280 this.target[ 'dispatch' ]( X_EVENT_MEDIA_PLAYING );
283 _onEnded : function(){
285 delete this._timerID;
288 time = X_Audio_WebAudio_context.currentTime * 1000 - this._startTime - this._endPosition + this._startPos | 0;
289 //console.log( '> onEnd ' + ( this.playing && ( X_Audio_WebAudio_context.currentTime * 1000 - this._startTime ) ) + ' < ' + ( this._endPosition - this._startPos ) );
292 if( time < 0 ) return;
295 console.log( '> onEnd crt:' + ( X_Audio_WebAudio_context.currentTime * 1000 ) + ' startTime:' + this._startTime +
296 ' from:' + this._startPos + ' to:' + this._endPosition );
297 this._timerID = X_Timer_once( -time, this, this._onEnded );
303 if( !( this.target[ 'dispatch' ]( X_EVENT_MEDIA_BEFORE_LOOP ) & X_CALLBACK_PREVENT_DEFAULT ) ){
305 this.target[ 'dispatch' ]( X_EVENT_MEDIA_LOOPED );
310 this.target[ 'dispatch' ]( X_EVENT_MEDIA_ENDED );
315 actualPause : function(){
316 if( !this.playing ) return this;
318 console.log( '[WebAudio] pause' );
320 this.seekTime = this.getActualCurrentTime();
322 this._timerID && X_Timer_remove( this._timerID );
323 delete this._timerID;
326 if( this.bufferSource ){
327 if( this.bufferSource.onended ) delete this.bufferSource.onended;
329 this.bufferSource.stop ?
330 this.bufferSource.stop( 0 ) : this.bufferSource[ 'noteOff' ]( 0 );
334 getActualCurrentTime : function(){
335 return X_Audio_WebAudio_context.currentTime * 1000 - this._startTime + this._startPos | 0;
338 afterUpdateState : function( result ){
339 if( result & 2 || result & 1 ){ // seek
343 this.gainNode[ 'gain' ].value = this.gain;
350 X_Audio_BACKENDS.push(
352 backendName : 'Web Audio',
354 canPlay : {}, // TODO HTMLAudio と同じ
357 detect : function( proxy, source, ext ){
358 proxy[ 'asyncDispatch' ]( { type : X_EVENT_COMPLETE, canPlay : X_Audio_codecs[ ext ] } );
361 klass : X_Audio_WebAudioWrapper