17.01.2013 Views

musicdsp.org source code archive - WSInf

musicdsp.org source code archive - WSInf

musicdsp.org source code archive - WSInf

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Phase modulation Vs. Frequency modulation (click this to go back to the index)<br />

References : Posted by Bram<br />

Linked file : SimpleOscillator.h (this linked file is included below)<br />

Notes :<br />

This <strong>code</strong> shows what the difference is betwee FM and PM.<br />

The <strong>code</strong> is NOT optimised, nor should it be used like this.<br />

It is an EXAMPLE<br />

See linked file.<br />

Linked files<br />

////////////////////////////////////////////////////<br />

//<br />

// this <strong>code</strong> was NEVER MEANT TO BE USED.<br />

//<br />

// use as EXPLANATION ONLY for the difference between<br />

// Phase Modulation and Frequency Modulation.<br />

// there are MANY ways to speed this <strong>code</strong> up.<br />

//<br />

// bram@<strong>musicdsp</strong>.<strong>org</strong> | bram@smartelectronix.com<br />

//<br />

// ps:<br />

// we use the 'previous' value of the phase in all the algo's to make sure that<br />

// the first call to the getSampleXX() function returns the wave at phase 'zero'<br />

//<br />

////////////////////////////////////////////////////<br />

#include "math.h";<br />

#define Pi 3.141592f<br />

class SimpleOscillator<br />

{<br />

SimpleOscillator(const float sampleRate = 44100.f, const long tableSize = 4096)<br />

{<br />

this->tableSize = tableSize;<br />

this->sampleRate = sampleRate;<br />

phase = 0.f;<br />

makeTable();<br />

}<br />

~SimpleOscillator()<br />

{<br />

delete [] table;<br />

}<br />

// normal oscillator, no modulation<br />

//<br />

float generateSample(const float frequency)<br />

{<br />

float lookupPhase = phase;<br />

phase += frequency * (float)tableSize / sampleRate;<br />

wrap(phase);<br />

return lookup(lookupPhase);<br />

}<br />

// frequency modulation<br />

// the fm input should be in HZ.<br />

//<br />

// example:<br />

// osc1.getSampleFM(440.f, osc2.getSample(0.5f) * 5.f )<br />

// would give a signal where the frequency of the signal is<br />

// modulated between 435hz and 445hz at a 0.5hz rate<br />

//<br />

float generateSampleFM(const float frequency, const float fm)<br />

{<br />

float lookupPhase = phase;<br />

phase += (frequency + fm) * (float)tableSize / sampleRate;<br />

wrap(phase);<br />

return lookup(lookupPhase);<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!