This is from a program I wrote while taking an electrical engineering course at uni. The ultimate goal was to make a spectrum analyzer. However, this was accomplished in small steps. First, we simply made a note generator and played the note through the speakers. Now that I am reviewing the stuff I did, I'm having a hard time figuring out how I made this code play a note. For my IDE, I'm using notepad and Cygwin as my compiler. When I redirect the program's output, the note, to a .txt file, I find that it does not include newlines as is written in the program. Although I don't remember the file format that was used to play the note, I'm pretty sure it required a specific arrangement for each line, starting with ";Samplerate" for line 1.
TL;DR: How do I make this note generator code, compiled in Cygwin, make a playable audio file?
#include<stdio.h>
#include<math.h>
#define AMPLITUDE 1
#define SAMPLERATE 48000
#define DURATION 5*48000
double freq(char note, char mod, int oct) {
double f; // returns the frequency given the equation
switch (note) { // converts note letter input to how far shifted the note is from middle C
case 'A':
note = 0;
break;
case 'B':
note = 2;
break;
case 'C':
note = -9;
break;
case 'D':
note = -7;
break;
case 'E':
note = -5;
break;
case 'F':
note = -4;
break;
case 'G':
note = -2;
break;
}
// accounts for potential sharp of flat notes
if (mod != ' ') {
if (!(note == 'E' && note == 'B')) {
if (mod == '#') {
note++;
}
}
if (!(note == 'C'&& note == 'F')) {
if (mod == 'b') { note--; }
}
}
f = 440 * pow(1.05946, 12 * oct - 48 + note);
return f;
}
int main() {
double i;
double a;
double t;
char note, mod;
int oct;
scanf("%c%c%d", ¬e, &mod, &oct); // takes note mod and octive in through terminal
//prints the signal/note (time next to amplitude)
printf(";Sample Rate %d\n", SAMPLERATE);
printf(";the frequency of %c%c%d is %lf\n", note, mod, oct, freq(note, mod, oct));
for (i = 0; i <= DURATION; i++) {
a = AMPLITUDE * sin((2 * M_PI*freq(note, mod, oct)*i) / 48000.0);
t = i / 48000;
printf("%lf %lf\n", t, a);
}
return 0;
}