Audaspace:
[blender-staging.git] / intern / audaspace / intern / AUD_SoftwareDevice.cpp
1 /*
2  * $Id$
3  *
4  * ***** BEGIN LGPL LICENSE BLOCK *****
5  *
6  * Copyright 2009 Jörg Hermann Müller
7  *
8  * This file is part of AudaSpace.
9  *
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.
14  *
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.
19  *
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/>.
22  *
23  * ***** END LGPL LICENSE BLOCK *****
24  */
25
26 #include "AUD_SoftwareDevice.h"
27 #include "AUD_IReader.h"
28 #include "AUD_DefaultMixer.h"
29 #include "AUD_IFactory.h"
30
31 #include <cstring>
32 #include <limits>
33
34 /// Saves the data for playback.
35 struct AUD_SoftwareHandle : AUD_Handle
36 {
37         /// The reader source.
38         AUD_IReader* reader;
39
40         /// Whether to keep the source if end of it is reached.
41         bool keep;
42
43         /// The volume of the source.
44         float volume;
45
46         /// The loop count of the source.
47         int loopcount;
48
49         /// The stop callback.
50         stopCallback stop;
51
52         /// Stop callback data.
53         void* stop_data;
54 };
55
56 typedef std::list<AUD_SoftwareHandle*>::iterator AUD_HandleIterator;
57
58 void AUD_SoftwareDevice::create()
59 {
60         m_playback = false;
61         m_volume = 1.0f;
62         m_mixer = new AUD_DefaultMixer(m_specs);
63
64         pthread_mutexattr_t attr;
65         pthread_mutexattr_init(&attr);
66         pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
67
68         pthread_mutex_init(&m_mutex, &attr);
69
70         pthread_mutexattr_destroy(&attr);
71 }
72
73 void AUD_SoftwareDevice::destroy()
74 {
75         if(m_playback)
76                 playing(m_playback = false);
77
78         delete m_mixer;
79
80         AUD_SoftwareHandle* handle;
81
82         // delete all playing sounds
83         while(!m_playingSounds.empty())
84         {
85                 handle = m_playingSounds.front();
86                 m_playingSounds.pop_front();
87                 delete handle->reader;
88                 delete handle;
89         }
90
91         // delete all paused sounds
92         while(!m_pausedSounds.empty())
93         {
94                 handle = m_pausedSounds.front();
95                 m_pausedSounds.pop_front();
96                 delete handle->reader;
97                 delete handle;
98         }
99
100         pthread_mutex_destroy(&m_mutex);
101 }
102
103 void AUD_SoftwareDevice::mix(data_t* buffer, int length)
104 {
105         lock();
106
107         {
108                 AUD_SoftwareHandle* sound;
109                 int len;
110                 int pos;
111                 sample_t* buf;
112                 std::list<AUD_SoftwareHandle*> stopSounds;
113                 std::list<AUD_Buffer*> tempBufs;
114                 AUD_Buffer* tempbuf;
115                 int samplesize = AUD_SAMPLE_SIZE(m_specs);
116
117                 // for all sounds
118                 AUD_HandleIterator it = m_playingSounds.begin();
119                 while(it != m_playingSounds.end())
120                 {
121                         sound = *it;
122                         // increment the iterator to make sure it's valid,
123                         // in case the sound gets deleted after stopping
124                         ++it;
125
126                         // get the buffer from the source
127                         pos = 0;
128                         len = length;
129                         sound->reader->read(len, buf);
130
131                         // in case of looping
132                         while(pos + len < length && sound->loopcount)
133                         {
134                                 tempbuf = new AUD_Buffer(len * samplesize);
135                                 memcpy(tempbuf->getBuffer(), buf, len * samplesize);
136                                 tempBufs.push_back(tempbuf);
137                                 m_mixer->add(tempbuf->getBuffer(), pos, len, sound->volume);
138
139                                 pos += len;
140
141                                 if(sound->loopcount > 0)
142                                         sound->loopcount--;
143
144                                 sound->reader->seek(0);
145
146                                 len = length - pos;
147                                 sound->reader->read(len, buf);
148
149                                 // prevent endless loop
150                                 if(!len)
151                                         break;
152                         }
153
154                         m_mixer->add(buf, pos, len, sound->volume);
155                         pos += len;
156
157                         // in case the end of the sound is reached
158                         if(pos < length)
159                         {
160                                 if(sound->stop)
161                                         sound->stop(sound->stop_data);
162
163                                 if(sound->keep)
164                                         pause(sound);
165                                 else
166                                         stopSounds.push_back(sound);
167                         }
168                 }
169
170                 // superpose
171                 m_mixer->superpose(buffer, length, m_volume);
172
173                 // cleanup
174                 while(!stopSounds.empty())
175                 {
176                         sound = stopSounds.front();
177                         stopSounds.pop_front();
178                         stop(sound);
179                 }
180
181                 while(!tempBufs.empty())
182                 {
183                         tempbuf = tempBufs.front();
184                         tempBufs.pop_front();
185                         delete tempbuf;
186                 }
187         }
188
189         unlock();
190 }
191
192 bool AUD_SoftwareDevice::isValid(AUD_Handle* handle)
193 {
194         for(AUD_HandleIterator i = m_playingSounds.begin();
195                 i != m_playingSounds.end(); i++)
196                 if(*i == handle)
197                         return true;
198         for(AUD_HandleIterator i = m_pausedSounds.begin();
199                 i != m_pausedSounds.end(); i++)
200                 if(*i == handle)
201                         return true;
202         return false;
203 }
204
205 AUD_DeviceSpecs AUD_SoftwareDevice::getSpecs() const
206 {
207         return m_specs;
208 }
209
210 AUD_Handle* AUD_SoftwareDevice::play(AUD_IFactory* factory, bool keep)
211 {
212         AUD_IReader* reader = factory->createReader();
213
214         // prepare the reader
215         reader = m_mixer->prepare(reader);
216         if(reader == NULL)
217                 return NULL;
218
219         // play sound
220         AUD_SoftwareHandle* sound = new AUD_SoftwareHandle;
221         sound->keep = keep;
222         sound->reader = reader;
223         sound->volume = 1.0f;
224         sound->loopcount = 0;
225         sound->stop = NULL;
226         sound->stop_data = NULL;
227
228         lock();
229         m_playingSounds.push_back(sound);
230
231         if(!m_playback)
232                 playing(m_playback = true);
233         unlock();
234
235         return sound;
236 }
237
238 bool AUD_SoftwareDevice::pause(AUD_Handle* handle)
239 {
240         bool result = false;
241
242         lock();
243
244         // only songs that are played can be paused
245         for(AUD_HandleIterator i = m_playingSounds.begin();
246                 i != m_playingSounds.end(); i++)
247         {
248                 if(*i == handle)
249                 {
250                         m_pausedSounds.push_back(*i);
251                         m_playingSounds.erase(i);
252                         if(m_playingSounds.empty())
253                                 playing(m_playback = false);
254                         result = true;
255                         break;
256                 }
257         }
258
259         unlock();
260
261         return result;
262 }
263
264 bool AUD_SoftwareDevice::resume(AUD_Handle* handle)
265 {
266         bool result = false;
267
268         lock();
269
270         // only songs that are paused can be resumed
271         for(AUD_HandleIterator i = m_pausedSounds.begin();
272                 i != m_pausedSounds.end(); i++)
273         {
274                 if(*i == handle)
275                 {
276                         m_playingSounds.push_back(*i);
277                         m_pausedSounds.erase(i);
278                         if(!m_playback)
279                                 playing(m_playback = true);
280                         result = true;
281                         break;
282                 }
283         }
284
285         unlock();
286
287         return result;
288 }
289
290 bool AUD_SoftwareDevice::stop(AUD_Handle* handle)
291 {
292         bool result = false;
293
294         lock();
295
296         for(AUD_HandleIterator i = m_playingSounds.begin();
297                 i != m_playingSounds.end(); i++)
298         {
299                 if(*i == handle)
300                 {
301                         delete (*i)->reader;
302                         delete *i;
303                         m_playingSounds.erase(i);
304                         if(m_playingSounds.empty())
305                                 playing(m_playback = false);
306                         result = true;
307                         break;
308                 }
309         }
310         if(!result)
311         {
312                 for(AUD_HandleIterator i = m_pausedSounds.begin();
313                         i != m_pausedSounds.end(); i++)
314                 {
315                         if(*i == handle)
316                         {
317                                 delete (*i)->reader;
318                                 delete *i;
319                                 m_pausedSounds.erase(i);
320                                 result = true;
321                                 break;
322                         }
323                 }
324         }
325
326         unlock();
327
328         return result;
329 }
330
331 bool AUD_SoftwareDevice::getKeep(AUD_Handle* handle)
332 {
333         bool result = false;
334
335         lock();
336
337         if(isValid(handle))
338                 result = ((AUD_SoftwareHandle*)handle)->keep;
339
340         unlock();
341
342         return result;
343 }
344
345 bool AUD_SoftwareDevice::setKeep(AUD_Handle* handle, bool keep)
346 {
347         bool result = false;
348
349         lock();
350
351         if(isValid(handle))
352         {
353                 ((AUD_SoftwareHandle*)handle)->keep = keep;
354                 result = true;
355         }
356
357         unlock();
358
359         return result;
360 }
361
362 bool AUD_SoftwareDevice::seek(AUD_Handle* handle, float position)
363 {
364         lock();
365
366         bool result = false;
367
368         if(isValid(handle))
369         {
370                 AUD_IReader* reader = ((AUD_SoftwareHandle*)handle)->reader;
371                 reader->seek((int)(position * reader->getSpecs().rate));
372                 result = true;
373         }
374
375         unlock();
376
377         return result;
378 }
379
380 float AUD_SoftwareDevice::getPosition(AUD_Handle* handle)
381 {
382         lock();
383
384         float position = 0.0f;
385
386         if(isValid(handle))
387         {
388                 AUD_SoftwareHandle* h = (AUD_SoftwareHandle*)handle;
389                 position = h->reader->getPosition() / (float)m_specs.rate;
390         }
391
392         unlock();
393
394         return position;
395 }
396
397 AUD_Status AUD_SoftwareDevice::getStatus(AUD_Handle* handle)
398 {
399         AUD_Status status = AUD_STATUS_INVALID;
400
401         lock();
402
403         for(AUD_HandleIterator i = m_playingSounds.begin();
404                 i != m_playingSounds.end(); i++)
405         {
406                 if(*i == handle)
407                 {
408                         status = AUD_STATUS_PLAYING;
409                         break;
410                 }
411         }
412         if(status == AUD_STATUS_INVALID)
413         {
414                 for(AUD_HandleIterator i = m_pausedSounds.begin();
415                         i != m_pausedSounds.end(); i++)
416                 {
417                         if(*i == handle)
418                         {
419                                 status = AUD_STATUS_PAUSED;
420                                 break;
421                         }
422                 }
423         }
424
425         unlock();
426
427         return status;
428 }
429
430 void AUD_SoftwareDevice::lock()
431 {
432         pthread_mutex_lock(&m_mutex);
433 }
434
435 void AUD_SoftwareDevice::unlock()
436 {
437         pthread_mutex_unlock(&m_mutex);
438 }
439
440 float AUD_SoftwareDevice::getVolume() const
441 {
442         return m_volume;
443 }
444
445 void AUD_SoftwareDevice::setVolume(float volume)
446 {
447         m_volume = volume;
448 }
449
450 float AUD_SoftwareDevice::getVolume(AUD_Handle* handle)
451 {
452         lock();
453         float result = std::numeric_limits<float>::quiet_NaN();
454         if(isValid(handle))
455                 result = ((AUD_SoftwareHandle*)handle)->volume;
456         unlock();
457         return result;
458 }
459
460 bool AUD_SoftwareDevice::setVolume(AUD_Handle* handle, float volume)
461 {
462         lock();
463         bool result = isValid(handle);
464         if(result)
465                 ((AUD_SoftwareHandle*)handle)->volume = volume;
466         unlock();
467         return result;
468 }
469
470 float AUD_SoftwareDevice::getPitch(AUD_Handle* handle)
471 {
472         return std::numeric_limits<float>::quiet_NaN();
473 }
474
475 bool AUD_SoftwareDevice::setPitch(AUD_Handle* handle, float pitch)
476 {
477         return false;
478 }
479
480 int AUD_SoftwareDevice::getLoopCount(AUD_Handle* handle)
481 {
482         lock();
483         int result = 0;
484         if(isValid(handle))
485                 result = ((AUD_SoftwareHandle*)handle)->loopcount;
486         unlock();
487         return result;
488 }
489
490 bool AUD_SoftwareDevice::setLoopCount(AUD_Handle* handle, int count)
491 {
492         lock();
493         bool result = isValid(handle);
494         if(result)
495                 ((AUD_SoftwareHandle*)handle)->loopcount = count;
496         unlock();
497         return result;
498 }
499
500 bool AUD_SoftwareDevice::setStopCallback(AUD_Handle* handle, stopCallback callback, void* data)
501 {
502         lock();
503         bool result = isValid(handle);
504         if(result)
505         {
506                 AUD_SoftwareHandle* h = (AUD_SoftwareHandle*)handle;
507                 h->stop = callback;
508                 h->stop_data = data;
509         }
510         unlock();
511         return result;
512 }