

Frequency Modulation (FM) with Web Audio API - gren
http://greweb.me/2013/08/FM-audio-api/

======
boomlinde
It's a nice article, but I think there are some important things to note here:

Typically, what we call "FM synthesis" is implemented as phase modulation --
instead of modulating the frequency of the carrier, you modulate a phase
offset. That way, the amplitude of the modulator won't have to be scaled with
the frequency of the carrier for a uniform timbre over the whole frequency.

My second note is that most FM synthesizers (the whole Yamaha range,
basically) don't define the frequency ratio increments in octaves or quarter
octaves, but simply as multiples of the base frequency, producing a harmonic
series. Most implementations do this in integer increments, but others like
the DX7 and TX81z let you use fractional frequency multiples.

~~~
gren
I have no music background, so thanks a lot, your feedback is really helpful
:)

When you say modulating a phase offset instead of frequency, does this mean
you have like 2 oscillators A and B set to the same frequency, where you
modulate the offset of A, and you connect both A and B to the audio output?

About your second note, you are so right! Actually it was a mistake in my
article, this is what I've done actually but my explanation was a bit wrong.
My first demo is a multiple of the frequency. My second demo is a multiple of
(carrier frequency/4). Nothing related to octave, I shouldn't have mentioned
that (I will fix it ASAP).

~~~
boomlinde
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

------
tfj9eor9e
For what it is worth, I have made a similar work with much less details and
very poorly designed, but here are some cool FM instruments:
[https://github.com/tfjgeorge/jsfm#gong](https://github.com/tfjgeorge/jsfm#gong)

To hear the sound click the speaker on the right (works at least with Chrome).

~~~
gren
Woo, that's cool man! All these demos around Web Audio API.. Future is
awesome!

