21
21
02110-1301 USA
22
22
*/
23
23
24
+ import libcsound from './libcsound.js'
25
+
24
26
// Setup a single global AudioContext object
25
- var CSOUND_AUDIO_CONTEXT = CSOUND_AUDIO_CONTEXT ||
27
+ window . CSOUND_AUDIO_CONTEXT = window . CSOUND_AUDIO_CONTEXT ||
26
28
( function ( ) {
27
29
try {
28
30
var AudioContext = window . AudioContext || window . webkitAudioContext ;
@@ -35,9 +37,10 @@ var CSOUND_AUDIO_CONTEXT = CSOUND_AUDIO_CONTEXT ||
35
37
return null ;
36
38
} ( ) ) ;
37
39
40
+ let WAM = window . WAM = { }
41
+
38
42
39
43
// Global singleton variables
40
- var AudioWorkletGlobalScope = AudioWorkletGlobalScope || { } ;
41
44
var CSOUND ;
42
45
43
46
/** This E6 class is used to setup scripts and
@@ -62,51 +65,46 @@ class CsoundScriptProcessorNodeFactory {
62
65
*/
63
66
static importScripts ( script_base = './' ) {
64
67
return new Promise ( ( resolve ) => {
65
- CsoundScriptProcessorNodeFactory . loadScript ( script_base + 'libcsound.js' , ( ) => {
66
- AudioWorkletGlobalScope . WAM = { }
67
- let WAM = AudioWorkletGlobalScope . WAM ;
68
-
69
- WAM [ "ENVIRONMENT" ] = "WEB" ;
70
- WAM [ "print" ] = ( t ) => console . log ( t ) ;
71
- WAM [ "printErr" ] = ( t ) => console . log ( t ) ;
72
- WAM [ "locateFile" ] = ( f ) => script_base + f ;
73
-
74
- AudioWorkletGlobalScope . libcsound ( WAM ) . then ( ( ) => {
75
-
76
- // Cache cwrap functions into CSOUND global object
77
- CSOUND = {
78
- new : WAM . cwrap ( 'CsoundObj_new' , [ 'number' ] , null ) ,
79
- compileCSD : WAM . cwrap ( 'CsoundObj_compileCSD' , [ 'number' ] , [ 'number' , 'string' ] ) ,
80
- evaluateCode : WAM . cwrap ( 'CsoundObj_evaluateCode' , [ 'number' ] , [ 'number' , 'string' ] ) ,
81
- readScore : WAM . cwrap ( 'CsoundObj_readScore' , [ 'number' ] , [ 'number' , 'string' ] ) ,
82
- reset : WAM . cwrap ( 'CsoundObj_reset' , null , [ 'number' ] ) ,
83
- getOutputBuffer : WAM . cwrap ( 'CsoundObj_getOutputBuffer' , [ 'number' ] , [ 'number' ] ) ,
84
- getInputBuffer : WAM . cwrap ( 'CsoundObj_getInputBuffer' , [ 'number' ] , [ 'number' ] ) ,
85
- getControlChannel : WAM . cwrap ( 'CsoundObj_getControlChannel' , [ 'number' ] , [ 'number' , 'string' ] ) ,
86
- setControlChannel : WAM . cwrap ( 'CsoundObj_setControlChannel' , null , [ 'number' , 'string' , 'number' ] ) ,
87
- setStringChannel : WAM . cwrap ( 'CsoundObj_setStringChannel' , null , [ 'number' , 'string' , 'string' ] ) ,
88
- getKsmps : WAM . cwrap ( 'CsoundObj_getKsmps' , [ 'number' ] , [ 'number' ] ) ,
89
- performKsmps : WAM . cwrap ( 'CsoundObj_performKsmps' , [ 'number' ] , [ 'number' ] ) ,
90
- render : WAM . cwrap ( 'CsoundObj_render' , null , [ 'number' ] ) ,
91
- getInputChannelCount : WAM . cwrap ( 'CsoundObj_getInputChannelCount' , [ 'number' ] , [ 'number' ] ) ,
92
- getOutputChannelCount : WAM . cwrap ( 'CsoundObj_getOutputChannelCount' , [ 'number' ] , [ 'number' ] ) ,
93
- getTableLength : WAM . cwrap ( 'CsoundObj_getTableLength' , [ 'number' ] , [ 'number' , 'number' ] ) ,
94
- getTable : WAM . cwrap ( 'CsoundObj_getTable' , [ 'number' ] , [ 'number' , 'number' ] ) ,
95
- getZerodBFS : WAM . cwrap ( 'CsoundObj_getZerodBFS' , [ 'number' ] , [ 'number' ] ) ,
96
- setMidiCallbacks : WAM . cwrap ( 'CsoundObj_setMidiCallbacks' , null , [ 'number' ] ) ,
97
- pushMidiMessage : WAM . cwrap ( 'CsoundObj_pushMidiMessage' , null , [ 'number' , 'number' , 'number' , 'number' ] ) ,
98
- setOutputChannelCallback : WAM . cwrap ( 'CsoundObj_setOutputChannelCallback' , null , [ 'number' , 'number' ] ) ,
99
- compileOrc : WAM . cwrap ( 'CsoundObj_compileOrc' , 'number' , [ 'number' , 'string' ] ) ,
100
- setOption : WAM . cwrap ( 'CsoundObj_setOption' , null , [ 'number' , 'string' ] ) ,
101
- prepareRT : WAM . cwrap ( 'CsoundObj_prepareRT' , null , [ 'number' ] ) ,
102
- getScoreTime : WAM . cwrap ( 'CsoundObj_getScoreTime' , null , [ 'number' ] ) ,
103
- setTable : WAM . cwrap ( 'CsoundObj_setTable' , null , [ 'number' , 'number' , 'number' , 'number' ] ) ,
104
- destroy : WAM . cwrap ( 'CsoundObj_destroy' , null , [ 'number' ] )
105
- }
106
-
107
- resolve ( ) ;
108
- } )
109
- } )
68
+ WAM [ "ENVIRONMENT" ] = "WEB" ;
69
+ WAM [ "print" ] = ( t ) => console . log ( t ) ;
70
+ WAM [ "printErr" ] = ( t ) => console . log ( t ) ;
71
+ WAM [ "locateFile" ] = ( f ) => script_base + f ;
72
+
73
+ libcsound ( WAM ) . then ( ( ) => {
74
+
75
+ // Cache cwrap functions into CSOUND global object
76
+ CSOUND = {
77
+ new : WAM . cwrap ( 'CsoundObj_new' , [ 'number' ] , null ) ,
78
+ compileCSD : WAM . cwrap ( 'CsoundObj_compileCSD' , [ 'number' ] , [ 'number' , 'string' ] ) ,
79
+ evaluateCode : WAM . cwrap ( 'CsoundObj_evaluateCode' , [ 'number' ] , [ 'number' , 'string' ] ) ,
80
+ readScore : WAM . cwrap ( 'CsoundObj_readScore' , [ 'number' ] , [ 'number' , 'string' ] ) ,
81
+ reset : WAM . cwrap ( 'CsoundObj_reset' , null , [ 'number' ] ) ,
82
+ getOutputBuffer : WAM . cwrap ( 'CsoundObj_getOutputBuffer' , [ 'number' ] , [ 'number' ] ) ,
83
+ getInputBuffer : WAM . cwrap ( 'CsoundObj_getInputBuffer' , [ 'number' ] , [ 'number' ] ) ,
84
+ getControlChannel : WAM . cwrap ( 'CsoundObj_getControlChannel' , [ 'number' ] , [ 'number' , 'string' ] ) ,
85
+ setControlChannel : WAM . cwrap ( 'CsoundObj_setControlChannel' , null , [ 'number' , 'string' , 'number' ] ) ,
86
+ setStringChannel : WAM . cwrap ( 'CsoundObj_setStringChannel' , null , [ 'number' , 'string' , 'string' ] ) ,
87
+ getKsmps : WAM . cwrap ( 'CsoundObj_getKsmps' , [ 'number' ] , [ 'number' ] ) ,
88
+ performKsmps : WAM . cwrap ( 'CsoundObj_performKsmps' , [ 'number' ] , [ 'number' ] ) ,
89
+ render : WAM . cwrap ( 'CsoundObj_render' , null , [ 'number' ] ) ,
90
+ getInputChannelCount : WAM . cwrap ( 'CsoundObj_getInputChannelCount' , [ 'number' ] , [ 'number' ] ) ,
91
+ getOutputChannelCount : WAM . cwrap ( 'CsoundObj_getOutputChannelCount' , [ 'number' ] , [ 'number' ] ) ,
92
+ getTableLength : WAM . cwrap ( 'CsoundObj_getTableLength' , [ 'number' ] , [ 'number' , 'number' ] ) ,
93
+ getTable : WAM . cwrap ( 'CsoundObj_getTable' , [ 'number' ] , [ 'number' , 'number' ] ) ,
94
+ getZerodBFS : WAM . cwrap ( 'CsoundObj_getZerodBFS' , [ 'number' ] , [ 'number' ] ) ,
95
+ setMidiCallbacks : WAM . cwrap ( 'CsoundObj_setMidiCallbacks' , null , [ 'number' ] ) ,
96
+ pushMidiMessage : WAM . cwrap ( 'CsoundObj_pushMidiMessage' , null , [ 'number' , 'number' , 'number' , 'number' ] ) ,
97
+ setOutputChannelCallback : WAM . cwrap ( 'CsoundObj_setOutputChannelCallback' , null , [ 'number' , 'number' ] ) ,
98
+ compileOrc : WAM . cwrap ( 'CsoundObj_compileOrc' , 'number' , [ 'number' , 'string' ] ) ,
99
+ setOption : WAM . cwrap ( 'CsoundObj_setOption' , null , [ 'number' , 'string' ] ) ,
100
+ prepareRT : WAM . cwrap ( 'CsoundObj_prepareRT' , null , [ 'number' ] ) ,
101
+ getScoreTime : WAM . cwrap ( 'CsoundObj_getScoreTime' , null , [ 'number' ] ) ,
102
+ setTable : WAM . cwrap ( 'CsoundObj_setTable' , null , [ 'number' , 'number' , 'number' , 'number' ] ) ,
103
+ destroy : WAM . cwrap ( 'CsoundObj_destroy' , null , [ 'number' ] )
104
+ }
105
+
106
+ resolve ( ) ;
107
+ } )
110
108
} ) ;
111
109
}
112
110
@@ -132,7 +130,7 @@ class CsoundScriptProcessorNodeFactory {
132
130
* numberOfOutputs
133
131
* @returns {object } A new CsoundScriptProcessorNode
134
132
*/
135
- CsoundScriptProcessorNode = function ( context , options ) {
133
+ let CsoundScriptProcessorNode = function ( context , options ) {
136
134
var spn = context . createScriptProcessor ( 0 , options . numberOfInputs , options . numberOfOutputs ) ;
137
135
spn . inputCount = options . numberOfInputs ;
138
136
spn . outputCount = options . numberOfOutputs ;
@@ -481,10 +479,13 @@ CsoundScriptProcessorNode = function(context, options) {
481
479
}
482
480
}
483
481
484
- let WAM = AudioWorkletGlobalScope . WAM ;
485
482
WAM [ "print" ] = ( t ) => spn . msgCallback ( t ) ;
486
483
WAM [ "printErr" ] = ( t ) => spn . msgCallback ( t ) ;
487
484
488
485
return Object . assign ( spn , CsoundMixin ) ;
489
486
}
490
487
488
+ window . CsoundScriptProcessorNodeFactory = CsoundScriptProcessorNodeFactory ;
489
+ window . CsoundScriptProcessorNode = CsoundScriptProcessorNode ;
490
+
491
+ export default { CsoundScriptProcessorNode, CsoundScriptProcessorNodeFactory} ;
0 commit comments