@@ -22,7 +22,10 @@ export interface IAudioContext {
2222
2323 createBuffer ( numberOfChannels : number , length : number , sampleRate : number ) : AudioBuffer ;
2424 createBuffer ( buffer : ArrayBuffer , mixToMono : boolean ) : AudioBuffer ;
25- decodeAudioData ( audioData : ArrayBuffer , decodeSuccessCallback ?: Function , decodeErrorCallback ?: Function ) : void ;
25+ // old decodeAudioData
26+ //decodeAudioData(audioData: ArrayBuffer, decodeSuccessCallback?: Function, decodeErrorCallback?: Function): void;
27+ // newer decodeAudioData
28+ decodeAudioData ( audioData : ArrayBuffer ) : Promise < AudioBuffer > ;
2629 createBufferSource ( ) : AudioBufferSourceNode ;
2730 createMediaElementSource ( mediaElement : HTMLMediaElement ) : MediaElementAudioSourceNode ;
2831 createMediaStreamSource ( mediaStreamMediaStream : MediaStream ) : MediaStreamAudioSourceNode ;
@@ -86,8 +89,10 @@ export interface IAudioGraph {
8689 waveShaperNode ?: WaveShaperNode ;
8790}
8891
89- export interface IAudioGraphOptions {
92+ export interface IAudioOptions {
9093 volume : number ;
94+ customAudioContext ?: IAudioContext ;
95+ customAudioGraph ?: IAudioGraph ;
9196}
9297
9398export interface ISourceNodeOptions {
@@ -97,21 +102,24 @@ export interface ISourceNodeOptions {
97102
98103export class PlayerAudio {
99104
105+ protected _volume : number ;
100106 protected _audioContext : IAudioContext | null = null ;
101107 protected _contextState : string ;
102108 protected _audioGraph : IAudioGraph | null = null ;
103109
104- constructor ( customAudioContext ?: IAudioContext , customAudioGraph ?: IAudioGraph ) {
110+ constructor ( options ?: IAudioOptions ) {
105111
106112 // initial context state is still "closed"
107113 this . _contextState = 'closed' ;
108114
109- if ( customAudioContext !== undefined ) {
110- this . _audioContext = customAudioContext ;
115+ this . _volume = options . volume ;
116+
117+ if ( options . customAudioContext !== undefined ) {
118+ this . _audioContext = options . customAudioContext ;
111119 }
112120
113- if ( customAudioGraph !== undefined ) {
114- this . _audioGraph = customAudioGraph ;
121+ if ( options . customAudioGraph !== undefined ) {
122+ this . _audioGraph = options . customAudioGraph ;
115123 } else {
116124 this . _createAudioGraph ( ) ;
117125 }
@@ -125,9 +133,16 @@ export class PlayerAudio {
125133
126134 return this . getAudioContext ( ) . then ( ( audioContext : IAudioContext ) => {
127135
136+ // Note to self:
137+ // newer decodeAudioData returns promise, older accept as second
138+ // and third parameter a success and an error callback funtion
139+ // https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/decodeAudioData
140+
141+ let audioBufferPromise = audioContext . decodeAudioData ( arrayBuffer ) ;
142+
128143 // decodeAudioData returns a promise of type PromiseLike
129144 // using resolve to return a promise of type Promise
130- return Promise . resolve ( audioContext . decodeAudioData ( arrayBuffer ) ) ;
145+ return Promise . resolve ( audioBufferPromise ) ;
131146
132147 } ) ;
133148
@@ -159,7 +174,7 @@ export class PlayerAudio {
159174
160175 }
161176
162- public getAudioContext ( ) : Promise < { } > {
177+ public getAudioContext ( ) : Promise < IAudioContext > {
163178
164179 return new Promise ( ( resolve , reject ) => {
165180
@@ -255,9 +270,28 @@ export class PlayerAudio {
255270
256271 }
257272
258- public getAudioGraph ( ) : IAudioGraph {
273+ public getAudioGraph ( ) : Promise < IAudioGraph > {
274+
275+ return new Promise ( ( resolve , reject ) => {
276+
277+ if ( this . _audioGraph !== null ) {
278+
279+ resolve ( this . _audioGraph ) ;
280+
281+ } else {
282+
283+ this . _createAudioGraph ( )
284+ . then ( ( audioGraph : IAudioGraph ) => {
285+
286+ this . _audioGraph = audioGraph ;
259287
260- return this . _audioGraph ;
288+ resolve ( audioGraph ) ;
289+
290+ } ) . catch ( reject ) ;
291+
292+ }
293+
294+ } ) ;
261295
262296 }
263297
@@ -304,21 +338,30 @@ export class PlayerAudio {
304338
305339 }
306340
307- protected _createAudioGraph ( ) {
341+ protected _createAudioGraph ( ) : Promise < IAudioGraph > {
308342
309- this . getAudioContext ( ) . then ( ( audioContext : IAudioContext ) => {
343+ return new Promise ( ( resolve , reject ) => {
310344
311- if ( this . _audioGraph === null ) {
345+ this . getAudioContext ( ) . then ( ( audioContext : IAudioContext ) => {
312346
313- this . _audioGraph = {
314- gainNode : audioContext . createGain ( )
315- } ;
347+ if ( this . _audioGraph === null ) {
316348
317- }
349+ this . _audioGraph = {
350+ gainNode : audioContext . createGain ( )
351+ } ;
352+
353+ }
354+
355+ // connect the gain node to the destination (speakers)
356+ // https://developer.mozilla.org/en-US/docs/Web/API/AudioDestinationNode
357+ this . _audioGraph . gainNode . connect ( audioContext . destination ) ;
358+
359+ // update volume
360+ this . changeGainValue ( this . _volume ) ;
361+
362+ resolve ( this . _audioGraph ) ;
318363
319- // connect the gain node to the destination (speakers)
320- // https://developer.mozilla.org/en-US/docs/Web/API/AudioDestinationNode
321- this . _audioGraph . gainNode . connect ( audioContext . destination ) ;
364+ } ) ;
322365
323366 } ) ;
324367
@@ -344,9 +387,9 @@ export class PlayerAudio {
344387
345388 public changeGainValue ( volume : number ) {
346389
347- this . getAudioContext ( ) . then ( ( audioContext : IAudioContext ) => {
390+ this . getAudioGraph ( ) . then ( ( audioGraph : IAudioGraph ) => {
348391
349- this . _audioGraph . gainNode . gain . value = volume / 100 ;
392+ audioGraph . gainNode . gain . value = volume / 100 ;
350393
351394 } ) ;
352395
0 commit comments