-
Notifications
You must be signed in to change notification settings - Fork 80
/
NVDSP.mm
180 lines (143 loc) · 4.72 KB
/
NVDSP.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
//
// NVDSP.mm
// NVDSP
//
// Created by Bart Olsthoorn on 13/05/2012.
// Copyright (c) 2012 - 2014 Bart Olsthoorn
// MIT licensed, see license.txt
// http://bartolsthoorn.nl
//
#import "NVDSP.h"
#define MAX_CHANNEL_COUNT 2
@implementation NVDSP
- (id)initWithSamplingRate:(float)sr {
if (self = [self init]) {
samplingRate = sr;
for (int i = 0; i < 5; i++) {
coefficients[i] = 0.0f;
}
for (int i = 0; i < MAX_CHANNEL_COUNT; i++) {
gInputKeepBuffer[i] = (float *)calloc(2, sizeof(float));
gOutputKeepBuffer[i] = (float *)calloc(2, sizeof(float));
}
zero = 0.0f;
one = 1.0f;
}
return self;
}
- (void)dealloc {
for (int i = 0; i < MAX_CHANNEL_COUNT; i++) {
free(gInputKeepBuffer[i]);
free(gOutputKeepBuffer[i]);
}
#if !__has_feature(objc_arc)
[super dealloc];
#endif
}
#pragma mark - Setters
- (void) setCoefficients {
// // { b0/a0, b1/a0, b2/a0, a1/a0, a2/a0 }
coefficients[0] = b0;
coefficients[1] = b1;
coefficients[2] = b2;
coefficients[3] = a1;
coefficients[4] = a2;
[self stabilityWarning];
}
#pragma mark - Effects
- (void) applyGain:(float *)data length:(vDSP_Length)length gain:(float)gain {
vDSP_vsmul(data, 1, &gain, data, 1, length);
}
- (void) filterContiguousData: (float *)data numFrames:(UInt32)numFrames channel:(UInt32)channel {
// Provide buffer for processing
float tInputBuffer[numFrames + 2];
float tOutputBuffer[numFrames + 2];
// Copy the data
memcpy(tInputBuffer, gInputKeepBuffer[channel], 2 * sizeof(float));
memcpy(tOutputBuffer, gOutputKeepBuffer[channel], 2 * sizeof(float));
memcpy(&(tInputBuffer[2]), data, numFrames * sizeof(float));
// Do the processing
vDSP_deq22(tInputBuffer, 1, coefficients, tOutputBuffer, 1, numFrames);
// Copy the data
memcpy(data, tOutputBuffer, numFrames * sizeof(float));
memcpy(gInputKeepBuffer[channel], &(tInputBuffer[numFrames]), 2 * sizeof(float));
memcpy(gOutputKeepBuffer[channel], &(tOutputBuffer[numFrames]), 2 * sizeof(float));
}
- (void)filterData:(float *)data numFrames:(UInt32)numFrames numChannels:(UInt32)numChannels {
switch (numChannels) {
case 1:
[self filterContiguousData:data numFrames:numFrames channel:0];
break;
case 2: {
float left[numFrames + 2];
float right[numFrames + 2];
[self deinterleave:data left:left right:right length:numFrames];
[self filterContiguousData:left numFrames:numFrames channel:0];
[self filterContiguousData:right numFrames:numFrames channel:1];
[self interleave:data left:left right:right length:numFrames];
break;
}
default:
NSLog(@"WARNING: Unsupported number of channels %u", (unsigned int)numChannels);
break;
}
}
#pragma mark - Etc
- (void) deinterleave: (float *)data left: (float*) left right: (float*) right length: (vDSP_Length)length {
vDSP_vsadd(data, 2, &zero, left, 1, length);
vDSP_vsadd(data+1, 2, &zero, right, 1, length);
}
- (void) interleave: (float *)data left: (float*) left right: (float*) right length: (vDSP_Length)length {
vDSP_vsadd(left, 1, &zero, data, 2, length);
vDSP_vsadd(right, 1, &zero, data+1, 2, length);
}
- (void) mono: (float *)data left: (float*) left right: (float*) right length: (vDSP_Length)length {
[self applyGain:left length:length gain:0.5];
[self applyGain:right length:length gain:0.5];
vDSP_vadd(left, 1, right, 1, left, 1, length);
[self interleave:data left:left right:left length:length];
}
- (void) intermediateVariables: (float)Fc Q: (float)Q {
omega = 2*M_PI*Fc/samplingRate;
omegaS = sin(omega);
omegaC = cos(omega);
alpha = omegaS / (2*Q);
}
#pragma mark - Debug
- (void) logCoefficients {
NSLog(@"------------\n");
NSLog(@"Coefficients:\n");
NSLog(@"b0: %f\n", b0);
NSLog(@"b1: %f\n", b1);
NSLog(@"b2: %f\n", b2);
NSLog(@"a1: %f\n", a0);
NSLog(@"a1: %f\n", a1);
NSLog(@"a2: %f\n", a2);
NSLog(@"\n");
NSLog(@"|a1| < 1 + a2 ");
if (abs(a1) < (1 + a2)) {
NSLog(@"a1 is stable\n");
} else {
NSLog(@"a1 is unstable\n");
}
NSLog(@"|a2| < 1");
if (abs(a2) < 1) {
NSLog(@"a2 is stable\n");
} else {
NSLog(@"a2 is unstable\n");
}
NSLog(@"------------\n");
}
- (void) stabilityWarning {
if (abs(a1) < (1 + a2)) {
} else {
NSLog(@"|a1| < 1 + a2 ");
NSLog(@"Warning: a1 is unstable\n");
}
if (abs(a2) < 1) {
} else {
NSLog(@"|a2| < 1");
NSLog(@"Warning: a2 is unstable\n");
}
}
@end