Skip to content

Comment on Frequency Modulation (FM) with Web Audio APIparent

Comments

Hey, no problem. FM synthesis is one of my greater interests! With modulating the phase offset I mean that instead of doing

    carrier = sin((f + modulator) * pi)
(a typical way of modulating the frequency) you can do
    carrier = sin(f * pi + modulator)
The modulator could be scaled by something from 0 to 2 pi but experiment with what sounds best.

The reason for doing this to begin with is probably that simply adding an offset in a sin table readout is probably a more effective design than adding the offset to the phase accumulator, especially if you want a consistent timbre without scaling the modulator amplitude along with the carrier frequency, and don't want to deal with negative frequencies.

Another nice thing you get for free with phase modulation is that you can do stable feedback modulation. In essence, you use the last calculated sample value for an operator (envelope * oscillator) as a modulation source in itself. In code, something like

    float generate_sample() {
        sample = envelope * sin(f * pi + old_sample * feedback_level);
        old_sample = sample;
    }
It's very effective for brass sounds or harsher sounds like trying to model distorted guitars
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.