-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathNoteAnalyzer.mm
279 lines (206 loc) · 7.76 KB
/
NoteAnalyzer.mm
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright (C) 2014 Jimmy Johnson
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// See the read me file for licensing information
/*
* This file contains the implemetation of the classes needed to set up and
* turn a frequency to a corresponding Note value and also to animate the
* display.
*/
#import <Foundation/NSTimer.h>
#import "NoteAnalyzer.h"
#include <iostream>
using namespace std;
@implementation Note
@end
@implementation NoteAnalyzer
/*
* Class - NoteAnalyzer
* Method - + createKeyName
*
* Parameters - (NSString *) note frequency - The name of the note
* - (int) freq - The frequency of the note in Hz
*
* Returns - NoteAnalyzer * name - A note name/key freq
*
* Details - This function pairs a freq. with a note and stores it in a FeqKeyName
* storage class
*
*/
+ (Note *) createKeyName: (NSString *) noteName frequency: (int) freq
{
Note * note = [[Note alloc] init];
note->frequency = freq;
note->keyName = [[NSString stringWithString: noteName] retain];
return note;
}
/*
* Class - NoteAnalyzer
* Method - getGuitarTonalFrequences
*
* Parameters - None
* Returns - NSMutableDictionary * freqTable - A diction with Notes as
* key values.
*
* Details - Creates a frequency to note value table using given values
* corresponding to a guitar
*
*/
+ (NSMutableDictionary *) getGuitarTonalFrequences
{
NSMutableDictionary * noteTable = [[[NSMutableDictionary alloc] init] retain];
[noteTable setObject: [NoteAnalyzer createKeyName: @"E2" frequency: 82] forKey: @"E2"];
[noteTable setObject: [NoteAnalyzer createKeyName: @"A2" frequency: 110] forKey: @"A2"];
[noteTable setObject: [NoteAnalyzer createKeyName: @"D3" frequency: 146] forKey: @"D"];
[noteTable setObject: [NoteAnalyzer createKeyName: @"G3" frequency: 196] forKey: @"G"];
[noteTable setObject: [NoteAnalyzer createKeyName: @"B3" frequency: 247] forKey: @"B"];
[noteTable setObject: [NoteAnalyzer createKeyName: @"E4" frequency: 329] forKey: @"E4"];
return noteTable;
}
/*
* Class - NoteAnalyzer
* Method - + getBassTonalFrequences
*
* Parameters - None
* Returns - NSMutableDictionary * freqTable - A diction with Notes as
* key values.
*
* Details - Creates a frequency to note value table using given values
* corresponding to a bass (not implemented yet)
*
*/
- (NSMutableDictionary *) getBassTonalFrequences
{
NSMutableDictionary * freqTable = [[[NSMutableDictionary alloc] init] retain];
/* Stub */
return freqTable;
}
- (void) initAnalyzer
{
// frequencyTable = self.getGuitarTonalFrequences;
}
/*
* Class - NoteAnalyzer
* Method - + getGuitarAllFrequences
*
* Parameters - None
* Returns - NSMutableDictionary * freqTable - A diction with Notes as
* key values.
*
* Details - The following creates a full cromatic tuning scale
* through all values of sharp and flat notes. It works (kinda) but there
* is overlap, frequency problems, ect.. Fix in the future?
*/
/*
- (NSMutableDictionary *) getAllTonalFrequences
{
Float32 well_tempered_ratio = powf(2, (1.0/12.0));
int octave;
NSString * note_name;
Float32 note_frequency = 32.703;
NSMutableDictionary * freqTable = [[[NSMutableDictionary alloc] init] retain];
// Create tuning table using logarithmic western scale
for(octave = 1; octave < 7; octave ++)
{
note_name = [NSString stringWithFormat: @"C%i",octave];
[freqTable setObject: [NoteAnalyzer createKeyName: note_name frequency: (int) note_frequency] forKey: note_name];
note_frequency *= well_tempered_ratio;
note_frequency *= well_tempered_ratio;
note_name = [NSString stringWithFormat: @"D%i", octave];
[freqTable setObject: [NoteAnalyzer createKeyName: note_name frequency: (int) note_frequency] forKey: note_name];
note_frequency *= well_tempered_ratio;
note_frequency *= well_tempered_ratio;
note_name = [NSString stringWithFormat: @"E%i", octave];
[freqTable setObject: [NoteAnalyzer createKeyName: note_name frequency: (int) note_frequency] forKey: note_name];
note_frequency *= well_tempered_ratio;
note_name = [NSString stringWithFormat: @"F%i", octave];
[freqTable setObject: [NoteAnalyzer createKeyName: note_name frequency: (int) note_frequency] forKey: note_name];
note_frequency *= well_tempered_ratio;
note_frequency *= well_tempered_ratio;
note_name = [NSString stringWithFormat: @"G%i", octave];
[freqTable setObject: [NoteAnalyzer createKeyName: note_name frequency: (int) note_frequency] forKey: note_name];
note_frequency *= well_tempered_ratio;
note_frequency *= well_tempered_ratio;
note_name = [NSString stringWithFormat: @"A%i", octave];
[freqTable setObject: [NoteAnalyzer createKeyName: note_name frequency: (int) note_frequency] forKey: note_name];
note_frequency *= well_tempered_ratio;
note_frequency *= well_tempered_ratio;
note_name = [NSString stringWithFormat: @"B%i", octave];
[freqTable setObject: [NoteAnalyzer createKeyName: note_name frequency: (int) note_frequency] forKey: note_name];
note_frequency *= well_tempered_ratio;
}
return freqTable;
}
*/
/*
* Class - ChromaTuner
* Method - + updateCTDisplay
*
* Parameters - (id) displayItems - Contains the bundle of display items to update
*
* Returns - void
*
* Details - Inifinite loop thread for comparing heard freqs. and the display
*/
+ (Note *) calculateNote : (int) freq
{
Note * note = [[Note alloc] init];
Note * freqAtKey;
NSArray * freqNoteArray = [[NoteAnalyzer getGuitarTonalFrequences] allValues];
int note_index;
int note_freq; // The acual target frequency and the fundamental frequency heard
int sharp_note, flat_note;
Float32 well_tempered_ratio = powf(2, (1.0/12.0)); // Well tempered western ratio
/* iterate through the hash table and see what frequencies seem to be closest */
for(note_index = 0; note_index < [freqNoteArray count]; note_index++)
{
freqAtKey = [freqNoteArray objectAtIndex:note_index];
note_freq = freqAtKey->frequency;
/* convert Hz to cents */
sharp_note = (float)note_freq*well_tempered_ratio;
flat_note = (float)note_freq/well_tempered_ratio;
/* use the well tempered ratio to find out if the freq. heard is within a flat or sharp tone of the target note */
if ((freq >= (flat_note)) && (freq <= (sharp_note)))
{
/* If its higher its sharp */
if(freq > note_freq)
{
note->cents = log2((note_freq + fabs(note_freq - freq))/note_freq)*1200/2;
note->keyName = [NSString stringWithString:freqAtKey->keyName];
note->frequency = freqAtKey->frequency;
return note;
}
/* If its lower its flat */
if(freq < note_freq)
{
note->cents = -1*log2((note_freq + fabs(note_freq - freq))/note_freq)*1200/2;
note->keyName = [NSString stringWithString:freqAtKey->keyName];
note->frequency = freqAtKey->frequency;
return note;
}
/* If its equal its tuned */
if (freq == note_freq)
{
note->cents = 0;
note->keyName = [NSString stringWithString:freqAtKey->keyName];
note->frequency = freqAtKey->frequency;
return note;
}
}
}
return nil;
}
@end // NoteAnalyzer