import { Filterable } from '../Filterable' ; import { WebAudioContext } from './WebAudioContext' ; import { WebAudioUtils } from './WebAudioUtils' ; /** Output for cloning source node. */ interface SourceClone { /** Cloned audio buffer source */ source : AudioBufferSourceNode ; /** Independent volume control */ gain : GainNode ; } memberof class WebAudioNodes extends Filterable { default public static BUFFER_SIZE = 0 ; readonly public bufferSource : AudioBufferSourceNode ; readonly public gain : GainNode ; readonly public analyser : AnalyserNode ; readonly public context : WebAudioContext ; /** Private reference to the script processor node. */ private _script : ScriptProcessorNode ; param context constructor ( context ) { const audioContext = context . audioContext ; const bufferSource = audioContext . createBufferSource ( ) ; const gain = audioContext . createGain ( ) ; const analyser = audioContext . createAnalyser ( ) ; bufferSource . connect ( analyser ) ; analyser . connect ( gain ) ; gain . connect ( context . destination ) ; super ( analyser , gain ) ; this . context = context ; this . bufferSource = bufferSource ; this . gain = gain ; this . analyser = analyser ; } readonly public get script ( ) : ScriptProcessorNode { if ( ! this . _script ) { this . _script = this . context . audioContext . createScriptProcessor ( WebAudioNodes . BUFFER_SIZE ) ; this . _script . connect ( this . context . destination ) ; } return this . _script ; } /** Cleans up. */ public destroy ( ) { super . destroy ( ) ; this . bufferSource . disconnect ( ) ; if ( this . _script ) { this . _script . disconnect ( ) ; } this . gain . disconnect ( ) ; this . analyser . disconnect ( ) ; this . bufferSource = null ; this . _script = null ; this . gain = null ; this . analyser = null ; this . context = null ; } returns { SourceClone } public cloneBufferSource ( ) { const orig = this . bufferSource ; const source = this . context . audioContext . createBufferSource ( ) ; source . buffer = orig . buffer ; WebAudioUtils . setParamValue ( source . playbackRate , orig . playbackRate . value ) ; source . loop = orig . loop ; const gain = this . context . audioContext . createGain ( ) ; source . connect ( gain ) ; gain . connect ( this . destination ) ; return { source , gain } ; } readonly get bufferSize ( ) : number { return this . script . bufferSize ; } } export type { SourceClone } ; export { WebAudioNodes } ;