@@ -19,38 +19,40 @@ type ProcessorPolyfill = {
1919window . AudioContext = window . AudioContext || window . webkitAudioContext ;
2020
2121if ( typeof AudioWorkletNode !== 'function' || ! ( 'audioWorklet' in AudioContext . prototype ) ) {
22- // @ts -ignore
23- AudioContext . prototype . audioWorklet = {
24- addModule : async function ( moduleURL : string | URL , options ?: WorkletOptions ) : Promise < void > {
25- return ;
26- } ,
27- } ;
28-
29- // @ts -ignore
30- // eslint-disable-next-line no-native-reassign
31- window . AudioWorkletNode = function ( context : AudioContext , processorName : string , options : any ) : ScriptProcessorNode {
32- const { numberOfChannels = 1 , frameLength = 512 } = options && options . processorOptions ;
33- const scriptProcessor : ScriptProcessorNode & ProcessorPolyfill = context . createScriptProcessor ( frameLength , numberOfChannels , numberOfChannels ) ;
34-
35- if ( ! scriptProcessor . port ) {
36- scriptProcessor . port = { } ;
37- }
38-
39- scriptProcessor . onaudioprocess = ( event : AudioProcessingEvent ) : void => {
40- if ( scriptProcessor . port && scriptProcessor . port . onmessage ) {
41- const buffer = [ ] ;
42- for ( let i = 0 ; i < event . inputBuffer . numberOfChannels ; i ++ ) {
43- buffer . push ( event . inputBuffer . getChannelData ( i ) ) ;
44- }
45- scriptProcessor . port . onmessage ( { data : { buffer } } as MessageEvent ) ;
46- }
22+ if ( AudioContext ) {
23+ // @ts -ignore
24+ AudioContext . prototype . audioWorklet = {
25+ addModule : async function ( moduleURL : string | URL , options ?: WorkletOptions ) : Promise < void > {
26+ return ;
27+ } ,
4728 } ;
4829
4930 // @ts -ignore
50- scriptProcessor . port . close = ( ) : void => {
51- return ;
52- } ;
31+ // eslint-disable-next-line no-native-reassign
32+ window . AudioWorkletNode = function ( context : AudioContext , processorName : string , options : any ) : ScriptProcessorNode {
33+ const { numberOfChannels = 1 , frameLength = 512 } = options && options . processorOptions ;
34+ const scriptProcessor : ScriptProcessorNode & ProcessorPolyfill = context . createScriptProcessor ( frameLength , numberOfChannels , numberOfChannels ) ;
35+
36+ if ( ! scriptProcessor . port ) {
37+ scriptProcessor . port = { } ;
38+ }
5339
54- return scriptProcessor ;
55- } ;
40+ scriptProcessor . onaudioprocess = ( event : AudioProcessingEvent ) : void => {
41+ if ( scriptProcessor . port && scriptProcessor . port . onmessage ) {
42+ const buffer = [ ] ;
43+ for ( let i = 0 ; i < event . inputBuffer . numberOfChannels ; i ++ ) {
44+ buffer . push ( event . inputBuffer . getChannelData ( i ) ) ;
45+ }
46+ scriptProcessor . port . onmessage ( { data : { buffer } } as MessageEvent ) ;
47+ }
48+ } ;
49+
50+ // @ts -ignore
51+ scriptProcessor . port . close = ( ) : void => {
52+ return ;
53+ } ;
54+
55+ return scriptProcessor ;
56+ } ;
57+ }
5658}
0 commit comments