Fix: setting an audio callback before audio device initialization.
[blender.git] / source / blender / blenkernel / intern / sound.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/sound.c
29  *  \ingroup bke
30  */
31
32 #include <string.h>
33 #include <stdlib.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_blenlib.h"
38 #include "BLI_math.h"
39 #include "BLI_threads.h"
40
41 #include "DNA_anim_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_scene_types.h"
44 #include "DNA_sequence_types.h"
45 #include "DNA_packedFile_types.h"
46 #include "DNA_screen_types.h"
47 #include "DNA_sound_types.h"
48 #include "DNA_speaker_types.h"
49
50 #ifdef WITH_AUDASPACE
51 #  include AUD_SOUND_H
52 #  include AUD_SEQUENCE_H
53 #  include AUD_HANDLE_H
54 #  include AUD_SPECIAL_H
55 #  ifdef WITH_SYSTEM_AUDASPACE
56 #    include "../../../intern/audaspace/intern/AUD_Set.h"
57 #  endif
58 #endif
59
60 #include "BKE_global.h"
61 #include "BKE_main.h"
62 #include "BKE_sound.h"
63 #include "BKE_library.h"
64 #include "BKE_packedFile.h"
65 #include "BKE_sequencer.h"
66 #include "BKE_scene.h"
67
68 #ifdef WITH_AUDASPACE
69 /* evil globals ;-) */
70 static int sound_cfra;
71 static char **audio_device_names = NULL;
72 #endif
73
74 bSound *BKE_sound_new_file(struct Main *bmain, const char *filepath)
75 {
76         bSound *sound;
77         const char *path;
78         char str[FILE_MAX];
79
80         BLI_strncpy(str, filepath, sizeof(str));
81
82         path = /*bmain ? bmain->name :*/ G.main->name;
83
84         BLI_path_abs(str, path);
85
86         sound = BKE_libblock_alloc(bmain, ID_SO, BLI_path_basename(filepath));
87         BLI_strncpy(sound->name, filepath, FILE_MAX);
88         /* sound->type = SOUND_TYPE_FILE; */ /* XXX unused currently */
89
90         BKE_sound_load(bmain, sound);
91
92         return sound;
93 }
94
95 bSound *BKE_sound_new_file_exists_ex(struct Main *bmain, const char *filepath, bool *r_exists)
96 {
97         bSound *sound;
98         char str[FILE_MAX], strtest[FILE_MAX];
99
100         BLI_strncpy(str, filepath, sizeof(str));
101         BLI_path_abs(str, bmain->name);
102
103         /* first search an identical filepath */
104         for (sound = bmain->sound.first; sound; sound = sound->id.next) {
105                 BLI_strncpy(strtest, sound->name, sizeof(sound->name));
106                 BLI_path_abs(strtest, ID_BLEND_PATH(bmain, &sound->id));
107
108                 if (BLI_path_cmp(strtest, str) == 0) {
109                         id_us_plus(&sound->id);  /* officially should not, it doesn't link here! */
110                         if (r_exists)
111                                 *r_exists = true;
112                         return sound;
113                 }
114         }
115
116         if (r_exists)
117                 *r_exists = false;
118         return BKE_sound_new_file(bmain, filepath);
119 }
120
121 bSound *BKE_sound_new_file_exists(struct Main *bmain, const char *filepath)
122 {
123         return BKE_sound_new_file_exists_ex(bmain, filepath, NULL);
124 }
125
126 /** Free (or release) any data used by this sound (does not free the sound itself). */
127 void BKE_sound_free(bSound *sound)
128 {
129         /* No animdata here. */
130
131         if (sound->packedfile) {
132                 freePackedFile(sound->packedfile);
133                 sound->packedfile = NULL;
134         }
135
136 #ifdef WITH_AUDASPACE
137         if (sound->handle) {
138                 AUD_Sound_free(sound->handle);
139                 sound->handle = NULL;
140                 sound->playback_handle = NULL;
141         }
142
143         if (sound->cache) {
144                 AUD_Sound_free(sound->cache);
145                 sound->cache = NULL;
146         }
147
148         BKE_sound_free_waveform(sound);
149         
150         if (sound->spinlock) {
151                 BLI_spin_end(sound->spinlock);
152                 MEM_freeN(sound->spinlock);
153                 sound->spinlock = NULL;
154         }       
155 #endif  /* WITH_AUDASPACE */
156 }
157
158 void BKE_sound_make_local(Main *bmain, bSound *sound, const bool lib_local)
159 {
160         BKE_id_make_local_generic(bmain, &sound->id, true, lib_local);
161 }
162
163 #ifdef WITH_AUDASPACE
164
165 static const char *force_device = NULL;
166
167 #ifdef WITH_JACK
168 static void sound_sync_callback(void *data, int mode, float time)
169 {
170         struct Main *bmain = (struct Main *)data;
171         struct Scene *scene;
172
173         scene = bmain->scene.first;
174         while (scene) {
175                 if (scene->audio.flag & AUDIO_SYNC) {
176                         if (mode)
177                                 BKE_sound_play_scene(scene);
178                         else
179                                 BKE_sound_stop_scene(scene);
180                         if (scene->playback_handle)
181                                 AUD_Handle_setPosition(scene->playback_handle, time);
182                 }
183                 scene = scene->id.next;
184         }
185 }
186 #endif
187
188 void BKE_sound_force_device(const char *device)
189 {
190         force_device = device;
191 }
192
193 void BKE_sound_init_once(void)
194 {
195         AUD_initOnce();
196         atexit(BKE_sound_exit_once);
197 }
198
199 static AUD_Device *sound_device;
200
201 void *BKE_sound_get_device(void)
202 {
203         return sound_device;
204 }
205
206 void BKE_sound_init(struct Main *bmain)
207 {
208         AUD_DeviceSpecs specs;
209         int device, buffersize;
210         const char *device_name;
211
212         device = U.audiodevice;
213         buffersize = U.mixbufsize;
214         specs.channels = U.audiochannels;
215         specs.format = U.audioformat;
216         specs.rate = U.audiorate;
217
218         if (force_device == NULL) {
219                 int i;
220                 char **names = BKE_sound_get_device_names();
221                 device_name = names[0];
222
223                 /* make sure device is within the bounds of the array */
224                 for (i = 0; names[i]; i++) {
225                         if (i == device) {
226                                 device_name = names[i];
227                         }
228                 }
229         }
230         else
231                 device_name = force_device;
232
233         if (buffersize < 128)
234                 buffersize = 1024;
235
236         if (specs.rate < AUD_RATE_8000)
237                 specs.rate = AUD_RATE_48000;
238
239         if (specs.format <= AUD_FORMAT_INVALID)
240                 specs.format = AUD_FORMAT_S16;
241
242         if (specs.channels <= AUD_CHANNELS_INVALID)
243                 specs.channels = AUD_CHANNELS_STEREO;
244
245         if (!(sound_device = AUD_init(device_name, specs, buffersize, "Blender")))
246                 sound_device = AUD_init("Null", specs, buffersize, "Blender");
247
248         BKE_sound_init_main(bmain);
249 }
250
251 void BKE_sound_init_main(struct Main *bmain)
252 {
253 #ifdef WITH_JACK
254         if (sound_device)
255                 AUD_setSynchronizerCallback(sound_sync_callback, bmain);
256 #else
257         (void)bmain; /* unused */
258 #endif
259 }
260
261 void BKE_sound_exit(void)
262 {
263         AUD_exit(sound_device);
264         sound_device = NULL;
265 }
266
267 void BKE_sound_exit_once(void)
268 {
269         AUD_exit(sound_device);
270         sound_device = NULL;
271         AUD_exitOnce();
272
273 #ifdef WITH_SYSTEM_AUDASPACE
274         if (audio_device_names != NULL) {
275                 int i;
276                 for (i = 0; audio_device_names[i]; i++) {
277                         free(audio_device_names[i]);
278                 }
279                 free(audio_device_names);
280                 audio_device_names = NULL;
281         }
282 #endif
283 }
284
285 /* XXX unused currently */
286 #if 0
287 bSound *BKE_sound_new_buffer(struct Main *bmain, bSound *source)
288 {
289         bSound *sound = NULL;
290
291         char name[MAX_ID_NAME + 5];
292         strcpy(name, "buf_");
293         strcpy(name + 4, source->id.name);
294
295         sound = BKE_libblock_alloc(bmain, ID_SO, name);
296
297         sound->child_sound = source;
298         sound->type = SOUND_TYPE_BUFFER;
299
300         sound_load(bmain, sound);
301
302         return sound;
303 }
304
305 bSound *BKE_sound_new_limiter(struct Main *bmain, bSound *source, float start, float end)
306 {
307         bSound *sound = NULL;
308
309         char name[MAX_ID_NAME + 5];
310         strcpy(name, "lim_");
311         strcpy(name + 4, source->id.name);
312
313         sound = BKE_libblock_alloc(bmain, ID_SO, name);
314
315         sound->child_sound = source;
316         sound->start = start;
317         sound->end = end;
318         sound->type = SOUND_TYPE_LIMITER;
319
320         sound_load(bmain, sound);
321
322         return sound;
323 }
324 #endif
325
326 void BKE_sound_cache(bSound *sound)
327 {
328         sound->flags |= SOUND_FLAGS_CACHING;
329         if (sound->cache)
330                 AUD_Sound_free(sound->cache);
331
332         sound->cache = AUD_Sound_cache(sound->handle);
333         if (sound->cache)
334                 sound->playback_handle = sound->cache;
335         else
336                 sound->playback_handle = sound->handle;
337 }
338
339 void BKE_sound_delete_cache(bSound *sound)
340 {
341         sound->flags &= ~SOUND_FLAGS_CACHING;
342         if (sound->cache) {
343                 AUD_Sound_free(sound->cache);
344                 sound->cache = NULL;
345                 sound->playback_handle = sound->handle;
346         }
347 }
348
349 void BKE_sound_load(struct Main *bmain, bSound *sound)
350 {
351         if (sound) {
352                 if (sound->cache) {
353                         AUD_Sound_free(sound->cache);
354                         sound->cache = NULL;
355                 }
356
357                 if (sound->handle) {
358                         AUD_Sound_free(sound->handle);
359                         sound->handle = NULL;
360                         sound->playback_handle = NULL;
361                 }
362
363                 BKE_sound_free_waveform(sound);
364
365 /* XXX unused currently */
366 #if 0
367                 switch (sound->type)
368                 {
369                         case SOUND_TYPE_FILE:
370 #endif
371                 {
372                         char fullpath[FILE_MAX];
373
374                         /* load sound */
375                         PackedFile *pf = sound->packedfile;
376
377                         /* don't modify soundact->sound->name, only change a copy */
378                         BLI_strncpy(fullpath, sound->name, sizeof(fullpath));
379                         BLI_path_abs(fullpath, ID_BLEND_PATH(bmain, &sound->id));
380
381                         /* but we need a packed file then */
382                         if (pf)
383                                 sound->handle = AUD_Sound_bufferFile((unsigned char *) pf->data, pf->size);
384                         /* or else load it from disk */
385                         else
386                                 sound->handle = AUD_Sound_file(fullpath);
387                 }
388 /* XXX unused currently */
389 #if 0
390                         break;
391                 }
392                 case SOUND_TYPE_BUFFER:
393                         if (sound->child_sound && sound->child_sound->handle)
394                                 sound->handle = AUD_bufferSound(sound->child_sound->handle);
395                         break;
396                 case SOUND_TYPE_LIMITER:
397                         if (sound->child_sound && sound->child_sound->handle)
398                                 sound->handle = AUD_limitSound(sound->child_sound, sound->start, sound->end);
399                         break;
400         }
401 #endif
402                 if (sound->flags & SOUND_FLAGS_MONO) {
403                         void *handle = AUD_Sound_rechannel(sound->handle, AUD_CHANNELS_MONO);
404                         AUD_Sound_free(sound->handle);
405                         sound->handle = handle;
406                 }
407
408                 if (sound->flags & SOUND_FLAGS_CACHING) {
409                         sound->cache = AUD_Sound_cache(sound->handle);
410                 }
411
412                 if (sound->cache)
413                         sound->playback_handle = sound->cache;
414                 else
415                         sound->playback_handle = sound->handle;
416
417                 BKE_sound_update_sequencer(bmain, sound);
418         }
419 }
420
421 AUD_Device *BKE_sound_mixdown(struct Scene *scene, AUD_DeviceSpecs specs, int start, float volume)
422 {
423         return AUD_openMixdownDevice(specs, scene->sound_scene, volume, start / FPS);
424 }
425
426 void BKE_sound_create_scene(struct Scene *scene)
427 {
428         /* should be done in version patch, but this gets called before */
429         if (scene->r.frs_sec_base == 0)
430                 scene->r.frs_sec_base = 1;
431
432         scene->sound_scene = AUD_Sequence_create(FPS, scene->audio.flag & AUDIO_MUTE);
433         AUD_Sequence_setSpeedOfSound(scene->sound_scene, scene->audio.speed_of_sound);
434         AUD_Sequence_setDopplerFactor(scene->sound_scene, scene->audio.doppler_factor);
435         AUD_Sequence_setDistanceModel(scene->sound_scene, scene->audio.distance_model);
436         scene->playback_handle = NULL;
437         scene->sound_scrub_handle = NULL;
438         scene->speaker_handles = NULL;
439 }
440
441 void BKE_sound_destroy_scene(struct Scene *scene)
442 {
443         if (scene->playback_handle)
444                 AUD_Handle_stop(scene->playback_handle);
445         if (scene->sound_scrub_handle)
446                 AUD_Handle_stop(scene->sound_scrub_handle);
447         if (scene->sound_scene)
448                 AUD_Sequence_free(scene->sound_scene);
449         if (scene->speaker_handles)
450                 AUD_destroySet(scene->speaker_handles);
451 }
452
453 void BKE_sound_mute_scene(struct Scene *scene, int muted)
454 {
455         if (scene->sound_scene)
456                 AUD_Sequence_setMuted(scene->sound_scene, muted);
457 }
458
459 void BKE_sound_update_fps(struct Scene *scene)
460 {
461         if (scene->sound_scene)
462                 AUD_Sequence_setFPS(scene->sound_scene, FPS);
463
464         BKE_sequencer_refresh_sound_length(scene);
465 }
466
467 void BKE_sound_update_scene_listener(struct Scene *scene)
468 {
469         AUD_Sequence_setSpeedOfSound(scene->sound_scene, scene->audio.speed_of_sound);
470         AUD_Sequence_setDopplerFactor(scene->sound_scene, scene->audio.doppler_factor);
471         AUD_Sequence_setDistanceModel(scene->sound_scene, scene->audio.distance_model);
472 }
473
474 void *BKE_sound_scene_add_scene_sound(struct Scene *scene, struct Sequence *sequence,
475                                   int startframe, int endframe, int frameskip)
476 {
477         if (scene != sequence->scene) {
478                 const double fps = FPS;
479                 return AUD_Sequence_add(scene->sound_scene, sequence->scene->sound_scene,
480                                        startframe / fps, endframe / fps, frameskip / fps);
481         }
482         return NULL;
483 }
484
485 void *BKE_sound_scene_add_scene_sound_defaults(struct Scene *scene, struct Sequence *sequence)
486 {
487         return BKE_sound_scene_add_scene_sound(scene, sequence,
488                                            sequence->startdisp, sequence->enddisp,
489                                            sequence->startofs + sequence->anim_startofs);
490 }
491
492 void *BKE_sound_add_scene_sound(struct Scene *scene, struct Sequence *sequence, int startframe, int endframe, int frameskip)
493 {
494         const double fps = FPS;
495         void *handle = AUD_Sequence_add(scene->sound_scene, sequence->sound->playback_handle,
496                                        startframe / fps, endframe / fps, frameskip / fps);
497         AUD_SequenceEntry_setMuted(handle, (sequence->flag & SEQ_MUTE) != 0);
498         AUD_SequenceEntry_setAnimationData(handle, AUD_AP_VOLUME, CFRA, &sequence->volume, 0);
499         AUD_SequenceEntry_setAnimationData(handle, AUD_AP_PITCH, CFRA, &sequence->pitch, 0);
500         AUD_SequenceEntry_setAnimationData(handle, AUD_AP_PANNING, CFRA, &sequence->pan, 0);
501         return handle;
502 }
503
504 void *BKE_sound_add_scene_sound_defaults(struct Scene *scene, struct Sequence *sequence)
505 {
506         return BKE_sound_add_scene_sound(scene, sequence,
507                                      sequence->startdisp, sequence->enddisp,
508                                      sequence->startofs + sequence->anim_startofs);
509 }
510
511 void BKE_sound_remove_scene_sound(struct Scene *scene, void *handle)
512 {
513         AUD_Sequence_remove(scene->sound_scene, handle);
514 }
515
516 void BKE_sound_mute_scene_sound(void *handle, char mute)
517 {
518         AUD_SequenceEntry_setMuted(handle, mute);
519 }
520
521 void BKE_sound_move_scene_sound(struct Scene *scene, void *handle, int startframe, int endframe, int frameskip)
522 {
523         const double fps = FPS;
524         AUD_SequenceEntry_move(handle, startframe / fps, endframe / fps, frameskip / fps);
525 }
526
527 void BKE_sound_move_scene_sound_defaults(struct Scene *scene, struct Sequence *sequence)
528 {
529         if (sequence->scene_sound) {
530                 BKE_sound_move_scene_sound(scene, sequence->scene_sound,
531                                        sequence->startdisp, sequence->enddisp,
532                                        sequence->startofs + sequence->anim_startofs);
533         }
534 }
535
536 void BKE_sound_update_scene_sound(void *handle, bSound *sound)
537 {
538         AUD_SequenceEntry_setSound(handle, sound->playback_handle);
539 }
540
541 void BKE_sound_set_cfra(int cfra)
542 {
543         sound_cfra = cfra;
544 }
545
546 void BKE_sound_set_scene_volume(struct Scene *scene, float volume)
547 {
548         AUD_Sequence_setAnimationData(scene->sound_scene, AUD_AP_VOLUME, CFRA, &volume,
549                                  (scene->audio.flag & AUDIO_VOLUME_ANIMATED) != 0);
550 }
551
552 void BKE_sound_set_scene_sound_volume(void *handle, float volume, char animated)
553 {
554         AUD_SequenceEntry_setAnimationData(handle, AUD_AP_VOLUME, sound_cfra, &volume, animated);
555 }
556
557 void BKE_sound_set_scene_sound_pitch(void *handle, float pitch, char animated)
558 {
559         AUD_SequenceEntry_setAnimationData(handle, AUD_AP_PITCH, sound_cfra, &pitch, animated);
560 }
561
562 void BKE_sound_set_scene_sound_pan(void *handle, float pan, char animated)
563 {
564         printf("%s\n", __func__);
565         AUD_SequenceEntry_setAnimationData(handle, AUD_AP_PANNING, sound_cfra, &pan, animated);
566 }
567
568 void BKE_sound_update_sequencer(struct Main *main, bSound *sound)
569 {
570         struct Scene *scene;
571
572         for (scene = main->scene.first; scene; scene = scene->id.next) {
573                 BKE_sequencer_update_sound(scene, sound);
574         }
575 }
576
577 static void sound_start_play_scene(struct Scene *scene)
578 {
579         AUD_Specs specs;
580
581         if (scene->playback_handle)
582                 AUD_Handle_stop(scene->playback_handle);
583
584         specs.channels = AUD_Device_getChannels(sound_device);
585         specs.rate = AUD_Device_getRate(sound_device);
586
587         AUD_Sequence_setSpecs(scene->sound_scene, specs);
588
589         if ((scene->playback_handle = AUD_Device_play(sound_device, scene->sound_scene, 1)))
590                 AUD_Handle_setLoopCount(scene->playback_handle, -1);
591 }
592
593 void BKE_sound_play_scene(struct Scene *scene)
594 {
595         AUD_Status status;
596         const float cur_time = (float)((double)CFRA / FPS);
597
598         AUD_Device_lock(sound_device);
599
600         status = scene->playback_handle ? AUD_Handle_getStatus(scene->playback_handle) : AUD_STATUS_INVALID;
601
602         if (status == AUD_STATUS_INVALID) {
603                 sound_start_play_scene(scene);
604
605                 if (!scene->playback_handle) {
606                         AUD_Device_unlock(sound_device);
607                         return;
608                 }
609         }
610
611         if (status != AUD_STATUS_PLAYING) {
612                 AUD_Handle_setPosition(scene->playback_handle, cur_time);
613                 AUD_Handle_resume(scene->playback_handle);
614         }
615
616         if (scene->audio.flag & AUDIO_SYNC)
617                 AUD_playSynchronizer();
618
619         AUD_Device_unlock(sound_device);
620 }
621
622 void BKE_sound_stop_scene(struct Scene *scene)
623 {
624         if (scene->playback_handle) {
625                 AUD_Handle_pause(scene->playback_handle);
626
627                 if (scene->audio.flag & AUDIO_SYNC)
628                         AUD_stopSynchronizer();
629         }
630 }
631
632 void BKE_sound_seek_scene(struct Main *bmain, struct Scene *scene)
633 {
634         AUD_Status status;
635         bScreen *screen;
636         int animation_playing;
637
638         const float one_frame = (float)(1.0 / FPS);
639         const float cur_time = (float)((double)CFRA / FPS);
640
641         AUD_Device_lock(sound_device);
642
643         status = scene->playback_handle ? AUD_Handle_getStatus(scene->playback_handle) : AUD_STATUS_INVALID;
644
645         if (status == AUD_STATUS_INVALID) {
646                 sound_start_play_scene(scene);
647
648                 if (!scene->playback_handle) {
649                         AUD_Device_unlock(sound_device);
650                         return;
651                 }
652
653                 AUD_Handle_pause(scene->playback_handle);
654         }
655
656         animation_playing = 0;
657         for (screen = bmain->screen.first; screen; screen = screen->id.next) {
658                 if (screen->animtimer) {
659                         animation_playing = 1;
660                         break;
661                 }
662         }
663
664         if (scene->audio.flag & AUDIO_SCRUB && !animation_playing) {
665                 AUD_Handle_setPosition(scene->playback_handle, cur_time);
666                 if (scene->audio.flag & AUDIO_SYNC) {
667                         AUD_seekSynchronizer(scene->playback_handle, cur_time);
668                 }
669                 AUD_Handle_resume(scene->playback_handle);
670                 if (scene->sound_scrub_handle && AUD_Handle_getStatus(scene->sound_scrub_handle) != AUD_STATUS_INVALID) {
671                         AUD_Handle_setPosition(scene->sound_scrub_handle, 0);
672                 }
673                 else {
674                         if (scene->sound_scrub_handle) {
675                                 AUD_Handle_stop(scene->sound_scrub_handle);
676                         }
677                         scene->sound_scrub_handle = AUD_pauseAfter(scene->playback_handle, one_frame);
678                 }
679         }
680         else {
681                 if (scene->audio.flag & AUDIO_SYNC) {
682                         AUD_seekSynchronizer(scene->playback_handle, cur_time);
683                 }
684                 else {
685                         if (status == AUD_STATUS_PLAYING) {
686                                 AUD_Handle_setPosition(scene->playback_handle, cur_time);
687                         }
688                 }
689         }
690
691         AUD_Device_unlock(sound_device);
692 }
693
694 float BKE_sound_sync_scene(struct Scene *scene)
695 {
696         if (scene->playback_handle) {
697                 if (scene->audio.flag & AUDIO_SYNC)
698                         return AUD_getSynchronizerPosition(scene->playback_handle);
699                 else
700                         return AUD_Handle_getPosition(scene->playback_handle);
701         }
702         return NAN_FLT;
703 }
704
705 int BKE_sound_scene_playing(struct Scene *scene)
706 {
707         if (scene->audio.flag & AUDIO_SYNC)
708                 return AUD_isSynchronizerPlaying();
709         else
710                 return -1;
711 }
712
713 void BKE_sound_free_waveform(bSound *sound)
714 {
715         SoundWaveform *waveform = sound->waveform;
716         if (waveform) {
717                 if (waveform->data) {
718                         MEM_freeN(waveform->data);
719                 }
720                 MEM_freeN(waveform);
721         }
722
723         sound->waveform = NULL;
724 }
725
726 void BKE_sound_read_waveform(bSound *sound, short *stop)
727 {
728         AUD_SoundInfo info = AUD_getInfo(sound->playback_handle);
729         SoundWaveform *waveform = MEM_mallocN(sizeof(SoundWaveform), "SoundWaveform");
730
731         if (info.length > 0) {
732                 int length = info.length * SOUND_WAVE_SAMPLES_PER_SECOND;
733                 
734                 waveform->data = MEM_mallocN(length * sizeof(float) * 3, "SoundWaveform.samples");
735                 waveform->length = AUD_readSound(sound->playback_handle, waveform->data, length, SOUND_WAVE_SAMPLES_PER_SECOND, stop);
736         }
737         else {
738                 /* Create an empty waveform here if the sound couldn't be
739                  * read. This indicates that reading the waveform is "done",
740                  * whereas just setting sound->waveform to NULL causes other
741                  * code to think the waveform still needs to be created. */
742                 waveform->data = NULL;
743                 waveform->length = 0;
744         }
745
746         if (*stop) {
747                 if (waveform->data) {
748                         MEM_freeN(waveform->data);
749                 }
750                 MEM_freeN(waveform);
751                 BLI_spin_lock(sound->spinlock);
752                 sound->flags &= ~SOUND_FLAGS_WAVEFORM_LOADING;
753                 BLI_spin_unlock(sound->spinlock);
754                 return;
755         }
756                 
757         BKE_sound_free_waveform(sound);
758         
759         BLI_spin_lock(sound->spinlock);
760         sound->waveform = waveform;
761         sound->flags &= ~SOUND_FLAGS_WAVEFORM_LOADING;
762         BLI_spin_unlock(sound->spinlock);
763 }
764
765 void BKE_sound_update_scene(Main *bmain, struct Scene *scene)
766 {
767         Object *ob;
768         Base *base;
769         NlaTrack *track;
770         NlaStrip *strip;
771         Speaker *speaker;
772         Scene *sce_it;
773
774         void *new_set = AUD_createSet();
775         void *handle;
776         float quat[4];
777
778         /* cheap test to skip looping over all objects (no speakers is a common case) */
779         if (!BLI_listbase_is_empty(&bmain->speaker)) {
780                 for (SETLOOPER(scene, sce_it, base)) {
781                         ob = base->object;
782                         if ((ob->type != OB_SPEAKER) || !ob->adt) {
783                                 continue;
784                         }
785                         for (track = ob->adt->nla_tracks.first; track; track = track->next) {
786                                 for (strip = track->strips.first; strip; strip = strip->next) {
787                                         if (strip->type != NLASTRIP_TYPE_SOUND) {
788                                                 continue;
789                                         }
790                                         speaker = (Speaker *)ob->data;
791
792                                         if (AUD_removeSet(scene->speaker_handles, strip->speaker_handle)) {
793                                                 if (speaker->sound) {
794                                                         AUD_SequenceEntry_move(strip->speaker_handle, (double)strip->start / FPS, FLT_MAX, 0);
795                                                 }
796                                                 else {
797                                                         AUD_Sequence_remove(scene->sound_scene, strip->speaker_handle);
798                                                         strip->speaker_handle = NULL;
799                                                 }
800                                         }
801                                         else {
802                                                 if (speaker->sound) {
803                                                         strip->speaker_handle = AUD_Sequence_add(scene->sound_scene,
804                                                                                                 speaker->sound->playback_handle,
805                                                                                                 (double)strip->start / FPS, FLT_MAX, 0);
806                                                         AUD_SequenceEntry_setRelative(strip->speaker_handle, 0);
807                                                 }
808                                         }
809
810                                         if (strip->speaker_handle) {
811                                                 const bool mute = ((strip->flag & NLASTRIP_FLAG_MUTED) || (speaker->flag & SPK_MUTED));
812                                                 AUD_addSet(new_set, strip->speaker_handle);
813                                                 AUD_SequenceEntry_setVolumeMaximum(strip->speaker_handle, speaker->volume_max);
814                                                 AUD_SequenceEntry_setVolumeMinimum(strip->speaker_handle, speaker->volume_min);
815                                                 AUD_SequenceEntry_setDistanceMaximum(strip->speaker_handle, speaker->distance_max);
816                                                 AUD_SequenceEntry_setDistanceReference(strip->speaker_handle, speaker->distance_reference);
817                                                 AUD_SequenceEntry_setAttenuation(strip->speaker_handle, speaker->attenuation);
818                                                 AUD_SequenceEntry_setConeAngleOuter(strip->speaker_handle, speaker->cone_angle_outer);
819                                                 AUD_SequenceEntry_setConeAngleInner(strip->speaker_handle, speaker->cone_angle_inner);
820                                                 AUD_SequenceEntry_setConeVolumeOuter(strip->speaker_handle, speaker->cone_volume_outer);
821
822                                                 mat4_to_quat(quat, ob->obmat);
823                                                 AUD_SequenceEntry_setAnimationData(strip->speaker_handle, AUD_AP_LOCATION, CFRA, ob->obmat[3], 1);
824                                                 AUD_SequenceEntry_setAnimationData(strip->speaker_handle, AUD_AP_ORIENTATION, CFRA, quat, 1);
825                                                 AUD_SequenceEntry_setAnimationData(strip->speaker_handle, AUD_AP_VOLUME, CFRA, &speaker->volume, 1);
826                                                 AUD_SequenceEntry_setAnimationData(strip->speaker_handle, AUD_AP_PITCH, CFRA, &speaker->pitch, 1);
827                                                 AUD_SequenceEntry_setSound(strip->speaker_handle, speaker->sound->playback_handle);
828                                                 AUD_SequenceEntry_setMuted(strip->speaker_handle, mute);
829                                         }
830                                 }
831                         }
832                 }
833         }
834
835         while ((handle = AUD_getSet(scene->speaker_handles))) {
836                 AUD_Sequence_remove(scene->sound_scene, handle);
837         }
838
839         if (scene->camera) {
840                 mat4_to_quat(quat, scene->camera->obmat);
841                 AUD_Sequence_setAnimationData(scene->sound_scene, AUD_AP_LOCATION, CFRA, scene->camera->obmat[3], 1);
842                 AUD_Sequence_setAnimationData(scene->sound_scene, AUD_AP_ORIENTATION, CFRA, quat, 1);
843         }
844
845         AUD_destroySet(scene->speaker_handles);
846         scene->speaker_handles = new_set;
847 }
848
849 void *BKE_sound_get_factory(void *sound)
850 {
851         return ((bSound *)sound)->playback_handle;
852 }
853
854 /* stupid wrapper because AUD_C-API.h includes Python.h which makesrna doesn't like */
855 float BKE_sound_get_length(bSound *sound)
856 {
857         AUD_SoundInfo info = AUD_getInfo(sound->playback_handle);
858
859         return info.length;
860 }
861
862 char **BKE_sound_get_device_names(void)
863 {
864         if (audio_device_names == NULL) {
865 #ifdef WITH_SYSTEM_AUDASPACE
866                 audio_device_names = AUD_getDeviceNames();
867 #else
868                 static const char *names[] = {
869                         "Null", "SDL", "OpenAL", "JACK", NULL
870                 };
871                 audio_device_names = (char **)names;
872 #endif
873         }
874
875         return audio_device_names;
876 }
877
878 bool BKE_sound_is_jack_supported(void)
879 {
880 #ifdef WITH_SYSTEM_AUDASPACE
881         return 1;
882 #else
883         return (bool)AUD_isJackSupported();
884 #endif
885 }
886
887 #else  /* WITH_AUDASPACE */
888
889 #include "BLI_utildefines.h"
890
891 void BKE_sound_force_device(const char *UNUSED(device)) {}
892 void BKE_sound_init_once(void) {}
893 void BKE_sound_init(struct Main *UNUSED(bmain)) {}
894 void BKE_sound_exit(void) {}
895 void BKE_sound_exit_once(void) {}
896 void BKE_sound_cache(struct bSound *UNUSED(sound)) {}
897 void BKE_sound_delete_cache(struct bSound *UNUSED(sound)) {}
898 void BKE_sound_load(struct Main *UNUSED(bmain), struct bSound *UNUSED(sound)) {}
899 void BKE_sound_create_scene(struct Scene *UNUSED(scene)) {}
900 void BKE_sound_destroy_scene(struct Scene *UNUSED(scene)) {}
901 void BKE_sound_mute_scene(struct Scene *UNUSED(scene), int UNUSED(muted)) {}
902 void *BKE_sound_scene_add_scene_sound(struct Scene *UNUSED(scene), struct Sequence *UNUSED(sequence),
903                                       int UNUSED(startframe), int UNUSED(endframe), int UNUSED(frameskip)) { return NULL; }
904 void *BKE_sound_scene_add_scene_sound_defaults(struct Scene *UNUSED(scene),
905                                               struct Sequence *UNUSED(sequence)) { return NULL; }
906 void *BKE_sound_add_scene_sound(struct Scene *UNUSED(scene), struct Sequence *UNUSED(sequence), int UNUSED(startframe),
907                                 int UNUSED(endframe), int UNUSED(frameskip)) { return NULL; }
908 void *BKE_sound_add_scene_sound_defaults(struct Scene *UNUSED(scene), struct Sequence *UNUSED(sequence)) { return NULL; }
909 void BKE_sound_remove_scene_sound(struct Scene *UNUSED(scene), void *UNUSED(handle)) {}
910 void BKE_sound_mute_scene_sound(void *UNUSED(handle), char UNUSED(mute)) {}
911 void BKE_sound_move_scene_sound(struct Scene *UNUSED(scene), void *UNUSED(handle), int UNUSED(startframe),
912                                 int UNUSED(endframe), int UNUSED(frameskip)) {}
913 void BKE_sound_move_scene_sound_defaults(struct Scene *UNUSED(scene), struct Sequence *UNUSED(sequence)) {}
914 void BKE_sound_play_scene(struct Scene *UNUSED(scene)) {}
915 void BKE_sound_stop_scene(struct Scene *UNUSED(scene)) {}
916 void BKE_sound_seek_scene(struct Main *UNUSED(bmain), struct Scene *UNUSED(scene)) {}
917 float BKE_sound_sync_scene(struct Scene *UNUSED(scene)) { return NAN_FLT; }
918 int BKE_sound_scene_playing(struct Scene *UNUSED(scene)) { return -1; }
919 void BKE_sound_read_waveform(struct bSound *sound, short *stop) { UNUSED_VARS(sound, stop); }
920 void BKE_sound_init_main(struct Main *UNUSED(bmain)) {}
921 void BKE_sound_set_cfra(int UNUSED(cfra)) {}
922 void BKE_sound_update_sequencer(struct Main *UNUSED(main), struct bSound *UNUSED(sound)) {}
923 void BKE_sound_update_scene(struct Main *UNUSED(bmain), struct Scene *UNUSED(scene)) {}
924 void BKE_sound_update_scene_sound(void *UNUSED(handle), struct bSound *UNUSED(sound)) {}
925 void BKE_sound_update_scene_listener(struct Scene *UNUSED(scene)) {}
926 void BKE_sound_update_fps(struct Scene *UNUSED(scene)) {}
927 void BKE_sound_set_scene_sound_volume(void *UNUSED(handle), float UNUSED(volume), char UNUSED(animated)) {}
928 void BKE_sound_set_scene_sound_pan(void *UNUSED(handle), float UNUSED(pan), char UNUSED(animated)) {}
929 void BKE_sound_set_scene_volume(struct Scene *UNUSED(scene), float UNUSED(volume)) {}
930 void BKE_sound_set_scene_sound_pitch(void *UNUSED(handle), float UNUSED(pitch), char UNUSED(animated)) {}
931 float BKE_sound_get_length(struct bSound *UNUSED(sound)) { return 0; }
932 bool BKE_sound_is_jack_supported(void) { return false; }
933 #endif  /* WITH_AUDASPACE */