DOUAudioVisualizer.m
9.2 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* vim: set ft=objc fenc=utf-8 sw=2 ts=2 et: */
/*
* DOUAudioStreamer - A Core Audio based streaming audio player for iOS/Mac:
*
* https://github.com/douban/DOUAudioStreamer
*
* Copyright 2013-2016 Douban Inc. All rights reserved.
*
* Use and distribution licensed under the BSD license. See
* the LICENSE file for full text.
*
* Authors:
* Chongyu Zhu <i@lembacon.com>
*
*/
#if TARGET_OS_IPHONE
#import "DOUAudioVisualizer.h"
#import "DOUAudioStreamer.h"
#include <Accelerate/Accelerate.h>
#define kBarHeight 6.0
#define kBarHorizontalPadding 2.0
#define kBarVerticalPadding 1.0
@interface DOUAudioVisualizer () {
@private
struct {
float current[kDOUAudioAnalyzerLevelCount];
float last[kDOUAudioAnalyzerLevelCount];
float pacing[kDOUAudioAnalyzerLevelCount];
} _levels;
float _coefficient;
NSUInteger _step;
NSUInteger _stepCount;
DOUAudioVisualizerInterpolationType _interpolationType;
struct {
CGFloat width;
CGFloat height;
CGFloat horizontalPadding;
CGFloat verticalPadding;
NSUInteger horizontalCount;
NSUInteger verticalCount;
} _bar;
GLuint _vbo;
GLuint _ibo;
}
@end
@implementation DOUAudioVisualizer
@synthesize stepCount = _stepCount;
@synthesize interpolationType = _interpolationType;
#pragma mark - Shared Analyzer
+ (void)_applicationDidEnterBackgroundNotification:(NSNotification *)notification
{
[[self _sharedAnalyzer] setEnabled:NO];
}
+ (void)_applicationWillEnterForegroundNotification:(NSNotification *)notification
{
[[self _sharedAnalyzer] setEnabled:YES];
}
+ (void)_setupNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_applicationDidEnterBackgroundNotification:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_applicationWillEnterForegroundNotification:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
}
+ (DOUAudioAnalyzer *)_sharedAnalyzer
{
static DOUAudioAnalyzer *sharedAnalyzer = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedAnalyzer = [DOUAudioAnalyzer frequencyAnalyzer];
[sharedAnalyzer setEnabled:YES];
[DOUAudioStreamer setAnalyzers:@[sharedAnalyzer]];
[self performSelector:@selector(_setupNotifications)
withObject:nil
afterDelay:0.0];
});
return sharedAnalyzer;
}
#pragma mark - Miscellaneous
- (void)_applicationDidEnterBackgroundNotification:(NSNotification *)notification
{
[self setPaused:YES];
}
- (void)_applicationWillEnterForegroundNotification:(NSNotification *)notification
{
[self setPaused:NO];
}
- (void)_setupNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_applicationDidEnterBackgroundNotification:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_applicationWillEnterForegroundNotification:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
}
- (void)_setupVisualizer
{
_coefficient = 0.0f;
_step = 0;
_stepCount = 6;
_interpolationType = DOUAudioVisualizerLinearInterpolation;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self _setupNotifications];
[self _setupVisualizer];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self _setupNotifications];
[self _setupVisualizer];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - Animation
- (void)_updateStepAndLevels
{
#define COEFFICIENT ((float)_step / _stepCount)
#define INTERPOLATED_COEFFICIENT_LINEAR (COEFFICIENT)
#define INTERPOLATED_COEFFICIENT_SMOOTH (sinf(COEFFICIENT * M_PI - M_PI_2) * 0.5f + 0.5f)
switch (_interpolationType) {
default:
case DOUAudioVisualizerLinearInterpolation:
_coefficient = INTERPOLATED_COEFFICIENT_LINEAR;
break;
case DOUAudioVisualizerSmoothInterpolation:
_coefficient = INTERPOLATED_COEFFICIENT_SMOOTH;
break;
}
if (_coefficient >= 1.0f) {
_coefficient = 0.0f;
}
#undef COEFFICIENT
#undef INTERPOLATED_COEFFICIENT_LINEAR
#undef INTERPOLATED_COEFFICIENT_SMOOTH
if (++_step > _stepCount) {
_step = 0;
memcpy(_levels.last, _levels.current, sizeof(float) * kDOUAudioAnalyzerLevelCount);
[[[self class] _sharedAnalyzer] copyLevels:_levels.current];
}
}
- (void)_updatePacingLevels
{
vDSP_vintb(_levels.last, 1,
_levels.current, 1,
&_coefficient,
_levels.pacing, 1,
kDOUAudioAnalyzerLevelCount);
}
#pragma mark - Pre-calculation
- (void)_updateBarGeometries
{
CGFloat width = CGRectGetWidth([self bounds]);
CGFloat height = CGRectGetHeight([self bounds]);
_bar.width = width / kDOUAudioAnalyzerLevelCount;
_bar.height = kBarHeight;
_bar.horizontalPadding = kBarHorizontalPadding;
_bar.verticalPadding = kBarVerticalPadding;
_bar.horizontalCount = kDOUAudioAnalyzerLevelCount;
_bar.verticalCount = (NSUInteger)lrint(floor(height / kBarHeight));
}
- (void)_updateVBO
{
[self _updateBarGeometries];
NSUInteger verticesCount = _bar.verticalCount * 4 * 2;
GLfloat *vertices = (GLfloat *)malloc(sizeof(GLfloat) * verticesCount);
NSUInteger indicesCount = _bar.verticalCount * 4;
GLuint *indices = (GLuint *)malloc(sizeof(GLuint) * indicesCount);
for (NSUInteger i = 0; i < _bar.verticalCount; ++i) {
CGRect rect;
rect.origin.x = _bar.horizontalPadding;
rect.origin.y = _bar.verticalPadding + _bar.height * i;
rect.size.width = _bar.width - 2.0 * _bar.horizontalPadding;
rect.size.height = _bar.height - 2.0 * _bar.verticalPadding;
if (i & 1) {
vertices[i * 4 * 2 + 0 * 2 + 0] = CGRectGetMaxX(rect);
vertices[i * 4 * 2 + 0 * 2 + 1] = CGRectGetMinY(rect);
vertices[i * 4 * 2 + 1 * 2 + 0] = CGRectGetMaxX(rect);
vertices[i * 4 * 2 + 1 * 2 + 1] = CGRectGetMaxY(rect);
vertices[i * 4 * 2 + 2 * 2 + 0] = CGRectGetMinX(rect);
vertices[i * 4 * 2 + 2 * 2 + 1] = CGRectGetMinY(rect);
vertices[i * 4 * 2 + 3 * 2 + 0] = CGRectGetMinX(rect);
vertices[i * 4 * 2 + 3 * 2 + 1] = CGRectGetMaxY(rect);
}
else {
vertices[i * 4 * 2 + 0 * 2 + 0] = CGRectGetMinX(rect);
vertices[i * 4 * 2 + 0 * 2 + 1] = CGRectGetMinY(rect);
vertices[i * 4 * 2 + 1 * 2 + 0] = CGRectGetMinX(rect);
vertices[i * 4 * 2 + 1 * 2 + 1] = CGRectGetMaxY(rect);
vertices[i * 4 * 2 + 2 * 2 + 0] = CGRectGetMaxX(rect);
vertices[i * 4 * 2 + 2 * 2 + 1] = CGRectGetMinY(rect);
vertices[i * 4 * 2 + 3 * 2 + 0] = CGRectGetMaxX(rect);
vertices[i * 4 * 2 + 3 * 2 + 1] = CGRectGetMaxY(rect);
}
indices[i * 4 + 0] = (GLuint)i * 4 + 0;
indices[i * 4 + 1] = (GLuint)i * 4 + 1;
indices[i * 4 + 2] = (GLuint)i * 4 + 2;
indices[i * 4 + 3] = (GLuint)i * 4 + 3;
}
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)(sizeof(GLfloat) * verticesCount), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, (GLsizeiptr)(sizeof(GLuint) * indicesCount), indices, GL_STATIC_DRAW);
free(vertices);
free(indices);
}
#pragma mark - Renderer
- (void)prepare
{
glGenBuffers(1, &_vbo);
glGenBuffers(1, &_ibo);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glColor4f(0.784f, 0.867f, 0.839f, 1.0f);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
}
- (void)cleanup
{
glDeleteBuffers(1, &_vbo);
glDeleteBuffers(1, &_ibo);
}
- (void)reshape
{
CGFloat width = CGRectGetWidth([self bounds]);
CGFloat height = CGRectGetHeight([self bounds]);
CGFloat scale = [[UIScreen mainScreen] scale];
glViewport(0, 0, (GLsizei)lrint(width * scale), (GLsizei)lrint(height * scale));
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(0.0f, width, 0.0f, height, -100.0f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
[self _updateVBO];
}
- (void)render
{
glClear(GL_COLOR_BUFFER_BIT);
[self _updateStepAndLevels];
[self _updatePacingLevels];
glEnableClientState(GL_VERTEX_ARRAY);
for (NSUInteger i = 0; i < _bar.horizontalCount; ++i) {
NSUInteger verticalCount = (NSUInteger)lroundf(_levels.pacing[i] * _bar.verticalCount);
glPushMatrix();
glTranslatef(_bar.width * i, 0.0f, 0.0f);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glVertexPointer(2, GL_FLOAT, 0, NULL);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ibo);
glDrawElements(GL_TRIANGLE_STRIP, (GLsizei)verticalCount * 4, GL_UNSIGNED_INT_OES, NULL);
glPopMatrix();
}
glDisableClientState(GL_VERTEX_ARRAY);
}
@end
#endif /* TARGET_OS_IPHONE */