@@ -2,15 +2,74 @@ import { generatorLimits } from "../../../soundbank/basic_soundbank/generator_ty
22import type { MIDIChannel } from "./midi_channel" ;
33import type { Voice } from "./voice" ;
44import { modulatorSources } from "../../../soundbank/enums" ;
5- import { NON_CC_INDEX_OFFSET } from "./controller_tables" ; /**
5+ import { NON_CC_INDEX_OFFSET } from "./controller_tables" ;
6+ import { customControllers } from "../../enums" ;
7+
8+ /**
69 * Compute_modulator.ts
710 * purpose: contains a function for computing all modulators
811 */
12+ const EFFECT_MODULATOR_TRANSFORM_MULTIPLIER = 1000 / 200 ;
913
1014/**
11- * Compute_modulator.ts
12- * purpose: contains a function for computing all modulators
15+ * Computes a given modulator
16+ * @param voice the voice of this modulator.
17+ * @param pitchWheel the pitch wheel value, as channel determines if it's a per-note or a global value.
18+ * @param modulatorIndex the modulator to compute
19+ * @returns the computed value
1320 */
21+ export function computeModulator (
22+ this : MIDIChannel ,
23+ voice : Voice ,
24+ pitchWheel : number ,
25+ modulatorIndex : number
26+ ) {
27+ const modulator = voice . modulators [ modulatorIndex ] ;
28+ if ( modulator . transformAmount === 0 ) {
29+ voice . modulatorValues [ modulatorIndex ] = 0 ;
30+ return 0 ;
31+ }
32+ const sourceValue = modulator . primarySource . getValue (
33+ this . midiControllers ,
34+ pitchWheel ,
35+ voice
36+ ) ;
37+ const secondSrcValue = modulator . secondarySource . getValue (
38+ this . midiControllers ,
39+ pitchWheel ,
40+ voice
41+ ) ;
42+
43+ // See the comment for isEffectModulator (modulator.ts in basic_soundbank) for explanation
44+ let transformAmount = modulator . transformAmount ;
45+ if ( modulator . isEffectModulator && transformAmount <= 1000 ) {
46+ transformAmount *= EFFECT_MODULATOR_TRANSFORM_MULTIPLIER ;
47+ transformAmount = Math . min ( transformAmount , 1000 ) ;
48+ }
49+
50+ // Compute the modulator
51+ let computedValue = sourceValue * secondSrcValue * transformAmount ;
52+
53+ if ( modulator . transformType === 2 ) {
54+ // Abs value
55+ computedValue = Math . abs ( computedValue ) ;
56+ }
57+
58+ // Resonant modulator: take its value and ensure that it won't change the final gain
59+ if ( modulator . isDefaultResonantModulator ) {
60+ // Half the gain, negates the filter
61+ voice . resonanceOffset = Math . max ( 0 , computedValue / 2 ) ;
62+ }
63+
64+ // Modulation depth
65+ if ( modulator . isModWheelModulator ) {
66+ computedValue *=
67+ this . customControllers [ customControllers . modulationMultiplier ] ;
68+ }
69+
70+ voice . modulatorValues [ modulatorIndex ] = computedValue ;
71+ return computedValue ;
72+ }
1473
1574/**
1675 * Computes modulators of a given voice. Source and index indicate what modulators shall be computed.
@@ -53,7 +112,7 @@ export function computeModulators(
53112 Math . max (
54113 - 32_768 ,
55114 modulatedGenerators [ mod . destination ] +
56- voice . computeModulator ( this . midiControllers , pitch , i )
115+ this . computeModulator ( voice , pitch , i )
57116 )
58117 ) ;
59118 }
@@ -87,7 +146,7 @@ export function computeModulators(
87146 const destination = mod . destination ;
88147 let outputValue = generators [ destination ] ;
89148 // Compute our modulator
90- voice . computeModulator ( this . midiControllers , pitch , i ) ;
149+ this . computeModulator ( voice , pitch , i ) ;
91150
92151 // Sum the values of all modulators for this destination
93152 for ( let j = 0 ; j < modulators . length ; j ++ ) {
0 commit comments