Last active
December 1, 2018 01:14
-
-
Save Jacajack/79044fbc3cb4284306069ca793cc7fab to your computer and use it in GitHub Desktop.
🎶 FM synthesis example for use with aplay - simple electric piano sound 🎶
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
#include <time.h> | |
#define SAMPLE_RATE 8000.0 | |
// Sample output | |
void outsample( float v ) | |
{ | |
// Clamp | |
if ( v > 1 ) v = 1; | |
else if ( v < -1 ) v = -1; | |
putchar( 255 * ( v + 1 ) / 2 ); | |
} | |
// Single harmonic function | |
float sinewave( double t, float freq ) | |
{ | |
return sin( t * freq * M_PI * 2 ); | |
} | |
int main( ) | |
{ | |
long cnt = 0; | |
double t = 0; | |
double dt = 1.0 / SAMPLE_RATE; | |
double tcarrier = 0; | |
float envelope = 0; | |
while ( 1 ) | |
{ | |
t = cnt / SAMPLE_RATE; | |
// Branch FM algorithm | |
tcarrier += dt \ | |
+ 0.0011 * sinewave( t, 1000 ) | |
+ 0.0015 * sinewave( t, 1002 ); | |
// Carrier frequency | |
float carrier = sinewave( tcarrier, 110 ); | |
// Simple envelope | |
if ( cnt % (long)( SAMPLE_RATE * 3 ) == 0 ) envelope = 1; | |
else if ( envelope <= 0 ) envelope = 0; | |
else envelope -= 0.75 / SAMPLE_RATE; | |
// Output the sample | |
outsample( carrier * envelope ); | |
cnt++; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment