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 *****
26 #include "AUD_PyAPI.h"
27 #include "structmember.h"
29 #include "AUD_I3DDevice.h"
30 #include "AUD_NULLDevice.h"
31 #include "AUD_DelayFactory.h"
32 #include "AUD_DoubleFactory.h"
33 #include "AUD_FaderFactory.h"
34 #include "AUD_HighpassFactory.h"
35 #include "AUD_LimiterFactory.h"
36 #include "AUD_LoopFactory.h"
37 #include "AUD_LowpassFactory.h"
38 #include "AUD_PingPongFactory.h"
39 #include "AUD_PitchFactory.h"
40 #include "AUD_ReverseFactory.h"
41 #include "AUD_SinusFactory.h"
42 #include "AUD_FileFactory.h"
43 #include "AUD_SquareFactory.h"
44 #include "AUD_StreamBufferFactory.h"
45 #include "AUD_SuperposeFactory.h"
46 #include "AUD_VolumeFactory.h"
47 #include "AUD_IIRFilterFactory.h"
50 #include "AUD_SDLDevice.h"
54 #include "AUD_OpenALDevice.h"
58 #include "AUD_JackDevice.h"
61 // ====================================================================
72 // ====================================================================
74 #define PY_MODULE_ADD_CONSTANT(module, name) PyModule_AddIntConstant(module, #name, name)
76 // ====================================================================
78 static PyObject* AUDError;
80 static const char* device_not_3d_error = "Device is not a 3D device!";
82 // ====================================================================
85 Factory_dealloc(Factory* self)
89 Py_XDECREF(self->child_list);
90 Py_TYPE(self)->tp_free((PyObject*)self);
94 Factory_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
98 self = (Factory*)type->tp_alloc(type, 0);
101 static const char *kwlist[] = {"filename", NULL};
102 const char* filename = NULL;
104 if(!PyArg_ParseTupleAndKeywords(args, kwds, "s:Factory", const_cast<char**>(kwlist), &filename))
112 self->factory = new AUD_FileFactory(filename);
114 catch(AUD_Exception& e)
117 PyErr_SetString(AUDError, e.str);
122 return (PyObject *)self;
125 PyDoc_STRVAR(M_aud_Factory_sine_doc,
126 "sine(frequency, rate=44100)\n\n"
127 "Creates a sine factory which plays a sine wave.\n\n"
128 ":arg frequency: The frequency of the sine wave in Hz.\n"
129 ":type frequency: float\n"
130 ":arg rate: The sampling rate in Hz. It's recommended to set this "
131 "value to the playback device's samling rate to avoid resamping.\n"
133 ":return: The created :class:`Factory` object.\n"
134 ":rtype: :class:`Factory`");
137 Factory_sine(PyTypeObject* type, PyObject* args)
142 if(!PyArg_ParseTuple(args, "f|i:sine", &frequency, &rate))
147 self = (Factory*)type->tp_alloc(type, 0);
152 self->factory = new AUD_SinusFactory(frequency, (AUD_SampleRate)rate);
154 catch(AUD_Exception& e)
157 PyErr_SetString(AUDError, e.str);
162 return (PyObject *)self;
165 PyDoc_STRVAR(M_aud_Factory_file_doc,
167 "Creates a factory object of a sound file.\n\n"
168 ":arg filename: Path of the file.\n"
169 ":type filename: string\n"
170 ":return: The created :class:`Factory` object.\n"
171 ":rtype: :class:`Factory`\n\n"
172 ".. warning:: If the file doesn't exist or can't be read you will "
173 "not get an exception immediately, but when you try to start "
174 "playback of that factory.");
177 Factory_file(PyTypeObject* type, PyObject* args)
179 const char* filename = NULL;
181 if(!PyArg_ParseTuple(args, "s:file", &filename))
186 self = (Factory*)type->tp_alloc(type, 0);
191 self->factory = new AUD_FileFactory(filename);
193 catch(AUD_Exception& e)
196 PyErr_SetString(AUDError, e.str);
201 return (PyObject *)self;
204 PyDoc_STRVAR(M_aud_Factory_lowpass_doc,
205 "lowpass(frequency, Q=0.5)\n\n"
206 "Creates a second order lowpass filter based on the transfer "
207 "function H(s) = 1 / (s^2 + s/Q + 1)\n\n"
208 ":arg frequency: The cut off trequency of the lowpass.\n"
209 ":type frequency: float\n"
210 ":arg Q: Q factor of the lowpass.\n"
212 ":return: The created :class:`Factory` object.\n"
213 ":rtype: :class:`Factory`");
216 Factory_lowpass(Factory* self, PyObject* args)
221 if(!PyArg_ParseTuple(args, "f|f:lowpass", &frequency, &Q))
224 PyTypeObject* type = ((PyObject*)self)->ob_type;
225 Factory *parent = (Factory*)type->tp_alloc(type, 0);
230 parent->child_list = (PyObject*)self;
234 parent->factory = new AUD_LowpassFactory(self->factory, frequency, Q);
236 catch(AUD_Exception& e)
239 PyErr_SetString(AUDError, e.str);
244 return (PyObject *)parent;
247 PyDoc_STRVAR(M_aud_Factory_delay_doc,
249 "Delays by playing adding silence in front of the other factory's "
251 ":arg time: How many seconds of silence should be added before "
253 ":type time: float\n"
254 ":return: The created :class:`Factory` object.\n"
255 ":rtype: :class:`Factory`");
258 Factory_delay(Factory* self, PyObject* args)
262 if(!PyArg_ParseTuple(args, "f:delay", &delay))
265 PyTypeObject* type = ((PyObject*)self)->ob_type;
266 Factory *parent = (Factory*)type->tp_alloc(type, 0);
271 parent->child_list = (PyObject*)self;
275 parent->factory = new AUD_DelayFactory(self->factory, delay);
277 catch(AUD_Exception& e)
280 PyErr_SetString(AUDError, e.str);
285 return (PyObject *)parent;
288 PyDoc_STRVAR(M_aud_Factory_join_doc,
290 "Plays two factories in sequence.\n\n"
291 ":arg factory: The factory to play second.\n"
292 ":type factory: :class:`Factory`\n"
293 ":return: The created :class:`Factory` object.\n"
294 ":rtype: :class:`Factory`\n\n"
295 ".. note:: The two factories have to have the same specifications "
296 "(channels and samplerate).");
299 Factory_join(Factory* self, PyObject* object)
301 PyTypeObject* type = ((PyObject*)self)->ob_type;
303 if(!PyObject_TypeCheck(object, type))
305 PyErr_SetString(PyExc_TypeError, "Object has to be of type Factory!");
310 Factory *child = (Factory*)object;
312 parent = (Factory*)type->tp_alloc(type, 0);
315 parent->child_list = Py_BuildValue("(OO)", self, object);
319 parent->factory = new AUD_DoubleFactory(self->factory, child->factory);
321 catch(AUD_Exception& e)
324 PyErr_SetString(AUDError, e.str);
329 return (PyObject *)parent;
332 PyDoc_STRVAR(M_aud_Factory_highpass_doc,
333 "highpass(frequency, Q=0.5)\n\n"
334 "Creates a second order highpass filter based on the transfer "
335 "function H(s) = s^2 / (s^2 + s/Q + 1)\n\n"
336 ":arg frequency: The cut off trequency of the highpass.\n"
337 ":type frequency: float\n"
338 ":arg Q: Q factor of the lowpass.\n"
340 ":return: The created :class:`Factory` object.\n"
341 ":rtype: :class:`Factory`");
344 Factory_highpass(Factory* self, PyObject* args)
349 if(!PyArg_ParseTuple(args, "f|f:highpass", &frequency, &Q))
352 PyTypeObject* type = ((PyObject*)self)->ob_type;
353 Factory *parent = (Factory*)type->tp_alloc(type, 0);
358 parent->child_list = (PyObject*)self;
362 parent->factory = new AUD_HighpassFactory(self->factory, frequency, Q);
364 catch(AUD_Exception& e)
367 PyErr_SetString(AUDError, e.str);
372 return (PyObject *)parent;
375 PyDoc_STRVAR(M_aud_Factory_limit_doc,
376 "limit(start, end)\n\n"
377 "Limits a factory within a specific start and end time.\n\n"
378 ":arg start: Start time in seconds.\n"
379 ":type start: float\n"
380 ":arg end: End time in seconds.\n"
382 ":return: The created :class:`Factory` object.\n"
383 ":rtype: :class:`Factory`");
386 Factory_limit(Factory* self, PyObject* args)
390 if(!PyArg_ParseTuple(args, "ff:limit", &start, &end))
393 PyTypeObject* type = ((PyObject*)self)->ob_type;
394 Factory *parent = (Factory*)type->tp_alloc(type, 0);
399 parent->child_list = (PyObject*)self;
403 parent->factory = new AUD_LimiterFactory(self->factory, start, end);
405 catch(AUD_Exception& e)
408 PyErr_SetString(AUDError, e.str);
413 return (PyObject *)parent;
416 PyDoc_STRVAR(M_aud_Factory_pitch_doc,
418 "Changes the pitch of a factory with a specific factor.\n\n"
419 ":arg factor: The factor to change the pitch with.\n"
420 ":type factor: float\n"
421 ":return: The created :class:`Factory` object.\n"
422 ":rtype: :class:`Factory`\n\n"
423 ".. note:: This is done by changing the sample rate of the "
424 "underlying factory, which has to be an integer, so the factor "
425 "value rounded and the factor may not be 100 % accurate.\n\n"
426 ".. note:: This is a filter function, you might consider using "
427 ":attr:`Handle.pitch` instead.");
430 Factory_pitch(Factory* self, PyObject* args)
434 if(!PyArg_ParseTuple(args, "f:pitch", &factor))
437 PyTypeObject* type = ((PyObject*)self)->ob_type;
438 Factory *parent = (Factory*)type->tp_alloc(type, 0);
443 parent->child_list = (PyObject*)self;
447 parent->factory = new AUD_PitchFactory(self->factory, factor);
449 catch(AUD_Exception& e)
452 PyErr_SetString(AUDError, e.str);
457 return (PyObject *)parent;
460 PyDoc_STRVAR(M_aud_Factory_volume_doc,
462 "Changes the volume of a factory.\n\n"
463 ":arg volume: The new volume..\n"
464 ":type volume: float\n"
465 ":return: The created :class:`Factory` object.\n"
466 ":rtype: :class:`Factory`\n\n"
467 ".. note:: Should be in the range [0, 1] to avoid clipping.\n\n"
468 ".. note:: This is a filter function, you might consider using "
469 ":attr:`Handle.volume` instead.");
472 Factory_volume(Factory* self, PyObject* args)
476 if(!PyArg_ParseTuple(args, "f:volume", &volume))
479 PyTypeObject* type = ((PyObject*)self)->ob_type;
480 Factory *parent = (Factory*)type->tp_alloc(type, 0);
485 parent->child_list = (PyObject*)self;
489 parent->factory = new AUD_VolumeFactory(self->factory, volume);
491 catch(AUD_Exception& e)
494 PyErr_SetString(AUDError, e.str);
499 return (PyObject *)parent;
502 PyDoc_STRVAR(M_aud_Factory_fadein_doc,
503 "fadein(start, length)\n\n"
504 "Fades a factory in by raising the volume linearly in the given "
506 ":arg start: Time in seconds when the fading should start.\n"
507 ":type start: float\n"
508 ":arg length: Time in seconds how long the fading should last.\n"
509 ":type length: float\n"
510 ":return: The created :class:`Factory` object.\n"
511 ":rtype: :class:`Factory`\n\n"
512 ".. note:: Before the fade starts it plays silence.");
515 Factory_fadein(Factory* self, PyObject* args)
519 if(!PyArg_ParseTuple(args, "ff:fadein", &start, &length))
522 PyTypeObject* type = ((PyObject*)self)->ob_type;
523 Factory *parent = (Factory*)type->tp_alloc(type, 0);
528 parent->child_list = (PyObject*)self;
532 parent->factory = new AUD_FaderFactory(self->factory, AUD_FADE_IN, start, length);
534 catch(AUD_Exception& e)
537 PyErr_SetString(AUDError, e.str);
542 return (PyObject *)parent;
545 PyDoc_STRVAR(M_aud_Factory_fadeout_doc,
546 "fadeout(start, length)\n\n"
547 "Fades a factory in by lowering the volume linearly in the given "
549 ":arg start: Time in seconds when the fading should start.\n"
550 ":type start: float\n"
551 ":arg length: Time in seconds how long the fading should last.\n"
552 ":type length: float\n"
553 ":return: The created :class:`Factory` object.\n"
554 ":rtype: :class:`Factory`\n\n"
555 ".. note:: After the fade this factory plays silence, so that "
556 "the length of the factory is not altered.");
559 Factory_fadeout(Factory* self, PyObject* args)
563 if(!PyArg_ParseTuple(args, "ff:fadeout", &start, &length))
566 PyTypeObject* type = ((PyObject*)self)->ob_type;
567 Factory *parent = (Factory*)type->tp_alloc(type, 0);
572 parent->child_list = (PyObject*)self;
576 parent->factory = new AUD_FaderFactory(self->factory, AUD_FADE_OUT, start, length);
578 catch(AUD_Exception& e)
581 PyErr_SetString(AUDError, e.str);
586 return (PyObject *)parent;
589 PyDoc_STRVAR(M_aud_Factory_loop_doc,
591 "Loops a factory.\n\n"
592 ":arg count: How often the factory should be looped. "
593 "Negative values mean endlessly.\n"
594 ":type count: integer\n"
595 ":return: The created :class:`Factory` object.\n"
596 ":rtype: :class:`Factory`\n\n"
597 ".. note:: This is a filter function, you might consider using "
598 ":attr:`Handle.loop_count` instead.");
601 Factory_loop(Factory* self, PyObject* args)
605 if(!PyArg_ParseTuple(args, "i:loop", &loop))
608 PyTypeObject* type = ((PyObject*)self)->ob_type;
609 Factory *parent = (Factory*)type->tp_alloc(type, 0);
614 parent->child_list = (PyObject*)self;
618 parent->factory = new AUD_LoopFactory(self->factory, loop);
620 catch(AUD_Exception& e)
623 PyErr_SetString(AUDError, e.str);
628 return (PyObject *)parent;
631 PyDoc_STRVAR(M_aud_Factory_mix_doc,
633 "Mixes two factories.\n\n"
634 ":arg factory: The factory to mix over the other.\n"
635 ":type factory: :class:`Factory`\n"
636 ":return: The created :class:`Factory` object.\n"
637 ":rtype: :class:`Factory`\n\n"
638 ".. note:: The two factories have to have the same specifications "
639 "(channels and samplerate).");
642 Factory_mix(Factory* self, PyObject* object)
644 PyTypeObject* type = ((PyObject*)self)->ob_type;
646 if(!PyObject_TypeCheck(object, type))
648 PyErr_SetString(PyExc_TypeError, "Object is not of type Factory!");
652 Factory *parent = (Factory*)type->tp_alloc(type, 0);
653 Factory *child = (Factory*)object;
657 parent->child_list = Py_BuildValue("(OO)", self, object);
661 parent->factory = new AUD_SuperposeFactory(self->factory, child->factory);
663 catch(AUD_Exception& e)
666 PyErr_SetString(AUDError, e.str);
671 return (PyObject *)parent;
674 PyDoc_STRVAR(M_aud_Factory_pingpong_doc,
676 "Plays a factory forward and then backward.\n"
677 "This is like joining a factory with its reverse.\n\n"
678 ":return: The created :class:`Factory` object.\n"
679 ":rtype: :class:`Factory`");
682 Factory_pingpong(Factory* self)
684 PyTypeObject* type = ((PyObject*)self)->ob_type;
685 Factory *parent = (Factory*)type->tp_alloc(type, 0);
690 parent->child_list = (PyObject*)self;
694 parent->factory = new AUD_PingPongFactory(self->factory);
696 catch(AUD_Exception& e)
699 PyErr_SetString(AUDError, e.str);
704 return (PyObject *)parent;
707 PyDoc_STRVAR(M_aud_Factory_reverse_doc,
709 "Plays a factory reversed.\n\n"
710 ":return: The created :class:`Factory` object.\n"
711 ":rtype: :class:`Factory`\n\n"
712 ".. note:: The factory has to have a finite length and has to be "
713 "seekable. It's recommended to use this only with factories with "
714 "fast and accurate seeking, which is not true for encoded audio "
715 "files, such ones should be buffered using :meth:`buffer` before "
716 "being played reversed.\n\n"
717 ".. warning:: If seeking is not accurate in the underlying factory "
718 "you'll likely hear skips/jumps/cracks.");
721 Factory_reverse(Factory* self)
723 PyTypeObject* type = ((PyObject*)self)->ob_type;
724 Factory *parent = (Factory*)type->tp_alloc(type, 0);
729 parent->child_list = (PyObject*)self;
733 parent->factory = new AUD_ReverseFactory(self->factory);
735 catch(AUD_Exception& e)
738 PyErr_SetString(AUDError, e.str);
743 return (PyObject *)parent;
746 PyDoc_STRVAR(M_aud_Factory_buffer_doc,
748 "Buffers a factory into RAM.\n"
749 "This saves CPU usage needed for decoding and file access if the "
750 "underlying factory reads from a file on the harddisk, but it "
751 "consumes a lot of memory.\n\n"
752 ":return: The created :class:`Factory` object.\n"
753 ":rtype: :class:`Factory`\n\n"
754 ".. note:: Only known-length factories can be buffered.\n\n"
755 ".. warning:: Raw PCM data needs a lot of space, only buffer "
759 Factory_buffer(Factory* self)
761 PyTypeObject* type = ((PyObject*)self)->ob_type;
762 Factory *parent = (Factory*)type->tp_alloc(type, 0);
768 parent->factory = new AUD_StreamBufferFactory(self->factory);
770 catch(AUD_Exception& e)
773 PyErr_SetString(AUDError, e.str);
778 return (PyObject *)parent;
781 PyDoc_STRVAR(M_aud_Factory_square_doc,
782 "square(threshold = 0)\n\n"
783 "Makes a square wave out of an audio wave by setting all samples "
784 "with a amplitude >= threshold to 1, all <= -threshold to -1 and "
785 "all between to 0.\n\n"
786 ":arg threshold: Threshold value over which an amplitude counts "
788 ":type threshold: float\n"
789 ":return: The created :class:`Factory` object.\n"
790 ":rtype: :class:`Factory`");
793 Factory_square(Factory* self, PyObject* args)
797 if(!PyArg_ParseTuple(args, "|f:square", &threshold))
800 PyTypeObject* type = ((PyObject*)self)->ob_type;
801 Factory *parent = (Factory*)type->tp_alloc(type, 0);
806 parent->child_list = (PyObject*)self;
810 parent->factory = new AUD_SquareFactory(self->factory, threshold);
812 catch(AUD_Exception& e)
815 PyErr_SetString(AUDError, e.str);
820 return (PyObject *)parent;
823 PyDoc_STRVAR(M_aud_Factory_filter_doc,
824 "filter(b, a = (1))\n\n"
825 "Filters a factory with the supplied IIR filter coefficients.\n"
826 "Without the second parameter you'll get a FIR filter.\n"
827 "If the first value of the a sequence is 0 it will be set to 1 "
829 "If the first value of the a sequence is neither 0 nor 1, all "
830 "filter coefficients will be scaled by this value so that it is 1 "
831 "in the end, you don't have to scale yourself.\n\n"
832 ":arg b: The nominator filter coefficients.\n"
833 ":type b: sequence of float\n"
834 ":arg a: The denominator filter coefficients.\n"
835 ":type a: sequence of float\n"
836 ":return: The created :class:`Factory` object.\n"
837 ":rtype: :class:`Factory`");
840 Factory_filter(Factory* self, PyObject* args)
843 PyObject* py_a = NULL;
845 if(!PyArg_ParseTuple(args, "O|O:filter", &py_b, &py_a))
848 if(!PySequence_Check(py_b) || (py_a != NULL && !PySequence_Check(py_a)))
850 PyErr_SetString(PyExc_TypeError, "Parameter is not a sequence!");
854 if(!PySequence_Length(py_b) || (py_a != NULL && !PySequence_Length(py_a)))
856 PyErr_SetString(PyExc_ValueError, "The sequence has to contain at least one value!");
860 std::vector<float> a, b;
865 for(int i = 0; i < PySequence_Length(py_b); i++)
867 py_value = PySequence_GetItem(py_b, i);
868 result = PyArg_Parse(py_value, "f:filter", &value);
879 for(int i = 0; i < PySequence_Length(py_a); i++)
881 py_value = PySequence_GetItem(py_a, i);
882 result = PyArg_Parse(py_value, "f:filter", &value);
897 PyTypeObject* type = ((PyObject*)self)->ob_type;
898 Factory *parent = (Factory*)type->tp_alloc(type, 0);
903 parent->child_list = (PyObject*)self;
907 parent->factory = new AUD_IIRFilterFactory(self->factory, b, a);
909 catch(AUD_Exception& e)
912 PyErr_SetString(AUDError, e.str);
917 return (PyObject *)parent;
920 static PyMethodDef Factory_methods[] = {
921 {"sine", (PyCFunction)Factory_sine, METH_VARARGS | METH_CLASS,
922 M_aud_Factory_sine_doc
924 {"file", (PyCFunction)Factory_file, METH_VARARGS | METH_CLASS,
925 M_aud_Factory_file_doc
927 {"lowpass", (PyCFunction)Factory_lowpass, METH_VARARGS,
928 M_aud_Factory_lowpass_doc
930 {"delay", (PyCFunction)Factory_delay, METH_VARARGS,
931 M_aud_Factory_delay_doc
933 {"join", (PyCFunction)Factory_join, METH_O,
934 M_aud_Factory_join_doc
936 {"highpass", (PyCFunction)Factory_highpass, METH_VARARGS,
937 M_aud_Factory_highpass_doc
939 {"limit", (PyCFunction)Factory_limit, METH_VARARGS,
940 M_aud_Factory_limit_doc
942 {"pitch", (PyCFunction)Factory_pitch, METH_VARARGS,
943 M_aud_Factory_pitch_doc
945 {"volume", (PyCFunction)Factory_volume, METH_VARARGS,
946 M_aud_Factory_volume_doc
948 {"fadein", (PyCFunction)Factory_fadein, METH_VARARGS,
949 M_aud_Factory_fadein_doc
951 {"fadeout", (PyCFunction)Factory_fadeout, METH_VARARGS,
952 M_aud_Factory_fadeout_doc
954 {"loop", (PyCFunction)Factory_loop, METH_VARARGS,
955 M_aud_Factory_loop_doc
957 {"mix", (PyCFunction)Factory_mix, METH_O,
958 M_aud_Factory_mix_doc
960 {"pingpong", (PyCFunction)Factory_pingpong, METH_NOARGS,
961 M_aud_Factory_pingpong_doc
963 {"reverse", (PyCFunction)Factory_reverse, METH_NOARGS,
964 M_aud_Factory_reverse_doc
966 {"buffer", (PyCFunction)Factory_buffer, METH_NOARGS,
967 M_aud_Factory_buffer_doc
969 {"square", (PyCFunction)Factory_square, METH_VARARGS,
970 M_aud_Factory_square_doc
972 {"filter", (PyCFunction)Factory_filter, METH_VARARGS,
973 M_aud_Factory_filter_doc
975 {NULL} /* Sentinel */
978 PyDoc_STRVAR(M_aud_Factory_doc,
979 "Factory objects are immutable and represent a sound that can be "
980 "played simultaneously multiple times. They are called factories "
981 "because they create reader objects internally that are used for "
984 static PyTypeObject FactoryType = {
985 PyVarObject_HEAD_INIT(NULL, 0)
986 "aud.Factory", /* tp_name */
987 sizeof(Factory), /* tp_basicsize */
989 (destructor)Factory_dealloc, /* tp_dealloc */
995 0, /* tp_as_number */
996 0, /* tp_as_sequence */
997 0, /* tp_as_mapping */
1001 0, /* tp_getattro */
1002 0, /* tp_setattro */
1003 0, /* tp_as_buffer */
1004 Py_TPFLAGS_DEFAULT, /* tp_flags */
1005 M_aud_Factory_doc, /* tp_doc */
1006 0, /* tp_traverse */
1008 0, /* tp_richcompare */
1009 0, /* tp_weaklistoffset */
1011 0, /* tp_iternext */
1012 Factory_methods, /* tp_methods */
1017 0, /* tp_descr_get */
1018 0, /* tp_descr_set */
1019 0, /* tp_dictoffset */
1022 Factory_new, /* tp_new */
1025 // ========== Handle ==================================================
1028 Handle_dealloc(Handle* self)
1030 Py_XDECREF(self->device);
1031 Py_TYPE(self)->tp_free((PyObject*)self);
1034 PyDoc_STRVAR(M_aud_Handle_pause_doc,
1036 "Pauses playback.\n\n"
1037 ":return: Whether the action succeeded.\n"
1041 Handle_pause(Handle *self)
1043 Device* device = (Device*)self->device;
1047 if(device->device->pause(self->handle))
1052 catch(AUD_Exception& e)
1054 PyErr_SetString(AUDError, e.str);
1061 PyDoc_STRVAR(M_aud_Handle_resume_doc,
1063 "Resumes playback.\n\n"
1064 ":return: Whether the action succeeded.\n"
1068 Handle_resume(Handle *self)
1070 Device* device = (Device*)self->device;
1074 if(device->device->resume(self->handle))
1079 catch(AUD_Exception& e)
1081 PyErr_SetString(AUDError, e.str);
1088 PyDoc_STRVAR(M_aud_Handle_stop_doc,
1090 "Stops playback.\n\n"
1091 ":return: Whether the action succeeded.\n"
1093 ".. note:: This makes the handle invalid.");
1096 Handle_stop(Handle *self)
1098 Device* device = (Device*)self->device;
1102 if(device->device->stop(self->handle))
1107 catch(AUD_Exception& e)
1109 PyErr_SetString(AUDError, e.str);
1116 static PyMethodDef Handle_methods[] = {
1117 {"pause", (PyCFunction)Handle_pause, METH_NOARGS,
1118 M_aud_Handle_pause_doc
1120 {"resume", (PyCFunction)Handle_resume, METH_NOARGS,
1121 M_aud_Handle_resume_doc
1123 {"stop", (PyCFunction)Handle_stop, METH_NOARGS,
1124 M_aud_Handle_stop_doc
1126 {NULL} /* Sentinel */
1129 PyDoc_STRVAR(M_aud_Handle_position_doc,
1130 "The playback position of the sound in seconds.");
1133 Handle_get_position(Handle *self, void* nothing)
1135 Device* device = (Device*)self->device;
1139 return Py_BuildValue("f", device->device->getPosition(self->handle));
1141 catch(AUD_Exception& e)
1143 PyErr_SetString(AUDError, e.str);
1149 Handle_set_position(Handle *self, PyObject* args, void* nothing)
1153 if(!PyArg_Parse(args, "f:position", &position))
1156 Device* device = (Device*)self->device;
1160 if(device->device->seek(self->handle, position))
1162 PyErr_SetString(AUDError, "Couldn't seek the sound!");
1164 catch(AUD_Exception& e)
1166 PyErr_SetString(AUDError, e.str);
1172 PyDoc_STRVAR(M_aud_Handle_keep_doc,
1173 "Whether the sound should be kept paused in the device when its "
1175 "This can be used to seek the sound to some position and start "
1176 "playback again.\n\n"
1177 ".. warning:: If this is set to true and you forget stopping this "
1178 "equals a memory leak as the handle exists until the device is "
1182 Handle_get_keep(Handle *self, void* nothing)
1184 Device* device = (Device*)self->device;
1188 if(device->device->getKeep(self->handle))
1197 catch(AUD_Exception& e)
1199 PyErr_SetString(AUDError, e.str);
1205 Handle_set_keep(Handle *self, PyObject* args, void* nothing)
1207 if(!PyBool_Check(args))
1209 PyErr_SetString(PyExc_TypeError, "keep is not a boolean!");
1213 bool keep = args == Py_True;
1214 Device* device = (Device*)self->device;
1218 if(device->device->setKeep(self->handle, keep))
1220 PyErr_SetString(AUDError, "Couldn't set keep of the sound!");
1222 catch(AUD_Exception& e)
1224 PyErr_SetString(AUDError, e.str);
1230 PyDoc_STRVAR(M_aud_Handle_status_doc,
1231 "Whether the sound is playing, paused or stopped (=invalid).");
1234 Handle_get_status(Handle *self, void* nothing)
1236 Device* device = (Device*)self->device;
1240 return Py_BuildValue("i", device->device->getStatus(self->handle));
1242 catch(AUD_Exception& e)
1244 PyErr_SetString(AUDError, e.str);
1249 PyDoc_STRVAR(M_aud_Handle_volume_doc,
1250 "The volume of the sound.");
1253 Handle_get_volume(Handle *self, void* nothing)
1255 Device* device = (Device*)self->device;
1259 return Py_BuildValue("f", device->device->getVolume(self->handle));
1261 catch(AUD_Exception& e)
1263 PyErr_SetString(AUDError, e.str);
1269 Handle_set_volume(Handle *self, PyObject* args, void* nothing)
1273 if(!PyArg_Parse(args, "f:volume", &volume))
1276 Device* device = (Device*)self->device;
1280 if(device->device->setVolume(self->handle, volume))
1282 PyErr_SetString(AUDError, "Couldn't set the sound volume!");
1284 catch(AUD_Exception& e)
1286 PyErr_SetString(AUDError, e.str);
1292 PyDoc_STRVAR(M_aud_Handle_pitch_doc,
1293 "The pitch of the sound.");
1296 Handle_get_pitch(Handle *self, void* nothing)
1298 Device* device = (Device*)self->device;
1302 return Py_BuildValue("f", device->device->getPitch(self->handle));
1304 catch(AUD_Exception& e)
1306 PyErr_SetString(AUDError, e.str);
1312 Handle_set_pitch(Handle *self, PyObject* args, void* nothing)
1316 if(!PyArg_Parse(args, "f:pitch", &pitch))
1319 Device* device = (Device*)self->device;
1323 if(device->device->setPitch(self->handle, pitch))
1325 PyErr_SetString(AUDError, "Couldn't set the sound pitch!");
1327 catch(AUD_Exception& e)
1329 PyErr_SetString(AUDError, e.str);
1335 PyDoc_STRVAR(M_aud_Handle_loop_count_doc,
1336 "The (remaining) loop count of the sound. A negative value indicates infinity.");
1339 Handle_get_loop_count(Handle *self, void* nothing)
1341 Device* device = (Device*)self->device;
1345 return Py_BuildValue("i", device->device->getLoopCount(self->handle));
1347 catch(AUD_Exception& e)
1349 PyErr_SetString(AUDError, e.str);
1355 Handle_set_loop_count(Handle *self, PyObject* args, void* nothing)
1359 if(!PyArg_Parse(args, "i:loop_count", &loops))
1362 Device* device = (Device*)self->device;
1366 if(device->device->setLoopCount(self->handle, loops))
1368 PyErr_SetString(AUDError, "Couldn't set the loop count!");
1370 catch(AUD_Exception& e)
1372 PyErr_SetString(AUDError, e.str);
1378 PyDoc_STRVAR(M_aud_Handle_location_doc,
1379 "The source's location in 3D space, a 3D tuple of floats.");
1382 Handle_get_location(Handle *self, void* nothing)
1384 Device* dev = (Device*)self->device;
1388 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(dev->device);
1391 AUD_Vector3 v = device->getSourceLocation(self->handle);
1392 return Py_BuildValue("(fff)", v.x(), v.y(), v.z());
1396 PyErr_SetString(AUDError, device_not_3d_error);
1399 catch(AUD_Exception& e)
1401 PyErr_SetString(AUDError, e.str);
1408 Handle_set_location(Handle *self, PyObject* args, void* nothing)
1412 if(!PyArg_Parse(args, "(fff):location", &x, &y, &z))
1415 Device* dev = (Device*)self->device;
1419 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(dev->device);
1422 AUD_Vector3 location(x, y, z);
1423 if(device->setSourceLocation(self->handle, location))
1425 PyErr_SetString(AUDError, "Location couldn't be set!");
1428 PyErr_SetString(AUDError, device_not_3d_error);
1430 catch(AUD_Exception& e)
1432 PyErr_SetString(AUDError, e.str);
1438 PyDoc_STRVAR(M_aud_Handle_velocity_doc,
1439 "The source's velocity in 3D space, a 3D tuple of floats.");
1442 Handle_get_velocity(Handle *self, void* nothing)
1444 Device* dev = (Device*)self->device;
1448 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(dev->device);
1451 AUD_Vector3 v = device->getSourceVelocity(self->handle);
1452 return Py_BuildValue("(fff)", v.x(), v.y(), v.z());
1456 PyErr_SetString(AUDError, device_not_3d_error);
1459 catch(AUD_Exception& e)
1461 PyErr_SetString(AUDError, e.str);
1468 Handle_set_velocity(Handle *self, PyObject* args, void* nothing)
1472 if(!PyArg_Parse(args, "(fff):velocity", &x, &y, &z))
1475 Device* dev = (Device*)self->device;
1479 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(dev->device);
1482 AUD_Vector3 velocity(x, y, z);
1483 if(device->setSourceVelocity(self->handle, velocity))
1485 PyErr_SetString(AUDError, "Couldn't set the velocity!");
1488 PyErr_SetString(AUDError, device_not_3d_error);
1490 catch(AUD_Exception& e)
1492 PyErr_SetString(AUDError, e.str);
1498 PyDoc_STRVAR(M_aud_Handle_orientation_doc,
1499 "The source's orientation in 3D space as quaternion, a 4 float tuple.");
1502 Handle_get_orientation(Handle *self, void* nothing)
1504 Device* dev = (Device*)self->device;
1508 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(dev->device);
1511 AUD_Quaternion o = device->getSourceOrientation(self->handle);
1512 return Py_BuildValue("(ffff)", o.w(), o.x(), o.y(), o.z());
1516 PyErr_SetString(AUDError, device_not_3d_error);
1519 catch(AUD_Exception& e)
1521 PyErr_SetString(AUDError, e.str);
1528 Handle_set_orientation(Handle *self, PyObject* args, void* nothing)
1532 if(!PyArg_Parse(args, "(ffff):orientation", &w, &x, &y, &z))
1535 Device* dev = (Device*)self->device;
1539 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(dev->device);
1542 AUD_Quaternion orientation(w, x, y, z);
1543 if(device->setSourceOrientation(self->handle, orientation))
1545 PyErr_SetString(AUDError, "Couldn't set the orientation!");
1548 PyErr_SetString(AUDError, device_not_3d_error);
1550 catch(AUD_Exception& e)
1552 PyErr_SetString(AUDError, e.str);
1558 PyDoc_STRVAR(M_aud_Handle_relative_doc,
1559 "Whether the source's location, velocity and orientation is relative or absolute to the listener.");
1562 Handle_get_relative(Handle *self, void* nothing)
1564 Device* dev = (Device*)self->device;
1568 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(dev->device);
1571 if(device->isRelative(self->handle))
1582 PyErr_SetString(AUDError, device_not_3d_error);
1585 catch(AUD_Exception& e)
1587 PyErr_SetString(AUDError, e.str);
1594 Handle_set_relative(Handle *self, PyObject* args, void* nothing)
1596 if(!PyBool_Check(args))
1598 PyErr_SetString(PyExc_TypeError, "Value is not a boolean!");
1602 bool relative = (args == Py_True);
1603 Device* dev = (Device*)self->device;
1607 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(dev->device);
1610 if(device->setRelative(self->handle, relative))
1612 PyErr_SetString(AUDError, "Couldn't set the relativeness!");
1615 PyErr_SetString(AUDError, device_not_3d_error);
1617 catch(AUD_Exception& e)
1619 PyErr_SetString(AUDError, e.str);
1625 PyDoc_STRVAR(M_aud_Handle_volume_minimum_doc,
1626 "The minimum volume of the source.\n\n"
1627 ".. seealso:: :attr:`Device.distance_model`");
1630 Handle_get_volume_minimum(Handle *self, void* nothing)
1632 Device* dev = (Device*)self->device;
1636 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(dev->device);
1639 return Py_BuildValue("f", device->getVolumeMinimum(self->handle));
1643 PyErr_SetString(AUDError, device_not_3d_error);
1647 catch(AUD_Exception& e)
1649 PyErr_SetString(AUDError, e.str);
1655 Handle_set_volume_minimum(Handle *self, PyObject* args, void* nothing)
1659 if(!PyArg_Parse(args, "f:volume_minimum", &volume))
1662 Device* dev = (Device*)self->device;
1666 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(dev->device);
1669 if(device->setVolumeMinimum(self->handle, volume))
1671 PyErr_SetString(AUDError, "Couldn't set the minimum volume!");
1674 PyErr_SetString(AUDError, device_not_3d_error);
1676 catch(AUD_Exception& e)
1678 PyErr_SetString(AUDError, e.str);
1684 PyDoc_STRVAR(M_aud_Handle_volume_maximum_doc,
1685 "The maximum volume of the source.\n\n"
1686 ".. seealso:: :attr:`Device.distance_model`");
1689 Handle_get_volume_maximum(Handle *self, void* nothing)
1691 Device* dev = (Device*)self->device;
1695 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(dev->device);
1698 return Py_BuildValue("f", device->getVolumeMaximum(self->handle));
1702 PyErr_SetString(AUDError, device_not_3d_error);
1706 catch(AUD_Exception& e)
1708 PyErr_SetString(AUDError, e.str);
1714 Handle_set_volume_maximum(Handle *self, PyObject* args, void* nothing)
1718 if(!PyArg_Parse(args, "f:volume_maximum", &volume))
1721 Device* dev = (Device*)self->device;
1725 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(dev->device);
1728 if(device->setVolumeMaximum(self->handle, volume))
1730 PyErr_SetString(AUDError, "Couldn't set the maximum volume!");
1733 PyErr_SetString(AUDError, device_not_3d_error);
1735 catch(AUD_Exception& e)
1737 PyErr_SetString(AUDError, e.str);
1743 PyDoc_STRVAR(M_aud_Handle_distance_reference_doc,
1744 "The reference distance of the source.\n"
1745 "At this distance the volume will be exactly :attr:`volume`.\n\n"
1746 ".. seealso:: :attr:`Device.distance_model`");
1749 Handle_get_distance_reference(Handle *self, void* nothing)
1751 Device* dev = (Device*)self->device;
1755 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(dev->device);
1758 return Py_BuildValue("f", device->getDistanceReference(self->handle));
1762 PyErr_SetString(AUDError, device_not_3d_error);
1766 catch(AUD_Exception& e)
1768 PyErr_SetString(AUDError, e.str);
1774 Handle_set_distance_reference(Handle *self, PyObject* args, void* nothing)
1778 if(!PyArg_Parse(args, "f:distance_reference", &distance))
1781 Device* dev = (Device*)self->device;
1785 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(dev->device);
1788 if(device->setDistanceReference(self->handle, distance))
1790 PyErr_SetString(AUDError, "Couldn't set the reference distance!");
1793 PyErr_SetString(AUDError, device_not_3d_error);
1795 catch(AUD_Exception& e)
1797 PyErr_SetString(AUDError, e.str);
1803 PyDoc_STRVAR(M_aud_Handle_distance_maximum_doc,
1804 "The maximum distance of the source.\n"
1805 "If the listener is further away the source volume will be 0.\n\n"
1806 ".. seealso:: :attr:`Device.distance_model`");
1809 Handle_get_distance_maximum(Handle *self, void* nothing)
1811 Device* dev = (Device*)self->device;
1815 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(dev->device);
1818 return Py_BuildValue("f", device->getDistanceMaximum(self->handle));
1822 PyErr_SetString(AUDError, device_not_3d_error);
1826 catch(AUD_Exception& e)
1828 PyErr_SetString(AUDError, e.str);
1834 Handle_set_distance_maximum(Handle *self, PyObject* args, void* nothing)
1838 if(!PyArg_Parse(args, "f:distance_maximum", &distance))
1841 Device* dev = (Device*)self->device;
1845 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(dev->device);
1848 if(device->setDistanceMaximum(self->handle, distance))
1850 PyErr_SetString(AUDError, "Couldn't set the maximum distance!");
1853 PyErr_SetString(AUDError, device_not_3d_error);
1855 catch(AUD_Exception& e)
1857 PyErr_SetString(AUDError, e.str);
1863 PyDoc_STRVAR(M_aud_Handle_attenuation_doc,
1864 "This factor is used for distance based attenuation of the "
1866 ".. seealso:: :attr:`Device.distance_model`");
1869 Handle_get_attenuation(Handle *self, void* nothing)
1871 Device* dev = (Device*)self->device;
1875 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(dev->device);
1878 return Py_BuildValue("f", device->getAttenuation(self->handle));
1882 PyErr_SetString(AUDError, device_not_3d_error);
1886 catch(AUD_Exception& e)
1888 PyErr_SetString(AUDError, e.str);
1894 Handle_set_attenuation(Handle *self, PyObject* args, void* nothing)
1898 if(!PyArg_Parse(args, "f:attenuation", &factor))
1901 Device* dev = (Device*)self->device;
1905 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(dev->device);
1908 if(device->setAttenuation(self->handle, factor))
1910 PyErr_SetString(AUDError, "Couldn't set the attenuation!");
1913 PyErr_SetString(AUDError, device_not_3d_error);
1915 catch(AUD_Exception& e)
1917 PyErr_SetString(AUDError, e.str);
1923 PyDoc_STRVAR(M_aud_Handle_cone_angle_inner_doc,
1924 "The opening angle of the inner cone of the source. If the cone "
1925 "values of a source are set there are two (audible) cones with "
1926 "the apex at the :attr:`location` of the source and with infinite "
1927 "height, heading in the direction of the source's "
1928 ":attr:`orientation`.\n"
1929 "In the inner cone the volume is normal. Outside the outer cone "
1930 "the volume will be :attr:`cone_volume_outer` and in the area "
1931 "between the volume will be interpolated linearly.");
1934 Handle_get_cone_angle_inner(Handle *self, void* nothing)
1936 Device* dev = (Device*)self->device;
1940 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(dev->device);
1943 return Py_BuildValue("f", device->getConeAngleInner(self->handle));
1947 PyErr_SetString(AUDError, device_not_3d_error);
1951 catch(AUD_Exception& e)
1953 PyErr_SetString(AUDError, e.str);
1959 Handle_set_cone_angle_inner(Handle *self, PyObject* args, void* nothing)
1963 if(!PyArg_Parse(args, "f:cone_angle_inner", &angle))
1966 Device* dev = (Device*)self->device;
1970 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(dev->device);
1973 if(device->setConeAngleInner(self->handle, angle))
1975 PyErr_SetString(AUDError, "Couldn't set the cone inner angle!");
1978 PyErr_SetString(AUDError, device_not_3d_error);
1980 catch(AUD_Exception& e)
1982 PyErr_SetString(AUDError, e.str);
1988 PyDoc_STRVAR(M_aud_Handle_cone_angle_outer_doc,
1989 "The opening angle of the outer cone of the source.\n\n"
1990 ".. seealso:: :attr:`cone_angle_inner`");
1993 Handle_get_cone_angle_outer(Handle *self, void* nothing)
1995 Device* dev = (Device*)self->device;
1999 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(dev->device);
2002 return Py_BuildValue("f", device->getConeAngleOuter(self->handle));
2006 PyErr_SetString(AUDError, device_not_3d_error);
2010 catch(AUD_Exception& e)
2012 PyErr_SetString(AUDError, e.str);
2018 Handle_set_cone_angle_outer(Handle *self, PyObject* args, void* nothing)
2022 if(!PyArg_Parse(args, "f:cone_angle_outer", &angle))
2025 Device* dev = (Device*)self->device;
2029 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(dev->device);
2032 if(device->setConeAngleOuter(self->handle, angle))
2034 PyErr_SetString(AUDError, "Couldn't set the cone outer angle!");
2037 PyErr_SetString(AUDError, device_not_3d_error);
2039 catch(AUD_Exception& e)
2041 PyErr_SetString(AUDError, e.str);
2047 PyDoc_STRVAR(M_aud_Handle_cone_volume_outer_doc,
2048 "The volume outside the outer cone of the source.\n\n"
2049 ".. seealso:: :attr:`cone_angle_inner`");
2052 Handle_get_cone_volume_outer(Handle *self, void* nothing)
2054 Device* dev = (Device*)self->device;
2058 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(dev->device);
2061 return Py_BuildValue("f", device->getConeVolumeOuter(self->handle));
2065 PyErr_SetString(AUDError, device_not_3d_error);
2069 catch(AUD_Exception& e)
2071 PyErr_SetString(AUDError, e.str);
2077 Handle_set_cone_volume_outer(Handle *self, PyObject* args, void* nothing)
2081 if(!PyArg_Parse(args, "f:cone_volume_outer", &volume))
2084 Device* dev = (Device*)self->device;
2088 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(dev->device);
2091 if(device->setConeVolumeOuter(self->handle, volume))
2093 PyErr_SetString(AUDError, "Couldn't set the cone outer volume!");
2096 PyErr_SetString(AUDError, device_not_3d_error);
2098 catch(AUD_Exception& e)
2100 PyErr_SetString(AUDError, e.str);
2106 static PyGetSetDef Handle_properties[] = {
2107 {(char*)"position", (getter)Handle_get_position, (setter)Handle_set_position,
2108 M_aud_Handle_position_doc, NULL },
2109 {(char*)"keep", (getter)Handle_get_keep, (setter)Handle_set_keep,
2110 M_aud_Handle_keep_doc, NULL },
2111 {(char*)"status", (getter)Handle_get_status, NULL,
2112 M_aud_Handle_status_doc, NULL },
2113 {(char*)"volume", (getter)Handle_get_volume, (setter)Handle_set_volume,
2114 M_aud_Handle_volume_doc, NULL },
2115 {(char*)"pitch", (getter)Handle_get_pitch, (setter)Handle_set_pitch,
2116 M_aud_Handle_pitch_doc, NULL },
2117 {(char*)"loop_count", (getter)Handle_get_loop_count, (setter)Handle_set_loop_count,
2118 M_aud_Handle_loop_count_doc, NULL },
2119 {(char*)"location", (getter)Handle_get_location, (setter)Handle_set_location,
2120 M_aud_Handle_location_doc, NULL },
2121 {(char*)"velocity", (getter)Handle_get_velocity, (setter)Handle_set_velocity,
2122 M_aud_Handle_velocity_doc, NULL },
2123 {(char*)"orientation", (getter)Handle_get_orientation, (setter)Handle_set_orientation,
2124 M_aud_Handle_orientation_doc, NULL },
2125 {(char*)"relative", (getter)Handle_get_relative, (setter)Handle_set_relative,
2126 M_aud_Handle_relative_doc, NULL },
2127 {(char*)"volume_minimum", (getter)Handle_get_volume_minimum, (setter)Handle_set_volume_minimum,
2128 M_aud_Handle_volume_minimum_doc, NULL },
2129 {(char*)"volume_maximum", (getter)Handle_get_volume_maximum, (setter)Handle_set_volume_maximum,
2130 M_aud_Handle_volume_maximum_doc, NULL },
2131 {(char*)"distance_reference", (getter)Handle_get_distance_reference, (setter)Handle_set_distance_reference,
2132 M_aud_Handle_distance_reference_doc, NULL },
2133 {(char*)"distance_maximum", (getter)Handle_get_distance_maximum, (setter)Handle_set_distance_maximum,
2134 M_aud_Handle_distance_maximum_doc, NULL },
2135 {(char*)"attenuation", (getter)Handle_get_attenuation, (setter)Handle_set_attenuation,
2136 M_aud_Handle_attenuation_doc, NULL },
2137 {(char*)"cone_angle_inner", (getter)Handle_get_cone_angle_inner, (setter)Handle_set_cone_angle_inner,
2138 M_aud_Handle_cone_angle_inner_doc, NULL },
2139 {(char*)"cone_angle_outer", (getter)Handle_get_cone_angle_outer, (setter)Handle_set_cone_angle_outer,
2140 M_aud_Handle_cone_angle_outer_doc, NULL },
2141 {(char*)"cone_volume_outer", (getter)Handle_get_cone_volume_outer, (setter)Handle_set_cone_volume_outer,
2142 M_aud_Handle_cone_volume_outer_doc, NULL },
2143 {NULL} /* Sentinel */
2146 PyDoc_STRVAR(M_aud_Handle_doc,
2147 "Handle objects are playback handles that can be used to control "
2148 "playback of a sound. If a sound is played back multiple times "
2149 "then there are as many handles.");
2151 static PyTypeObject HandleType = {
2152 PyVarObject_HEAD_INIT(NULL, 0)
2153 "aud.Handle", /* tp_name */
2154 sizeof(Handle), /* tp_basicsize */
2155 0, /* tp_itemsize */
2156 (destructor)Handle_dealloc,/* tp_dealloc */
2160 0, /* tp_reserved */
2162 0, /* tp_as_number */
2163 0, /* tp_as_sequence */
2164 0, /* tp_as_mapping */
2168 0, /* tp_getattro */
2169 0, /* tp_setattro */
2170 0, /* tp_as_buffer */
2171 Py_TPFLAGS_DEFAULT, /* tp_flags */
2172 M_aud_Handle_doc, /* tp_doc */
2173 0, /* tp_traverse */
2175 0, /* tp_richcompare */
2176 0, /* tp_weaklistoffset */
2178 0, /* tp_iternext */
2179 Handle_methods, /* tp_methods */
2181 Handle_properties, /* tp_getset */
2184 0, /* tp_descr_get */
2185 0, /* tp_descr_set */
2186 0, /* tp_dictoffset */
2192 // ========== Device ==================================================
2195 Device_dealloc(Device* self)
2198 delete self->device;
2199 Py_TYPE(self)->tp_free((PyObject*)self);
2203 Device_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2207 static const char *kwlist[] = {"type", "rate", "channels", "format", "buffer_size", "name", NULL};
2209 int rate = AUD_RATE_44100;
2210 int channels = AUD_CHANNELS_STEREO;
2211 int format = AUD_FORMAT_FLOAT32;
2212 int buffersize = AUD_DEFAULT_BUFFER_SIZE;
2213 const char* name = "Audaspace";
2215 if(!PyArg_ParseTupleAndKeywords(args, kwds, "i|iiiis:Device", const_cast<char**>(kwlist),
2216 &device, &rate, &channels, &format, &buffersize, &name))
2219 if(buffersize < 128)
2221 PyErr_SetString(PyExc_ValueError, "buffer_size must be greater than 127!");
2225 self = (Device*)type->tp_alloc(type, 0);
2228 AUD_DeviceSpecs specs;
2229 specs.channels = (AUD_Channels)channels;
2230 specs.format = (AUD_SampleFormat)format;
2231 specs.rate = (AUD_SampleRate)rate;
2233 self->device = NULL;
2239 case AUD_DEVICE_NULL:
2240 self->device = new AUD_NULLDevice();
2242 case AUD_DEVICE_OPENAL:
2244 self->device = new AUD_OpenALDevice(specs, buffersize);
2247 case AUD_DEVICE_SDL:
2249 self->device = new AUD_SDLDevice(specs, buffersize);
2252 case AUD_DEVICE_JACK:
2254 self->device = new AUD_JackDevice(name, specs, buffersize);
2257 case AUD_DEVICE_READ:
2262 catch(AUD_Exception& e)
2265 PyErr_SetString(AUDError, e.str);
2272 PyErr_SetString(AUDError, "Unsupported device type!");
2277 return (PyObject *)self;
2280 PyDoc_STRVAR(M_aud_Device_play_doc,
2281 "play(factory, keep=False)\n\n"
2282 "Plays a factory.\n\n"
2283 ":arg factory: The factory to play.\n"
2284 ":type factory: :class:`Factory`\n"
2285 ":arg keep: See :attr:`Handle.keep`.\n"
2286 ":type keep: bool\n"
2287 ":return: The playback handle with which playback can be "
2288 "controlled with.\n"
2289 ":rtype: :class:`Handle`");
2292 Device_play(Device *self, PyObject *args, PyObject *kwds)
2295 PyObject* keepo = NULL;
2299 static const char *kwlist[] = {"factory", "keep", NULL};
2301 if(!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:play", const_cast<char**>(kwlist), &object, &keepo))
2304 if(!PyObject_TypeCheck(object, &FactoryType))
2306 PyErr_SetString(PyExc_TypeError, "Object is not of type Factory!");
2312 if(!PyBool_Check(keepo))
2314 PyErr_SetString(PyExc_TypeError, "keep is not a boolean!");
2318 keep = keepo == Py_True;
2321 Factory* sound = (Factory*)object;
2324 handle = (Handle*)HandleType.tp_alloc(&HandleType, 0);
2327 handle->device = (PyObject*)self;
2332 handle->handle = self->device->play(sound->factory, keep);
2334 catch(AUD_Exception& e)
2337 PyErr_SetString(AUDError, e.str);
2342 return (PyObject *)handle;
2345 PyDoc_STRVAR(M_aud_Device_lock_doc,
2347 "Locks the device so that it's guaranteed, that no samples are "
2348 "read from the streams until :meth:`unlock` is called.\n"
2349 "This is useful if you want to do start/stop/pause/resume some "
2350 "sounds at the same time.\n\n"
2351 ".. note:: The device has to be unlocked as often as locked to be "
2352 "able to continue playback.\n\n"
2353 ".. warning:: Make sure the time between locking and unlocking is "
2354 "as short as possible to avoid clicks.");
2357 Device_lock(Device *self)
2361 self->device->lock();
2364 catch(AUD_Exception& e)
2366 PyErr_SetString(AUDError, e.str);
2371 PyDoc_STRVAR(M_aud_Device_unlock_doc,
2373 "Unlocks the device after a lock call, see :meth:`lock` for "
2377 Device_unlock(Device *self)
2381 self->device->unlock();
2384 catch(AUD_Exception& e)
2386 PyErr_SetString(AUDError, e.str);
2391 static PyMethodDef Device_methods[] = {
2392 {"play", (PyCFunction)Device_play, METH_VARARGS | METH_KEYWORDS,
2393 M_aud_Device_play_doc
2395 {"lock", (PyCFunction)Device_lock, METH_NOARGS,
2396 M_aud_Device_lock_doc
2398 {"unlock", (PyCFunction)Device_unlock, METH_NOARGS,
2399 M_aud_Device_unlock_doc
2401 {NULL} /* Sentinel */
2404 PyDoc_STRVAR(M_aud_Device_rate_doc,
2405 "The sampling rate of the device in Hz.");
2408 Device_get_rate(Device *self, void* nothing)
2412 AUD_DeviceSpecs specs = self->device->getSpecs();
2413 return Py_BuildValue("i", specs.rate);
2415 catch(AUD_Exception& e)
2417 PyErr_SetString(AUDError, e.str);
2422 PyDoc_STRVAR(M_aud_Device_format_doc,
2423 "The native sample format of the device.");
2426 Device_get_format(Device *self, void* nothing)
2430 AUD_DeviceSpecs specs = self->device->getSpecs();
2431 return Py_BuildValue("i", specs.format);
2433 catch(AUD_Exception& e)
2435 PyErr_SetString(AUDError, e.str);
2440 PyDoc_STRVAR(M_aud_Device_channels_doc,
2441 "The channel count of the device.");
2444 Device_get_channels(Device *self, void* nothing)
2448 AUD_DeviceSpecs specs = self->device->getSpecs();
2449 return Py_BuildValue("i", specs.channels);
2451 catch(AUD_Exception& e)
2453 PyErr_SetString(AUDError, e.str);
2458 PyDoc_STRVAR(M_aud_Device_volume_doc,
2459 "The overall volume of the device.");
2462 Device_get_volume(Device *self, void* nothing)
2466 return Py_BuildValue("f", self->device->getVolume());
2468 catch(AUD_Exception& e)
2470 PyErr_SetString(AUDError, e.str);
2476 Device_set_volume(Device *self, PyObject* args, void* nothing)
2480 if(!PyArg_Parse(args, "f:volume", &volume))
2485 self->device->setVolume(volume);
2488 catch(AUD_Exception& e)
2490 PyErr_SetString(AUDError, e.str);
2495 PyDoc_STRVAR(M_aud_Device_listener_location_doc,
2496 "The listeners's location in 3D space, a 3D tuple of floats.");
2499 Device_get_listener_location(Device *self, void* nothing)
2503 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(self->device);
2506 AUD_Vector3 v = device->getListenerLocation();
2507 return Py_BuildValue("(fff)", v.x(), v.y(), v.z());
2511 PyErr_SetString(AUDError, device_not_3d_error);
2514 catch(AUD_Exception& e)
2516 PyErr_SetString(AUDError, e.str);
2523 Device_set_listener_location(Device *self, PyObject* args, void* nothing)
2527 if(!PyArg_Parse(args, "(fff):listener_location", &x, &y, &z))
2532 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(self->device);
2535 AUD_Vector3 location(x, y, z);
2536 device->setListenerLocation(location);
2540 PyErr_SetString(AUDError, device_not_3d_error);
2542 catch(AUD_Exception& e)
2544 PyErr_SetString(AUDError, e.str);
2550 PyDoc_STRVAR(M_aud_Device_listener_velocity_doc,
2551 "The listener's velocity in 3D space, a 3D tuple of floats.");
2554 Device_get_listener_velocity(Device *self, void* nothing)
2558 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(self->device);
2561 AUD_Vector3 v = device->getListenerVelocity();
2562 return Py_BuildValue("(fff)", v.x(), v.y(), v.z());
2566 PyErr_SetString(AUDError, device_not_3d_error);
2569 catch(AUD_Exception& e)
2571 PyErr_SetString(AUDError, e.str);
2578 Device_set_listener_velocity(Device *self, PyObject* args, void* nothing)
2582 if(!PyArg_Parse(args, "(fff):listener_velocity", &x, &y, &z))
2587 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(self->device);
2590 AUD_Vector3 velocity(x, y, z);
2591 device->setListenerVelocity(velocity);
2595 PyErr_SetString(AUDError, device_not_3d_error);
2597 catch(AUD_Exception& e)
2599 PyErr_SetString(AUDError, e.str);
2605 PyDoc_STRVAR(M_aud_Device_listener_orientation_doc,
2606 "The listener's orientation in 3D space as quaternion, a 4 float tuple.");
2609 Device_get_listener_orientation(Device *self, void* nothing)
2613 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(self->device);
2616 AUD_Quaternion o = device->getListenerOrientation();
2617 return Py_BuildValue("(ffff)", o.w(), o.x(), o.y(), o.z());
2621 PyErr_SetString(AUDError, device_not_3d_error);
2624 catch(AUD_Exception& e)
2626 PyErr_SetString(AUDError, e.str);
2633 Device_set_listener_orientation(Device *self, PyObject* args, void* nothing)
2637 if(!PyArg_Parse(args, "(ffff):listener_orientation", &w, &x, &y, &z))
2642 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(self->device);
2645 AUD_Quaternion orientation(w, x, y, z);
2646 device->setListenerOrientation(orientation);
2650 PyErr_SetString(AUDError, device_not_3d_error);
2652 catch(AUD_Exception& e)
2654 PyErr_SetString(AUDError, e.str);
2660 PyDoc_STRVAR(M_aud_Device_speed_of_sound_doc,
2661 "The speed of sound of the device.\n"
2662 "The speed of sound in air is typically 343 m/s.");
2665 Device_get_speed_of_sound(Device *self, void* nothing)
2669 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(self->device);
2672 return Py_BuildValue("f", device->getSpeedOfSound());
2676 PyErr_SetString(AUDError, device_not_3d_error);
2680 catch(AUD_Exception& e)
2682 PyErr_SetString(AUDError, e.str);
2688 Device_set_speed_of_sound(Device *self, PyObject* args, void* nothing)
2692 if(!PyArg_Parse(args, "f:speed_of_sound", &speed))
2697 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(self->device);
2700 device->setSpeedOfSound(speed);
2704 PyErr_SetString(AUDError, device_not_3d_error);
2706 catch(AUD_Exception& e)
2708 PyErr_SetString(AUDError, e.str);
2714 PyDoc_STRVAR(M_aud_Device_doppler_factor_doc,
2715 "The doppler factor of the device.\n"
2716 "This factor is a scaling factor for the velocity vectors in "
2717 "doppler calculation. So a value bigger than 1 will exaggerate "
2718 "the effect as it raises the velocity.");
2721 Device_get_doppler_factor(Device *self, void* nothing)
2725 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(self->device);
2728 return Py_BuildValue("f", device->getDopplerFactor());
2732 PyErr_SetString(AUDError, device_not_3d_error);
2736 catch(AUD_Exception& e)
2738 PyErr_SetString(AUDError, e.str);
2744 Device_set_doppler_factor(Device *self, PyObject* args, void* nothing)
2748 if(!PyArg_Parse(args, "f:doppler_factor", &factor))
2753 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(self->device);
2756 device->setDopplerFactor(factor);
2760 PyErr_SetString(AUDError, device_not_3d_error);
2762 catch(AUD_Exception& e)
2764 PyErr_SetString(AUDError, e.str);
2770 PyDoc_STRVAR(M_aud_Device_distance_model_doc,
2771 "The distance model of the device.\n\n"
2772 ".. seealso:: http://connect.creativelabs.com/openal/Documentation/OpenAL%201.1%20Specification.htm#_Toc199835864");
2775 Device_get_distance_model(Device *self, void* nothing)
2779 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(self->device);
2782 return Py_BuildValue("i", int(device->getDistanceModel()));
2786 PyErr_SetString(AUDError, device_not_3d_error);
2790 catch(AUD_Exception& e)
2792 PyErr_SetString(AUDError, e.str);
2798 Device_set_distance_model(Device *self, PyObject* args, void* nothing)
2802 if(!PyArg_Parse(args, "i:distance_model", &model))
2807 AUD_I3DDevice* device = dynamic_cast<AUD_I3DDevice*>(self->device);
2810 device->setDistanceModel(AUD_DistanceModel(model));
2814 PyErr_SetString(AUDError, device_not_3d_error);
2816 catch(AUD_Exception& e)
2818 PyErr_SetString(AUDError, e.str);
2824 static PyGetSetDef Device_properties[] = {
2825 {(char*)"rate", (getter)Device_get_rate, NULL,
2826 M_aud_Device_rate_doc, NULL },
2827 {(char*)"format", (getter)Device_get_format, NULL,
2828 M_aud_Device_format_doc, NULL },
2829 {(char*)"channels", (getter)Device_get_channels, NULL,
2830 M_aud_Device_channels_doc, NULL },
2831 {(char*)"volume", (getter)Device_get_volume, (setter)Device_set_volume,
2832 M_aud_Device_volume_doc, NULL },
2833 {(char*)"listener_location", (getter)Device_get_listener_location, (setter)Device_set_listener_location,
2834 M_aud_Device_listener_location_doc, NULL },
2835 {(char*)"listener_velocity", (getter)Device_get_listener_velocity, (setter)Device_set_listener_velocity,
2836 M_aud_Device_listener_velocity_doc, NULL },
2837 {(char*)"listener_orientation", (getter)Device_get_listener_orientation, (setter)Device_set_listener_orientation,
2838 M_aud_Device_listener_orientation_doc, NULL },
2839 {(char*)"speed_of_sound", (getter)Device_get_speed_of_sound, (setter)Device_set_speed_of_sound,
2840 M_aud_Device_speed_of_sound_doc, NULL },
2841 {(char*)"doppler_factor", (getter)Device_get_doppler_factor, (setter)Device_set_doppler_factor,
2842 M_aud_Device_doppler_factor_doc, NULL },
2843 {(char*)"distance_model", (getter)Device_get_distance_model, (setter)Device_set_distance_model,
2844 M_aud_Device_distance_model_doc, NULL },
2845 {NULL} /* Sentinel */
2848 PyDoc_STRVAR(M_aud_Device_doc,
2849 "Device objects represent an audio output backend like OpenAL or "
2850 "SDL, but might also represent a file output or RAM buffer "
2853 static PyTypeObject DeviceType = {
2854 PyVarObject_HEAD_INIT(NULL, 0)
2855 "aud.Device", /* tp_name */
2856 sizeof(Device), /* tp_basicsize */
2857 0, /* tp_itemsize */
2858 (destructor)Device_dealloc,/* tp_dealloc */
2862 0, /* tp_reserved */
2864 0, /* tp_as_number */
2865 0, /* tp_as_sequence */
2866 0, /* tp_as_mapping */
2870 0, /* tp_getattro */
2871 0, /* tp_setattro */
2872 0, /* tp_as_buffer */
2873 Py_TPFLAGS_DEFAULT, /* tp_flags */
2874 M_aud_Device_doc, /* tp_doc */
2875 0, /* tp_traverse */
2877 0, /* tp_richcompare */
2878 0, /* tp_weaklistoffset */
2880 0, /* tp_iternext */
2881 Device_methods, /* tp_methods */
2883 Device_properties, /* tp_getset */
2886 0, /* tp_descr_get */
2887 0, /* tp_descr_set */
2888 0, /* tp_dictoffset */
2891 Device_new, /* tp_new */
2897 return DeviceType.tp_alloc(&DeviceType, 0);
2900 // ====================================================================
2902 PyDoc_STRVAR(M_aud_doc,
2903 "This module provides access to the audaspace audio library.");
2905 static struct PyModuleDef audmodule = {
2906 PyModuleDef_HEAD_INIT,
2907 "aud", /* name of module */
2908 M_aud_doc, /* module documentation */
2909 -1, /* size of per-interpreter state of the module,
2910 or -1 if the module keeps state in global variables. */
2911 NULL, NULL, NULL, NULL, NULL
2919 if(PyType_Ready(&FactoryType) < 0)
2922 if(PyType_Ready(&DeviceType) < 0)
2925 if(PyType_Ready(&HandleType) < 0)
2928 m = PyModule_Create(&audmodule);
2932 Py_INCREF(&FactoryType);
2933 PyModule_AddObject(m, "Factory", (PyObject*)&FactoryType);
2935 Py_INCREF(&DeviceType);
2936 PyModule_AddObject(m, "Device", (PyObject*)&DeviceType);
2938 Py_INCREF(&HandleType);
2939 PyModule_AddObject(m, "Handle", (PyObject*)&HandleType);
2941 AUDError = PyErr_NewException("aud.error", NULL, NULL);
2942 Py_INCREF(AUDError);
2943 PyModule_AddObject(m, "error", AUDError);
2946 PY_MODULE_ADD_CONSTANT(m, AUD_DEVICE_NULL);
2947 PY_MODULE_ADD_CONSTANT(m, AUD_DEVICE_OPENAL);
2948 PY_MODULE_ADD_CONSTANT(m, AUD_DEVICE_SDL);
2949 PY_MODULE_ADD_CONSTANT(m, AUD_DEVICE_JACK);
2950 //PY_MODULE_ADD_CONSTANT(m, AUD_DEVICE_READ);
2952 PY_MODULE_ADD_CONSTANT(m, AUD_FORMAT_FLOAT32);
2953 PY_MODULE_ADD_CONSTANT(m, AUD_FORMAT_FLOAT64);
2954 PY_MODULE_ADD_CONSTANT(m, AUD_FORMAT_INVALID);
2955 PY_MODULE_ADD_CONSTANT(m, AUD_FORMAT_S16);
2956 PY_MODULE_ADD_CONSTANT(m, AUD_FORMAT_S24);
2957 PY_MODULE_ADD_CONSTANT(m, AUD_FORMAT_S32);
2958 PY_MODULE_ADD_CONSTANT(m, AUD_FORMAT_U8);
2960 PY_MODULE_ADD_CONSTANT(m, AUD_STATUS_INVALID);
2961 PY_MODULE_ADD_CONSTANT(m, AUD_STATUS_PAUSED);
2962 PY_MODULE_ADD_CONSTANT(m, AUD_STATUS_PLAYING);
2963 // distance model constants
2964 PY_MODULE_ADD_CONSTANT(m, AUD_DISTANCE_MODEL_EXPONENT);
2965 PY_MODULE_ADD_CONSTANT(m, AUD_DISTANCE_MODEL_EXPONENT_CLAMPED);
2966 PY_MODULE_ADD_CONSTANT(m, AUD_DISTANCE_MODEL_INVERSE);
2967 PY_MODULE_ADD_CONSTANT(m, AUD_DISTANCE_MODEL_INVERSE_CLAMPED);
2968 PY_MODULE_ADD_CONSTANT(m, AUD_DISTANCE_MODEL_LINEAR);
2969 PY_MODULE_ADD_CONSTANT(m, AUD_DISTANCE_MODEL_LINEAR_CLAMPED);
2970 PY_MODULE_ADD_CONSTANT(m, AUD_DISTANCE_MODEL_INVALID);