Skip to content

Instantly share code, notes, and snippets.

@jamesu
Created August 8, 2012 17:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesu/3296747 to your computer and use it in GitHub Desktop.
Save jamesu/3296747 to your computer and use it in GitHub Desktop.
Generate DTMF tones
//
// Generate DTMF tones
//
#include <stdio.h>
#include <math.h>
float OutData[44100];
typedef struct ToneInfo {
const char *name;
int tone1;
int tone2;
} ToneInfo;
ToneInfo TonesToGenerate[] = {
{"1", 1209, 697},
{"2", 1336, 697},
{"3", 1477, 697},
{"A", 1633, 697},
{"4", 1209, 770},
{"5", 1336, 770},
{"6", 1477, 770},
{"B", 1633, 770},
{"7", 1209, 852},
{"8", 1336, 852},
{"9", 1477, 852},
{"C", 1633, 852},
{"*", 1209, 941},
{"0", 1336, 941},
{"#", 1477, 941},
{"D", 1633, 941}
};
void generate_dtmf(int tone1, int tone2, int rate, float *out)
{
float increment = (2.0f * M_PI) / ((float)rate / tone1);
float sampleValue = 0;
int i;
// Add first tone at half volume
for (i=0; i<rate; i++) {
out[i] = sinf(sampleValue) * 0.5;
sampleValue += increment;
}
// Add second tone at half volume
increment = (2.0f * M_PI) / ((float)rate / tone2);
sampleValue = 0;
for (i=0; i<rate; i++) {
out[i] += sinf(sampleValue) * 0.5;
sampleValue += increment;
}
}
int main(int argc, char **argv)
{
int i;
// Generate all the tones as 32bit float
for (i=0; i<sizeof(TonesToGenerate)/sizeof(ToneInfo); i++) {
char buffer[128];
sprintf(buffer, "T-%s.raw", TonesToGenerate[i].name);
FILE *fp = fopen(buffer, "w");
generate_dtmf(TonesToGenerate[i].tone1, TonesToGenerate[i].tone2, 44100, OutData);
fwrite(OutData, sizeof(float), 44100, fp);
fclose(fp);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment