4 * ***** BEGIN LGPL LICENSE BLOCK *****
6 * Copyright 2009 Jörg Hermann Müller
8 * This file is part of AudaSpace.
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.
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.
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/>.
23 * ***** END LGPL LICENSE BLOCK *****
27 #include "AUD_PyAPI.h"
30 bool g_pyinitialized = false;
34 #ifndef __STDC_CONSTANT_MACROS
35 #define __STDC_CONSTANT_MACROS
42 #include "AUD_NULLDevice.h"
43 #include "AUD_I3DDevice.h"
44 #include "AUD_FileFactory.h"
45 #include "AUD_StreamBufferFactory.h"
46 #include "AUD_DelayFactory.h"
47 #include "AUD_LimiterFactory.h"
48 #include "AUD_PingPongFactory.h"
49 #include "AUD_LoopFactory.h"
50 #include "AUD_RectifyFactory.h"
51 #include "AUD_EnvelopeFactory.h"
52 #include "AUD_LinearResampleFactory.h"
53 #include "AUD_LowpassFactory.h"
54 #include "AUD_HighpassFactory.h"
55 #include "AUD_AccumulatorFactory.h"
56 #include "AUD_SumFactory.h"
57 #include "AUD_SquareFactory.h"
58 #include "AUD_ChannelMapperFactory.h"
59 #include "AUD_Buffer.h"
60 #include "AUD_ReadDevice.h"
61 #include "AUD_IReader.h"
62 #include "AUD_SequencerFactory.h"
63 #include "AUD_SilenceFactory.h"
66 #include "AUD_SDLDevice.h"
70 #include "AUD_OpenALDevice.h"
74 #include "AUD_JackDevice.h"
80 #include <libavformat/avformat.h>
86 typedef AUD_IFactory AUD_Sound;
87 typedef AUD_ReadDevice AUD_Device;
88 typedef AUD_Handle AUD_Channel;
90 #define AUD_CAPI_IMPLEMENTATION
91 #include "AUD_C-API.h"
97 static AUD_IDevice* AUD_device = NULL;
98 static int AUD_available_devices[4];
99 static AUD_I3DDevice* AUD_3ddevice = NULL;
108 int AUD_init(AUD_DeviceType device, AUD_DeviceSpecs specs, int buffersize)
110 AUD_IDevice* dev = NULL;
119 case AUD_NULL_DEVICE:
120 dev = new AUD_NULLDevice();
124 dev = new AUD_SDLDevice(specs, buffersize);
128 case AUD_OPENAL_DEVICE:
129 dev = new AUD_OpenALDevice(specs, buffersize);
133 case AUD_JACK_DEVICE:
134 dev = new AUD_JackDevice("Blender", specs, buffersize);
142 AUD_3ddevice = dynamic_cast<AUD_I3DDevice*>(AUD_device);
147 g_device = (Device*)Device_empty();
150 g_device->device = dev;
157 catch(AUD_Exception&)
163 int* AUD_enumDevices()
167 AUD_available_devices[i++] = AUD_SDL_DEVICE;
170 AUD_available_devices[i++] = AUD_OPENAL_DEVICE;
173 AUD_available_devices[i++] = AUD_JACK_DEVICE;
175 AUD_available_devices[i++] = AUD_NULL_DEVICE;
176 return AUD_available_devices;
184 Py_XDECREF(g_device);
196 static PyObject* AUD_getCDevice(PyObject* self)
201 return (PyObject*)g_device;
206 static PyMethodDef meth_getcdevice[] = {{ "device", (PyCFunction)AUD_getCDevice, METH_NOARGS,
208 "Returns the application's :class:`Device`.\n\n"
209 ":return: The application's :class:`Device`.\n"
210 ":rtype: :class:`Device`"}};
212 PyObject* AUD_initPython()
214 PyObject* module = PyInit_aud();
215 PyModule_AddObject(module, "device", (PyObject *)PyCFunction_New(meth_getcdevice, NULL));
216 PyDict_SetItemString(PySys_GetObject("modules"), "aud", module);
219 g_device = (Device*)Device_empty();
222 g_device->device = AUD_device;
225 g_pyinitialized = true;
240 AUD_device->unlock();
243 AUD_SoundInfo AUD_getInfo(AUD_Sound* sound)
248 info.specs.channels = AUD_CHANNELS_INVALID;
249 info.specs.rate = AUD_RATE_INVALID;
254 AUD_IReader* reader = sound->createReader();
258 info.specs = reader->getSpecs();
259 info.length = reader->getLength() / (float) info.specs.rate;
262 catch(AUD_Exception&)
269 AUD_Sound* AUD_load(const char* filename)
272 return new AUD_FileFactory(filename);
275 AUD_Sound* AUD_loadBuffer(unsigned char* buffer, int size)
278 return new AUD_FileFactory(buffer, size);
281 AUD_Sound* AUD_bufferSound(AUD_Sound* sound)
287 return new AUD_StreamBufferFactory(sound);
289 catch(AUD_Exception&)
295 AUD_Sound* AUD_delaySound(AUD_Sound* sound, float delay)
301 return new AUD_DelayFactory(sound, delay);
303 catch(AUD_Exception&)
309 AUD_Sound* AUD_limitSound(AUD_Sound* sound, float start, float end)
315 return new AUD_LimiterFactory(sound, start, end);
317 catch(AUD_Exception&)
323 AUD_Sound* AUD_pingpongSound(AUD_Sound* sound)
329 return new AUD_PingPongFactory(sound);
331 catch(AUD_Exception&)
337 AUD_Sound* AUD_loopSound(AUD_Sound* sound)
343 return new AUD_LoopFactory(sound);
345 catch(AUD_Exception&)
351 int AUD_setLoop(AUD_Channel* handle, int loops)
357 return AUD_device->setLoopCount(handle, loops);
359 catch(AUD_Exception&)
366 AUD_Sound* AUD_rectifySound(AUD_Sound* sound)
372 return new AUD_RectifyFactory(sound);
374 catch(AUD_Exception&)
380 void AUD_unload(AUD_Sound* sound)
386 AUD_Channel* AUD_play(AUD_Sound* sound, int keep)
392 return AUD_device->play(sound, keep);
394 catch(AUD_Exception&)
400 int AUD_pause(AUD_Channel* handle)
403 return AUD_device->pause(handle);
406 int AUD_resume(AUD_Channel* handle)
409 return AUD_device->resume(handle);
412 int AUD_stop(AUD_Channel* handle)
415 return AUD_device->stop(handle);
419 int AUD_setKeep(AUD_Channel* handle, int keep)
422 return AUD_device->setKeep(handle, keep);
425 int AUD_seek(AUD_Channel* handle, float seekTo)
428 return AUD_device->seek(handle, seekTo);
431 float AUD_getPosition(AUD_Channel* handle)
434 return AUD_device->getPosition(handle);
437 AUD_Status AUD_getStatus(AUD_Channel* handle)
440 return AUD_device->getStatus(handle);
443 int AUD_setListenerLocation(const float* location)
449 AUD_Vector3 v(location[0], location[1], location[2]);
450 AUD_3ddevice->setListenerLocation(v);
457 int AUD_setListenerVelocity(const float* velocity)
463 AUD_Vector3 v(velocity[0], velocity[1], velocity[2]);
464 AUD_3ddevice->setListenerVelocity(v);
471 int AUD_setListenerOrientation(const float* orientation)
477 AUD_Quaternion q(orientation[3], orientation[0], orientation[1], orientation[2]);
478 AUD_3ddevice->setListenerOrientation(q);
485 int AUD_setSpeedOfSound(float speed)
491 AUD_3ddevice->setSpeedOfSound(speed);
498 int AUD_setDopplerFactor(float factor)
504 AUD_3ddevice->setDopplerFactor(factor);
511 int AUD_setDistanceModel(AUD_DistanceModel model)
517 AUD_3ddevice->setDistanceModel(model);
524 int AUD_setSourceLocation(AUD_Channel* handle, const float* location)
531 AUD_Vector3 v(location[0], location[1], location[2]);
532 return AUD_3ddevice->setSourceLocation(handle, v);
538 int AUD_setSourceVelocity(AUD_Channel* handle, const float* velocity)
545 AUD_Vector3 v(velocity[0], velocity[1], velocity[2]);
546 return AUD_3ddevice->setSourceVelocity(handle, v);
552 int AUD_setSourceOrientation(AUD_Channel* handle, const float* orientation)
559 AUD_Quaternion q(orientation[3], orientation[0], orientation[1], orientation[2]);
560 return AUD_3ddevice->setSourceOrientation(handle, q);
566 int AUD_setRelative(AUD_Channel* handle, int relative)
573 return AUD_3ddevice->setRelative(handle, relative);
579 int AUD_setVolumeMaximum(AUD_Channel* handle, float volume)
586 return AUD_3ddevice->setVolumeMaximum(handle, volume);
592 int AUD_setVolumeMinimum(AUD_Channel* handle, float volume)
599 return AUD_3ddevice->setVolumeMinimum(handle, volume);
605 int AUD_setDistanceMaximum(AUD_Channel* handle, float distance)
612 return AUD_3ddevice->setDistanceMaximum(handle, distance);
618 int AUD_setDistanceReference(AUD_Channel* handle, float distance)
625 return AUD_3ddevice->setDistanceReference(handle, distance);
631 int AUD_setAttenuation(AUD_Channel* handle, float factor)
638 return AUD_3ddevice->setAttenuation(handle, factor);
644 int AUD_setConeAngleOuter(AUD_Channel* handle, float angle)
651 return AUD_3ddevice->setConeAngleOuter(handle, angle);
657 int AUD_setConeAngleInner(AUD_Channel* handle, float angle)
664 return AUD_3ddevice->setConeAngleInner(handle, angle);
670 int AUD_setConeVolumeOuter(AUD_Channel* handle, float volume)
677 return AUD_3ddevice->setConeVolumeOuter(handle, volume);
683 int AUD_setSoundVolume(AUD_Channel* handle, float volume)
691 return AUD_device->setVolume(handle, volume);
693 catch(AUD_Exception&) {}
698 int AUD_setSoundPitch(AUD_Channel* handle, float pitch)
706 return AUD_device->setPitch(handle, pitch);
708 catch(AUD_Exception&) {}
713 AUD_Device* AUD_openReadDevice(AUD_DeviceSpecs specs)
717 return new AUD_ReadDevice(specs);
719 catch(AUD_Exception&)
725 AUD_Channel* AUD_playDevice(AUD_Device* device, AUD_Sound* sound, float seek)
732 AUD_Channel* handle = device->play(sound);
733 device->seek(handle, seek);
736 catch(AUD_Exception&)
742 int AUD_setDeviceVolume(AUD_Device* device, float volume)
748 device->setVolume(volume);
751 catch(AUD_Exception&) {}
756 int AUD_setDeviceSoundVolume(AUD_Device* device, AUD_Channel* handle,
765 return device->setVolume(handle, volume);
767 catch(AUD_Exception&) {}
772 int AUD_readDevice(AUD_Device* device, data_t* buffer, int length)
779 return device->read(buffer, length);
781 catch(AUD_Exception&)
787 void AUD_closeReadDevice(AUD_Device* device)
795 catch(AUD_Exception&)
800 float* AUD_readSoundBuffer(const char* filename, float low, float high,
801 float attack, float release, float threshold,
802 int accumulate, int additive, int square,
803 float sthreshold, int samplerate, int* length)
806 AUD_DeviceSpecs specs;
807 specs.channels = AUD_CHANNELS_MONO;
808 specs.rate = (AUD_SampleRate)samplerate;
811 AUD_FileFactory file(filename);
812 AUD_ChannelMapperFactory mapper(&file, specs);
813 AUD_LowpassFactory lowpass(&mapper, high);
814 AUD_HighpassFactory highpass(&lowpass, low);
815 AUD_EnvelopeFactory envelope(&highpass, attack, release, threshold, 0.1f);
816 AUD_LinearResampleFactory resampler(&envelope, specs);
818 AUD_SquareFactory squaref(sound, sthreshold);
821 AUD_AccumulatorFactory accumulator(sound, additive);
822 AUD_SumFactory sum(sound);
824 sound = &accumulator;
828 AUD_IReader* reader = sound->createReader();
835 sample_t* readbuffer;
839 buffer.resize((position + len) * sizeof(float), true);
840 reader->read(len, readbuffer);
841 memcpy(buffer.getBuffer() + position, readbuffer, len * sizeof(float));
846 float* result = (float*)malloc(position * sizeof(float));
847 memcpy(result, buffer.getBuffer(), position * sizeof(float));
852 static void pauseSound(AUD_Channel* handle)
856 AUD_device->pause(handle);
859 AUD_Channel* AUD_pauseAfter(AUD_Channel* handle, float seconds)
863 AUD_SilenceFactory silence;
864 AUD_LimiterFactory limiter(&silence, 0, seconds);
868 AUD_Channel* channel = AUD_device->play(&limiter);
869 AUD_device->setStopCallback(channel, (stopCallback)pauseSound, handle);
872 catch(AUD_Exception&)
878 AUD_Sound* AUD_createSequencer(void* data, AUD_volumeFunction volume)
880 /* AUD_XXX should be this: but AUD_createSequencer is called before the device
883 return new AUD_SequencerFactory(AUD_device->getSpecs().specs, data, volume);
886 specs.channels = AUD_CHANNELS_STEREO;
887 specs.rate = AUD_RATE_44100;
888 return new AUD_SequencerFactory(specs, data, volume);
891 void AUD_destroySequencer(AUD_Sound* sequencer)
893 delete ((AUD_SequencerFactory*)sequencer);
896 AUD_SequencerEntry* AUD_addSequencer(AUD_Sound** sequencer, AUD_Sound* sound,
897 float begin, float end, float skip, void* data)
899 return ((AUD_SequencerFactory*)sequencer)->add((AUD_IFactory**) sound, begin, end, skip, data);
902 void AUD_removeSequencer(AUD_Sound* sequencer, AUD_SequencerEntry* entry)
904 ((AUD_SequencerFactory*)sequencer)->remove(entry);
907 void AUD_moveSequencer(AUD_Sound* sequencer, AUD_SequencerEntry* entry,
908 float begin, float end, float skip)
910 ((AUD_SequencerFactory*)sequencer)->move(entry, begin, end, skip);
913 void AUD_muteSequencer(AUD_Sound* sequencer, AUD_SequencerEntry* entry, char mute)
915 ((AUD_SequencerFactory*)sequencer)->mute(entry, mute);
918 int AUD_readSound(AUD_Sound* sound, sample_t* buffer, int length)
920 AUD_DeviceSpecs specs;
923 specs.rate = AUD_RATE_INVALID;
924 specs.channels = AUD_CHANNELS_MONO;
925 specs.format = AUD_FORMAT_INVALID;
927 AUD_ChannelMapperFactory mapper(sound, specs);
929 AUD_IReader* reader = mapper.createReader();
931 int len = reader->getLength();
932 float samplejump = (float)len / (float)length;
935 for(int i = 0; i < length; i++)
937 len = floor(samplejump * (i+1)) - floor(samplejump * i);
938 reader->read(len, buf);
947 for(int j = 1; j < len; j++)
954 buffer[i * 2 + 1] = max;
963 void AUD_startPlayback()
966 AUD_JackDevice* device = dynamic_cast<AUD_JackDevice*>(AUD_device);
968 device->startPlayback();
972 void AUD_stopPlayback()
975 AUD_JackDevice* device = dynamic_cast<AUD_JackDevice*>(AUD_device);
977 device->stopPlayback();
981 void AUD_seekSequencer(AUD_Channel* handle, float time)
984 AUD_JackDevice* device = dynamic_cast<AUD_JackDevice*>(AUD_device);
986 device->seekPlayback(time);
990 AUD_device->seek(handle, time);
994 float AUD_getSequencerPosition(AUD_Channel* handle)
997 AUD_JackDevice* device = dynamic_cast<AUD_JackDevice*>(AUD_device);
999 return device->getPlaybackPosition();
1003 return AUD_device->getPosition(handle);
1008 void AUD_setSyncCallback(AUD_syncFunction function, void* data)
1010 AUD_JackDevice* device = dynamic_cast<AUD_JackDevice*>(AUD_device);
1012 device->setSyncCallback(function, data);
1016 int AUD_doesPlayback()
1019 AUD_JackDevice* device = dynamic_cast<AUD_JackDevice*>(AUD_device);
1021 return device->doesPlayback();