2.5 Audio: The jack backend is now realtime capable and will not produce so much...
[blender.git] / intern / audaspace / intern / AUD_C-API.cpp
1 /*
2  * $Id$
3  *
4  * ***** BEGIN LGPL LICENSE BLOCK *****
5  *
6  * Copyright 2009 Jörg Hermann Müller
7  *
8  * This file is part of AudaSpace.
9  *
10  * AudaSpace is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * AudaSpace is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with AudaSpace.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * ***** END LGPL LICENSE BLOCK *****
24  */
25
26 #include <cstdlib>
27 #include <cstring>
28 #include <cmath>
29
30 #include "AUD_NULLDevice.h"
31 #include "AUD_I3DDevice.h"
32 #include "AUD_FileFactory.h"
33 #include "AUD_StreamBufferFactory.h"
34 #include "AUD_DelayFactory.h"
35 #include "AUD_LimiterFactory.h"
36 #include "AUD_PingPongFactory.h"
37 #include "AUD_LoopFactory.h"
38 #include "AUD_RectifyFactory.h"
39 #include "AUD_EnvelopeFactory.h"
40 #include "AUD_LinearResampleFactory.h"
41 #include "AUD_LowpassFactory.h"
42 #include "AUD_HighpassFactory.h"
43 #include "AUD_AccumulatorFactory.h"
44 #include "AUD_SumFactory.h"
45 #include "AUD_SquareFactory.h"
46 #include "AUD_ChannelMapperFactory.h"
47 #include "AUD_Buffer.h"
48 #include "AUD_ReadDevice.h"
49 #include "AUD_SourceCaps.h"
50 #include "AUD_IReader.h"
51 #include "AUD_SequencerFactory.h"
52
53 #ifdef WITH_SDL
54 #include "AUD_SDLDevice.h"
55 #endif
56
57 #ifdef WITH_OPENAL
58 #include "AUD_OpenALDevice.h"
59 #endif
60
61 #ifdef WITH_JACK
62 #include "AUD_JackDevice.h"
63 #endif
64
65 #ifdef WITH_FFMPEG
66 extern "C" {
67 #include <libavformat/avformat.h>
68 }
69 #endif
70
71 #include <cassert>
72
73 typedef AUD_IFactory AUD_Sound;
74 typedef AUD_ReadDevice AUD_Device;
75
76 #define AUD_CAPI_IMPLEMENTATION
77 #include "AUD_C-API.h"
78
79 #ifndef NULL
80 #define NULL 0
81 #endif
82
83 static AUD_IDevice* AUD_device = NULL;
84 static int AUD_available_devices[4];
85 static AUD_I3DDevice* AUD_3ddevice = NULL;
86
87 int AUD_init(AUD_DeviceType device, AUD_DeviceSpecs specs, int buffersize)
88 {
89 #ifdef WITH_FFMPEG
90         av_register_all();
91 #endif
92         AUD_IDevice* dev = NULL;
93
94         if(AUD_device)
95                 AUD_exit();
96
97         try
98         {
99                 switch(device)
100                 {
101                 case AUD_NULL_DEVICE:
102                         dev = new AUD_NULLDevice();
103                         break;
104 #ifdef WITH_SDL
105                 case AUD_SDL_DEVICE:
106                         dev = new AUD_SDLDevice(specs, buffersize);
107                         break;
108 #endif
109 #ifdef WITH_OPENAL
110                 case AUD_OPENAL_DEVICE:
111                         dev = new AUD_OpenALDevice(specs, buffersize);
112                         break;
113 #endif
114 #ifdef WITH_JACK
115                 case AUD_JACK_DEVICE:
116                         dev = new AUD_JackDevice(specs, buffersize);
117                         break;
118 #endif
119                 default:
120                         return false;
121                 }
122
123                 AUD_device = dev;
124                 if(AUD_device->checkCapability(AUD_CAPS_3D_DEVICE))
125                         AUD_3ddevice = dynamic_cast<AUD_I3DDevice*>(AUD_device);
126
127                 return true;
128         }
129         catch(AUD_Exception)
130         {
131                 return false;
132         }
133 }
134
135 int* AUD_enumDevices()
136 {
137         int i = 0;
138 #ifdef WITH_SDL
139         AUD_available_devices[i++] = AUD_SDL_DEVICE;
140 #endif
141 #ifdef WITH_OPENAL
142         AUD_available_devices[i++] = AUD_OPENAL_DEVICE;
143 #endif
144 #ifdef WITH_JACK
145         AUD_available_devices[i++] = AUD_JACK_DEVICE;
146 #endif
147         AUD_available_devices[i++] = AUD_NULL_DEVICE;
148         return AUD_available_devices;
149 }
150
151 void AUD_exit()
152 {
153         if(AUD_device)
154         {
155                 delete AUD_device;
156                 AUD_device = NULL;
157                 AUD_3ddevice = NULL;
158         }
159 }
160
161 void AUD_lock()
162 {
163         assert(AUD_device);
164         AUD_device->lock();
165 }
166
167 void AUD_unlock()
168 {
169         assert(AUD_device);
170         AUD_device->unlock();
171 }
172
173 AUD_SoundInfo AUD_getInfo(AUD_Sound* sound)
174 {
175         assert(sound);
176
177         AUD_IReader* reader = sound->createReader();
178
179         AUD_SoundInfo info;
180
181         if(reader)
182         {
183                 info.specs = reader->getSpecs();
184                 info.length = reader->getLength() / (float) info.specs.rate;
185         }
186         else
187         {
188                 info.specs.channels = AUD_CHANNELS_INVALID;
189                 info.specs.rate = AUD_RATE_INVALID;
190                 info.length = 0.0f;
191         }
192
193         return info;
194 }
195
196 AUD_Sound* AUD_load(const char* filename)
197 {
198         assert(filename);
199         return new AUD_FileFactory(filename);
200 }
201
202 AUD_Sound* AUD_loadBuffer(unsigned char* buffer, int size)
203 {
204         assert(buffer);
205         return new AUD_FileFactory(buffer, size);
206 }
207
208 AUD_Sound* AUD_bufferSound(AUD_Sound* sound)
209 {
210         assert(sound);
211
212         try
213         {
214                 return new AUD_StreamBufferFactory(sound);
215         }
216         catch(AUD_Exception)
217         {
218                 return NULL;
219         }
220 }
221
222 AUD_Sound* AUD_delaySound(AUD_Sound* sound, float delay)
223 {
224         assert(sound);
225
226         try
227         {
228                 return new AUD_DelayFactory(sound, delay);
229         }
230         catch(AUD_Exception)
231         {
232                 return NULL;
233         }
234 }
235
236 AUD_Sound* AUD_limitSound(AUD_Sound* sound, float start, float end)
237 {
238         assert(sound);
239
240         try
241         {
242                 return new AUD_LimiterFactory(sound, start, end);
243         }
244         catch(AUD_Exception)
245         {
246                 return NULL;
247         }
248 }
249
250 AUD_Sound* AUD_pingpongSound(AUD_Sound* sound)
251 {
252         assert(sound);
253
254         try
255         {
256                 return new AUD_PingPongFactory(sound);
257         }
258         catch(AUD_Exception)
259         {
260                 return NULL;
261         }
262 }
263
264 AUD_Sound* AUD_loopSound(AUD_Sound* sound)
265 {
266         assert(sound);
267
268         try
269         {
270                 return new AUD_LoopFactory(sound);
271         }
272         catch(AUD_Exception)
273         {
274                 return NULL;
275         }
276 }
277
278 int AUD_setLoop(AUD_Handle* handle, int loops, float time)
279 {
280         if(handle)
281         {
282                 AUD_Message message;
283                 message.type = AUD_MSG_LOOP;
284                 message.loopcount = loops;
285                 message.time = time;
286
287                 try
288                 {
289                         return AUD_device->sendMessage(handle, message);
290                 }
291                 catch(AUD_Exception)
292                 {
293                 }
294         }
295         return false;
296 }
297
298 AUD_Sound* AUD_rectifySound(AUD_Sound* sound)
299 {
300         assert(sound);
301
302         try
303         {
304                 return new AUD_RectifyFactory(sound);
305         }
306         catch(AUD_Exception)
307         {
308                 return NULL;
309         }
310 }
311
312 void AUD_unload(AUD_Sound* sound)
313 {
314         assert(sound);
315         delete sound;
316 }
317
318 AUD_Handle* AUD_play(AUD_Sound* sound, int keep)
319 {
320         assert(AUD_device);
321         assert(sound);
322         try
323         {
324                 return AUD_device->play(sound, keep);
325         }
326         catch(AUD_Exception)
327         {
328                 return NULL;
329         }
330 }
331
332 int AUD_pause(AUD_Handle* handle)
333 {
334         assert(AUD_device);
335         return AUD_device->pause(handle);
336 }
337
338 int AUD_resume(AUD_Handle* handle)
339 {
340         assert(AUD_device);
341         return AUD_device->resume(handle);
342 }
343
344 int AUD_stop(AUD_Handle* handle)
345 {
346         if(AUD_device)
347                 return AUD_device->stop(handle);
348         return false;
349 }
350
351 int AUD_setKeep(AUD_Handle* handle, int keep)
352 {
353         assert(AUD_device);
354         return AUD_device->setKeep(handle, keep);
355 }
356
357 int AUD_seek(AUD_Handle* handle, float seekTo)
358 {
359         assert(AUD_device);
360         return AUD_device->seek(handle, seekTo);
361 }
362
363 float AUD_getPosition(AUD_Handle* handle)
364 {
365         assert(AUD_device);
366         return AUD_device->getPosition(handle);
367 }
368
369 AUD_Status AUD_getStatus(AUD_Handle* handle)
370 {
371         assert(AUD_device);
372         return AUD_device->getStatus(handle);
373 }
374
375 AUD_Handle* AUD_play3D(AUD_Sound* sound, int keep)
376 {
377         assert(AUD_device);
378         assert(sound);
379
380         try
381         {
382                 if(AUD_3ddevice)
383                         return AUD_3ddevice->play3D(sound, keep);
384                 else
385                         return AUD_device->play(sound, keep);
386         }
387         catch(AUD_Exception)
388         {
389                 return NULL;
390         }
391 }
392
393 int AUD_updateListener(AUD_3DData* data)
394 {
395         assert(AUD_device);
396         assert(data);
397
398         try
399         {
400                 if(AUD_3ddevice)
401                         return AUD_3ddevice->updateListener(*data);
402         }
403         catch(AUD_Exception)
404         {
405         }
406         return false;
407 }
408
409 int AUD_set3DSetting(AUD_3DSetting setting, float value)
410 {
411         assert(AUD_device);
412
413         try
414         {
415                 if(AUD_3ddevice)
416                         return AUD_3ddevice->setSetting(setting, value);
417         }
418         catch(AUD_Exception)
419         {
420         }
421         return false;
422 }
423
424 float AUD_get3DSetting(AUD_3DSetting setting)
425 {
426         assert(AUD_device);
427
428         try
429         {
430                 if(AUD_3ddevice)
431                         return AUD_3ddevice->getSetting(setting);
432         }
433         catch(AUD_Exception)
434         {
435         }
436         return 0.0f;
437 }
438
439 int AUD_update3DSource(AUD_Handle* handle, AUD_3DData* data)
440 {
441         if(handle)
442         {
443                 assert(AUD_device);
444                 assert(data);
445
446                 try
447                 {
448                         if(AUD_3ddevice)
449                                 return AUD_3ddevice->updateSource(handle, *data);
450                 }
451                 catch(AUD_Exception)
452                 {
453                 }
454         }
455         return false;
456 }
457
458 int AUD_set3DSourceSetting(AUD_Handle* handle,
459                                                    AUD_3DSourceSetting setting, float value)
460 {
461         if(handle)
462         {
463                 assert(AUD_device);
464
465                 try
466                 {
467                         if(AUD_3ddevice)
468                                 return AUD_3ddevice->setSourceSetting(handle, setting, value);
469                 }
470                 catch(AUD_Exception)
471                 {
472                 }
473         }
474         return false;
475 }
476
477 float AUD_get3DSourceSetting(AUD_Handle* handle, AUD_3DSourceSetting setting)
478 {
479         if(handle)
480         {
481                 assert(AUD_device);
482
483                 try
484                 {
485                         if(AUD_3ddevice)
486                                 return AUD_3ddevice->getSourceSetting(handle, setting);
487                 }
488                 catch(AUD_Exception)
489                 {
490                 }
491         }
492         return 0.0f;
493 }
494
495 int AUD_setSoundVolume(AUD_Handle* handle, float volume)
496 {
497         if(handle)
498         {
499                 assert(AUD_device);
500                 AUD_SourceCaps caps;
501                 caps.handle = handle;
502                 caps.value = volume;
503
504                 try
505                 {
506                         return AUD_device->setCapability(AUD_CAPS_SOURCE_VOLUME, &caps);
507                 }
508                 catch(AUD_Exception) {}
509         }
510         return false;
511 }
512
513 int AUD_setSoundPitch(AUD_Handle* handle, float pitch)
514 {
515         if(handle)
516         {
517                 assert(AUD_device);
518                 AUD_SourceCaps caps;
519                 caps.handle = handle;
520                 caps.value = pitch;
521
522                 try
523                 {
524                         return AUD_device->setCapability(AUD_CAPS_SOURCE_PITCH, &caps);
525                 }
526                 catch(AUD_Exception) {}
527         }
528         return false;
529 }
530
531 AUD_Device* AUD_openReadDevice(AUD_DeviceSpecs specs)
532 {
533         try
534         {
535                 return new AUD_ReadDevice(specs);
536         }
537         catch(AUD_Exception)
538         {
539                 return NULL;
540         }
541 }
542
543 AUD_Handle* AUD_playDevice(AUD_Device* device, AUD_Sound* sound, float seek)
544 {
545         assert(device);
546         assert(sound);
547
548         try
549         {
550                 AUD_Handle* handle = device->play(sound);
551                 device->seek(handle, seek);
552                 return handle;
553         }
554         catch(AUD_Exception)
555         {
556                 return NULL;
557         }
558 }
559
560 int AUD_setDeviceVolume(AUD_Device* device, float volume)
561 {
562         assert(device);
563
564         try
565         {
566                 return device->setCapability(AUD_CAPS_VOLUME, &volume);
567         }
568         catch(AUD_Exception) {}
569         
570         return false;
571 }
572
573 int AUD_setDeviceSoundVolume(AUD_Device* device, AUD_Handle* handle,
574                                                          float volume)
575 {
576         if(handle)
577         {
578                 assert(device);
579                 AUD_SourceCaps caps;
580                 caps.handle = handle;
581                 caps.value = volume;
582
583                 try
584                 {
585                         return device->setCapability(AUD_CAPS_SOURCE_VOLUME, &caps);
586                 }
587                 catch(AUD_Exception) {}
588         }
589         return false;
590 }
591
592 int AUD_readDevice(AUD_Device* device, data_t* buffer, int length)
593 {
594         assert(device);
595         assert(buffer);
596
597         try
598         {
599                 return device->read(buffer, length);
600         }
601         catch(AUD_Exception)
602         {
603                 return false;
604         }
605 }
606
607 void AUD_closeReadDevice(AUD_Device* device)
608 {
609         assert(device);
610
611         try
612         {
613                 delete device;
614         }
615         catch(AUD_Exception)
616         {
617         }
618 }
619
620 float* AUD_readSoundBuffer(const char* filename, float low, float high,
621                                                    float attack, float release, float threshold,
622                                                    int accumulate, int additive, int square,
623                                                    float sthreshold, int samplerate, int* length)
624 {
625         AUD_Buffer buffer;
626         AUD_DeviceSpecs specs;
627         specs.channels = AUD_CHANNELS_MONO;
628         specs.rate = (AUD_SampleRate)samplerate;
629         AUD_Sound* sound;
630
631         AUD_FileFactory file(filename);
632         AUD_ChannelMapperFactory mapper(&file, specs);
633         AUD_LowpassFactory lowpass(&mapper, high);
634         AUD_HighpassFactory highpass(&lowpass, low);
635         AUD_EnvelopeFactory envelope(&highpass, attack, release, threshold, 0.1f);
636         AUD_LinearResampleFactory resampler(&envelope, specs);
637         sound = &resampler;
638         AUD_SquareFactory squaref(sound, sthreshold);
639         if(square)
640                 sound = &squaref;
641         AUD_AccumulatorFactory accumulator(sound, additive);
642         AUD_SumFactory sum(sound);
643         if(accumulate)
644                 sound = &accumulator;
645         else if(additive)
646                 sound = &sum;
647
648         AUD_IReader* reader = sound->createReader();
649
650         if(reader == NULL)
651                 return NULL;
652
653         int len;
654         int position = 0;
655         sample_t* readbuffer;
656         do
657         {
658                 len = samplerate;
659                 buffer.resize((position + len) * sizeof(float), true);
660                 reader->read(len, readbuffer);
661                 memcpy(buffer.getBuffer() + position, readbuffer, len * sizeof(float));
662                 position += len;
663         } while(len != 0);
664         delete reader;
665
666         float* result = (float*)malloc(position * sizeof(float));
667         memcpy(result, buffer.getBuffer(), position * sizeof(float));
668         *length = position;
669         return result;
670 }
671
672 AUD_Sound* AUD_createSequencer(void* data, AUD_volumeFunction volume)
673 {
674 /* AUD_XXX should be this: but AUD_createSequencer is called before the device
675  * is initialized.
676
677         return new AUD_SequencerFactory(AUD_device->getSpecs().specs, data, volume);
678 */
679         AUD_Specs specs;
680         specs.channels = AUD_CHANNELS_STEREO;
681         specs.rate = AUD_RATE_44100;
682         return new AUD_SequencerFactory(specs, data, volume);
683 }
684
685 void AUD_destroySequencer(AUD_Sound* sequencer)
686 {
687         delete ((AUD_SequencerFactory*)sequencer);
688 }
689
690 AUD_SequencerEntry* AUD_addSequencer(AUD_Sound** sequencer, AUD_Sound* sound,
691                                                                  float begin, float end, float skip, void* data)
692 {
693         return ((AUD_SequencerFactory*)sequencer)->add((AUD_IFactory**) sound, begin, end, skip, data);
694 }
695
696 void AUD_removeSequencer(AUD_Sound* sequencer, AUD_SequencerEntry* entry)
697 {
698         ((AUD_SequencerFactory*)sequencer)->remove(entry);
699 }
700
701 void AUD_moveSequencer(AUD_Sound* sequencer, AUD_SequencerEntry* entry,
702                                    float begin, float end, float skip)
703 {
704         ((AUD_SequencerFactory*)sequencer)->move(entry, begin, end, skip);
705 }
706
707 void AUD_muteSequencer(AUD_Sound* sequencer, AUD_SequencerEntry* entry, char mute)
708 {
709         ((AUD_SequencerFactory*)sequencer)->mute(entry, mute);
710 }
711
712 int AUD_readSound(AUD_Sound* sound, sample_t* buffer, int length)
713 {
714         AUD_IReader* reader = sound->createReader();
715         AUD_DeviceSpecs specs;
716         sample_t* buf;
717
718         specs.specs = reader->getSpecs();
719         specs.channels = AUD_CHANNELS_MONO;
720         specs.format = AUD_FORMAT_FLOAT32;
721
722         AUD_ChannelMapperFactory mapper(reader, specs);
723
724         if(!reader || reader->getType() != AUD_TYPE_BUFFER)
725                 return -1;
726
727         reader = mapper.createReader();
728
729         if(!reader)
730                 return -1;
731
732         int len = reader->getLength();
733         float samplejump = (float)len / (float)length;
734         float min, max;
735
736         for(int i = 0; i < length; i++)
737         {
738                 len = floor(samplejump * (i+1)) - floor(samplejump * i);
739                 reader->read(len, buf);
740
741                 if(len < 1)
742                 {
743                         length = i;
744                         break;
745                 }
746
747                 max = min = *buf;
748                 for(int j = 1; j < len; j++)
749                 {
750                         if(buf[j] < min)
751                                 min = buf[j];
752                         if(buf[j] > max)
753                                 max = buf[j];
754                         buffer[i * 2] = min;
755                         buffer[i * 2 + 1] = max;
756                 }
757         }
758
759         delete reader; AUD_DELETE("reader")
760
761         return length;
762 }
763
764 #ifdef AUD_DEBUG_MEMORY
765 int AUD_References(int count, const char* text)
766 {
767         static int m_count = 0;
768         m_count += count;
769         if(count > 0)
770                 printf("+%s\n", text);
771         if(count < 0)
772                 printf("-%s\n", text);
773         return m_count;
774 }
775 #endif