Skip to content

Commit b7636ab

Browse files
committed
use promise for get audio graph
1 parent 757dcfa commit b7636ab

File tree

9 files changed

+161
-64
lines changed

9 files changed

+161
-64
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
[![Dependencies](https://david-dm.org/chrisweb/web-audio-api-player/status.svg)](https://david-dm.org/chrisweb/web-audio-api-player)
12
[![Dependencies](https://david-dm.org/chrisweb/web-audio-api-player/dev-status.png)](https://david-dm.org/chrisweb/web-audio-api-player)
23
[![GitHub release](https://img.shields.io/github/release/chrisweb/web-audio-api-player.svg)](https://github.com/chrisweb/web-audio-api-player/releases)
34

build/@types/web-audio-api-player/library/audio.d.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface IAudioContext {
1010
state: string;
1111
createBuffer(numberOfChannels: number, length: number, sampleRate: number): AudioBuffer;
1212
createBuffer(buffer: ArrayBuffer, mixToMono: boolean): AudioBuffer;
13-
decodeAudioData(audioData: ArrayBuffer, decodeSuccessCallback?: Function, decodeErrorCallback?: Function): void;
13+
decodeAudioData(audioData: ArrayBuffer): Promise<AudioBuffer>;
1414
createBufferSource(): AudioBufferSourceNode;
1515
createMediaElementSource(mediaElement: HTMLMediaElement): MediaElementAudioSourceNode;
1616
createMediaStreamSource(mediaStreamMediaStream: MediaStream): MediaStreamAudioSourceNode;
@@ -48,31 +48,34 @@ export interface IAudioGraph {
4848
oscillatorNode?: OscillatorNode;
4949
waveShaperNode?: WaveShaperNode;
5050
}
51-
export interface IAudioGraphOptions {
51+
export interface IAudioOptions {
5252
volume: number;
53+
customAudioContext?: IAudioContext;
54+
customAudioGraph?: IAudioGraph;
5355
}
5456
export interface ISourceNodeOptions {
5557
loop: boolean;
5658
onEnded: Function;
5759
}
5860
export declare class PlayerAudio {
61+
protected _volume: number;
5962
protected _audioContext: IAudioContext | null;
6063
protected _contextState: string;
6164
protected _audioGraph: IAudioGraph | null;
62-
constructor(customAudioContext?: IAudioContext, customAudioGraph?: IAudioGraph);
65+
constructor(options?: IAudioOptions);
6366
decodeAudio(arrayBuffer: ArrayBuffer): Promise<AudioBuffer>;
6467
protected _createAudioContext(): IAudioContext;
6568
protected _bindContextStateListener(audioContext: IAudioContext): void;
66-
getAudioContext(): Promise<{}>;
69+
getAudioContext(): Promise<IAudioContext>;
6770
setAudioContext(audioContext: IAudioContext): void;
6871
protected _destroyAudioContext(): void;
6972
protected _unfreezeAudioContext(): Promise<void>;
7073
protected _freezeAudioContext(): Promise<void>;
7174
setAudioGraph(audioGraph: IAudioGraph): void;
72-
getAudioGraph(): IAudioGraph;
75+
getAudioGraph(): Promise<IAudioGraph>;
7376
createSourceNode(sourceNodeOptions: ISourceNodeOptions): Promise<AudioBufferSourceNode>;
7477
connectSourceNodeToGraphNodes(sourceNode: AudioBufferSourceNode): void;
75-
protected _createAudioGraph(): void;
78+
protected _createAudioGraph(): Promise<IAudioGraph>;
7679
protected _destroyAudioGraph(): void;
7780
destroySourceNode(sourceNode: AudioBufferSourceNode): AudioBufferSourceNode;
7881
changeGainValue(volume: number): void;

build/@types/web-audio-api-player/library/core.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export declare class PlayerCore {
6767
last(): void;
6868
protected _playingProgress(sound: ISound): void;
6969
setAudioGraph(customAudioGraph: IAudioGraph): void;
70-
getAudioGraph(): IAudioGraph;
70+
getAudioGraph(): Promise<IAudioGraph>;
7171
setAudioContext(customAudioContext: IAudioContext): void;
7272
getAudioContext(): Promise<IAudioContext>;
7373
}

build/library/audio.js

Lines changed: 42 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/library/core.js

Lines changed: 15 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/visualizer-player/client/bootstrap.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,14 @@ $(function () {
167167

168168
let options: ICoreOptions = {
169169
soundsBaseUrl: 'https://mp3l.jamendo.com/?trackid=',
170-
playingProgressIntervalTime: 500
170+
playingProgressIntervalTime: 500,
171+
//volume: 80
171172
};
172-
173+
173174
let player = new PlayerCore(options);
175+
176+
player.setVolume(80);
177+
174178
let visualizerAudioGraph: any = {};
175179

176180
player.getAudioContext().then((audioContext) => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"scripts": {
2424
"test": "echo \"Error: no test specified\" && exit 1"
2525
},
26-
"version": "0.7.1",
26+
"version": "0.8.0",
2727
"dependencies": {
2828
"@types/webaudioapi": "^0.0.27"
2929
},

source/library/audio.ts

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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

9398
export interface ISourceNodeOptions {
@@ -97,21 +102,24 @@ export interface ISourceNodeOptions {
97102

98103
export 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

source/library/core.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
'use strict';
33

44
import { PlayerSound, ISound, ISoundAttributes, ISoundSource } from './sound';
5-
import { PlayerAudio, IAudioGraph, IAudioContext } from './audio';
5+
import { PlayerAudio, IAudioGraph, IAudioContext, IAudioOptions } from './audio';
66
import { PlayerRequest } from './request';
77
import { PlayerError, IPlayerError } from './error';
88

@@ -99,8 +99,14 @@ export class PlayerCore {
9999
this._isWebAudioApiSupported = false;
100100
}
101101

102+
let audioOptions: IAudioOptions = {
103+
volume: this._volume,
104+
customAudioContext: this._customAudioContext,
105+
customAudioGraph: this._customAudioGraph
106+
};
107+
102108
// player audio library instance
103-
this._playerAudio = new PlayerAudio(this._customAudioContext, this._customAudioGraph);
109+
this._playerAudio = new PlayerAudio(audioOptions);
104110

105111
}
106112

@@ -796,12 +802,20 @@ export class PlayerCore {
796802

797803
}
798804

799-
public getAudioGraph(): IAudioGraph {
805+
public getAudioGraph(): Promise<IAudioGraph> {
806+
807+
return new Promise((resolve, reject) => {
808+
809+
this._playerAudio.getAudioGraph().then((audioGraph: IAudioGraph) => {
810+
811+
this._customAudioGraph = audioGraph;
800812

801-
this._customAudioGraph = this._playerAudio.getAudioGraph();
813+
resolve(audioGraph);
802814

803-
return this._customAudioGraph;
815+
}).catch(reject);
804816

817+
});
818+
805819
}
806820

807821
public setAudioContext(customAudioContext: IAudioContext) {
@@ -822,7 +836,7 @@ export class PlayerCore {
822836

823837
resolve(audioContext);
824838

825-
});
839+
}).catch(reject);
826840

827841
});
828842

0 commit comments

Comments
 (0)