forked from mareksuscak/cs50
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.c
59 lines (48 loc) Β· 1.29 KB
/
notes.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Prints frequencies of and outputs WAV file with all notes in an octave
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include "helpers.h"
#include "wav.h"
// Notes in an octave
const string NOTES[] = {"C", "C#", "D", "D#", "E", "F",
"F#", "G", "G#", "A", "A#", "B"
};
// Default octave
#define OCTAVE 4
int main(int argc, string argv[])
{
// Override default octave if specified at command line
int octave = OCTAVE;
if (argc == 2)
{
octave = atoi(argv[1]);
if (octave < 0 || octave > 8)
{
fprintf(stderr, "Invalid octave\n");
return 1;
}
}
else if (argc > 2)
{
fprintf(stderr, "Usage: notes [OCTAVE]\n");
return 1;
}
// Open file for writing
song s = song_open("notes.wav");
// Add each semitone
for (int i = 0, n = sizeof(NOTES) / sizeof(string); i < n; i++)
{
// Append octave to note
char note[4];
sprintf(note, "%s%i", NOTES[i], octave);
// Calculate frequency of note
int f = frequency(note);
// Print note to screen
printf("%3s: %i\n", note, f);
// Write (eighth) note to file
note_write(s, f, 1);
}
// Close file
song_close(s);
}