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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
/*******************************************************************************
* Copyright 2009-2016 Jörg Müller
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
#include "Exception.h"
#include "IReader.h"
#include "file/File.h"
#include "respec/ChannelMapper.h"
#include "fx/Lowpass.h"
#include "fx/Highpass.h"
#include "fx/Envelope.h"
#include "respec/LinearResample.h"
#include "fx/Threshold.h"
#include "fx/Accumulator.h"
#include "fx/Sum.h"
#include "generator/Silence.h"
#include "fx/Limiter.h"
#include "devices/DeviceManager.h"
#include "sequence/Sequence.h"
#include "file/FileWriter.h"
#include "devices/ReadDevice.h"
#include "plugin/PluginManager.h"
#include "devices/DeviceManager.h"
#include "devices/IDeviceFactory.h"
#include "devices/NULLDevice.h"
#include <cassert>
#include <cstring>
#include <cmath>
#include <sstream>
using namespace aud;
#define AUD_CAPI_IMPLEMENTATION
#include "AUD_Special.h"
static inline AUD_Specs convSpecToC(aud::Specs specs)
{
AUD_Specs s;
s.channels = static_cast<AUD_Channels>(specs.channels);
s.rate = static_cast<AUD_SampleRate>(specs.rate);
return s;
}
static inline aud::Specs convCToSpec(AUD_Specs specs)
{
aud::Specs s;
s.channels = static_cast<Channels>(specs.channels);
s.rate = static_cast<SampleRate>(specs.rate);
return s;
}
static inline AUD_DeviceSpecs convDSpecToC(aud::DeviceSpecs specs)
{
AUD_DeviceSpecs s;
s.specs = convSpecToC(specs.specs);
s.format = static_cast<AUD_SampleFormat>(specs.format);
return s;
}
static inline aud::DeviceSpecs convCToDSpec(AUD_DeviceSpecs specs)
{
aud::DeviceSpecs s;
s.specs = convCToSpec(specs.specs);
s.format = static_cast<SampleFormat>(specs.format);
return s;
}
AUD_API AUD_SoundInfo AUD_getInfo(AUD_Sound* sound)
{
assert(sound);
AUD_SoundInfo info;
info.specs.channels = AUD_CHANNELS_INVALID;
info.specs.rate = AUD_RATE_INVALID;
info.length = 0.0f;
try
{
std::shared_ptr<IReader> reader = (*sound)->createReader();
if(reader.get())
{
info.specs = convSpecToC(reader->getSpecs());
info.length = reader->getLength() / (float) info.specs.rate;
}
}
catch(Exception&)
{
}
return info;
}
AUD_API float* AUD_readSoundBuffer(const char* filename, float low, float high,
float attack, float release, float threshold,
int accumulate, int additive, int square,
float sthreshold, double samplerate, int* length, int stream)
{
Buffer buffer;
DeviceSpecs specs;
specs.channels = CHANNELS_MONO;
specs.rate = (SampleRate)samplerate;
std::shared_ptr<ISound> sound;
std::shared_ptr<ISound> file = std::shared_ptr<ISound>(new File(filename, stream));
int position = 0;
try
{
std::shared_ptr<IReader> reader = file->createReader();
SampleRate rate = reader->getSpecs().rate;
sound = std::shared_ptr<ISound>(new ChannelMapper(file, specs));
if(high < rate)
sound = std::shared_ptr<ISound>(new Lowpass(sound, high));
if(low > 0)
sound = std::shared_ptr<ISound>(new Highpass(sound, low));
sound = std::shared_ptr<ISound>(new Envelope(sound, attack, release, threshold, 0.1f));
sound = std::shared_ptr<ISound>(new LinearResample(sound, specs));
if(square)
sound = std::shared_ptr<ISound>(new Threshold(sound, sthreshold));
if(accumulate)
sound = std::shared_ptr<ISound>(new Accumulator(sound, additive));
else if(additive)
sound = std::shared_ptr<ISound>(new Sum(sound));
reader = sound->createReader();
if(!reader.get())
return nullptr;
int len;
bool eos;
do
{
len = samplerate;
buffer.resize((position + len) * sizeof(float), true);
reader->read(len, eos, buffer.getBuffer() + position);
position += len;
} while(!eos);
}
catch(Exception&)
{
return nullptr;
}
float * result = (float *)malloc(position * sizeof(float));
std::memcpy(result, buffer.getBuffer(), position * sizeof(float));
*length = position;
return result;
}
static void pauseSound(AUD_Handle* handle)
{
assert(handle);
(*handle)->pause();
}
AUD_API AUD_Handle* AUD_pauseAfter(AUD_Handle* handle, double seconds)
{
auto device = DeviceManager::getDevice();
std::shared_ptr<ISound> silence = std::shared_ptr<ISound>(new Silence(device->getSpecs().rate));
std::shared_ptr<ISound> limiter = std::shared_ptr<ISound>(new Limiter(silence, 0, seconds));
std::lock_guard<ILockable> lock(*device);
try
{
AUD_Handle handle2 = device->play(limiter);
if(handle2.get())
{
handle2->setStopCallback((stopCallback)pauseSound, handle);
return new AUD_Handle(handle2);
}
}
catch(Exception&)
{
}
return nullptr;
}
AUD_API int AUD_readSound(AUD_Sound* sound, float* buffer, int length, int samples_per_second, short* interrupt)
{
DeviceSpecs specs;
float* buf;
Buffer aBuffer;
specs.rate = RATE_INVALID;
specs.channels = CHANNELS_MONO;
specs.format = FORMAT_INVALID;
std::shared_ptr<IReader> reader = ChannelMapper(*sound, specs).createReader();
specs.specs = reader->getSpecs();
int len;
float samplejump = specs.rate / samples_per_second;
float min, max, power, overallmax;
bool eos;
overallmax = 0;
for(int i = 0; i < length; i++)
{
len = floor(samplejump * (i+1)) - floor(samplejump * i);
if(*interrupt)
return 0;
aBuffer.assureSize(len * AUD_SAMPLE_SIZE(specs));
buf = aBuffer.getBuffer();
reader->read(len, eos, buf);
max = min = *buf;
power = *buf * *buf;
for(int j = 1; j < len; j++)
{
if(buf[j] < min)
min = buf[j];
if(buf[j] > max)
max = buf[j];
power += buf[j] * buf[j];
}
buffer[i * 3] = min;
buffer[i * 3 + 1] = max;
buffer[i * 3 + 2] = std::sqrt(power / len);
if(overallmax < max)
overallmax = max;
if(overallmax < -min)
overallmax = -min;
if(eos)
{
length = i;
break;
}
}
if(overallmax > 1.0f)
{
for(int i = 0; i < length * 3; i++)
{
buffer[i] /= overallmax;
}
}
return length;
}
AUD_API int AUD_mixdown(AUD_Sound* sound, unsigned int start, unsigned int length, unsigned int buffersize, const char* filename, AUD_DeviceSpecs specs, AUD_Container format, AUD_Codec codec, unsigned int bitrate, void(*callback)(float, void*), void* data, char* error, size_t errorsize)
{
try
{
Sequence* f = dynamic_cast<Sequence *>(sound->get());
f->setSpecs(convCToSpec(specs.specs));
std::shared_ptr<IReader> reader = f->createQualityReader();
reader->seek(start);
std::shared_ptr<IWriter> writer = FileWriter::createWriter(filename, convCToDSpec(specs), static_cast<Container>(format), static_cast<Codec>(codec), bitrate);
FileWriter::writeReader(reader, writer, length, buffersize, callback, data);
return 1;
}
catch(Exception& e)
{
if(error && errorsize) {
std::strncpy(error, e.getMessage().c_str(), errorsize);
error[errorsize - 1] = '\0';
}
return 0;
}
}
AUD_API int AUD_mixdown_per_channel(AUD_Sound* sound, unsigned int start, unsigned int length, unsigned int buffersize, const char* filename, AUD_DeviceSpecs specs, AUD_Container format, AUD_Codec codec, unsigned int bitrate, void(*callback)(float, void*), void* data, char* error, size_t errorsize)
{
try
{
Sequence* f = dynamic_cast<Sequence *>(sound->get());
f->setSpecs(convCToSpec(specs.specs));
std::vector<std::shared_ptr<IWriter> > writers;
int channels = specs.channels;
specs.channels = AUD_CHANNELS_MONO;
for(int i = 0; i < channels; i++)
{
std::stringstream stream;
std::string fn = filename;
size_t index = fn.find_last_of('.');
size_t index_slash = fn.find_last_of('/');
size_t index_backslash = fn.find_last_of('\\');
if((index == std::string::npos) ||
((index < index_slash) && (index_slash != std::string::npos)) ||
((index < index_backslash) && (index_backslash != std::string::npos)))
{
stream << filename << "_" << (i + 1);
}
else
{
stream << fn.substr(0, index) << "_" << (i + 1) << fn.substr(index);
}
writers.push_back(FileWriter::createWriter(stream.str(), convCToDSpec(specs), static_cast<Container>(format), static_cast<Codec>(codec), bitrate));
}
std::shared_ptr<IReader> reader = f->createQualityReader();
reader->seek(start);
FileWriter::writeReader(reader, writers, length, buffersize, callback, data);
return 1;
}
catch(Exception& e)
{
if(error && errorsize) {
std::strncpy(error, e.getMessage().c_str(), errorsize);
error[errorsize - 1] = '\0';
}
return 0;
}
}
AUD_API AUD_Device* AUD_openMixdownDevice(AUD_DeviceSpecs specs, AUD_Sound* sequencer, float volume, double start)
{
try
{
ReadDevice* device = new ReadDevice(convCToDSpec(specs));
device->setQuality(true);
device->setVolume(volume);
Sequence* f = dynamic_cast<Sequence*>(sequencer->get());
f->setSpecs(convCToSpec(specs.specs));
AUD_Handle handle = device->play(f->createQualityReader());
if(handle.get())
{
handle->seek(start);
}
return new AUD_Device(device);
}
catch(Exception&)
{
return nullptr;
}
}
AUD_API void AUD_initOnce()
{
PluginManager::loadPlugins();
NULLDevice::registerPlugin();
}
AUD_API void AUD_exitOnce()
{
}
AUD_API AUD_Device* AUD_init(const char* device, AUD_DeviceSpecs specs, int buffersize, const char* name)
{
try
{
std::shared_ptr<IDeviceFactory> factory = device ? DeviceManager::getDeviceFactory(device) : DeviceManager::getDefaultDeviceFactory();
if(factory)
{
factory->setName(name);
factory->setBufferSize(buffersize);
factory->setSpecs(convCToDSpec(specs));
auto device = factory->openDevice();
DeviceManager::setDevice(device);
return new AUD_Device(device);
}
}
catch(Exception&)
{
}
return nullptr;
}
AUD_API void AUD_exit(AUD_Device* device)
{
delete device;
DeviceManager::releaseDevice();
}
AUD_API char** AUD_getDeviceNames()
{
std::vector<std::string> v_names = DeviceManager::getAvailableDeviceNames();
char** names = (char**) malloc(sizeof(char*) * (v_names.size() + 1));
for(int i = 0; i < v_names.size(); i++)
{
std::string name = v_names[i];
names[i] = (char*) malloc(sizeof(char) * (name.length() + 1));
strcpy(names[i], name.c_str());
}
names[v_names.size()] = nullptr;
return names;
}
|