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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
|
/*
* $Id$
*
* ***** BEGIN LGPL LICENSE BLOCK *****
*
* Copyright 2009 Jörg Hermann Müller
*
* This file is part of AudaSpace.
*
* AudaSpace is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AudaSpace is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with AudaSpace. If not, see <http://www.gnu.org/licenses/>.
*
* ***** END LGPL LICENSE BLOCK *****
*/
#include "AUD_NULLDevice.h"
#include "AUD_I3DDevice.h"
#include "AUD_FileFactory.h"
#include "AUD_StreamBufferFactory.h"
#include "AUD_DelayFactory.h"
#include "AUD_LimiterFactory.h"
#include "AUD_PingPongFactory.h"
#include "AUD_LoopFactory.h"
#include "AUD_ReadDevice.h"
#include "AUD_SourceCaps.h"
#include "AUD_IReader.h"
#ifdef WITH_SDL
#include "AUD_SDLDevice.h"
#include "AUD_FloatMixer.h"
#endif
#ifdef WITH_OPENAL
#include "AUD_OpenALDevice.h"
#endif
#ifdef WITH_JACK
#include "AUD_JackDevice.h"
#endif
#ifdef WITH_FFMPEG
extern "C" {
#include <libavformat/avformat.h>
}
#endif
#include <assert.h>
typedef AUD_IFactory AUD_Sound;
typedef AUD_ReadDevice AUD_Device;
#define AUD_CAPI_IMPLEMENTATION
#include "AUD_C-API.h"
#ifndef NULL
#define NULL 0
#endif
static AUD_IDevice* AUD_device = NULL;
static int AUD_available_devices[4];
static AUD_I3DDevice* AUD_3ddevice = NULL;
int AUD_init(AUD_DeviceType device, AUD_Specs specs, int buffersize)
{
#ifdef WITH_FFMPEG
av_register_all();
#endif
AUD_IDevice* dev = NULL;
if(AUD_device)
AUD_exit();
try
{
switch(device)
{
case AUD_NULL_DEVICE:
dev = new AUD_NULLDevice();
break;
#ifdef WITH_SDL
case AUD_SDL_DEVICE:
{
dev = new AUD_SDLDevice(specs, buffersize);
AUD_FloatMixer* mixer = new AUD_FloatMixer();
((AUD_SDLDevice*)dev)->setMixer(mixer);
break;
}
#endif
#ifdef WITH_OPENAL
case AUD_OPENAL_DEVICE:
dev = new AUD_OpenALDevice(specs, buffersize);
break;
#endif
#ifdef WITH_JACK
case AUD_JACK_DEVICE:
dev = new AUD_JackDevice(specs);
break;
#endif
default:
return false;
}
AUD_device = dev;
if(AUD_device->checkCapability(AUD_CAPS_3D_DEVICE))
AUD_3ddevice = dynamic_cast<AUD_I3DDevice*>(AUD_device);
return true;
}
catch(AUD_Exception)
{
return false;
}
}
int* AUD_enumDevices()
{
int i = 0;
#ifdef WITH_SDL
AUD_available_devices[i++] = AUD_SDL_DEVICE;
#endif
#ifdef WITH_OPENAL
AUD_available_devices[i++] = AUD_OPENAL_DEVICE;
#endif
#ifdef WITH_JACK
AUD_available_devices[i++] = AUD_JACK_DEVICE;
#endif
AUD_available_devices[i++] = AUD_NULL_DEVICE;
return AUD_available_devices;
}
void AUD_exit()
{
if(AUD_device)
{
delete AUD_device;
AUD_device = NULL;
AUD_3ddevice = NULL;
}
}
void AUD_lock()
{
assert(AUD_device);
AUD_device->lock();
}
void AUD_unlock()
{
assert(AUD_device);
AUD_device->unlock();
}
AUD_SoundInfo AUD_getInfo(AUD_Sound* sound)
{
assert(sound);
AUD_IReader* reader = sound->createReader();
AUD_SoundInfo info;
if(reader)
{
info.specs = reader->getSpecs();
info.length = reader->getLength() / (float) info.specs.rate;
}
else
{
info.specs.channels = AUD_CHANNELS_INVALID;
info.specs.format = AUD_FORMAT_INVALID;
info.specs.rate = AUD_RATE_INVALID;
info.length = 0.0;
}
return info;
}
AUD_Sound* AUD_load(const char* filename)
{
assert(filename);
return new AUD_FileFactory(filename);
}
AUD_Sound* AUD_loadBuffer(unsigned char* buffer, int size)
{
assert(buffer);
return new AUD_FileFactory(buffer, size);
}
AUD_Sound* AUD_bufferSound(AUD_Sound* sound)
{
assert(sound);
try
{
return new AUD_StreamBufferFactory(sound);
}
catch(AUD_Exception)
{
return NULL;
}
}
AUD_Sound* AUD_delaySound(AUD_Sound* sound, float delay)
{
assert(sound);
try
{
return new AUD_DelayFactory(sound, delay);
}
catch(AUD_Exception)
{
return NULL;
}
}
extern AUD_Sound* AUD_limitSound(AUD_Sound* sound, float start, float end)
{
assert(sound);
try
{
return new AUD_LimiterFactory(sound, start, end);
}
catch(AUD_Exception)
{
return NULL;
}
}
AUD_Sound* AUD_pingpongSound(AUD_Sound* sound)
{
assert(sound);
try
{
return new AUD_PingPongFactory(sound);
}
catch(AUD_Exception)
{
return NULL;
}
}
AUD_Sound* AUD_loopSound(AUD_Sound* sound)
{
assert(sound);
try
{
return new AUD_LoopFactory(sound);
}
catch(AUD_Exception)
{
return NULL;
}
}
int AUD_stopLoop(AUD_Handle* handle)
{
if(handle)
{
AUD_Message message;
message.type = AUD_MSG_LOOP;
message.loopcount = 0;
try
{
return AUD_device->sendMessage(handle, message);
}
catch(AUD_Exception)
{
}
}
return false;
}
void AUD_unload(AUD_Sound* sound)
{
assert(sound);
delete sound;
}
AUD_Handle* AUD_play(AUD_Sound* sound, int keep)
{
assert(AUD_device);
assert(sound);
try
{
return AUD_device->play(sound, keep);
}
catch(AUD_Exception)
{
return NULL;
}
}
int AUD_pause(AUD_Handle* handle)
{
assert(AUD_device);
return AUD_device->pause(handle);
}
int AUD_resume(AUD_Handle* handle)
{
assert(AUD_device);
return AUD_device->resume(handle);
}
int AUD_stop(AUD_Handle* handle)
{
if(AUD_device)
return AUD_device->stop(handle);
return false;
}
int AUD_setKeep(AUD_Handle* handle, int keep)
{
assert(AUD_device);
return AUD_device->setKeep(handle, keep);
}
int AUD_seek(AUD_Handle* handle, float seekTo)
{
assert(AUD_device);
return AUD_device->seek(handle, seekTo);
}
float AUD_getPosition(AUD_Handle* handle)
{
assert(AUD_device);
return AUD_device->getPosition(handle);
}
AUD_Status AUD_getStatus(AUD_Handle* handle)
{
assert(AUD_device);
return AUD_device->getStatus(handle);
}
AUD_Handle* AUD_play3D(AUD_Sound* sound, int keep)
{
assert(AUD_device);
assert(sound);
try
{
if(AUD_3ddevice)
return AUD_3ddevice->play3D(sound, keep);
else
return AUD_device->play(sound, keep);
}
catch(AUD_Exception)
{
return NULL;
}
}
int AUD_updateListener(AUD_3DData* data)
{
assert(AUD_device);
assert(data);
try
{
if(AUD_3ddevice)
return AUD_3ddevice->updateListener(*data);
}
catch(AUD_Exception)
{
}
return false;
}
int AUD_set3DSetting(AUD_3DSetting setting, float value)
{
assert(AUD_device);
try
{
if(AUD_3ddevice)
return AUD_3ddevice->setSetting(setting, value);
}
catch(AUD_Exception)
{
}
return false;
}
float AUD_get3DSetting(AUD_3DSetting setting)
{
assert(AUD_device);
try
{
if(AUD_3ddevice)
return AUD_3ddevice->getSetting(setting);
}
catch(AUD_Exception)
{
}
return 0.0;
}
int AUD_update3DSource(AUD_Handle* handle, AUD_3DData* data)
{
if(handle)
{
assert(AUD_device);
assert(data);
try
{
if(AUD_3ddevice)
return AUD_3ddevice->updateSource(handle, *data);
}
catch(AUD_Exception)
{
}
}
return false;
}
int AUD_set3DSourceSetting(AUD_Handle* handle,
AUD_3DSourceSetting setting, float value)
{
if(handle)
{
assert(AUD_device);
try
{
if(AUD_3ddevice)
return AUD_3ddevice->setSourceSetting(handle, setting, value);
}
catch(AUD_Exception)
{
}
}
return false;
}
float AUD_get3DSourceSetting(AUD_Handle* handle, AUD_3DSourceSetting setting)
{
if(handle)
{
assert(AUD_device);
try
{
if(AUD_3ddevice)
return AUD_3ddevice->getSourceSetting(handle, setting);
}
catch(AUD_Exception)
{
}
}
return 0.0;
}
int AUD_setSoundVolume(AUD_Handle* handle, float volume)
{
if(handle)
{
assert(AUD_device);
AUD_SourceCaps caps;
caps.handle = handle;
caps.value = volume;
try
{
return AUD_device->setCapability(AUD_CAPS_SOURCE_VOLUME, &caps);
}
catch(AUD_Exception) {}
}
return false;
}
int AUD_setSoundPitch(AUD_Handle* handle, float pitch)
{
if(handle)
{
assert(AUD_device);
AUD_SourceCaps caps;
caps.handle = handle;
caps.value = pitch;
try
{
return AUD_device->setCapability(AUD_CAPS_SOURCE_PITCH, &caps);
}
catch(AUD_Exception) {}
}
return false;
}
AUD_Device* AUD_openReadDevice(AUD_Specs specs)
{
try
{
return new AUD_ReadDevice(specs);
}
catch(AUD_Exception)
{
return NULL;
}
}
AUD_Handle* AUD_playDevice(AUD_Device* device, AUD_Sound* sound)
{
assert(device);
assert(sound);
try
{
return device->play(sound);
}
catch(AUD_Exception)
{
return NULL;
}
}
int AUD_setDeviceVolume(AUD_Device* device, float volume)
{
assert(device);
try
{
return device->setCapability(AUD_CAPS_VOLUME, &volume);
}
catch(AUD_Exception) {}
return false;
}
int AUD_setDeviceSoundVolume(AUD_Device* device, AUD_Handle* handle,
float volume)
{
if(handle)
{
assert(device);
AUD_SourceCaps caps;
caps.handle = handle;
caps.value = volume;
try
{
return device->setCapability(AUD_CAPS_SOURCE_VOLUME, &caps);
}
catch(AUD_Exception) {}
}
return false;
}
int AUD_readDevice(AUD_Device* device, sample_t* buffer, int length)
{
assert(device);
assert(buffer);
try
{
return device->read(buffer, length);
}
catch(AUD_Exception)
{
return false;
}
}
void AUD_closeReadDevice(AUD_Device* device)
{
assert(device);
try
{
delete device;
}
catch(AUD_Exception)
{
}
}
|