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_SourceCaps.h"
62 #include "AUD_IReader.h"
63 #include "AUD_SequencerFactory.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(specs, buffersize);
142 if(AUD_device->checkCapability(AUD_CAPS_3D_DEVICE))
143 AUD_3ddevice = dynamic_cast<AUD_I3DDevice*>(AUD_device);
148 g_device = (Device*)Device_empty();
151 g_device->device = dev;
164 int* AUD_enumDevices()
168 AUD_available_devices[i++] = AUD_SDL_DEVICE;
171 AUD_available_devices[i++] = AUD_OPENAL_DEVICE;
174 AUD_available_devices[i++] = AUD_JACK_DEVICE;
176 AUD_available_devices[i++] = AUD_NULL_DEVICE;
177 return AUD_available_devices;
185 Py_XDECREF(g_device);
197 static PyObject* AUD_getCDevice(PyObject* self)
202 return (PyObject*)g_device;
207 static PyMethodDef meth_getcdevice[] = {{ "getCDevice", (PyCFunction)AUD_getCDevice, METH_NOARGS, "Returns the C API Device."}};
209 PyObject* AUD_initPython()
211 PyObject* module = PyInit_aud();
212 PyModule_AddObject(module, "getCDevice", (PyObject *)PyCFunction_New(meth_getcdevice, NULL));
213 PyDict_SetItemString(PySys_GetObject("modules"), "aud", module);
216 g_device = (Device*)Device_empty();
219 g_device->device = AUD_device;
222 g_pyinitialized = true;
237 AUD_device->unlock();
240 AUD_SoundInfo AUD_getInfo(AUD_Sound* sound)
244 AUD_IReader* reader = sound->createReader();
250 info.specs = reader->getSpecs();
251 info.length = reader->getLength() / (float) info.specs.rate;
255 info.specs.channels = AUD_CHANNELS_INVALID;
256 info.specs.rate = AUD_RATE_INVALID;
263 AUD_Sound* AUD_load(const char* filename)
266 return new AUD_FileFactory(filename);
269 AUD_Sound* AUD_loadBuffer(unsigned char* buffer, int size)
272 return new AUD_FileFactory(buffer, size);
275 AUD_Sound* AUD_bufferSound(AUD_Sound* sound)
281 return new AUD_StreamBufferFactory(sound);
289 AUD_Sound* AUD_delaySound(AUD_Sound* sound, float delay)
295 return new AUD_DelayFactory(sound, delay);
303 AUD_Sound* AUD_limitSound(AUD_Sound* sound, float start, float end)
309 return new AUD_LimiterFactory(sound, start, end);
317 AUD_Sound* AUD_pingpongSound(AUD_Sound* sound)
323 return new AUD_PingPongFactory(sound);
331 AUD_Sound* AUD_loopSound(AUD_Sound* sound)
337 return new AUD_LoopFactory(sound);
345 int AUD_setLoop(AUD_Channel* handle, int loops, float time)
350 message.type = AUD_MSG_LOOP;
351 message.loopcount = loops;
356 return AUD_device->sendMessage(handle, message);
365 AUD_Sound* AUD_rectifySound(AUD_Sound* sound)
371 return new AUD_RectifyFactory(sound);
379 void AUD_unload(AUD_Sound* sound)
385 AUD_Channel* AUD_play(AUD_Sound* sound, int keep)
391 return AUD_device->play(sound, keep);
399 int AUD_pause(AUD_Channel* handle)
402 return AUD_device->pause(handle);
405 int AUD_resume(AUD_Channel* handle)
408 return AUD_device->resume(handle);
411 int AUD_stop(AUD_Channel* handle)
414 return AUD_device->stop(handle);
418 int AUD_setKeep(AUD_Channel* handle, int keep)
421 return AUD_device->setKeep(handle, keep);
424 int AUD_seek(AUD_Channel* handle, float seekTo)
427 return AUD_device->seek(handle, seekTo);
430 float AUD_getPosition(AUD_Channel* handle)
433 return AUD_device->getPosition(handle);
436 AUD_Status AUD_getStatus(AUD_Channel* handle)
439 return AUD_device->getStatus(handle);
442 AUD_Channel* AUD_play3D(AUD_Sound* sound, int keep)
450 return AUD_3ddevice->play3D(sound, keep);
452 return AUD_device->play(sound, keep);
460 int AUD_updateListener(AUD_3DData* data)
468 return AUD_3ddevice->updateListener(*data);
476 int AUD_set3DSetting(AUD_3DSetting setting, float value)
483 return AUD_3ddevice->setSetting(setting, value);
491 float AUD_get3DSetting(AUD_3DSetting setting)
498 return AUD_3ddevice->getSetting(setting);
506 int AUD_update3DSource(AUD_Channel* handle, AUD_3DData* data)
516 return AUD_3ddevice->updateSource(handle, *data);
525 int AUD_set3DSourceSetting(AUD_Channel* handle,
526 AUD_3DSourceSetting setting, float value)
535 return AUD_3ddevice->setSourceSetting(handle, setting, value);
544 float AUD_get3DSourceSetting(AUD_Channel* handle, AUD_3DSourceSetting setting)
553 return AUD_3ddevice->getSourceSetting(handle, setting);
562 int AUD_setSoundVolume(AUD_Channel* handle, float volume)
568 caps.handle = handle;
573 return AUD_device->setCapability(AUD_CAPS_SOURCE_VOLUME, &caps);
575 catch(AUD_Exception) {}
580 int AUD_setSoundPitch(AUD_Channel* handle, float pitch)
586 caps.handle = handle;
591 return AUD_device->setCapability(AUD_CAPS_SOURCE_PITCH, &caps);
593 catch(AUD_Exception) {}
598 AUD_Device* AUD_openReadDevice(AUD_DeviceSpecs specs)
602 return new AUD_ReadDevice(specs);
610 AUD_Channel* AUD_playDevice(AUD_Device* device, AUD_Sound* sound, float seek)
617 AUD_Channel* handle = device->play(sound);
618 device->seek(handle, seek);
627 int AUD_setDeviceVolume(AUD_Device* device, float volume)
633 return device->setCapability(AUD_CAPS_VOLUME, &volume);
635 catch(AUD_Exception) {}
640 int AUD_setDeviceSoundVolume(AUD_Device* device, AUD_Channel* handle,
647 caps.handle = handle;
652 return device->setCapability(AUD_CAPS_SOURCE_VOLUME, &caps);
654 catch(AUD_Exception) {}
659 int AUD_readDevice(AUD_Device* device, data_t* buffer, int length)
666 return device->read(buffer, length);
674 void AUD_closeReadDevice(AUD_Device* device)
687 float* AUD_readSoundBuffer(const char* filename, float low, float high,
688 float attack, float release, float threshold,
689 int accumulate, int additive, int square,
690 float sthreshold, int samplerate, int* length)
693 AUD_DeviceSpecs specs;
694 specs.channels = AUD_CHANNELS_MONO;
695 specs.rate = (AUD_SampleRate)samplerate;
698 AUD_FileFactory file(filename);
699 AUD_ChannelMapperFactory mapper(&file, specs);
700 AUD_LowpassFactory lowpass(&mapper, high);
701 AUD_HighpassFactory highpass(&lowpass, low);
702 AUD_EnvelopeFactory envelope(&highpass, attack, release, threshold, 0.1f);
703 AUD_LinearResampleFactory resampler(&envelope, specs);
705 AUD_SquareFactory squaref(sound, sthreshold);
708 AUD_AccumulatorFactory accumulator(sound, additive);
709 AUD_SumFactory sum(sound);
711 sound = &accumulator;
715 AUD_IReader* reader = sound->createReader();
722 sample_t* readbuffer;
726 buffer.resize((position + len) * sizeof(float), true);
727 reader->read(len, readbuffer);
728 memcpy(buffer.getBuffer() + position, readbuffer, len * sizeof(float));
733 float* result = (float*)malloc(position * sizeof(float));
734 memcpy(result, buffer.getBuffer(), position * sizeof(float));
739 AUD_Sound* AUD_createSequencer(void* data, AUD_volumeFunction volume)
741 /* AUD_XXX should be this: but AUD_createSequencer is called before the device
744 return new AUD_SequencerFactory(AUD_device->getSpecs().specs, data, volume);
747 specs.channels = AUD_CHANNELS_STEREO;
748 specs.rate = AUD_RATE_44100;
749 return new AUD_SequencerFactory(specs, data, volume);
752 void AUD_destroySequencer(AUD_Sound* sequencer)
754 delete ((AUD_SequencerFactory*)sequencer);
757 AUD_SequencerEntry* AUD_addSequencer(AUD_Sound** sequencer, AUD_Sound* sound,
758 float begin, float end, float skip, void* data)
760 return ((AUD_SequencerFactory*)sequencer)->add((AUD_IFactory**) sound, begin, end, skip, data);
763 void AUD_removeSequencer(AUD_Sound* sequencer, AUD_SequencerEntry* entry)
765 ((AUD_SequencerFactory*)sequencer)->remove(entry);
768 void AUD_moveSequencer(AUD_Sound* sequencer, AUD_SequencerEntry* entry,
769 float begin, float end, float skip)
771 ((AUD_SequencerFactory*)sequencer)->move(entry, begin, end, skip);
774 void AUD_muteSequencer(AUD_Sound* sequencer, AUD_SequencerEntry* entry, char mute)
776 ((AUD_SequencerFactory*)sequencer)->mute(entry, mute);
779 int AUD_readSound(AUD_Sound* sound, sample_t* buffer, int length)
781 AUD_IReader* reader = sound->createReader();
782 AUD_DeviceSpecs specs;
785 specs.specs = reader->getSpecs();
786 specs.channels = AUD_CHANNELS_MONO;
787 specs.format = AUD_FORMAT_FLOAT32;
789 AUD_ChannelMapperFactory mapper(reader, specs);
791 if(!reader || reader->getType() != AUD_TYPE_BUFFER)
794 reader = mapper.createReader();
799 int len = reader->getLength();
800 float samplejump = (float)len / (float)length;
803 for(int i = 0; i < length; i++)
805 len = floor(samplejump * (i+1)) - floor(samplejump * i);
806 reader->read(len, buf);
815 for(int j = 1; j < len; j++)
822 buffer[i * 2 + 1] = max;
826 delete reader; AUD_DELETE("reader")
831 void AUD_startPlayback()
834 AUD_JackDevice* device = dynamic_cast<AUD_JackDevice*>(AUD_device);
836 device->startPlayback();
840 void AUD_stopPlayback()
843 AUD_JackDevice* device = dynamic_cast<AUD_JackDevice*>(AUD_device);
845 device->stopPlayback();
849 void AUD_seekSequencer(AUD_Channel* handle, float time)
852 AUD_JackDevice* device = dynamic_cast<AUD_JackDevice*>(AUD_device);
854 device->seekPlayback(time);
858 AUD_device->seek(handle, time);
862 float AUD_getSequencerPosition(AUD_Channel* handle)
865 AUD_JackDevice* device = dynamic_cast<AUD_JackDevice*>(AUD_device);
867 return device->getPlaybackPosition();
871 return AUD_device->getPosition(handle);
876 void AUD_setSyncCallback(AUD_syncFunction function, void* data)
878 AUD_JackDevice* device = dynamic_cast<AUD_JackDevice*>(AUD_device);
880 device->setSyncCallback(function, data);
884 int AUD_doesPlayback()
887 AUD_JackDevice* device = dynamic_cast<AUD_JackDevice*>(AUD_device);
889 return device->doesPlayback();
894 #ifdef AUD_DEBUG_MEMORY
895 int AUD_References(int count, const char* text)
897 static int m_count = 0;
900 printf("+%s\n", text);
902 printf("-%s\n", text);