forked from stephencelis/sc_listener
-
Notifications
You must be signed in to change notification settings - Fork 8
/
SCListener.m
314 lines (248 loc) · 7.37 KB
/
SCListener.m
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//
// SCListener 1.0.1
// http://github.com/stephencelis/sc_listener
//
// (c) 2009-* Stephen Celis, <[email protected]>.
// Released under the MIT License.
//
#import "SCListener.h"
@interface SCListener (Private)
- (void)updateLevels;
- (void)setupQueue;
- (void)setupFormat;
- (void)setupBuffers;
- (void)setupMetering;
- (Float32)getFreqFromBuffer: (short*) buffer length: (UInt32) length;
- (UInt32)findOptimalSampleLength: (UInt32) samples;
- (void)setAudioBuffer: (short*) buffer length: (UInt32) length;
@end
static SCListener *sharedListener = nil;
static void listeningCallback(void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer, const AudioTimeStamp *inStartTime, UInt32 inNumberPacketsDescriptions, const AudioStreamPacketDescription *inPacketDescs) {
SCListener *listener = (SCListener *)inUserData;
if ([listener isListening]){
[listener setAudioBuffer:inBuffer->mAudioData length: inBuffer->mAudioDataByteSize];
AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL);
}
}
@implementation SCListener
+ (SCListener *)sharedListener {
@synchronized(self) {
if (sharedListener == nil)
[[self alloc] init];
}
return sharedListener;
}
- (void)dealloc {
[sharedListener stop];
[super dealloc];
}
#pragma mark -
#pragma mark Listening
- (void)listen {
if (queue == nil){
[self setupQueue];
}
AudioQueueStart(queue, NULL);
}
- (void)pause {
if (![self isListening])
return;
AudioQueueStop(queue, true);
}
- (void)stop {
if (queue == nil)
return;
AudioQueueDispose(queue, true);
queue = nil;
}
- (BOOL)isListening {
if (queue == nil)
return NO;
UInt32 isListening, ioDataSize = sizeof(UInt32);
OSStatus result = AudioQueueGetProperty(queue, kAudioQueueProperty_IsRunning, &isListening, &ioDataSize);
return (result != noErr) ? NO : isListening;
}
#pragma mark -
#pragma mark Levels getters
- (Float32)averagePower {
if (![self isListening])
return 0.0;
return [self levels][0].mAveragePower;
}
- (Float32)peakPower {
if (![self isListening])
return 0.0;
return [self levels][0].mPeakPower;
}
- (AudioQueueLevelMeterState *)levels {
if (![self isListening])
return nil;
[self updateLevels];
return levels;
}
- (void)updateLevels {
UInt32 ioDataSize = format.mChannelsPerFrame * sizeof(AudioQueueLevelMeterState);
AudioQueueGetProperty(queue, (AudioQueuePropertyID)kAudioQueueProperty_CurrentLevelMeter, levels, &ioDataSize);
}
#pragma mark -
#pragma mark Frequency
- (Float32)frequency {
short buffer[kBUFFERSIZE];
UInt32 buffer_length;
// Make a local copy of the buffer so we don't block the audio thread.
@synchronized(self) {
memcpy(buffer, audio_data, kBUFFERSIZE);
buffer_length = audio_data_len;
}
if(buffer_length ==0 )
return 0;
else
return [self getFreqFromBuffer: buffer length: buffer_length];
}
// Return all the data for the frequency spectrum.
- (double*) freq_db{
return freq_db;
}
- (double*) freq_db_harmonic{
return freq_db_harmonic;
}
// Set audio buffer. Called from audio thread.
- (void)setAudioBuffer: (short*) buffer length: (UInt32) length{
@synchronized(self) {
memcpy(audio_data, buffer, length);
audio_data_len = length;
}
}
// Some optional functions for performing a hamming window on the input.
- (double) hamming_window: (short) input totalSamples: (short) totalSamples{
double a = 2.0 * 3.141592654 / ( totalSamples - 1 );
double w;
w = 0.54 - 0.46 * cos( a * input );
return w;
}
- (void) performWindow: (short*) buffer totalSamples: (UInt32) totalSamples{
for (int i = 0; i < totalSamples; i++){
buffer[i] *= [self hamming_window: i totalSamples: totalSamples];
}
}
- (void) performFFT: (short*) buffer totalSamples: (UInt32) totalSamples{
// Populate FFT input for the samples we have, input is zero padded.
for(UInt32 i = 0; i < totalSamples; i++){
in_fft[i] = buffer[i];
}
// Run FFT
kiss_fftr_cfg kiss_cfg = kiss_fftr_alloc(kFFTSIZE, 0, NULL, NULL);
kiss_fftr(kiss_cfg, in_fft, out_fft);
free(kiss_cfg);
// Calculate amplitude. ( half the fft is a duplicate )
for(int i = 0; i < kFFTSIZE / 2; i++)
{
freq_db[i] = sqrt((double)out_fft[i].r * (double)out_fft[i].r +
(double)out_fft[i].i * (double)out_fft[i].i) / (kFFTSIZE / 2.0);
}
}
// To find harmonics we take the output and divide the frequencies by 2 and 3
// we then add this to the original input. It should give us a peak at the
// root note. This is needed for string instruments.
- (void) addHarmonics{
const int max_harmonics = 3;
int fft_range = kFFTSIZE / 2;
memcpy(freq_db_harmonic, freq_db, kFFTSIZE / 2 * sizeof(freq_db[0]));
for(int j = 2; j <= max_harmonics; j++){
int low_bin = 0;
int new_value = 0;
for(int i = 0; i < fft_range; i++){
int next_bin = round((double)i / (double)j);
if(next_bin > low_bin){
freq_db_harmonic[low_bin] += new_value;
low_bin = next_bin;
new_value = 0;
}
new_value = MAX(new_value, freq_db[i]);
}
}
}
// Find the highest peak.
- (UInt32) findTopSpike{
// Find highest db value in the output.
double max = 0;
UInt32 max_index = 0;
for(UInt32 i = 0; i < kFFTSIZE / 2; i++){
double db = freq_db_harmonic[i];
if(db > max){
max = db;
max_index = i;
}
}
return max_index;
}
// Calculate the frequency of an audio buffer using fft.
- (Float32)getFreqFromBuffer: (short*) buffer length: (UInt32) length{
memset(in_fft, 0, kFFTSIZE * sizeof(kiss_fft_scalar));
memset(out_fft, 0, kFFTSIZE * sizeof(kiss_fft_cpx));
memset(freq_db, 0, kFFTSIZE / 2 * sizeof(double));
memset(freq_db_harmonic, 0, kFFTSIZE / 2 * sizeof(double));
// Two bytes per sample.
UInt32 totalSamples = length/2;
//Optional :)
//[self performWindow: buffer totalSamples: totalSamples];
[self performFFT: buffer totalSamples: totalSamples];
[self addHarmonics];
int freq_index = [self findTopSpike];
// Calculate frequency from fft bin number.
return (Float32)freq_index * format.mSampleRate / kFFTSIZE;
}
#pragma mark -
#pragma mark Setup
- (void)setupQueue {
if (queue)
return;
[self setupFormat];
AudioQueueNewInput(&format, listeningCallback, self, NULL, NULL, 0, &queue);
[self setupBuffers];
[self setupMetering];
}
- (void)setupFormat {
format.mSampleRate = kSAMPLERATE;
format.mFormatID = kAudioFormatLinearPCM;
format.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
format.mFramesPerPacket = format.mChannelsPerFrame = 1;
format.mBitsPerChannel = 16;
format.mBytesPerPacket = format.mBytesPerFrame = 2;
}
- (void)setupBuffers {
AudioQueueBufferRef buffers[3];
for (NSInteger i = 0; i < 3; ++i) {
AudioQueueAllocateBuffer(queue, kBUFFERSIZE, &buffers[i]);
AudioQueueEnqueueBuffer(queue, buffers[i], 0, NULL);
}
}
- (void)setupMetering {
levels = (AudioQueueLevelMeterState *)calloc(sizeof(AudioQueueLevelMeterState), format.mChannelsPerFrame);
UInt32 trueValue = true;
AudioQueueSetProperty(queue, kAudioQueueProperty_EnableLevelMetering, &trueValue, sizeof(UInt32));
}
#pragma mark -
#pragma mark Singleton Pattern
+ (id)allocWithZone:(NSZone *)zone {
@synchronized(self) {
if (sharedListener == nil) {
sharedListener = [super allocWithZone:zone];
return sharedListener;
}
}
return nil;
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)init {
if ([super init] == nil){
return nil;
}
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[[AVAudioSession sharedInstance]
setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
return self;
}
@end