arduino-audio-tools
MusicalNotes.h
1 
5 #pragma once
6 
7 #define N_C0 16.35f
8 #define N_CS0 17.32f
9 #define N_D0 18.35f
10 #define N_DS0 19.45f
11 #define N_E0 20.60f
12 #define N_F0 21.83f
13 #define N_FS0 23.12f
14 #define N_G0 24.50f
15 #define N_GS0 25.96f
16 #define N_A0 27.50f
17 #define N_AS0 29.14f
18 #define N_B0 30.87f
19 #define N_C1 32.70f
20 #define N_CS1 34.65f
21 #define N_D1 36.71f
22 #define N_DS1 38.89f
23 #define N_E1 41.20f
24 #define N_F1 43.65f
25 #define N_FS1 46.25f
26 #define N_G1 49.00f
27 #define N_GS1 51.91f
28 #define N_A1 55.00f
29 #define N_AS1 58.27f
30 #define N_B1 61.74f
31 #define N_C2 65.41f
32 #define N_CS2 69.30f
33 #define N_D2 73.42f
34 #define N_DS2 77.78f
35 #define N_E2 82.41f
36 #define N_F2 87.31f
37 #define N_FS2 92.50f
38 #define N_G2 98.00f
39 #define N_GS2 103.83f
40 #define N_A2 110.00f
41 #define N_AS2 116.54f
42 #define N_B2 123.47f
43 #define N_C3 130.81f
44 #define N_CS3 138.59f
45 #define N_D3 146.83f
46 #define N_DS3 155.56f
47 #define N_E3 164.81f
48 #define N_F3 174.61f
49 #define N_FS3 185.00f
50 #define N_G3 196.00f
51 #define N_GS3 207.65f
52 #define N_A3 220.00f
53 #define N_AS3 233.08f
54 #define N_B3 246.94f
55 #define N_C4 261.63f
56 #define N_CS4 277.18f
57 #define N_D4 293.66f
58 #define N_DS4 311.13f
59 #define N_E4 329.63f
60 #define N_F4 349.23f
61 #define N_FS4 369.99f
62 #define N_G4 392.00f
63 #define N_GS4 415.30f
64 #define N_A4 440.00f
65 #define N_AS4 466.16f
66 #define N_B4 493.88f
67 #define N_C5 523.25f
68 #define N_CS5 554.37f
69 #define N_D5 587.33f
70 #define N_DS5 622.25f
71 #define N_E5 659.25f
72 #define N_F5 698.46f
73 #define N_FS5 739.99f
74 #define N_G5 783.99f
75 #define N_GS5 830.61f
76 #define N_A5 880.00f
77 #define N_AS5 932.33f
78 #define N_B5 987.77f
79 #define N_C6 1046.5f
80 #define N_CS6 1108.73f
81 #define N_D6 1174.66f
82 #define N_DS6 1244.51f
83 #define N_E6 1318.51f
84 #define N_F6 1396.91f
85 #define N_FS6 1479.89f
86 #define N_G6 1567.89f
87 #define N_GS6 1661.22f
88 #define N_A6 1760.00f
89 #define N_AS6 1864.66f
90 #define N_B6 1975.53f
91 #define N_C7 2093.00f
92 #define N_CS7 2217.46f
93 #define N_D7 2349.32f
94 #define N_DS7 2489.02f
95 #define N_E7 2637.02f
96 #define N_F7 2793.83f
97 #define N_FS7 2959.96f
98 #define N_G7 3135.96f
99 #define N_GS7 3322.44f
100 #define N_A7 3520.00f
101 #define N_AS7 3729.31f
102 #define N_B7 3951.07f
103 #define N_C8 4186.01f
104 #define N_CS8 4434.92f
105 #define N_D8 4698.63f
106 #define N_DS8 4978.03f
107 #define N_E8 5274.04f
108 #define N_F8 5587.65f
109 #define N_FS8 5919.91f
110 #define N_G8 6271.93f
111 #define N_GS8 6644.88f
112 #define N_A8 7040.00f
113 #define N_AS8 7458.62f
114 #define N_B8 7902.13f
115 
116 namespace audio_tools {
117 
126 public:
128  enum MusicalNotesEnum {C, CS, D, DS, E, F, FS, G, GS, A, AS, B};
129 
131  float frequency(MusicalNotesEnum note, uint8_t octave) const {
132  if (note>11) return 0;
133  if (octave>8) return 0;
134  return notes[octave][note];
135  }
136 
138  float frequency(uint16_t idx) const {
139  MusicalNotesEnum mainNote = (MusicalNotesEnum) (idx % 12);
140  uint8_t octave = idx / 12;
141  return frequency(mainNote, octave);
142  }
143 
144  int frequencyCount() const {
145  return 108;
146  }
147 
149  float mainFrequency(uint8_t mainNoteIdx, uint8_t octave) const {
150  if (mainNoteIdx>6) return 0;
151  static int mainNotes[] = {0,2,4,5,7,9,11};
152  MusicalNotesEnum note = (MusicalNotesEnum) mainNotes[mainNoteIdx];
153  return frequency(note, octave);
154  }
155 
157  float mainFrequency(uint64_t idx) const {
158  uint8_t mainNote = idx % 7;
159  uint8_t level = idx /7;
160  return mainFrequency(mainNote, level);
161  }
162 
164  bool isAudible(float frequency) const {
165  return frequency >= 20 && frequency<=20000;
166  }
167 
169  const char* note(float frequency, float &diff) const {
170  float* all_notes = (float*) notes;
171  const int note_count = 12*9;
172 
173  // find closest note
174  float min_diff = frequency;
175  int min_pos = 0;
176  for (int j=0; j<note_count; j++){
177  int tmp = abs(frequency - all_notes[j]);
178  if (tmp<min_diff){
179  min_diff = tmp;
180  min_pos = j;
181  }
182  }
183 
184  float noteFrequency = all_notes[min_pos];
185  diff = frequency - noteFrequency;
186  return notes_str[min_pos];
187  }
188 
190  const char* note(float frequency) const {
191  float diff;
192  return note(frequency, diff);
193  }
194 
196  const char* noteAt(int idx) {
197  return notes_str[idx];
198  }
199 
201  float midiNoteToFrequency(int x) const {
202  float a = 440; //frequency of A (coomon value is 440Hz)
203  return (a / 32) * pow(2, ((x - 9) / 12.0f));
204  }
205 
207  int frequencyToMidiNote(float freq) const {
208  return log(freq/440.0f)/log(2) * 12.0f + 69.0f;
209  }
210 
211  float stkNoteToFrequency(int noteNumber) const {
212  return 220.0f * pow( 2.0f, (noteNumber - 57.0f) / 12.0f );
213  }
214 
215 protected:
216 
217  float notes[9][12] = {
218  {N_C0, N_CS0, N_D0, N_DS0, N_E0, N_F0, N_FS0, N_G0, N_GS0, N_A0, N_AS0, N_B0},
219  {N_C1, N_CS1, N_D1, N_DS1, N_E1, N_F1, N_FS1, N_G1, N_GS1, N_A1, N_AS1, N_B1},
220  {N_C2, N_CS2, N_D2, N_DS2, N_E2, N_F2, N_FS2, N_G2, N_GS2, N_A2, N_AS2, N_B2},
221  {N_C3, N_CS3, N_D3, N_DS3, N_E3, N_F3, N_FS3, N_G3, N_GS3, N_A3, N_AS3, N_B3},
222  {N_C4, N_CS4, N_D4, N_DS4, N_E4, N_F4, N_FS4, N_G4, N_GS4, N_A4, N_AS4, N_B4},
223  {N_C5, N_CS5, N_D5, N_DS5, N_E5, N_F5, N_FS5, N_G5, N_GS5, N_A5, N_AS5, N_B5},
224  {N_C6, N_CS6, N_D6, N_DS6, N_E6, N_F6, N_FS6, N_G6, N_GS6, N_A6, N_AS6, N_B6},
225  {N_C7, N_CS7, N_D7, N_DS7, N_E7, N_F7, N_FS7, N_G7, N_GS7, N_A7, N_AS7, N_B7},
226  {N_C8, N_CS8, N_D8, N_DS8, N_E8, N_F8, N_FS8, N_G8, N_GS8, N_A8, N_AS8, N_B8}
227  };
228 
229  const char *notes_str[9*12]= {
230  "C0","CS0","D0","DS0","E0","F0","FS0","G0","GS0","A0","AS0","B0",
231  "C1","CS1","D1","DS1","E1","F1","FS1","G1","GS1","A1","AS1","B1",
232  "C2","CS2","D2","DS2","E2","F2","FS2","G2","GS2","A2","AS2","B2",
233  "C3","CS3","D3","DS3","E3","F3","FS3","G3","GS3","A3","AS3","B3",
234  "C4","CS4","D4","DS4","E4","F4","FS4","G4","GS4","A4","AS4","B4",
235  "C5","CS5","D5","DS5","E5","F5","FS5","G5","GS5","A5","AS5","B5",
236  "C6","CS6","D6","DS6","E6","F6","FS6","G6","GS6","A6","AS6","B6",
237  "C7","CS7","D7","DS7","E7","F7","FS7","G7","GS7","A7","AS7","B7",
238  "C8","CS8","D8","DS8","E8","F8","FS8","G8","GS8","A8","AS8","B8"
239  };
240 
241 
242 };
243 
244 }
245 
Eumlate FS using C++ or Posix functions.
Definition: File.h:199
Determination of the frequency of a music note.
Definition: MusicalNotes.h:125
const char * note(float frequency) const
Determines the closes note for a frequency.
Definition: MusicalNotes.h:190
float frequency(MusicalNotesEnum note, uint8_t octave) const
Determines the frequency of the indicate note and octave (0-8)
Definition: MusicalNotes.h:131
float midiNoteToFrequency(int x) const
Determine frequency of MIDI note.
Definition: MusicalNotes.h:201
const char * note(float frequency, float &diff) const
Determines the closes note for a frequency. We also return the frequency difference.
Definition: MusicalNotes.h:169
bool isAudible(float frequency) const
Returns true if the frequency is audible (in the range of 20 Hz to 20 kHz)
Definition: MusicalNotes.h:164
int frequencyToMidiNote(float freq) const
Provide MIDI note for frequency.
Definition: MusicalNotes.h:207
float mainFrequency(uint8_t mainNoteIdx, uint8_t octave) const
Determines the frequency of the indicate main note index (0-6) and octave (0-8)
Definition: MusicalNotes.h:149
const char * noteAt(int idx)
Provides the note name for an index position.
Definition: MusicalNotes.h:196
float frequency(uint16_t idx) const
Determines the frequency of the indicate note index from 0 to 107.
Definition: MusicalNotes.h:138
float mainFrequency(uint64_t idx) const
Determines the frequency of the indicate main note index from 0 to 62.
Definition: MusicalNotes.h:157
MusicalNotesEnum
Notes.
Definition: MusicalNotes.h:128
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:823