tmp
[blender.git] / extern / audaspace / bindings / python / PySource.cpp
1 /*******************************************************************************
2 * Copyright 2009-2015 Juan Francisco Crespo Gal├ín
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *   http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 ******************************************************************************/
16
17 #include "PySource.h"
18
19 #include "Exception.h"
20 #include "fx/Source.h"
21
22 #include <memory>
23
24 extern PyObject* AUDError;
25
26 static PyObject *
27 Source_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
28 {
29         SourceP* self = (SourceP*)type->tp_alloc(type, 0);
30
31         if(self != nullptr)
32         {
33                 float azimuth, elevation, distance;
34                 if(!PyArg_ParseTuple(args, "fff:angles", &azimuth, &elevation, &distance))
35                         return nullptr;
36
37                 try
38                 {
39                         self->source = new std::shared_ptr<aud::Source>(new aud::Source(azimuth, elevation, distance));
40                 }
41                 catch(aud::Exception& e)
42                 {
43                         Py_DECREF(self);
44                         PyErr_SetString(AUDError, e.what());
45                         return nullptr;
46                 }
47         }
48
49         return (PyObject *)self;
50 }
51
52 static void
53 Source_dealloc(SourceP* self)
54 {
55         if(self->source)
56                 delete reinterpret_cast<std::shared_ptr<aud::Source>*>(self->source);
57         Py_TYPE(self)->tp_free((PyObject *)self);
58 }
59
60 static PyMethodDef Source_methods[] = {
61         { nullptr }  /* Sentinel */
62 };
63
64 PyDoc_STRVAR(M_aud_Source_azimuth_doc,
65         "The azimuth angle.");
66
67 static int
68 Source_set_azimuth(SourceP* self, PyObject* args, void* nothing)
69 {
70         float azimuth;
71
72         if(!PyArg_Parse(args, "f:azimuth", &azimuth))
73                 return -1;
74
75         try
76         {
77                 (*reinterpret_cast<std::shared_ptr<aud::Source>*>(self->source))->setAzimuth(azimuth);
78                 return 0;
79         }
80         catch(aud::Exception& e)
81         {
82                 PyErr_SetString(AUDError, e.what());
83         }
84
85         return -1;
86 }
87
88 static PyObject *
89 Source_get_azimuth(SourceP* self, void* nothing)
90 {
91         try
92         {
93                 return Py_BuildValue("f", (*reinterpret_cast<std::shared_ptr<aud::Source>*>(self->source))->getAzimuth());
94         }
95         catch(aud::Exception& e)
96         {
97                 PyErr_SetString(AUDError, e.what());
98                 return nullptr;
99         }
100 }
101
102 PyDoc_STRVAR(M_aud_Source_elevation_doc,
103         "The elevation angle.");
104
105 static int
106 Source_set_elevation(SourceP* self, PyObject* args, void* nothing)
107 {
108         float elevation;
109
110         if(!PyArg_Parse(args, "f:elevation", &elevation))
111                 return -1;
112
113         try
114         {
115                 (*reinterpret_cast<std::shared_ptr<aud::Source>*>(self->source))->setElevation(elevation);
116                 return 0;
117         }
118         catch(aud::Exception& e)
119         {
120                 PyErr_SetString(AUDError, e.what());
121         }
122
123         return -1;
124 }
125
126 static PyObject *
127 Source_get_elevation(SourceP* self, void* nothing)
128 {
129         try
130         {
131                 return Py_BuildValue("f", (*reinterpret_cast<std::shared_ptr<aud::Source>*>(self->source))->getElevation());
132         }
133         catch(aud::Exception& e)
134         {
135                 PyErr_SetString(AUDError, e.what());
136                 return nullptr;
137         }
138 }
139
140 PyDoc_STRVAR(M_aud_Source_distance_doc,
141         "The distance value. 0 is min, 1 is max.");
142
143 static int
144 Source_set_distance(SourceP* self, PyObject* args, void* nothing)
145 {
146         float distance;
147
148         if(!PyArg_Parse(args, "f:distance", &distance))
149                 return -1;
150
151         try
152         {
153                 (*reinterpret_cast<std::shared_ptr<aud::Source>*>(self->source))->setDistance(distance);
154                 return 0;
155         }
156         catch(aud::Exception& e)
157         {
158                 PyErr_SetString(AUDError, e.what());
159         }
160
161         return -1;
162 }
163
164 static PyObject *
165 Source_get_distance(SourceP* self, void* nothing)
166 {
167         try
168         {
169                 return Py_BuildValue("f", (*reinterpret_cast<std::shared_ptr<aud::Source>*>(self->source))->getDistance());
170         }
171         catch(aud::Exception& e)
172         {
173                 PyErr_SetString(AUDError, e.what());
174                 return nullptr;
175         }
176 }
177
178 static PyGetSetDef Source_properties[] = {
179         { (char*)"azimuth", (getter)Source_get_azimuth, (setter)Source_set_azimuth,
180         M_aud_Source_azimuth_doc, nullptr },
181         { (char*)"elevation", (getter)Source_get_elevation, (setter)Source_set_elevation,
182         M_aud_Source_elevation_doc, nullptr },
183         { (char*)"distance", (getter)Source_get_distance, (setter)Source_set_distance,
184         M_aud_Source_distance_doc, nullptr },
185         { nullptr }  /* Sentinel */
186 };
187
188 PyDoc_STRVAR(M_aud_Source_doc,
189         "The source object represents the source position of a binaural sound.");
190
191 PyTypeObject SourceType = {
192         PyVarObject_HEAD_INIT(nullptr, 0)
193         "aud.Source",                                                   /* tp_name */
194         sizeof(SourceP),                                                /* tp_basicsize */
195         0,                                                                              /* tp_itemsize */
196         (destructor)Source_dealloc,                             /* tp_dealloc */
197         0,                                                                              /* tp_print */
198         0,                                                                              /* tp_getattr */
199         0,                                                                              /* tp_setattr */
200         0,                                                                              /* tp_reserved */
201         0,                                                                              /* tp_repr */
202         0,                                                                              /* tp_as_number */
203         0,                                                                              /* tp_as_sequence */
204         0,                                                                              /* tp_as_mapping */
205         0,                                                                              /* tp_hash  */
206         0,                                                                              /* tp_call */
207         0,                                                                              /* tp_str */
208         0,                                                                              /* tp_getattro */
209         0,                                                                              /* tp_setattro */
210         0,                                                                              /* tp_as_buffer */
211         Py_TPFLAGS_DEFAULT,                                             /* tp_flags */
212         M_aud_Source_doc,                                               /* tp_doc */
213         0,                                                                              /* tp_traverse */
214         0,                                                                              /* tp_clear */
215         0,                                                                              /* tp_richcompare */
216         0,                                                                              /* tp_weaklistoffset */
217         0,                                                                              /* tp_iter */
218         0,                                                                              /* tp_iternext */
219         Source_methods,                                                 /* tp_methods */
220         0,                                                                              /* tp_members */
221         Source_properties,                                              /* tp_getset */
222         0,                                                                              /* tp_base */
223         0,                                                                              /* tp_dict */
224         0,                                                                              /* tp_descr_get */
225         0,                                                                              /* tp_descr_set */
226         0,                                                                              /* tp_dictoffset */
227         0,                                                                              /* tp_init */
228         0,                                                                              /* tp_alloc */
229         Source_new,                                                             /* tp_new */
230 };
231
232 AUD_API PyObject* Source_empty()
233 {
234         return SourceType.tp_alloc(&SourceType, 0);
235 }
236
237
238 AUD_API SourceP* checkSource(PyObject* source)
239 {
240         if(!PyObject_TypeCheck(source, &SourceType))
241         {
242                 PyErr_SetString(PyExc_TypeError, "Object is not of type Source!");
243                 return nullptr;
244         }
245
246         return (SourceP*)source;
247 }
248
249
250 bool initializeSource()
251 {
252         return PyType_Ready(&SourceType) >= 0;
253 }
254
255
256 void addSourceToModule(PyObject* module)
257 {
258         Py_INCREF(&SourceType);
259         PyModule_AddObject(module, "Source", (PyObject *)&SourceType);
260 }