tmp
[blender.git] / extern / audaspace / bindings / C / AUD_Device.cpp
1 /*******************************************************************************
2  * Copyright 2009-2016 Jörg Müller
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 "devices/DeviceManager.h"
18 #include "devices/I3DDevice.h"
19 #include "devices/IDeviceFactory.h"
20 #include "devices/ReadDevice.h"
21 #include "Exception.h"
22
23 #include <cassert>
24
25 using namespace aud;
26
27 #define AUD_CAPI_IMPLEMENTATION
28 #include "AUD_Device.h"
29
30 static inline aud::Specs convCToSpec(AUD_Specs specs)
31 {
32         aud::Specs s;
33         s.channels = static_cast<Channels>(specs.channels);
34         s.rate = static_cast<SampleRate>(specs.rate);
35         return s;
36 }
37
38 static inline aud::DeviceSpecs convCToDSpec(AUD_DeviceSpecs specs)
39 {
40         aud::DeviceSpecs s;
41         s.specs = convCToSpec(specs.specs);
42         s.format = static_cast<SampleFormat>(specs.format);
43         return s;
44 }
45
46 AUD_API AUD_Device* AUD_Device_open(const char* type, AUD_DeviceSpecs specs, int buffersize, const char* name)
47 {
48         DeviceSpecs dspecs = convCToDSpec(specs);
49
50         if(dspecs.channels == CHANNELS_INVALID)
51                 dspecs.channels = CHANNELS_STEREO;
52         if(dspecs.format == FORMAT_INVALID)
53                 dspecs.format = FORMAT_FLOAT32;
54         if(dspecs.rate == RATE_INVALID)
55                 dspecs.rate = RATE_48000;
56         if(buffersize < 128)
57                 buffersize = AUD_DEFAULT_BUFFER_SIZE;
58         if(name == nullptr)
59                 name = "";
60
61         try
62         {
63                 if(!type)
64                 {
65                         auto device = DeviceManager::getDevice();
66                         if(!device)
67                         {
68                                 DeviceManager::openDefaultDevice();
69                                 device = DeviceManager::getDevice();
70                         }
71                         return new AUD_Device(device);
72                 }
73
74                 if(type == std::string("read"))
75                 {
76                         return new AUD_Device(new ReadDevice(dspecs));
77                 }
78
79                 std::shared_ptr<IDeviceFactory> factory;
80                 if(!*type)
81                         factory = DeviceManager::getDefaultDeviceFactory();
82                 else
83                         factory = DeviceManager::getDeviceFactory(type);
84
85                 if(factory)
86                 {
87                         factory->setName(name);
88                         factory->setSpecs(dspecs);
89                         factory->setBufferSize(buffersize);
90                         return new AUD_Device(factory->openDevice());
91                 }
92         }
93         catch(Exception&)
94         {
95         }
96         return nullptr;
97 }
98
99 AUD_API void AUD_Device_lock(AUD_Device* device)
100 {
101         auto dev = device ? *device : DeviceManager::getDevice();
102         dev->lock();
103 }
104
105 AUD_API AUD_Handle* AUD_Device_play(AUD_Device* device, AUD_Sound* sound, int keep)
106 {
107         assert(sound);
108         auto dev = device ? *device : DeviceManager::getDevice();
109
110         try
111         {
112                 AUD_Handle handle = dev->play(*sound, keep);
113                 if(handle.get())
114                 {
115                         return new AUD_Handle(handle);
116                 }
117         }
118         catch(Exception&)
119         {
120         }
121         return nullptr;
122 }
123
124 AUD_API void AUD_Device_stopAll(AUD_Device* device)
125 {
126         auto dev = device ? *device : DeviceManager::getDevice();
127         dev->stopAll();
128 }
129
130 AUD_API void AUD_Device_unlock(AUD_Device* device)
131 {
132         auto dev = device ? *device : DeviceManager::getDevice();
133         dev->unlock();
134 }
135
136 AUD_API AUD_Channels AUD_Device_getChannels(AUD_Device* device)
137 {
138         auto dev = device ? *device : DeviceManager::getDevice();
139         return static_cast<AUD_Channels>(dev->getSpecs().channels);
140 }
141
142 AUD_API AUD_DistanceModel AUD_Device_getDistanceModel(AUD_Device* device)
143 {
144         auto dev = device ? std::dynamic_pointer_cast<I3DDevice>(*device) : DeviceManager::get3DDevice();
145         return static_cast<AUD_DistanceModel>(dev->getDistanceModel());
146 }
147
148 AUD_API void AUD_Device_setDistanceModel(AUD_Device* device, AUD_DistanceModel value)
149 {
150         auto dev = device ? std::dynamic_pointer_cast<I3DDevice>(*device) : DeviceManager::get3DDevice();
151         dev->setDistanceModel(static_cast<DistanceModel>(value));
152 }
153
154 AUD_API float AUD_Device_getDopplerFactor(AUD_Device* device)
155 {
156         auto dev = device ? std::dynamic_pointer_cast<I3DDevice>(*device) : DeviceManager::get3DDevice();
157         return dev->getDopplerFactor();
158 }
159
160 AUD_API void AUD_Device_setDopplerFactor(AUD_Device* device, float value)
161 {
162         auto dev = device ? std::dynamic_pointer_cast<I3DDevice>(*device) : DeviceManager::get3DDevice();
163         dev->setDopplerFactor(value);
164 }
165
166 AUD_API AUD_SampleFormat AUD_Device_getFormat(AUD_Device* device)
167 {
168         auto dev = device ? *device : DeviceManager::getDevice();
169         return static_cast<AUD_SampleFormat>(dev->getSpecs().format);
170 }
171
172 AUD_API void AUD_Device_getListenerLocation(AUD_Device* device, float value[3])
173 {
174         auto dev = device ? std::dynamic_pointer_cast<I3DDevice>(*device) : DeviceManager::get3DDevice();
175         Vector3 v = dev->getListenerLocation();
176         value[0] = v.x();
177         value[1] = v.y();
178         value[2] = v.z();
179 }
180
181 AUD_API void AUD_Device_setListenerLocation(AUD_Device* device, const float value[3])
182 {
183         auto dev = device ? std::dynamic_pointer_cast<I3DDevice>(*device) : DeviceManager::get3DDevice();
184         Vector3 v(value[0], value[1], value[2]);
185         dev->setListenerLocation(v);
186 }
187
188 AUD_API void AUD_Device_getListenerOrientation(AUD_Device* device, float value[4])
189 {
190         auto dev = device ? std::dynamic_pointer_cast<I3DDevice>(*device) : DeviceManager::get3DDevice();
191         Quaternion v = dev->getListenerOrientation();
192         value[0] = v.x();
193         value[1] = v.y();
194         value[2] = v.z();
195         value[3] = v.w();
196 }
197
198 AUD_API void AUD_Device_setListenerOrientation(AUD_Device* device, const float value[4])
199 {
200         auto dev = device ? std::dynamic_pointer_cast<I3DDevice>(*device) : DeviceManager::get3DDevice();
201         Quaternion v(value[3], value[0], value[1], value[2]);
202         dev->setListenerOrientation(v);
203 }
204
205 AUD_API void AUD_Device_getListenerVelocity(AUD_Device* device, float value[3])
206 {
207         auto dev = device ? std::dynamic_pointer_cast<I3DDevice>(*device) : DeviceManager::get3DDevice();
208         Vector3 v = dev->getListenerVelocity();
209         value[0] = v.x();
210         value[1] = v.y();
211         value[2] = v.z();
212 }
213
214 AUD_API void AUD_Device_setListenerVelocity(AUD_Device* device, const float value[3])
215 {
216         auto dev = device ? std::dynamic_pointer_cast<I3DDevice>(*device) : DeviceManager::get3DDevice();
217         Vector3 v(value[0], value[1], value[2]);
218         dev->setListenerVelocity(v);
219 }
220
221 AUD_API double AUD_Device_getRate(AUD_Device* device)
222 {
223         auto dev = device ? *device : DeviceManager::getDevice();
224         return dev->getSpecs().rate;
225 }
226
227 AUD_API float AUD_Device_getSpeedOfSound(AUD_Device* device)
228 {
229         auto dev = device ? std::dynamic_pointer_cast<I3DDevice>(*device) : DeviceManager::get3DDevice();
230         return dev->getSpeedOfSound();
231 }
232
233 AUD_API void AUD_Device_setSpeedOfSound(AUD_Device* device, float value)
234 {
235         auto dev = device ? std::dynamic_pointer_cast<I3DDevice>(*device) : DeviceManager::get3DDevice();
236         dev->setSpeedOfSound(value);
237 }
238
239 AUD_API float AUD_Device_getVolume(AUD_Device* device)
240 {
241         auto dev = device ? *device : DeviceManager::getDevice();
242         return dev->getVolume();
243 }
244
245 AUD_API void AUD_Device_setVolume(AUD_Device* device, float value)
246 {
247         auto dev = device ? *device : DeviceManager::getDevice();
248         dev->setVolume(value);
249 }
250
251 AUD_API int AUD_Device_read(AUD_Device* device, unsigned char* buffer, int length)
252 {
253         assert(device);
254         assert(buffer);
255
256         auto readDevice = std::dynamic_pointer_cast<ReadDevice>(*device);
257         if(!readDevice)
258                 return false;
259
260         try
261         {
262                 return readDevice->read(buffer, length);
263         }
264         catch(Exception&)
265         {
266                 return false;
267         }
268 }
269
270 AUD_API void AUD_Device_free(AUD_Device* device)
271 {
272         assert(device);
273
274         try
275         {
276                 delete device;
277         }
278         catch(Exception&)
279         {
280         }
281 }
282
283 AUD_API AUD_Device* AUD_Device_getCurrent()
284 {
285         auto device = DeviceManager::getDevice();
286
287         if(!device)
288                 return nullptr;
289
290         return new AUD_Device(device);
291 }
292
293 AUD_API void AUD_seekSynchronizer(AUD_Handle* handle, float time)
294 {
295         auto synchronizer = DeviceManager::getDevice()->getSynchronizer();
296         if(synchronizer)
297                 synchronizer->seek(*reinterpret_cast<std::shared_ptr<IHandle>*>(handle), time);
298 }
299
300 AUD_API float AUD_getSynchronizerPosition(AUD_Handle* handle)
301 {
302         auto synchronizer = DeviceManager::getDevice()->getSynchronizer();
303         if(synchronizer)
304                 return synchronizer->getPosition(*reinterpret_cast<std::shared_ptr<IHandle>*>(handle));
305         return (*reinterpret_cast<std::shared_ptr<IHandle>*>(handle))->getPosition();
306 }
307
308 AUD_API void AUD_playSynchronizer()
309 {
310         auto synchronizer = DeviceManager::getDevice()->getSynchronizer();
311         if(synchronizer)
312                 synchronizer->play();
313 }
314
315 AUD_API void AUD_stopSynchronizer()
316 {
317         auto synchronizer = DeviceManager::getDevice()->getSynchronizer();
318         if(synchronizer)
319                 synchronizer->stop();
320 }
321
322 AUD_API void AUD_setSynchronizerCallback(AUD_syncFunction function, void* data)
323 {
324         auto synchronizer = DeviceManager::getDevice()->getSynchronizer();
325         if(synchronizer)
326                 synchronizer->setSyncCallback(function, data);
327 }
328
329 AUD_API int AUD_isSynchronizerPlaying()
330 {
331         auto synchronizer = DeviceManager::getDevice()->getSynchronizer();
332         if(synchronizer)
333                 return synchronizer->isPlaying();
334         return false;
335 }
336