|
@@ -2,14 +2,48 @@
|
2
|
2
|
// AudioVisualizer.m
|
3
|
3
|
// CaseLights
|
4
|
4
|
//
|
|
5
|
+// Based on the ideas in:
|
|
6
|
+// http://archive.gamedev.net/archive/reference/programming/features/beatdetection/
|
|
7
|
+//
|
|
8
|
+// The detected sound frequency of beats will be mapped to the hue of the resulting color,
|
|
9
|
+// the variance of the beat is mapped to the brightness of the color. The colors
|
|
10
|
+// of all detected beats will be added together to form the final displayed color.
|
|
11
|
+//
|
5
|
12
|
// Created by Thomas Buck on 01.01.16.
|
6
|
13
|
// Copyright © 2016 xythobuz. All rights reserved.
|
7
|
14
|
//
|
8
|
15
|
|
|
16
|
+// Enabling this will cause crashes when changing audio input
|
|
17
|
+// devices while the app is running. Select it before enabling.
|
|
18
|
+#define DEBUG_PLOT_FFT
|
|
19
|
+//#define DEBUG_PLOT_FFT_RAW
|
|
20
|
+
|
|
21
|
+#define DEBUG_LOG_BEATS
|
|
22
|
+
|
9
|
23
|
#import "AudioVisualizer.h"
|
10
|
24
|
#import "AppDelegate.h"
|
11
|
25
|
|
|
26
|
+#import "EZAudioFFT.h"
|
|
27
|
+
|
|
28
|
+#ifdef DEBUG_PLOT_FFT
|
|
29
|
+#import "EZAudioPlot.h"
|
|
30
|
+#endif
|
|
31
|
+
|
|
32
|
+// Parameters for fine-tuning beat detection
|
|
33
|
+#define FFT_BUCKET_COUNT 64
|
|
34
|
+#define FFT_BUCKET_HISTORY 43
|
|
35
|
+#define FFT_C_FACTOR 4.2
|
|
36
|
+#define FFT_V0_FACTOR 0.000015
|
|
37
|
+#define FFT_MAX_V0_COLOR 0.0002
|
|
38
|
+#define FFT_COLOR_DECAY 0.99
|
|
39
|
+
|
|
40
|
+// Factors for nicer debug display
|
|
41
|
+#define FFT_DEBUG_RAW_FACTOR 42.0
|
|
42
|
+#define FFT_DEBUG_FACTOR 230.0
|
|
43
|
+
|
12
|
44
|
static AppDelegate *appDelegate = nil;
|
|
45
|
+static EZAudioFFT *fft = nil;
|
|
46
|
+static int maxBufferSize = 0;
|
13
|
47
|
|
14
|
48
|
@implementation AudioVisualizer
|
15
|
49
|
|
|
@@ -18,7 +52,159 @@ static AppDelegate *appDelegate = nil;
|
18
|
52
|
}
|
19
|
53
|
|
20
|
54
|
+ (void)updateBuffer:(float *)buffer withBufferSize:(UInt32)bufferSize {
|
|
55
|
+ // Create Fast Fourier Transformation object
|
|
56
|
+ if (fft == nil) {
|
|
57
|
+ maxBufferSize = bufferSize;
|
|
58
|
+ fft = [EZAudioFFT fftWithMaximumBufferSize:maxBufferSize sampleRate:appDelegate.microphone.audioStreamBasicDescription.mSampleRate];
|
|
59
|
+
|
|
60
|
+#ifdef DEBUG
|
|
61
|
+ NSLog(@"Created FFT with max. freq.: %.2f\n", appDelegate.microphone.audioStreamBasicDescription.mSampleRate / 2);
|
|
62
|
+#endif
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ // Check for changing buffer sizes
|
|
66
|
+ if (bufferSize > maxBufferSize) {
|
|
67
|
+ NSLog(@"Buffer Size changed?! %d != %d\n", maxBufferSize, bufferSize);
|
|
68
|
+ maxBufferSize = bufferSize;
|
|
69
|
+ fft = [EZAudioFFT fftWithMaximumBufferSize:maxBufferSize sampleRate:appDelegate.microphone.audioStreamBasicDescription.mSampleRate];
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ [fft computeFFTWithBuffer:buffer withBufferSize:bufferSize];
|
|
73
|
+
|
|
74
|
+ static float history[FFT_BUCKET_COUNT][FFT_BUCKET_HISTORY];
|
|
75
|
+ static int nextHistory = 0;
|
|
76
|
+ static int samplesPerBucket = 0;
|
|
77
|
+
|
|
78
|
+ // Initialize static variables
|
|
79
|
+ if (samplesPerBucket == 0) {
|
|
80
|
+ samplesPerBucket = bufferSize / FFT_BUCKET_COUNT;
|
|
81
|
+ for (int i = 0; i < FFT_BUCKET_COUNT; i++) {
|
|
82
|
+ for (int j = 0; j < FFT_BUCKET_HISTORY; j++) {
|
|
83
|
+ history[i][j] = 0.5f;
|
|
84
|
+ }
|
|
85
|
+ }
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ // Split FFT output into a small number of 'buckets' or 'bins' and add to circular history buffer
|
|
89
|
+ for (int i = 0; i < FFT_BUCKET_COUNT; i++) {
|
|
90
|
+ float sum = 0.0f;
|
|
91
|
+ for (int j = 0; j < samplesPerBucket; j++) {
|
|
92
|
+ sum += fft.fftData[(i + samplesPerBucket) + j];
|
|
93
|
+ }
|
|
94
|
+ history[i][nextHistory] = sum / samplesPerBucket;
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+#ifdef DEBUG_PLOT_FFT
|
|
98
|
+ int beatCount = 0;
|
|
99
|
+#endif
|
|
100
|
+
|
|
101
|
+ static unsigned char lastRed = 0, lastGreen = 0, lastBlue = 0;
|
|
102
|
+ lastRed = lastRed * FFT_COLOR_DECAY;
|
|
103
|
+ lastGreen = lastGreen * FFT_COLOR_DECAY;
|
|
104
|
+ lastBlue = lastBlue * FFT_COLOR_DECAY;
|
|
105
|
+
|
|
106
|
+ // Check for any beats
|
|
107
|
+ for (int i = 0; i < FFT_BUCKET_COUNT; i++) {
|
|
108
|
+ float average = 0.0f;
|
|
109
|
+ for (int j = 0; j < FFT_BUCKET_HISTORY; j++) {
|
|
110
|
+ average += history[i][j];
|
|
111
|
+ }
|
|
112
|
+ average /= FFT_BUCKET_HISTORY;
|
|
113
|
+ float v = 0.0f;
|
|
114
|
+ for (int j = 0; j < FFT_BUCKET_HISTORY; j++) {
|
|
115
|
+ float tmp = history[i][j] - average;
|
|
116
|
+ tmp *= tmp;
|
|
117
|
+ v += tmp;
|
|
118
|
+ }
|
|
119
|
+ v /= FFT_BUCKET_HISTORY;
|
|
120
|
+ if ((history[i][nextHistory] > (FFT_C_FACTOR * average)) && (v > FFT_V0_FACTOR)) {
|
|
121
|
+ // Found a beat on this frequency band, map to a single color
|
|
122
|
+ if (v < FFT_V0_FACTOR) v = FFT_V0_FACTOR;
|
|
123
|
+ if (v > FFT_MAX_V0_COLOR) v = FFT_MAX_V0_COLOR;
|
|
124
|
+ float bright = [AppDelegate map:v FromMin:FFT_V0_FACTOR FromMax:FFT_MAX_V0_COLOR ToMin:0.0 ToMax:100.0];
|
|
125
|
+ float hue = [AppDelegate map:i FromMin:0.0 FromMax:FFT_BUCKET_COUNT ToMin:0.0 ToMax:360.0];
|
|
126
|
+ unsigned char r, g, b;
|
|
127
|
+ [AppDelegate convertH:hue S:1.0 V:bright toR:&r G:&g B:&b];
|
|
128
|
+
|
|
129
|
+ // Blend with last color using averaging
|
|
130
|
+ int tmpR = (lastRed + r) / 2;
|
|
131
|
+ int tmpG = (lastGreen + g) / 2;
|
|
132
|
+ int tmpB = (lastBlue + b) / 2;
|
|
133
|
+ lastRed = tmpR;
|
|
134
|
+ lastGreen = tmpG;
|
|
135
|
+ lastBlue = tmpB;
|
|
136
|
+
|
|
137
|
+#ifdef DEBUG_LOG_BEATS
|
|
138
|
+ NSLog(@"Beat in %d with c: %f v: %f", i, (history[i][nextHistory] / average), v);
|
|
139
|
+#endif
|
|
140
|
+#ifdef DEBUG_PLOT_FFT
|
|
141
|
+ beatCount++;
|
|
142
|
+#endif
|
|
143
|
+ }
|
|
144
|
+ }
|
|
145
|
+
|
|
146
|
+ [appDelegate setLightsR:lastRed G:lastGreen B:lastBlue];
|
|
147
|
+
|
|
148
|
+#ifdef DEBUG_PLOT_FFT
|
|
149
|
+ static NSWindow *window = nil;
|
|
150
|
+ static EZAudioPlot *plot = nil;
|
|
151
|
+ static NSTextField *label = nil;
|
|
152
|
+ if ((window == nil) || (plot == nil) || (label == nil)) {
|
|
153
|
+ NSRect frame = NSMakeRect(450, 300, 600, 400);
|
|
154
|
+ window = [[NSWindow alloc] initWithContentRect:frame
|
|
155
|
+ styleMask:NSClosableWindowMask | NSTitledWindowMask | NSBorderlessWindowMask
|
|
156
|
+ backing:NSBackingStoreBuffered
|
|
157
|
+ defer:NO];
|
|
158
|
+ [window setTitle:@"Debug FFT"];
|
|
159
|
+
|
|
160
|
+ plot = [[EZAudioPlot alloc] initWithFrame:window.contentView.frame];
|
|
161
|
+ plot.color = [NSColor whiteColor];
|
|
162
|
+ plot.shouldOptimizeForRealtimePlot = NO; // Not working with 'YES' here?!
|
|
163
|
+ plot.shouldFill = YES;
|
|
164
|
+ plot.shouldCenterYAxis = NO;
|
|
165
|
+ plot.shouldMirror = NO;
|
|
166
|
+ plot.plotType = EZPlotTypeBuffer;
|
|
167
|
+ [window.contentView addSubview:plot];
|
|
168
|
+
|
|
169
|
+ label = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 380, 600, 20)];
|
|
170
|
+ [label setTextColor:[NSColor whiteColor]];
|
|
171
|
+ [label setEditable:NO];
|
|
172
|
+ [label setBezeled:NO];
|
|
173
|
+ [label setDrawsBackground:NO];
|
|
174
|
+ [label setSelectable:NO];
|
|
175
|
+ [label setStringValue:@"-"];
|
|
176
|
+ [window.contentView addSubview:label];
|
|
177
|
+
|
|
178
|
+ [window makeKeyAndOrderFront:appDelegate.application];
|
|
179
|
+ NSLog(@"Created debugging FFT Plot window...\n");
|
|
180
|
+ }
|
|
181
|
+
|
|
182
|
+ // Scale so we can see something
|
|
183
|
+# ifdef DEBUG_PLOT_FFT_RAW
|
|
184
|
+ memcpy(buffer, fft.fftData, bufferSize * sizeof(float));
|
|
185
|
+ for (UInt32 i = 0; i < bufferSize; i++) {
|
|
186
|
+ buffer[i] *= FFT_DEBUG_RAW_FACTOR;
|
|
187
|
+# else
|
|
188
|
+ for (int i = 0; i < FFT_BUCKET_COUNT; i++) {
|
|
189
|
+ buffer[i] = history[i][nextHistory];
|
|
190
|
+ }
|
|
191
|
+ for (UInt32 i = 0; i < FFT_BUCKET_COUNT; i++) {
|
|
192
|
+ buffer[i] *= FFT_DEBUG_FACTOR;
|
|
193
|
+# endif
|
|
194
|
+ if (buffer[i] > 1.0f) buffer[i] = 1.0f;
|
|
195
|
+ if (buffer[i] < -1.0f) buffer[i] = -1.0f;
|
|
196
|
+ }
|
|
197
|
+ [plot updateBuffer:buffer withBufferSize:bufferSize];
|
21
|
198
|
|
|
199
|
+ [window setBackgroundColor:[NSColor colorWithCalibratedRed:lastRed / 255.0 green:lastGreen / 255.0 blue:lastBlue / 255.0 alpha:1.0]];
|
|
200
|
+ [label setStringValue:[NSString stringWithFormat:@"Beats: %d", beatCount]];
|
|
201
|
+#endif
|
|
202
|
+
|
|
203
|
+ // Point to next history buffer
|
|
204
|
+ nextHistory++;
|
|
205
|
+ if (nextHistory >= FFT_BUCKET_HISTORY) {
|
|
206
|
+ nextHistory = 0;
|
|
207
|
+ }
|
22
|
208
|
}
|
23
|
209
|
|
24
|
210
|
@end
|