33
44import { PlayerError , IPlayerError } from './error' ;
55
6+ // Note to self: AudioGraph documentation
7+ // https://developer.mozilla.org/en-US/docs/Web/API/AudioNode
8+
69export interface IAudioGraph {
10+ // https://developer.mozilla.org/en-US/docs/Web/API/GainNode
711 gainNode : GainNode ;
12+ // https://developer.mozilla.org/en-US/docs/Web/API/PannerNode
813 pannerNode ?: PannerNode ;
14+ // https://developer.mozilla.org/en-US/docs/Web/API/StereoPannerNode
915 stereoPannerNode ?: StereoPannerNode ;
16+ // https://developer.mozilla.org/en-US/docs/Web/API/DelayNode
17+ delayNode ?: DelayNode ;
18+ // https://developer.mozilla.org/en-US/docs/Web/API/ScriptProcessorNode
19+ scriptProcessorNode ?: ScriptProcessorNode ;
20+ // https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode
21+ analyserNode ?: AnalyserNode ;
22+ // https://developer.mozilla.org/en-US/docs/Web/API/BiquadFilterNode
23+ biquadFilterNode ?: BiquadFilterNode ;
24+ // https://developer.mozilla.org/en-US/docs/Web/API/ChannelMergerNode
25+ channelMergeNode ?: ChannelMergerNode ;
26+ // https://developer.mozilla.org/en-US/docs/Web/API/ChannelSplitterNode
27+ channelSplitterNode ?: ChannelSplitterNode ;
28+ // https://developer.mozilla.org/en-US/docs/Web/API/ConvolverNode
29+ convolverNode ?: ConvolverNode ;
30+ // https://developer.mozilla.org/en-US/docs/Web/API/DynamicsCompressorNode
31+ dynamicCompressorNode ?: DynamicsCompressorNode ;
32+ // https://developer.mozilla.org/en-US/docs/Web/API/OscillatorNode
33+ oscillatorNode ?: OscillatorNode ;
34+ // https://developer.mozilla.org/en-US/docs/Web/API/WaveShaperNode
35+ waveShaperNode ?: WaveShaperNode ;
1036}
1137
1238export interface IAudioGraphOptions {
@@ -22,13 +48,16 @@ export class PlayerAudio {
2248
2349 protected _context : AudioContext ;
2450 protected _contextState : string ;
25- protected _audioGraph : IAudioGraph | null ;
51+ protected _audioGraph : IAudioGraph | null = null ;
2652
27- constructor ( ) {
53+ constructor ( customAudioGraph ?: IAudioGraph ) {
2854
2955 // initial context state is still "closed"
3056 this . _contextState = 'closed' ;
31- this . _audioGraph = null ;
57+
58+ if ( customAudioGraph !== undefined ) {
59+ this . _audioGraph = customAudioGraph ;
60+ }
3261
3362 // TODO: to speed up things would it be better to create a context in the constructor?
3463 // and suspend the context upon creating it until it gets used?
@@ -69,7 +98,7 @@ export class PlayerAudio {
6998
7099 this . _context = audioContext ;
71100
72- this . _createAudioGraph ( ) ;
101+ this . _connectAudioGraphGainToDestination ( ) ;
73102
74103 resolve ( ) ;
75104
@@ -135,27 +164,26 @@ export class PlayerAudio {
135164
136165 }
137166
138- protected _createAudioGraph ( ) {
167+ protected _connectAudioGraphGainToDestination ( ) {
139168
140- // https://developer.mozilla.org/en-US/docs/Web/API/GainNode
169+ if ( this . _audioGraph === null ) {
141170
142- let audioGraph = {
143- gainNode : this . _context . createGain ( )
144- } ;
171+ this . _audioGraph = {
172+ gainNode : this . _context . createGain ( )
173+ }
145174
146- // connect the gain node to the destination (speakers)
147- audioGraph . gainNode . connect ( this . _context . destination ) ;
175+ }
148176
149- this . _audioGraph = audioGraph ;
177+ // connect the gain node to the destination (speakers)
178+ // https://developer.mozilla.org/en-US/docs/Web/API/AudioDestinationNode
179+ this . _audioGraph . gainNode . connect ( this . _context . destination ) ;
150180
151181 }
152182
153183 public setAudioGraph ( audioGraph : IAudioGraph ) {
154184
155185 if ( this . _audioGraph !== null ) {
156-
157186 this . _destroyAudioGraph ( ) ;
158-
159187 }
160188
161189 this . _audioGraph = audioGraph ;
@@ -197,7 +225,7 @@ export class PlayerAudio {
197225
198226 }
199227
200- public connectSourceNodeToGraph ( sourceNode : AudioBufferSourceNode ) {
228+ public connectSourceNodeToGraphGainNode ( sourceNode : AudioBufferSourceNode ) {
201229
202230 sourceNode . connect ( this . _audioGraph . gainNode ) ;
203231
0 commit comments