-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPrismFFTHelper.mm
164 lines (130 loc) · 4.69 KB
/
PrismFFTHelper.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
#import <Accelerate/Accelerate.h>
#import "PrismFFTHelper.h"
#include <notify.h>
static const UInt32 PrismFFTHelperInputBufferSize = 2048;
static const UInt32 PrismFFTHelperMaxBlocksBeforeSkipping = 4;
static BOOL screenIsBlack = NO;
static BOOL cancelOperations = YES;
static void screenDisplayStatus(CFNotificationCenterRef center, void* observer, CFStringRef name, const void* object, CFDictionaryRef userInfo) {
uint64_t state;
int token;
notify_register_check("com.apple.iokit.hid.displayStatus", &token);
notify_get_state(token, &state);
notify_cancel(token);
if (state) {
screenIsBlack = NO;
}else{
screenIsBlack = YES;
}
cancelOperations = YES;
NSLog(@"[PrismFFTHelper] isScreenBlack?: %ld", screenIsBlack);
}
@interface PrismFFTHelper ()
{
float * _window;
float * _inReal;
UInt32 _numberOfSamples;
COMPLEX_SPLIT _split;
FFTSetup _fftSetup;
}
@property (nonatomic, strong) NSOperationQueue * operationQueue;
@end
@implementation PrismFFTHelper
- (id)init
{
if( self = [self initWithNumberOfSamples:PrismFFTHelperInputBufferSize])
{
}
return self;
}
- (instancetype)initWithNumberOfSamples:(UInt32)numberOfSamples
{
if( self = [super init])
{
_numberOfSamples = numberOfSamples;
UInt32 nOver2 = _numberOfSamples/2;
vDSP_Length log2n = log2f(_numberOfSamples);
_fftSetup = vDSP_create_fftsetup(log2n, FFT_RADIX2);
_inReal = (float*)malloc(_numberOfSamples * sizeof(float));
_window = (float*)malloc(_numberOfSamples * sizeof(float));
_split.realp = (float*)malloc(nOver2 * sizeof(float));
_split.imagp = (float*)malloc(nOver2 * sizeof(float));
vDSP_hann_window(_window, _numberOfSamples, vDSP_HANN_DENORM);
_operationQueue = [NSOperationQueue new];
_operationQueue.maxConcurrentOperationCount = 1;
self.screenIsBlack = NO;
NSLog(@"[PrismFFTHelper]Adding display status observer.");
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, screenDisplayStatus, CFSTR("com.apple.iokit.hid.displayStatus"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
}
return self;
}
- (void) dealloc
{
[self.operationQueue cancelAllOperations];
vDSP_destroy_fftsetup(_fftSetup);
free(_inReal);
free(_window);
free(_split.realp);
free(_split.imagp);
}
-(void)performComputation:(AudioBufferList *)bufferListInOut numberFrames:(CMItemCount)numberFrames isNonInterleaved:(BOOL)isNonInterleaved completionHandler:(PrismFFTHelperCompletionBlock)completion
{
if(!completion)
{
return;
}
self.screenIsBlack = screenIsBlack;
if(self.operationQueue.operationCount > 1 || cancelOperations || self.screenIsBlack)
{
[self.operationQueue cancelAllOperations];
cancelOperations = NO;
}
if(self.screenIsBlack)
{
return;
}
[self.operationQueue addOperationWithBlock:^
{
float leftVol = 0.0;
float rightVol = 0.0;
for (UInt32 i = 0; i < bufferListInOut->mNumberBuffers; i++)
{
AudioBuffer *pBuffer = &bufferListInOut->mBuffers[i];
UInt32 cSamples = numberFrames * (isNonInterleaved ? 1 : pBuffer->mNumberChannels);
float *pData = (float *)pBuffer->mData;
float rms = 0.0f;
for (UInt32 j = 0; j < cSamples; j++)
{
rms += pData[j] * pData[j];
}
if (cSamples > 0)
{
rms = sqrtf(rms / cSamples);
}
if (0 == i)
{
leftVol = rms;
}
if (1 == i || (0 == i && 1 == bufferListInOut->mNumberBuffers))
{
rightVol = rms;
}
}
AudioBuffer * firstBuffer = &bufferListInOut->mBuffers[1];
float * bufferData = (float*)firstBuffer->mData;
vDSP_vmul(bufferData, 1 , _window, 1, _inReal, 1, _numberOfSamples);
vDSP_ctoz((COMPLEX*)_inReal, 2, &_split, 1, _numberOfSamples/2);
vDSP_Length log2n = log2f((float)_numberOfSamples);
vDSP_fft_zrip(_fftSetup, &_split, 1, log2n, FFT_FORWARD);
_split.imagp[0] = 0.0;
NSMutableArray * outData = [NSMutableArray new];
[outData addObject:[NSNumber numberWithFloat:0]];
for(UInt32 i=1; i < _numberOfSamples/2; i++)
{
float power = sqrtf(_split.realp[i] * _split.realp[i] + _split.imagp[i] * _split.imagp[i]);
[outData addObject:[NSNumber numberWithFloat:power]];
}
completion(outData, (leftVol+rightVol)/2.0, _numberOfSamples/2);
}];
}
@end