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;
37 #ifndef __STDC_CONSTANT_MACROS
39 #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)
247 AUD_IReader* reader = sound->createReader();
253 info.specs = reader->getSpecs();
254 info.length = reader->getLength() / (float) info.specs.rate;
258 info.specs.channels = AUD_CHANNELS_INVALID;
259 info.specs.rate = AUD_RATE_INVALID;
266 AUD_Sound* AUD_load(const char* filename)
269 return new AUD_FileFactory(filename);
272 AUD_Sound* AUD_loadBuffer(unsigned char* buffer, int size)
275 return new AUD_FileFactory(buffer, size);
278 AUD_Sound* AUD_bufferSound(AUD_Sound* sound)
284 return new AUD_StreamBufferFactory(sound);
286 catch(AUD_Exception&)
292 AUD_Sound* AUD_delaySound(AUD_Sound* sound, float delay)
298 return new AUD_DelayFactory(sound, delay);
300 catch(AUD_Exception&)
306 AUD_Sound* AUD_limitSound(AUD_Sound* sound, float start, float end)
312 return new AUD_LimiterFactory(sound, start, end);
314 catch(AUD_Exception&)
320 AUD_Sound* AUD_pingpongSound(AUD_Sound* sound)
326 return new AUD_PingPongFactory(sound);
328 catch(AUD_Exception&)
334 AUD_Sound* AUD_loopSound(AUD_Sound* sound)
340 return new AUD_LoopFactory(sound);
342 catch(AUD_Exception&)
348 int AUD_setLoop(AUD_Channel* handle, int loops)
354 return AUD_device->setLoopCount(handle, loops);
356 catch(AUD_Exception&)
363 AUD_Sound* AUD_rectifySound(AUD_Sound* sound)
369 return new AUD_RectifyFactory(sound);
371 catch(AUD_Exception&)
377 void AUD_unload(AUD_Sound* sound)
383 AUD_Channel* AUD_play(AUD_Sound* sound, int keep)
389 return AUD_device->play(sound, keep);
391 catch(AUD_Exception&)
397 int AUD_pause(AUD_Channel* handle)
400 return AUD_device->pause(handle);
403 int AUD_resume(AUD_Channel* handle)
406 return AUD_device->resume(handle);
409 int AUD_stop(AUD_Channel* handle)
412 return AUD_device->stop(handle);
416 int AUD_setKeep(AUD_Channel* handle, int keep)
419 return AUD_device->setKeep(handle, keep);
422 int AUD_seek(AUD_Channel* handle, float seekTo)
425 return AUD_device->seek(handle, seekTo);
428 float AUD_getPosition(AUD_Channel* handle)
431 return AUD_device->getPosition(handle);
434 AUD_Status AUD_getStatus(AUD_Channel* handle)
437 return AUD_device->getStatus(handle);
440 int AUD_setListenerLocation(const float* location)
446 AUD_Vector3 v(location[0], location[1], location[2]);
447 AUD_3ddevice->setListenerLocation(v);
454 int AUD_setListenerVelocity(const float* velocity)
460 AUD_Vector3 v(velocity[0], velocity[1], velocity[2]);
461 AUD_3ddevice->setListenerVelocity(v);
468 int AUD_setListenerOrientation(const float* orientation)
474 AUD_Quaternion q(orientation[0], orientation[1], orientation[2], orientation[3]);
475 AUD_3ddevice->setListenerOrientation(q);
482 int AUD_setSpeedOfSound(float speed)
488 AUD_3ddevice->setSpeedOfSound(speed);
495 int AUD_setDopplerFactor(float factor)
501 AUD_3ddevice->setDopplerFactor(factor);
508 int AUD_setDistanceModel(AUD_DistanceModel model)
514 AUD_3ddevice->setDistanceModel(model);
521 int AUD_setSourceLocation(AUD_Channel* handle, const float* location)
528 AUD_Vector3 v(location[0], location[1], location[2]);
529 return AUD_3ddevice->setSourceLocation(handle, v);
535 int AUD_setSourceVelocity(AUD_Channel* handle, const float* velocity)
542 AUD_Vector3 v(velocity[0], velocity[1], velocity[2]);
543 return AUD_3ddevice->setSourceVelocity(handle, v);
549 int AUD_setSourceOrientation(AUD_Channel* handle, const float* orientation)
556 AUD_Quaternion q(orientation[0], orientation[1], orientation[2], orientation[3]);
557 return AUD_3ddevice->setSourceOrientation(handle, q);
563 int AUD_setRelative(AUD_Channel* handle, int relative)
570 return AUD_3ddevice->setRelative(handle, relative);
576 int AUD_setVolumeMaximum(AUD_Channel* handle, float volume)
583 return AUD_3ddevice->setVolumeMaximum(handle, volume);
589 int AUD_setVolumeMinimum(AUD_Channel* handle, float volume)
596 return AUD_3ddevice->setVolumeMinimum(handle, volume);
602 int AUD_setDistanceMaximum(AUD_Channel* handle, float distance)
609 return AUD_3ddevice->setDistanceMaximum(handle, distance);
615 int AUD_setDistanceReference(AUD_Channel* handle, float distance)
622 return AUD_3ddevice->setDistanceReference(handle, distance);
628 int AUD_setAttenuation(AUD_Channel* handle, float factor)
635 return AUD_3ddevice->setAttenuation(handle, factor);
641 int AUD_setConeAngleOuter(AUD_Channel* handle, float angle)
648 return AUD_3ddevice->setConeAngleOuter(handle, angle);
654 int AUD_setConeAngleInner(AUD_Channel* handle, float angle)
661 return AUD_3ddevice->setConeAngleInner(handle, angle);
667 int AUD_setConeVolumeOuter(AUD_Channel* handle, float volume)
674 return AUD_3ddevice->setConeVolumeOuter(handle, volume);
680 int AUD_setSoundVolume(AUD_Channel* handle, float volume)
688 return AUD_device->setVolume(handle, volume);
690 catch(AUD_Exception&) {}
695 int AUD_setSoundPitch(AUD_Channel* handle, float pitch)
703 return AUD_device->setPitch(handle, pitch);
705 catch(AUD_Exception&) {}
710 AUD_Device* AUD_openReadDevice(AUD_DeviceSpecs specs)
714 return new AUD_ReadDevice(specs);
716 catch(AUD_Exception&)
722 AUD_Channel* AUD_playDevice(AUD_Device* device, AUD_Sound* sound, float seek)
729 AUD_Channel* handle = device->play(sound);
730 device->seek(handle, seek);
733 catch(AUD_Exception&)
739 int AUD_setDeviceVolume(AUD_Device* device, float volume)
745 device->setVolume(volume);
748 catch(AUD_Exception&) {}
753 int AUD_setDeviceSoundVolume(AUD_Device* device, AUD_Channel* handle,
762 return device->setVolume(handle, volume);
764 catch(AUD_Exception&) {}
769 int AUD_readDevice(AUD_Device* device, data_t* buffer, int length)
776 return device->read(buffer, length);
778 catch(AUD_Exception&)
784 void AUD_closeReadDevice(AUD_Device* device)
792 catch(AUD_Exception&)
797 float* AUD_readSoundBuffer(const char* filename, float low, float high,
798 float attack, float release, float threshold,
799 int accumulate, int additive, int square,
800 float sthreshold, int samplerate, int* length)
803 AUD_DeviceSpecs specs;
804 specs.channels = AUD_CHANNELS_MONO;
805 specs.rate = (AUD_SampleRate)samplerate;
808 AUD_FileFactory file(filename);
809 AUD_ChannelMapperFactory mapper(&file, specs);
810 AUD_LowpassFactory lowpass(&mapper, high);
811 AUD_HighpassFactory highpass(&lowpass, low);
812 AUD_EnvelopeFactory envelope(&highpass, attack, release, threshold, 0.1f);
813 AUD_LinearResampleFactory resampler(&envelope, specs);
815 AUD_SquareFactory squaref(sound, sthreshold);
818 AUD_AccumulatorFactory accumulator(sound, additive);
819 AUD_SumFactory sum(sound);
821 sound = &accumulator;
825 AUD_IReader* reader = sound->createReader();
832 sample_t* readbuffer;
836 buffer.resize((position + len) * sizeof(float), true);
837 reader->read(len, readbuffer);
838 memcpy(buffer.getBuffer() + position, readbuffer, len * sizeof(float));
843 float* result = (float*)malloc(position * sizeof(float));
844 memcpy(result, buffer.getBuffer(), position * sizeof(float));
849 static void pauseSound(AUD_Channel* handle)
853 AUD_device->pause(handle);
856 AUD_Channel* AUD_pauseAfter(AUD_Channel* handle, float seconds)
860 AUD_SilenceFactory silence;
861 AUD_LimiterFactory limiter(&silence, 0, seconds);
865 AUD_Channel* channel = AUD_device->play(&limiter);
866 AUD_device->setStopCallback(channel, (stopCallback)pauseSound, handle);
869 catch(AUD_Exception&)
875 AUD_Sound* AUD_createSequencer(void* data, AUD_volumeFunction volume)
877 /* AUD_XXX should be this: but AUD_createSequencer is called before the device
880 return new AUD_SequencerFactory(AUD_device->getSpecs().specs, data, volume);
883 specs.channels = AUD_CHANNELS_STEREO;
884 specs.rate = AUD_RATE_44100;
885 return new AUD_SequencerFactory(specs, data, volume);
888 void AUD_destroySequencer(AUD_Sound* sequencer)
890 delete ((AUD_SequencerFactory*)sequencer);
893 AUD_SequencerEntry* AUD_addSequencer(AUD_Sound** sequencer, AUD_Sound* sound,
894 float begin, float end, float skip, void* data)
896 return ((AUD_SequencerFactory*)sequencer)->add((AUD_IFactory**) sound, begin, end, skip, data);
899 void AUD_removeSequencer(AUD_Sound* sequencer, AUD_SequencerEntry* entry)
901 ((AUD_SequencerFactory*)sequencer)->remove(entry);
904 void AUD_moveSequencer(AUD_Sound* sequencer, AUD_SequencerEntry* entry,
905 float begin, float end, float skip)
907 ((AUD_SequencerFactory*)sequencer)->move(entry, begin, end, skip);
910 void AUD_muteSequencer(AUD_Sound* sequencer, AUD_SequencerEntry* entry, char mute)
912 ((AUD_SequencerFactory*)sequencer)->mute(entry, mute);
915 int AUD_readSound(AUD_Sound* sound, sample_t* buffer, int length)
917 AUD_DeviceSpecs specs;
920 specs.rate = AUD_RATE_INVALID;
921 specs.channels = AUD_CHANNELS_MONO;
922 specs.format = AUD_FORMAT_INVALID;
924 AUD_ChannelMapperFactory mapper(sound, specs);
926 AUD_IReader* reader = mapper.createReader();
928 int len = reader->getLength();
929 float samplejump = (float)len / (float)length;
932 for(int i = 0; i < length; i++)
934 len = floor(samplejump * (i+1)) - floor(samplejump * i);
935 reader->read(len, buf);
944 for(int j = 1; j < len; j++)
951 buffer[i * 2 + 1] = max;
960 void AUD_startPlayback()
963 AUD_JackDevice* device = dynamic_cast<AUD_JackDevice*>(AUD_device);
965 device->startPlayback();
969 void AUD_stopPlayback()
972 AUD_JackDevice* device = dynamic_cast<AUD_JackDevice*>(AUD_device);
974 device->stopPlayback();
978 void AUD_seekSequencer(AUD_Channel* handle, float time)
981 AUD_JackDevice* device = dynamic_cast<AUD_JackDevice*>(AUD_device);
983 device->seekPlayback(time);
987 AUD_device->seek(handle, time);
991 float AUD_getSequencerPosition(AUD_Channel* handle)
994 AUD_JackDevice* device = dynamic_cast<AUD_JackDevice*>(AUD_device);
996 return device->getPlaybackPosition();
1000 return AUD_device->getPosition(handle);
1005 void AUD_setSyncCallback(AUD_syncFunction function, void* data)
1007 AUD_JackDevice* device = dynamic_cast<AUD_JackDevice*>(AUD_device);
1009 device->setSyncCallback(function, data);
1013 int AUD_doesPlayback()
1016 AUD_JackDevice* device = dynamic_cast<AUD_JackDevice*>(AUD_device);
1018 return device->doesPlayback();