@@ -44,7 +44,7 @@ function AudioEffectStruct(_type) {
44
44
this . nodes = [ ] ;
45
45
46
46
this . type = _type ;
47
- this . params = { } ;
47
+ this . params = [ ] ;
48
48
49
49
// Define user-facing properties
50
50
Object . defineProperties ( this , {
@@ -60,42 +60,51 @@ function AudioEffectStruct(_type) {
60
60
gmlbypass : {
61
61
enumerable : true ,
62
62
get : ( ) => {
63
- return this . params . bypass ;
63
+ return this . params [ AudioEffectStruct . Index . Bypass ] ;
64
64
} ,
65
65
set : ( _state ) => {
66
- this . setParam ( AudioEffectStruct . paramDescriptors ( ) . bypass , _state ) ;
66
+ const val = this . setParam ( AudioEffectStruct . Index . Bypass , _state ) ;
67
67
68
68
this . nodes . forEach ( ( _node ) => {
69
69
const bypass = _node . parameters . get ( "bypass" ) ;
70
- bypass . value = this . params . bypass ;
70
+ bypass . value = val ;
71
71
} ) ;
72
72
}
73
73
}
74
74
} ) ;
75
75
}
76
76
77
- AudioEffectStruct . Create = function ( _type , _params ) {
77
+ AudioEffectStruct . GetStructType = function ( _type ) {
78
78
switch ( _type )
79
79
{
80
- case AudioEffect . Type . Bitcrusher : return new BitcrusherEffectStruct ( _params ) ;
81
- case AudioEffect . Type . Delay : return new DelayEffectStruct ( _params ) ;
82
- case AudioEffect . Type . Gain : return new GainEffectStruct ( _params ) ;
83
- case AudioEffect . Type . HPF2 : return new HPF2EffectStruct ( _params ) ;
84
- case AudioEffect . Type . LPF2 : return new LPF2EffectStruct ( _params ) ;
85
- case AudioEffect . Type . Reverb1 : return new Reverb1EffectStruct ( _params ) ;
86
- case AudioEffect . Type . Tremolo : return new TremoloEffectStruct ( _params ) ;
87
- case AudioEffect . Type . PeakEQ : return new PeakEQEffectStruct ( _params ) ;
88
- case AudioEffect . Type . HiShelf : return new HiShelfEffectStruct ( _params ) ;
89
- case AudioEffect . Type . LoShelf : return new LoShelfEffectStruct ( _params ) ;
90
- case AudioEffect . Type . EQ : return new EQEffectStruct ( _params ) ;
91
- case AudioEffect . Type . Compressor : return new CompressorEffectStruct ( _params ) ;
92
- default : return null ;
80
+ case AudioEffect . Type . Bitcrusher : return BitcrusherEffectStruct ;
81
+ case AudioEffect . Type . Delay : return DelayEffectStruct ;
82
+ case AudioEffect . Type . Gain : return GainEffectStruct ;
83
+ case AudioEffect . Type . HPF2 : return HPF2EffectStruct ;
84
+ case AudioEffect . Type . LPF2 : return LPF2EffectStruct ;
85
+ case AudioEffect . Type . Reverb1 : return Reverb1EffectStruct ;
86
+ case AudioEffect . Type . Tremolo : return TremoloEffectStruct ;
87
+ case AudioEffect . Type . PeakEQ : return PeakEQEffectStruct ;
88
+ case AudioEffect . Type . HiShelf : return HiShelfEffectStruct ;
89
+ case AudioEffect . Type . LoShelf : return LoShelfEffectStruct ;
90
+ case AudioEffect . Type . EQ : return EQEffectStruct ;
91
+ case AudioEffect . Type . Compressor : return CompressorEffectStruct ;
92
+ default : return undefined ;
93
93
}
94
94
} ;
95
95
96
- AudioEffectStruct . paramDescriptors = ( ) => ( {
97
- bypass : { name : "bypass" , integer : true , defaultValue : 0 , minValue : 0 , maxValue : 1 }
98
- } ) ;
96
+ AudioEffectStruct . Create = function ( _type , _params ) {
97
+ const structType = AudioEffectStruct . GetStructType ( _type ) ;
98
+ return ( structType === undefined ) ? undefined : new structType ( _params ) ;
99
+ } ;
100
+
101
+ AudioEffectStruct . Index = {
102
+ Bypass : 0
103
+ } ;
104
+
105
+ AudioEffectStruct . ParamDescriptors = [
106
+ { name : "bypass" , integer : true , defaultValue : 0 , minValue : 0 , maxValue : 1 }
107
+ ] ;
99
108
100
109
AudioEffectStruct . prototype . addInstance = function ( ) {
101
110
const node = g_WorkletNodeManager . createEffect ( this ) ;
@@ -105,27 +114,40 @@ AudioEffectStruct.prototype.addInstance = function() {
105
114
return ret ;
106
115
} ;
107
116
108
- AudioEffectStruct . prototype . initParams = function ( _params , _descriptors ) {
109
- Object . values ( _descriptors ) . forEach ( _desc => {
110
- const val = ( ( ) => {
111
- if ( _params === undefined || _params [ "gml" + _desc . name ] === undefined ) {
112
- return _desc . defaultValue ;
113
- }
114
-
115
- return _params [ "gml" + _desc . name ] ;
116
- } ) ( ) ;
117
+ AudioEffectStruct . prototype . initParams = function ( _params ) {
118
+ const descriptors = this . getParamDescriptors ( ) ;
119
+
120
+ descriptors . forEach ( ( _desc , _idx ) => {
121
+ let val = _desc . defaultValue ;
122
+
123
+ if ( _params !== undefined && _params [ "gml" + _desc . name ] !== undefined ) {
124
+ val = _params [ "gml" + _desc . name ] ;
125
+ }
117
126
118
- this . setParam ( _desc , val ) ;
127
+ this . setParam ( _idx , val ) ;
119
128
} ) ;
120
129
} ;
121
130
122
- AudioEffectStruct . prototype . setParam = function ( _desc , _val ) {
123
- _val = clamp ( _val , _desc . minValue , _desc . maxValue ) ;
131
+ AudioEffectStruct . prototype . setParam = function ( _idx , _val ) {
132
+ const structType = AudioEffectStruct . GetStructType ( this . type ) ;
133
+ const desc = structType . ParamDescriptors [ _idx ] ;
134
+
135
+ _val = clamp ( _val , desc . minValue , desc . maxValue ) ;
124
136
125
- if ( _desc . integer === true )
137
+ if ( desc . integer === true )
126
138
_val = ~ ~ _val ;
127
139
128
- this . params [ _desc . name ] = _val ;
140
+ this . params [ _idx ] = _val ;
141
+ return _val ;
142
+ } ;
143
+
144
+ AudioEffectStruct . prototype . getParamDescriptors = function ( ) {
145
+ const structType = AudioEffectStruct . GetStructType ( this . type ) ;
146
+ return structType . ParamDescriptors ;
147
+ } ;
148
+
149
+ AudioEffectStruct . prototype . getParamDescriptor = function ( _idx ) {
150
+ return this . getParamDescriptors ( ) [ _idx ] ;
129
151
} ;
130
152
131
153
AudioEffectStruct . prototype . removeNode = function ( _node ) {
@@ -136,4 +158,32 @@ AudioEffectStruct.prototype.removeNode = function(_node) {
136
158
this . nodes . splice ( idx , 1 ) ;
137
159
}
138
160
} ;
161
+
162
+ AudioEffectStruct . prototype . updateFreqDesc = function ( _desc ) {
163
+ if ( this . isFilter ( ) === false ) {
164
+ return _desc ;
165
+ }
166
+
167
+ if ( _desc . name !== "cutoff" && _desc . name !== "freq" ) {
168
+ return _desc ;
169
+ }
170
+
171
+ _desc . maxValue = g_WebAudioContext ? Math . min ( g_WebAudioContext . sampleRate / 2 , _desc . maxValue )
172
+ : _desc . maxValue ;
173
+ _desc . defaultValue = Math . min ( _desc . defaultValue , _desc . maxValue ) ;
174
+ return _desc ;
175
+ } ;
176
+
177
+ AudioEffectStruct . prototype . isFilter = function ( ) {
178
+ switch ( this . type ) {
179
+ case AudioEffect . Type . HiShelf :
180
+ case AudioEffect . Type . HPF2 :
181
+ case AudioEffect . Type . LoShelf :
182
+ case AudioEffect . Type . LPF2 :
183
+ case AudioEffect . Type . PeakEQ :
184
+ return true ;
185
+ default :
186
+ return false ;
187
+ }
188
+ } ;
139
189
// @endif
0 commit comments