-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvertNoteToMidi.c
183 lines (160 loc) · 2.97 KB
/
convertNoteToMidi.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// convertNotetoMidi.c
// A useful C library for converting a string of a note
// to a midi value, taking into account octave.
//
// v1.0
//
// G. Elvin White
// 22 November 2022
//
// Installation: Place convertNoteToMidi.h,
// convertNotetoMidi.c, and Makefile in the same directory as
// main.c. Then, run 'make convertNotetoMidi'. Examine the
// Makefile for the necessary compiling steps.
//
// Copyright (c) G. Elvin White, 2022. All rights reserved.
// Distributed under MIT License
//
// Include the header file
#include "convertNoteToMidi.h"
// Include others
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// Function definition
int convertNoteToMidi(char *note, int octave)
{
// Adjust octave to zero base (Midi octaves are -2 to 8)
octave = octave + 2;
if (octave < 0)
{
octave = 0;
printf("WARNING: Octave below -2. Resetting to -2\n");
}
if (octave > 10)
{
octave = 10;
printf("WARNING: Octave above 8. Resetting to 8.\n");
}
int octaveAdjust = octave * 12;
// Make a copy of the note in all lowercase
int noteLength = strlen(note);
char thisNote[noteLength];
for (int i = 0; i < noteLength; i++)
{
char thisLetter = note[i];
thisNote[i] = tolower(thisLetter);
}
// Return correct midi note
int noteBase = 0;
// C
if (!strcmp(thisNote, "b#"))
{
noteBase = 0;
}
else if (!strcmp(thisNote, "c"))
{
noteBase = 0;
}
// C# Db
else if (!strcmp(thisNote, "c#"))
{
noteBase = 1;
}
else if (!strcmp(thisNote, "db"))
{
noteBase = 1;
}
// D
else if (!strcmp(thisNote, "d"))
{
noteBase = 2;
}
// D# Eb
else if (!strcmp(thisNote, "d#"))
{
noteBase = 3;
}
else if (!strcmp(thisNote, "eb"))
{
noteBase = 3;
}
// E Fb
else if (!strcmp(thisNote, "e"))
{
noteBase = 4;
}
else if (!strcmp(thisNote, "fb"))
{
noteBase = 4;
}
// F E#
else if (!strcmp(thisNote, "f"))
{
noteBase = 5;
}
else if (!strcmp(thisNote, "e#"))
{
noteBase = 5;
}
// F# Gb
else if (!strcmp(thisNote, "f#"))
{
noteBase = 6;
}
else if (!strcmp(thisNote, "gb"))
{
noteBase = 6;
}
// G
else if (!strcmp(thisNote, "g"))
{
noteBase = 7;
}
// G# Ab
else if (!strcmp(thisNote, "g#"))
{
noteBase = 8;
}
else if (!strcmp(thisNote, "ab"))
{
noteBase = 8;
}
// A
else if (!strcmp(thisNote, "a"))
{
noteBase = 9;
}
// A# Bb
else if (!strcmp(thisNote, "a#"))
{
noteBase = 10;
}
else if (!strcmp(thisNote, "bb"))
{
noteBase = 10;
}
// B Cb
// A# Bb
else if (!strcmp(thisNote, "b"))
{
noteBase = 11;
}
else if (!strcmp(thisNote, "cb"))
{
noteBase = 11;
}
else
{
noteBase = 0;
}
// Adjust midi note for octave
int midiNote = noteBase + octaveAdjust;
// Adjust to ceiling of 127 if note over 127
if (midiNote > 127)
{
midiNote = noteBase + 108;
printf("WARNING: Note above range 127. Lowering to octave 7.\n");
}
return midiNote;
}