-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.c
83 lines (73 loc) · 1.78 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <math.h>
#include <string.h>
/* match two character notes before single character notes */
/* nb: particular array order, sharps come before flats */
/* `get_index_name(1)` returns "c#" not "db" */
/* clang-format off */
struct {
char *name;
int len;
int index;
} note_indices[] = {
{"a#", 2, 10},
{"bb", 2, 10},
{"hb", 2, 10},
{"c#", 2, 1},
{"db", 2, 1},
{"d#", 2, 3},
{"eb", 2, 3},
{"f#", 2, 6},
{"gb", 2, 6},
{"g#", 2, 8},
{"ab", 2, 8},
{ "a", 1, 9},
{ "b", 1, 11},
{ "h", 1, 11},
{ "c", 1, 0},
{ "d", 1, 2},
{ "e", 1, 4},
{ "f", 1, 5},
{ "g", 1, 7},
};
/* clang-format on */
#define NUM_NOTE_INDICES 19
int get_note_index(char *s) {
int i;
for (i = 0; i < NUM_NOTE_INDICES; i++) {
if (!strncmp(s, note_indices[i].name, note_indices[i].len)) {
return note_indices[i].index;
}
}
return -1;
};
char *get_index_name(int index) {
int i;
for (i = 0; i < NUM_NOTE_INDICES; i++) {
if (note_indices[i].index == index) {
return note_indices[i].name;
}
}
return NULL;
}
/*
** index 0 = C0 = 16.3516 Hz
*/
double get_index_frequency(int index) {
return pow(2.0, (double)index / 12.0) * 16.3516;
}
/* clang-format off */
double get_note_duration(int bpm, int note_type, int is_dotted) {
double spb;
spb = 60.0 / (double)bpm;
spb *= is_dotted ? 1.5 : 1.0;
switch (note_type) {
case 1: return spb * 4.0; // whole
case 2: return spb * 2.0; // half
case 4: return spb; // quarter
case 8: return spb / 2.0; // eighth
case 16: return spb / 4.0; // sixteenth
case 32: return spb / 8.0; // thirty-second
}
return -1.0;
}
/* clang-format on */