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"
65 #include "AUD_SDLDevice.h"
69 #include "AUD_OpenALDevice.h"
73 #include "AUD_JackDevice.h"
79 #include <libavformat/avformat.h>
85 typedef AUD_IFactory AUD_Sound;
86 typedef AUD_ReadDevice AUD_Device;
87 typedef AUD_Handle AUD_Channel;
89 #define AUD_CAPI_IMPLEMENTATION
90 #include "AUD_C-API.h"
96 static AUD_IDevice* AUD_device = NULL;
97 static int AUD_available_devices[4];
98 static AUD_I3DDevice* AUD_3ddevice = NULL;
107 int AUD_init(AUD_DeviceType device, AUD_DeviceSpecs specs, int buffersize)
109 AUD_IDevice* dev = NULL;
118 case AUD_NULL_DEVICE:
119 dev = new AUD_NULLDevice();
123 dev = new AUD_SDLDevice(specs, buffersize);
127 case AUD_OPENAL_DEVICE:
128 dev = new AUD_OpenALDevice(specs, buffersize);
132 case AUD_JACK_DEVICE:
133 dev = new AUD_JackDevice(specs, buffersize);
141 AUD_3ddevice = dynamic_cast<AUD_I3DDevice*>(AUD_device);
146 g_device = (Device*)Device_empty();
149 g_device->device = dev;
156 catch(AUD_Exception&)
162 int* AUD_enumDevices()
166 AUD_available_devices[i++] = AUD_SDL_DEVICE;
169 AUD_available_devices[i++] = AUD_OPENAL_DEVICE;
172 AUD_available_devices[i++] = AUD_JACK_DEVICE;
174 AUD_available_devices[i++] = AUD_NULL_DEVICE;
175 return AUD_available_devices;
183 Py_XDECREF(g_device);
195 static PyObject* AUD_getCDevice(PyObject* self)
200 return (PyObject*)g_device;
205 static PyMethodDef meth_getcdevice[] = {{ "device", (PyCFunction)AUD_getCDevice, METH_NOARGS,
207 "Returns the application's Device.\n\n"
208 ":return: The application's Device.\n"
209 ":rtype: aud.Device"}};
211 PyObject* AUD_initPython()
213 PyObject* module = PyInit_aud();
214 PyModule_AddObject(module, "device", (PyObject *)PyCFunction_New(meth_getcdevice, NULL));
215 PyDict_SetItemString(PySys_GetObject("modules"), "aud", module);
218 g_device = (Device*)Device_empty();
221 g_device->device = AUD_device;
224 g_pyinitialized = true;
239 AUD_device->unlock();
242 AUD_SoundInfo AUD_getInfo(AUD_Sound* sound)
246 AUD_IReader* reader = sound->createReader();
252 info.specs = reader->getSpecs();
253 info.length = reader->getLength() / (float) info.specs.rate;
257 info.specs.channels = AUD_CHANNELS_INVALID;
258 info.specs.rate = AUD_RATE_INVALID;
265 AUD_Sound* AUD_load(const char* filename)
268 return new AUD_FileFactory(filename);
271 AUD_Sound* AUD_loadBuffer(unsigned char* buffer, int size)
274 return new AUD_FileFactory(buffer, size);
277 AUD_Sound* AUD_bufferSound(AUD_Sound* sound)
283 return new AUD_StreamBufferFactory(sound);
285 catch(AUD_Exception&)
291 AUD_Sound* AUD_delaySound(AUD_Sound* sound, float delay)
297 return new AUD_DelayFactory(sound, delay);
299 catch(AUD_Exception&)
305 AUD_Sound* AUD_limitSound(AUD_Sound* sound, float start, float end)
311 return new AUD_LimiterFactory(sound, start, end);
313 catch(AUD_Exception&)
319 AUD_Sound* AUD_pingpongSound(AUD_Sound* sound)
325 return new AUD_PingPongFactory(sound);
327 catch(AUD_Exception&)
333 AUD_Sound* AUD_loopSound(AUD_Sound* sound)
339 return new AUD_LoopFactory(sound);
341 catch(AUD_Exception&)
347 int AUD_setLoop(AUD_Channel* handle, int loops, float time)
351 /* AUD_XXX Doesn't work atm, will come back
354 message.type = AUD_MSG_LOOP;
355 message.loopcount = loops;
360 return AUD_device->sendMessage(handle, message);
362 catch(AUD_Exception&)
369 AUD_Sound* AUD_rectifySound(AUD_Sound* sound)
375 return new AUD_RectifyFactory(sound);
377 catch(AUD_Exception&)
383 void AUD_unload(AUD_Sound* sound)
389 AUD_Channel* AUD_play(AUD_Sound* sound, int keep)
395 return AUD_device->play(sound, keep);
397 catch(AUD_Exception&)
403 int AUD_pause(AUD_Channel* handle)
406 return AUD_device->pause(handle);
409 int AUD_resume(AUD_Channel* handle)
412 return AUD_device->resume(handle);
415 int AUD_stop(AUD_Channel* handle)
418 return AUD_device->stop(handle);
422 int AUD_setKeep(AUD_Channel* handle, int keep)
425 return AUD_device->setKeep(handle, keep);
428 int AUD_seek(AUD_Channel* handle, float seekTo)
431 return AUD_device->seek(handle, seekTo);
434 float AUD_getPosition(AUD_Channel* handle)
437 return AUD_device->getPosition(handle);
440 AUD_Status AUD_getStatus(AUD_Channel* handle)
443 return AUD_device->getStatus(handle);
446 int AUD_setListenerLocation(const float* location)
452 AUD_Vector3 v(location[0], location[1], location[2]);
453 AUD_3ddevice->setListenerLocation(v);
460 int AUD_setListenerVelocity(const float* velocity)
466 AUD_Vector3 v(velocity[0], velocity[1], velocity[2]);
467 AUD_3ddevice->setListenerVelocity(v);
474 int AUD_setListenerOrientation(const float* orientation)
480 AUD_Quaternion q(orientation[0], orientation[1], orientation[2], orientation[3]);
481 AUD_3ddevice->setListenerOrientation(q);
488 int AUD_setSpeedOfSound(float speed)
494 AUD_3ddevice->setSpeedOfSound(speed);
501 int AUD_setDopplerFactor(float factor)
507 AUD_3ddevice->setDopplerFactor(factor);
514 int AUD_setDistanceModel(AUD_DistanceModel model)
520 AUD_3ddevice->setDistanceModel(model);
527 int AUD_setSourceLocation(AUD_Channel* handle, const float* location)
534 AUD_Vector3 v(location[0], location[1], location[2]);
535 return AUD_3ddevice->setSourceLocation(handle, v);
541 int AUD_setSourceVelocity(AUD_Channel* handle, const float* velocity)
548 AUD_Vector3 v(velocity[0], velocity[1], velocity[2]);
549 return AUD_3ddevice->setSourceVelocity(handle, v);
555 int AUD_setSourceOrientation(AUD_Channel* handle, const float* orientation)
562 AUD_Quaternion q(orientation[0], orientation[1], orientation[2], orientation[3]);
563 return AUD_3ddevice->setSourceOrientation(handle, q);
569 int AUD_setRelative(AUD_Channel* handle, int relative)
576 return AUD_3ddevice->setRelative(handle, relative);
582 int AUD_setVolumeMaximum(AUD_Channel* handle, float volume)
589 return AUD_3ddevice->setVolumeMaximum(handle, volume);
595 int AUD_setVolumeMinimum(AUD_Channel* handle, float volume)
602 return AUD_3ddevice->setVolumeMinimum(handle, volume);
608 int AUD_setDistanceMaximum(AUD_Channel* handle, float distance)
615 return AUD_3ddevice->setDistanceMaximum(handle, distance);
621 int AUD_setDistanceReference(AUD_Channel* handle, float distance)
628 return AUD_3ddevice->setDistanceReference(handle, distance);
634 int AUD_setAttenuation(AUD_Channel* handle, float factor)
641 return AUD_3ddevice->setAttenuation(handle, factor);
647 int AUD_setConeAngleOuter(AUD_Channel* handle, float angle)
654 return AUD_3ddevice->setConeAngleOuter(handle, angle);
660 int AUD_setConeAngleInner(AUD_Channel* handle, float angle)
667 return AUD_3ddevice->setConeAngleInner(handle, angle);
673 int AUD_setConeVolumeOuter(AUD_Channel* handle, float volume)
680 return AUD_3ddevice->setConeVolumeOuter(handle, volume);
686 int AUD_setSoundVolume(AUD_Channel* handle, float volume)
694 return AUD_device->setVolume(handle, volume);
696 catch(AUD_Exception&) {}
701 int AUD_setSoundPitch(AUD_Channel* handle, float pitch)
709 return AUD_device->setPitch(handle, pitch);
711 catch(AUD_Exception&) {}
716 AUD_Device* AUD_openReadDevice(AUD_DeviceSpecs specs)
720 return new AUD_ReadDevice(specs);
722 catch(AUD_Exception&)
728 AUD_Channel* AUD_playDevice(AUD_Device* device, AUD_Sound* sound, float seek)
735 AUD_Channel* handle = device->play(sound);
736 device->seek(handle, seek);
739 catch(AUD_Exception&)
745 int AUD_setDeviceVolume(AUD_Device* device, float volume)
751 device->setVolume(volume);
754 catch(AUD_Exception&) {}
759 int AUD_setDeviceSoundVolume(AUD_Device* device, AUD_Channel* handle,
768 return device->setVolume(handle, volume);
770 catch(AUD_Exception&) {}
775 int AUD_readDevice(AUD_Device* device, data_t* buffer, int length)
782 return device->read(buffer, length);
784 catch(AUD_Exception&)
790 void AUD_closeReadDevice(AUD_Device* device)
798 catch(AUD_Exception&)
803 float* AUD_readSoundBuffer(const char* filename, float low, float high,
804 float attack, float release, float threshold,
805 int accumulate, int additive, int square,
806 float sthreshold, int samplerate, int* length)
809 AUD_DeviceSpecs specs;
810 specs.channels = AUD_CHANNELS_MONO;
811 specs.rate = (AUD_SampleRate)samplerate;
814 AUD_FileFactory file(filename);
815 AUD_ChannelMapperFactory mapper(&file, specs);
816 AUD_LowpassFactory lowpass(&mapper, high);
817 AUD_HighpassFactory highpass(&lowpass, low);
818 AUD_EnvelopeFactory envelope(&highpass, attack, release, threshold, 0.1f);
819 AUD_LinearResampleFactory resampler(&envelope, specs);
821 AUD_SquareFactory squaref(sound, sthreshold);
824 AUD_AccumulatorFactory accumulator(sound, additive);
825 AUD_SumFactory sum(sound);
827 sound = &accumulator;
831 AUD_IReader* reader = sound->createReader();
838 sample_t* readbuffer;
842 buffer.resize((position + len) * sizeof(float), true);
843 reader->read(len, readbuffer);
844 memcpy(buffer.getBuffer() + position, readbuffer, len * sizeof(float));
849 float* result = (float*)malloc(position * sizeof(float));
850 memcpy(result, buffer.getBuffer(), position * sizeof(float));
855 AUD_Sound* AUD_createSequencer(void* data, AUD_volumeFunction volume)
857 /* AUD_XXX should be this: but AUD_createSequencer is called before the device
860 return new AUD_SequencerFactory(AUD_device->getSpecs().specs, data, volume);
863 specs.channels = AUD_CHANNELS_STEREO;
864 specs.rate = AUD_RATE_44100;
865 return new AUD_SequencerFactory(specs, data, volume);
868 void AUD_destroySequencer(AUD_Sound* sequencer)
870 delete ((AUD_SequencerFactory*)sequencer);
873 AUD_SequencerEntry* AUD_addSequencer(AUD_Sound** sequencer, AUD_Sound* sound,
874 float begin, float end, float skip, void* data)
876 return ((AUD_SequencerFactory*)sequencer)->add((AUD_IFactory**) sound, begin, end, skip, data);
879 void AUD_removeSequencer(AUD_Sound* sequencer, AUD_SequencerEntry* entry)
881 ((AUD_SequencerFactory*)sequencer)->remove(entry);
884 void AUD_moveSequencer(AUD_Sound* sequencer, AUD_SequencerEntry* entry,
885 float begin, float end, float skip)
887 ((AUD_SequencerFactory*)sequencer)->move(entry, begin, end, skip);
890 void AUD_muteSequencer(AUD_Sound* sequencer, AUD_SequencerEntry* entry, char mute)
892 ((AUD_SequencerFactory*)sequencer)->mute(entry, mute);
895 int AUD_readSound(AUD_Sound* sound, sample_t* buffer, int length)
897 AUD_DeviceSpecs specs;
900 specs.rate = AUD_RATE_INVALID;
901 specs.channels = AUD_CHANNELS_MONO;
902 specs.format = AUD_FORMAT_INVALID;
904 AUD_ChannelMapperFactory mapper(sound, specs);
906 AUD_IReader* reader = mapper.createReader();
908 int len = reader->getLength();
909 float samplejump = (float)len / (float)length;
912 for(int i = 0; i < length; i++)
914 len = floor(samplejump * (i+1)) - floor(samplejump * i);
915 reader->read(len, buf);
924 for(int j = 1; j < len; j++)
931 buffer[i * 2 + 1] = max;
940 void AUD_startPlayback()
943 AUD_JackDevice* device = dynamic_cast<AUD_JackDevice*>(AUD_device);
945 device->startPlayback();
949 void AUD_stopPlayback()
952 AUD_JackDevice* device = dynamic_cast<AUD_JackDevice*>(AUD_device);
954 device->stopPlayback();
958 void AUD_seekSequencer(AUD_Channel* handle, float time)
961 AUD_JackDevice* device = dynamic_cast<AUD_JackDevice*>(AUD_device);
963 device->seekPlayback(time);
967 AUD_device->seek(handle, time);
971 float AUD_getSequencerPosition(AUD_Channel* handle)
974 AUD_JackDevice* device = dynamic_cast<AUD_JackDevice*>(AUD_device);
976 return device->getPlaybackPosition();
980 return AUD_device->getPosition(handle);
985 void AUD_setSyncCallback(AUD_syncFunction function, void* data)
987 AUD_JackDevice* device = dynamic_cast<AUD_JackDevice*>(AUD_device);
989 device->setSyncCallback(function, data);
993 int AUD_doesPlayback()
996 AUD_JackDevice* device = dynamic_cast<AUD_JackDevice*>(AUD_device);
998 return device->doesPlayback();