sinshu / meltysynth

A SoundFont MIDI synthesizer for .NET
Other
136 stars 16 forks source link

Modulator support #48

Closed spessasus closed 3 months ago

spessasus commented 3 months ago

Hi, I've noticed that MeltySynth doesn't support modulators and I haven't seen an issue mentioning them so I'm opening this one.

Modulators are an integral part of the soundfont standard. They allow precise control of the parameters which allows the soundfont to define the way it responds to midi controllers. For example I could add support for the brightness controller (74) by mapping it to the initialFilterFC. But currently that doesn't work in MeltySynth.

sinshu commented 3 months ago

Thanks for the suggestion, but I do not intend to support modulators for the following reasons 😖

spessasus commented 3 months ago

Thanks for the quick response, @sinshu.

Modulators aren't as difficult as you might think. I've implemented them in my own synthesizer and they are essentially lookup tables. Here's how I did it: Create lookup tables that have values from 0 to 1 or -1 to 1 for all curve types, both directions and both polarities. Each array has 127 elements (midi Controllers have values from 0 to 127) Like this:

modulatorCurves[curveType][polarity][direction] = (127 long table for the controller value corresponding to the curve value of the controller)

Then when you compute the modulator, you simply do:

int controllerValue = midiControllerValues[modulator.sourceEnum]; // or if modulator doesn't use cc, get the value from the pitch, etc.
int secondaryControllerValue = // same here except for the second source
float sourceValue = modulatorCurves[modulator.souceCurveType][modulator.sourcePolarity][modulator.sourceDirection][controllerValue];
float secondarySourceValue= // same as above except for the second source
float value = modulator.amount * sourceValue * secondarySourceValue;
if(modulator.transformType == transformTypes.absolute)
   value = Math.abs(value);

voice.generators[modulator.destination] += value

You can see my implementation here and here also here (in my case I use 14 bit values for the controllers, but it works the same for 127 as described above)

spessasus commented 3 months ago

Also modulators are widely used in some popular soundfonts: Note: these numbers come from polyphone (when you open the soundfont, stats show at the bottom).

That's why I think that modulators should be supported by MeltySynth as it's the definitive soundfont synth for .NET.

sinshu commented 3 months ago

I took a look at your implementation, and the Modulator still seems complex to me. It would be difficult for me to implement and test this feature. Anyway, thank you for the suggestion ☺️