1

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", &note, &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;
            }
  • You might find this overview http://www.topherlee.com/software/pcm-tut-wavformat.html of the RIFF format (wav-file) together with a simple example in C at https://karplus4arduino.wordpress.com/2011/10/08/making-wav-files-from-c-programs/ useful for your task. – deamentiaemundi Jul 30 '18 at 19:24
  • Thanks! I think I understand most of it, but, given that I didn't go through any of that trouble to play the note the first time around, I'm v skeptical about going down this route. – Orin Pemulus Jul 30 '18 at 22:37
  • You probably used an external library for that. If you did it in Windows you could have used `sound()`, `delay()`, and `nosound()` from `dos.h`. On e.g.: Linux you could use the console-beep as described in https://stackoverflow.com/questions/10072909/beep-on-linux-in-c But both of these methods do not write a file, they just play a sound. Another, slightly simpler method would be to use the `au` format (does not need the file length in the header and is hence streamable. Sort of.) – deamentiaemundi Jul 30 '18 at 23:19

0 Answers0