-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSampleBasedContexts.mm
More file actions
272 lines (229 loc) · 7.95 KB
/
SampleBasedContexts.mm
File metadata and controls
272 lines (229 loc) · 7.95 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
// Copyright © 2021 Brad Howes. All rights reserved.
//
#include <AVFoundation/AVFoundation.h>
#include <iomanip>
#import "SF2Lib/Configuration.hpp"
#include "SampleBasedContexts.hpp"
#include "TestResources.hpp"
using namespace SF2;
using namespace SF2::Render;
typedef AUValue* AUValuePtr;
NSURL* PresetTestContextBase::getUrl(size_t urlIndex)
{
return [TestResources getResourceUrl:urlIndex];
}
SF2::IO::File& PresetTestContextBase::getFile(size_t urlIndex)
{
return [TestResources getFile:urlIndex];
}
BOOL PresetTestContextBase::playAudioInTests() {
#if PLAY_AUDIO
bool playAudio = YES;
#else
bool playAudio = Configuration.shared.testsPlayAudio;
#endif
return playAudio;
}
@implementation SamplePlayingTestCase
@synthesize sst;
@synthesize contexts;
@synthesize player;
@synthesize playedAudioExpectation;
@synthesize audioFileURL;
@synthesize samplesBuffer;
@synthesize deleteFile;
@synthesize playAudio;
@synthesize epsilon;
- (NSString *)pathForTemporaryFile
{
CFUUIDRef uuid = CFUUIDCreate(NULL);
CFStringRef uuidStr = CFUUIDCreateString(NULL, uuid);
NSString* result = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.caf",
uuidStr]];
CFRelease(uuidStr);
CFRelease(uuid);
return result;
}
- (void)setUp
{
self.contexts = new SampleBasedContexts();
self.epsilon = PresetTestContextBase::epsilonValue();
self.deleteFile = YES;
self.playAudio = PresetTestContextBase::playAudioInTests();
}
- (void)tearDown
{
delete self.contexts;
[super tearDown];
}
- (void)cleanup
{
NSString* path = [self.audioFileURL path];
if (self.deleteFile && path != nullptr) {
[[NSFileManager defaultManager] removeItemAtPath:path error:NULL];
}
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[self cleanup];
[self.playedAudioExpectation fulfill];
}
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError * __nullable)error
{
[self cleanup];
}
- (void)playSamples:(AVAudioPCMBuffer*)buffer count:(AVAudioFrameCount)sampleCount
{
if (!self.playAudio) {
[self cleanup];
return;
}
buffer.frameLength = sampleCount;
NSError* error = nil;
self.audioFileURL = [NSURL fileURLWithPath: [self pathForTemporaryFile] isDirectory:NO];
NSLog(@"audioFileURL: %@", self.audioFileURL);
NSDictionary* settings = [[buffer format] settings];
// NSLog(@"%@", [settings description]);
[settings setValue:0 forKey:@"AVLinearPCMIsNonInterleaved"];
AVAudioFile* audioFile = [[AVAudioFile alloc] initForWriting:self.audioFileURL
settings:settings
commonFormat:AVAudioPCMFormatFloat32
interleaved:false
error:&error];
if (error) {
XCTFail(@"failed with error: %@", error);
return;
}
[audioFile writeFromBuffer:buffer error:&error];
if (error) {
XCTFail(@"failed with error: %@", error);
return;
}
audioFile = nil;
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:self.audioFileURL error:&error];
if (self.player == nullptr && error != nullptr) {
XCTFail(@"Expectation Failed with error: %@", error);
return;
}
self.player.delegate = self;
self.playedAudioExpectation = [self expectationWithDescription:@"AVAudioPlayer finished"];
[self.player play];
[self waitForExpectationsWithTimeout:30.0 handler:^(NSError *err) {
if (err) {
XCTFail(@"Expectation Failed with error: %@", err);
}
}];
}
AVAudioPCMBuffer* makeBuffer(AVAudioFormat* format, AVAudioFrameCount sampleCount)
{
AVAudioPCMBuffer* buffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:format frameCapacity:sampleCount];
AudioBufferList* bufferList = buffer.mutableAudioBufferList;
for (AVAudioChannelCount index = 0; index < format.channelCount; ++index) {
UInt32 byteCount = sampleCount * sizeof(AUValue);
bufferList->mBuffers[index].mDataByteSize = byteCount;
memset(bufferList->mBuffers[index].mData, 0, byteCount);
}
return buffer;
}
- (AVAudioPCMBuffer*)allocateBuffer:(SF2::Float)sampleRate numberOfChannels:(AVAudioChannelCount)channels capacity:(AVAudioFrameCount)sampleCount
{
AVAudioFormat* format = [[AVAudioFormat alloc] initStandardFormatWithSampleRate:sampleRate channels:channels];
return makeBuffer(format, sampleCount);
}
- (AVAudioPCMBuffer*)allocateBufferFor:(const TestVoiceCollection&)voices capacity:(AVAudioFrameCount)sampleCount
{
AVAudioFormat* format = [[AVAudioFormat alloc] initStandardFormatWithSampleRate:voices.sampleRate()
channels:AVAudioChannelCount(voices.count())];
return makeBuffer(format, sampleCount);
}
- (size_t)renderInto:(AVAudioPCMBuffer*)buffer
mono:(SF2::Render::Voice::Voice&)voice
forCount:(AVAudioFrameCount)sampleCount
startingAt:(size_t)offset
{
AUValue* ptr = [buffer left] + offset;
for (size_t index = 0; index < sampleCount; ++index) {
auto sample = AUValue(voice.renderSample());
*ptr++ += sample;
}
return offset + sampleCount;
}
- (size_t)renderInto:(AVAudioPCMBuffer*)buffer
voices:(TestVoiceCollection&)voices
forCount:(AVAudioFrameCount)sampleCount
startingAt:(size_t)offset
{
for (size_t channel = 0; channel < voices.count(); ++channel) {
auto into = [buffer channel: channel] + offset;
auto& voice{voices[channel]};
for (size_t index = 0; index < sampleCount; ++index) {
*into++ += AUValue(voice.renderSample());
}
}
return offset + sampleCount;
}
- (size_t)renderInto:(AVAudioPCMBuffer*)buffer
voices:(TestVoiceCollection&)voices
forCount:(AVAudioFrameCount)sampleCount
startingAt:(size_t)offset
afterRenderSample:(void (^)(size_t))block
{
for (size_t channel = 0; channel < voices.count(); ++channel) {
auto into = [buffer channel:channel] + offset;
auto& voice{voices[channel]};
for (size_t index = 0; index < sampleCount; ++index) {
*into++ += AUValue(voice.renderSample());
block(index + offset);
}
}
return offset + sampleCount;
}
- (size_t)renderInto:(AVAudioPCMBuffer*)buffer
left:(SF2::Render::Voice::Voice&)left
right:(SF2::Render::Voice::Voice&)right
forCount:(AVAudioFrameCount)sampleCount
startingAt:(size_t)offset
{
AUValue* samplesLeft = [buffer left] + offset;
AUValue* samplesRight = [buffer right] + offset;
for (size_t index = 0; index < sampleCount; ++index) {
*samplesLeft++ += AUValue(left.renderSample());
*samplesRight++ += AUValue(right.renderSample());
}
return offset + sampleCount;
}
- (void)dumpPresets:(const SF2::IO::File&)file
{
for (size_t index = 0; index < file.presets().size(); ++index) {
std::cout << index << ' ' << file.presets()[index].name() << '\n';
}
}
- (void)dumpSamples:(const std::vector<AUValue>&)samples
{
std::cout << std::setprecision(12);
for (size_t index = 0; index < samples.size(); ++index) {
std::cout << "XCTAssertEqualWithAccuracy(" << samples[index] << ", samples[" << index << "], epsilon);\n";
}
}
@end
@implementation AVAudioPCMBuffer(Accessors)
- (void)normalize:(size_t)voices
{
for (AVAudioChannelCount channel = 0; channel < self.format.channelCount; ++channel) {
auto count = self.mutableAudioBufferList->mBuffers[channel].mDataByteSize / sizeof(AUValue);
auto ptr = AUValuePtr(self.mutableAudioBufferList->mBuffers[channel].mData);
while (count-- > 0) {
*ptr++ /= AUValue(voices);
}
}
}
- (AUValue*)left {
return AUValuePtr(self.mutableAudioBufferList->mBuffers[0].mData);
}
- (AUValue*)right {
return AUValuePtr(self.mutableAudioBufferList->mBuffers[1].mData);
}
- (AUValue*)channel:(size_t)index {
return AUValuePtr(self.mutableAudioBufferList->mBuffers[index].mData);
}
@end