Audaspace: use standalone library.
[blender-staging.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 #  ifdef WITH_EXTERNAL_AUDASPACE
52 #    include <audaspace/AUD_Sound.h>
53 #    include <audaspace/AUD_Sequence.h>
54 #    include <audaspace/AUD_Handle.h>
55 #    include <audaspace/AUD_Special.h>
56 #    include "AUD_Set.h"
57 #  else
58 #    include "AUD_C-API.h"
59 #  endif
60 #endif
61
62 #include "BKE_global.h"
63 #include "BKE_main.h"
64 #include "BKE_sound.h"
65 #include "BKE_library.h"
66 #include "BKE_packedFile.h"
67 #include "BKE_sequencer.h"
68 #include "BKE_scene.h"
69
70 #ifdef WITH_AUDASPACE
71 /* evil global ;-) */
72 static int sound_cfra;
73 #endif
74
75 bSound *BKE_sound_new_file(struct Main *bmain, const char *filename)
76 {
77         bSound *sound;
78
79         char str[FILE_MAX];
80         const char *path;
81
82         size_t len;
83
84         BLI_strncpy(str, filename, sizeof(str));
85
86         path = /*bmain ? bmain->name :*/ G.main->name;
87
88         BLI_path_abs(str, path);
89
90         len = strlen(filename);
91         while (len > 0 && filename[len - 1] != '/' && filename[len - 1] != '\\')
92                 len--;
93
94         sound = BKE_libblock_alloc(bmain, ID_SO, filename + len);
95         BLI_strncpy(sound->name, filename, FILE_MAX);
96         /* sound->type = SOUND_TYPE_FILE; */ /* XXX unused currently */
97
98         BKE_sound_load(bmain, sound);
99
100         return sound;
101 }
102
103 void BKE_sound_free(bSound *sound)
104 {
105         if (sound->packedfile) {
106                 freePackedFile(sound->packedfile);
107                 sound->packedfile = NULL;
108         }
109
110 #ifdef WITH_AUDASPACE
111         if (sound->handle) {
112                 AUD_unload(sound->handle);
113                 sound->handle = NULL;
114                 sound->playback_handle = NULL;
115         }
116
117         if (sound->cache) {
118                 AUD_unload(sound->cache);
119                 sound->cache = NULL;
120         }
121
122         BKE_sound_free_waveform(sound);
123         
124         if (sound->spinlock) {
125                 BLI_spin_end(sound->spinlock);
126                 MEM_freeN(sound->spinlock);
127                 sound->spinlock = NULL;
128         }
129         
130 #endif  /* WITH_AUDASPACE */
131 }
132
133 #ifdef WITH_AUDASPACE
134
135 static int force_device = -1;
136
137 #ifdef WITH_JACK
138 static void sound_sync_callback(void *data, int mode, float time)
139 {
140         struct Main *bmain = (struct Main *)data;
141         struct Scene *scene;
142
143         scene = bmain->scene.first;
144         while (scene) {
145                 if (scene->audio.flag & AUDIO_SYNC) {
146                         if (mode)
147                                 BKE_sound_play_scene(scene);
148                         else
149                                 BKE_sound_stop_scene(scene);
150                         if (scene->playback_handle)
151                                 AUD_seek(scene->playback_handle, time);
152                 }
153                 scene = scene->id.next;
154         }
155 }
156 #endif
157
158 int BKE_sound_define_from_str(const char *str)
159 {
160         if (BLI_strcaseeq(str, "NULL"))
161                 return 0;
162         if (BLI_strcaseeq(str, "SDL"))
163                 return 1;
164         if (BLI_strcaseeq(str, "OPENAL"))
165                 return 2;
166         if (BLI_strcaseeq(str, "JACK"))
167                 return 3;
168
169         return -1;
170 }
171
172 void BKE_sound_force_device(int device)
173 {
174         force_device = device;
175 }
176
177 void BKE_sound_init_once(void)
178 {
179         AUD_initOnce();
180         atexit(BKE_sound_exit_once);
181 }
182
183 void BKE_sound_init(struct Main *bmain)
184 {
185         AUD_DeviceSpecs specs;
186         int device, buffersize;
187         const char* device_name;
188
189         device = U.audiodevice;
190         buffersize = U.mixbufsize;
191         specs.channels = U.audiochannels;
192         specs.format = U.audioformat;
193         specs.rate = U.audiorate;
194
195         if (force_device >= 0)
196                 device = force_device;
197
198         switch(device)
199         {
200         case 1:
201                 device_name = "SDL";
202                 break;
203         case 2:
204                 device_name = "OpenAL";
205                 break;
206         case 3:
207                 device_name = "Jack";
208                 break;
209         default:
210                 device_name = "Null";
211                 break;
212         }
213
214         if (buffersize < 128)
215                 buffersize = 1024;
216
217         if (specs.rate < AUD_RATE_8000)
218                 specs.rate = AUD_RATE_44100;
219
220         if (specs.format <= AUD_FORMAT_INVALID)
221                 specs.format = AUD_FORMAT_S16;
222
223         if (specs.channels <= AUD_CHANNELS_INVALID)
224                 specs.channels = AUD_CHANNELS_STEREO;
225
226         if (!AUD_init(device_name, "Blender", specs, buffersize))
227                 AUD_init("Null", "Blender", specs, buffersize);
228
229         BKE_sound_init_main(bmain);
230 }
231
232 void BKE_sound_init_main(struct Main *bmain)
233 {
234 #ifdef WITH_JACK
235         AUD_setSynchronizerCallback(sound_sync_callback, bmain);
236 #else
237         (void)bmain; /* unused */
238 #endif
239 }
240
241 void BKE_sound_exit(void)
242 {
243         AUD_exit();
244 }
245
246 void BKE_sound_exit_once(void)
247 {
248         AUD_exit();
249         AUD_exitOnce();
250 }
251
252 /* XXX unused currently */
253 #if 0
254 bSound *BKE_sound_new_buffer(struct Main *bmain, bSound *source)
255 {
256         bSound *sound = NULL;
257
258         char name[MAX_ID_NAME + 5];
259         strcpy(name, "buf_");
260         strcpy(name + 4, source->id.name);
261
262         sound = BKE_libblock_alloc(bmain, ID_SO, name);
263
264         sound->child_sound = source;
265         sound->type = SOUND_TYPE_BUFFER;
266
267         sound_load(bmain, sound);
268
269         return sound;
270 }
271
272 bSound *BKE_sound_new_limiter(struct Main *bmain, bSound *source, float start, float end)
273 {
274         bSound *sound = NULL;
275
276         char name[MAX_ID_NAME + 5];
277         strcpy(name, "lim_");
278         strcpy(name + 4, source->id.name);
279
280         sound = BKE_libblock_alloc(bmain, ID_SO, name);
281
282         sound->child_sound = source;
283         sound->start = start;
284         sound->end = end;
285         sound->type = SOUND_TYPE_LIMITER;
286
287         sound_load(bmain, sound);
288
289         return sound;
290 }
291 #endif
292
293 void BKE_sound_delete(struct Main *bmain, bSound *sound)
294 {
295         if (sound) {
296                 BKE_sound_free(sound);
297
298                 BKE_libblock_free(bmain, sound);
299         }
300 }
301
302 void BKE_sound_cache(bSound *sound)
303 {
304         sound->flags |= SOUND_FLAGS_CACHING;
305         if (sound->cache)
306                 AUD_unload(sound->cache);
307
308         sound->cache = AUD_bufferSound(sound->handle);
309         if (sound->cache)
310                 sound->playback_handle = sound->cache;
311         else
312                 sound->playback_handle = sound->handle;
313 }
314
315 void BKE_sound_delete_cache(bSound *sound)
316 {
317         sound->flags &= ~SOUND_FLAGS_CACHING;
318         if (sound->cache) {
319                 AUD_unload(sound->cache);
320                 sound->cache = NULL;
321                 sound->playback_handle = sound->handle;
322         }
323 }
324
325 void BKE_sound_load(struct Main *bmain, bSound *sound)
326 {
327         if (sound) {
328                 if (sound->cache) {
329                         AUD_unload(sound->cache);
330                         sound->cache = NULL;
331                 }
332
333                 if (sound->handle) {
334                         AUD_unload(sound->handle);
335                         sound->handle = NULL;
336                         sound->playback_handle = NULL;
337                 }
338
339                 BKE_sound_free_waveform(sound);
340
341 /* XXX unused currently */
342 #if 0
343                 switch (sound->type)
344                 {
345                         case SOUND_TYPE_FILE:
346 #endif
347                 {
348                         char fullpath[FILE_MAX];
349
350                         /* load sound */
351                         PackedFile *pf = sound->packedfile;
352
353                         /* don't modify soundact->sound->name, only change a copy */
354                         BLI_strncpy(fullpath, sound->name, sizeof(fullpath));
355                         BLI_path_abs(fullpath, ID_BLEND_PATH(bmain, &sound->id));
356
357                         /* but we need a packed file then */
358                         if (pf)
359                                 sound->handle = AUD_loadBuffer((unsigned char *) pf->data, pf->size);
360                         /* or else load it from disk */
361                         else
362                                 sound->handle = AUD_load(fullpath);
363                 }
364 /* XXX unused currently */
365 #if 0
366                         break;
367                 }
368                 case SOUND_TYPE_BUFFER:
369                         if (sound->child_sound && sound->child_sound->handle)
370                                 sound->handle = AUD_bufferSound(sound->child_sound->handle);
371                         break;
372                 case SOUND_TYPE_LIMITER:
373                         if (sound->child_sound && sound->child_sound->handle)
374                                 sound->handle = AUD_limitSound(sound->child_sound, sound->start, sound->end);
375                         break;
376         }
377 #endif
378                 if (sound->flags & SOUND_FLAGS_MONO) {
379                         void *handle = AUD_monoSound(sound->handle);
380                         AUD_unload(sound->handle);
381                         sound->handle = handle;
382                 }
383
384                 if (sound->flags & SOUND_FLAGS_CACHING) {
385                         sound->cache = AUD_bufferSound(sound->handle);
386                 }
387
388                 if (sound->cache)
389                         sound->playback_handle = sound->cache;
390                 else
391                         sound->playback_handle = sound->handle;
392
393                 BKE_sound_update_sequencer(bmain, sound);
394         }
395 }
396
397 AUD_Device *BKE_sound_mixdown(struct Scene *scene, AUD_DeviceSpecs specs, int start, float volume)
398 {
399         return AUD_openMixdownDevice(specs, scene->sound_scene, volume, start / FPS);
400 }
401
402 void BKE_sound_create_scene(struct Scene *scene)
403 {
404         /* should be done in version patch, but this gets called before */
405         if (scene->r.frs_sec_base == 0)
406                 scene->r.frs_sec_base = 1;
407
408         scene->sound_scene = AUD_createSequencer(FPS, scene->audio.flag & AUDIO_MUTE);
409         AUD_updateSequencerData(scene->sound_scene, scene->audio.speed_of_sound,
410                                 scene->audio.doppler_factor, scene->audio.distance_model);
411         scene->playback_handle = NULL;
412         scene->sound_scrub_handle = NULL;
413         scene->speaker_handles = NULL;
414 }
415
416 void BKE_sound_destroy_scene(struct Scene *scene)
417 {
418         if (scene->playback_handle)
419                 AUD_stop(scene->playback_handle);
420         if (scene->sound_scrub_handle)
421                 AUD_stop(scene->sound_scrub_handle);
422         if (scene->sound_scene)
423                 AUD_destroySequencer(scene->sound_scene);
424         if (scene->speaker_handles)
425                 AUD_destroySet(scene->speaker_handles);
426 }
427
428 void BKE_sound_mute_scene(struct Scene *scene, int muted)
429 {
430         if (scene->sound_scene)
431                 AUD_setSequencerMuted(scene->sound_scene, muted);
432 }
433
434 void BKE_sound_update_fps(struct Scene *scene)
435 {
436         if (scene->sound_scene)
437                 AUD_setSequencerFPS(scene->sound_scene, FPS);
438
439         BKE_sequencer_refresh_sound_length(scene);
440 }
441
442 void BKE_sound_update_scene_listener(struct Scene *scene)
443 {
444         AUD_updateSequencerData(scene->sound_scene, scene->audio.speed_of_sound,
445                                 scene->audio.doppler_factor, scene->audio.distance_model);
446 }
447
448 void *BKE_sound_scene_add_scene_sound(struct Scene *scene, struct Sequence *sequence,
449                                   int startframe, int endframe, int frameskip)
450 {
451         if (scene != sequence->scene) {
452                 const double fps = FPS;
453                 return AUD_addSequence(scene->sound_scene, sequence->scene->sound_scene,
454                                        startframe / fps, endframe / fps, frameskip / fps);
455         }
456         return NULL;
457 }
458
459 void *BKE_sound_scene_add_scene_sound_defaults(struct Scene *scene, struct Sequence *sequence)
460 {
461         return BKE_sound_scene_add_scene_sound(scene, sequence,
462                                            sequence->startdisp, sequence->enddisp,
463                                            sequence->startofs + sequence->anim_startofs);
464 }
465
466 void *BKE_sound_add_scene_sound(struct Scene *scene, struct Sequence *sequence, int startframe, int endframe, int frameskip)
467 {
468         const double fps = FPS;
469         void *handle = AUD_addSequence(scene->sound_scene, sequence->sound->playback_handle,
470                                        startframe / fps, endframe / fps, frameskip / fps);
471         AUD_muteSequence(handle, (sequence->flag & SEQ_MUTE) != 0);
472         AUD_setSequenceAnimData(handle, AUD_AP_VOLUME, CFRA, &sequence->volume, 0);
473         AUD_setSequenceAnimData(handle, AUD_AP_PITCH, CFRA, &sequence->pitch, 0);
474         AUD_setSequenceAnimData(handle, AUD_AP_PANNING, CFRA, &sequence->pan, 0);
475         return handle;
476 }
477
478 void *BKE_sound_add_scene_sound_defaults(struct Scene *scene, struct Sequence *sequence)
479 {
480         return BKE_sound_add_scene_sound(scene, sequence,
481                                      sequence->startdisp, sequence->enddisp,
482                                      sequence->startofs + sequence->anim_startofs);
483 }
484
485 void BKE_sound_remove_scene_sound(struct Scene *scene, void *handle)
486 {
487         AUD_removeSequence(scene->sound_scene, handle);
488 }
489
490 void BKE_sound_mute_scene_sound(void *handle, char mute)
491 {
492         AUD_muteSequence(handle, mute);
493 }
494
495 void BKE_sound_move_scene_sound(struct Scene *scene, void *handle, int startframe, int endframe, int frameskip)
496 {
497         const double fps = FPS;
498         AUD_moveSequence(handle, startframe / fps, endframe / fps, frameskip / fps);
499 }
500
501 void BKE_sound_move_scene_sound_defaults(struct Scene *scene, struct Sequence *sequence)
502 {
503         if (sequence->scene_sound) {
504                 BKE_sound_move_scene_sound(scene, sequence->scene_sound,
505                                        sequence->startdisp, sequence->enddisp,
506                                        sequence->startofs + sequence->anim_startofs);
507         }
508 }
509
510 void BKE_sound_update_scene_sound(void *handle, bSound *sound)
511 {
512         AUD_updateSequenceSound(handle, sound->playback_handle);
513 }
514
515 void BKE_sound_set_cfra(int cfra)
516 {
517         sound_cfra = cfra;
518 }
519
520 void BKE_sound_set_scene_volume(struct Scene *scene, float volume)
521 {
522         AUD_setSequencerAnimData(scene->sound_scene, AUD_AP_VOLUME, CFRA, &volume,
523                                  (scene->audio.flag & AUDIO_VOLUME_ANIMATED) != 0);
524 }
525
526 void BKE_sound_set_scene_sound_volume(void *handle, float volume, char animated)
527 {
528         AUD_setSequenceAnimData(handle, AUD_AP_VOLUME, sound_cfra, &volume, animated);
529 }
530
531 void BKE_sound_set_scene_sound_pitch(void *handle, float pitch, char animated)
532 {
533         AUD_setSequenceAnimData(handle, AUD_AP_PITCH, sound_cfra, &pitch, animated);
534 }
535
536 void BKE_sound_set_scene_sound_pan(void *handle, float pan, char animated)
537 {
538         AUD_setSequenceAnimData(handle, AUD_AP_PANNING, sound_cfra, &pan, animated);
539 }
540
541 void BKE_sound_update_sequencer(struct Main *main, bSound *sound)
542 {
543         struct Scene *scene;
544
545         for (scene = main->scene.first; scene; scene = scene->id.next) {
546                 BKE_sequencer_update_sound(scene, sound);
547         }
548 }
549
550 static void sound_start_play_scene(struct Scene *scene)
551 {
552         if (scene->playback_handle)
553                 AUD_stop(scene->playback_handle);
554
555         AUD_setSequencerDeviceSpecs(scene->sound_scene);
556
557         if ((scene->playback_handle = AUD_play(scene->sound_scene, 1)))
558                 AUD_setLoop(scene->playback_handle, -1);
559 }
560
561 void BKE_sound_play_scene(struct Scene *scene)
562 {
563         AUD_Status status;
564         const float cur_time = (float)((double)CFRA / FPS);
565
566         AUD_lock();
567
568         status = scene->playback_handle ? AUD_getStatus(scene->playback_handle) : AUD_STATUS_INVALID;
569
570         if (status == AUD_STATUS_INVALID) {
571                 sound_start_play_scene(scene);
572
573                 if (!scene->playback_handle) {
574                         AUD_unlock();
575                         return;
576                 }
577         }
578
579         if (status != AUD_STATUS_PLAYING) {
580                 AUD_seek(scene->playback_handle, cur_time);
581                 AUD_resume(scene->playback_handle);
582         }
583
584         if (scene->audio.flag & AUDIO_SYNC)
585                 AUD_playSynchronizer();
586
587         AUD_unlock();
588 }
589
590 void BKE_sound_stop_scene(struct Scene *scene)
591 {
592         if (scene->playback_handle) {
593                 AUD_pause(scene->playback_handle);
594
595                 if (scene->audio.flag & AUDIO_SYNC)
596                         AUD_stopSynchronizer();
597         }
598 }
599
600 void BKE_sound_seek_scene(struct Main *bmain, struct Scene *scene)
601 {
602         AUD_Status status;
603         bScreen *screen;
604         int animation_playing;
605
606         const float one_frame = (float)(1.0 / FPS);
607         const float cur_time = (float)((double)CFRA / FPS);
608
609         AUD_lock();
610
611         status = scene->playback_handle ? AUD_getStatus(scene->playback_handle) : AUD_STATUS_INVALID;
612
613         if (status == AUD_STATUS_INVALID) {
614                 sound_start_play_scene(scene);
615
616                 if (!scene->playback_handle) {
617                         AUD_unlock();
618                         return;
619                 }
620
621                 AUD_pause(scene->playback_handle);
622         }
623
624         animation_playing = 0;
625         for (screen = bmain->screen.first; screen; screen = screen->id.next) {
626                 if (screen->animtimer) {
627                         animation_playing = 1;
628                         break;
629                 }
630         }
631
632         if (scene->audio.flag & AUDIO_SCRUB && !animation_playing) {
633                 if (scene->audio.flag & AUDIO_SYNC) {
634                         AUD_seek(scene->playback_handle, cur_time);
635                         AUD_seekSynchronizer(scene->playback_handle, cur_time);
636                 }
637                 else {
638                         AUD_seek(scene->playback_handle, cur_time);
639                 }
640                 AUD_resume(scene->playback_handle);
641                 if (scene->sound_scrub_handle && AUD_getStatus(scene->sound_scrub_handle) != AUD_STATUS_INVALID) {
642                         AUD_seek(scene->sound_scrub_handle, 0);
643                 }
644                 else {
645                         if (scene->sound_scrub_handle) {
646                                 AUD_stop(scene->sound_scrub_handle);
647                         }
648                         scene->sound_scrub_handle = AUD_pauseAfter(scene->playback_handle, one_frame);
649                 }
650         }
651         else {
652                 if (scene->audio.flag & AUDIO_SYNC) {
653                         AUD_seekSynchronizer(scene->playback_handle, cur_time);
654                 }
655                 else {
656                         if (status == AUD_STATUS_PLAYING) {
657                                 AUD_seek(scene->playback_handle, cur_time);
658                         }
659                 }
660         }
661
662         AUD_unlock();
663 }
664
665 float BKE_sound_sync_scene(struct Scene *scene)
666 {
667         if (scene->playback_handle) {
668                 if (scene->audio.flag & AUDIO_SYNC)
669                         return AUD_getSynchronizerPosition(scene->playback_handle);
670                 else
671                         return AUD_getPosition(scene->playback_handle);
672         }
673         return NAN_FLT;
674 }
675
676 int BKE_sound_scene_playing(struct Scene *scene)
677 {
678         if (scene->audio.flag & AUDIO_SYNC)
679                 return AUD_isSynchronizerPlaying();
680         else
681                 return -1;
682 }
683
684 void BKE_sound_free_waveform(bSound *sound)
685 {
686         SoundWaveform *waveform = sound->waveform;
687         if (waveform) {
688                 if (waveform->data) {
689                         MEM_freeN(waveform->data);
690                 }
691                 MEM_freeN(waveform);
692         }
693
694         sound->waveform = NULL;
695 }
696
697 void BKE_sound_read_waveform(bSound *sound, short *stop)
698 {
699         AUD_SoundInfo info = AUD_getInfo(sound->playback_handle);
700         SoundWaveform *waveform = MEM_mallocN(sizeof(SoundWaveform), "SoundWaveform");
701
702         if (info.length > 0) {
703                 int length = info.length * SOUND_WAVE_SAMPLES_PER_SECOND;
704                 
705                 waveform->data = MEM_mallocN(length * sizeof(float) * 3, "SoundWaveform.samples");
706                 waveform->length = AUD_readSound(sound->playback_handle, waveform->data, length, SOUND_WAVE_SAMPLES_PER_SECOND, stop);
707         }
708         else {
709                 /* Create an empty waveform here if the sound couldn't be
710                  * read. This indicates that reading the waveform is "done",
711                  * whereas just setting sound->waveform to NULL causes other
712                  * code to think the waveform still needs to be created. */
713                 waveform->data = NULL;
714                 waveform->length = 0;
715         }
716
717         if (*stop) {
718                 if (waveform->data) {
719                         MEM_freeN(waveform->data);
720                 }
721                 MEM_freeN(waveform);
722                 BLI_spin_lock(sound->spinlock);
723                 sound->flags &= ~SOUND_FLAGS_WAVEFORM_LOADING;
724                 BLI_spin_unlock(sound->spinlock);
725                 return;
726         }
727                 
728         BKE_sound_free_waveform(sound);
729         
730         BLI_spin_lock(sound->spinlock);
731         sound->waveform = waveform;
732         sound->flags &= ~SOUND_FLAGS_WAVEFORM_LOADING;
733         BLI_spin_unlock(sound->spinlock);
734 }
735
736 void BKE_sound_update_scene(Main *bmain, struct Scene *scene)
737 {
738         Object *ob;
739         Base *base;
740         NlaTrack *track;
741         NlaStrip *strip;
742         Speaker *speaker;
743         Scene *sce_it;
744
745         void *new_set = AUD_createSet();
746         void *handle;
747         float quat[4];
748
749         /* cheap test to skip looping over all objects (no speakers is a common case) */
750         if (!BLI_listbase_is_empty(&bmain->speaker)) {
751                 for (SETLOOPER(scene, sce_it, base)) {
752                         ob = base->object;
753                         if ((ob->type != OB_SPEAKER) || !ob->adt) {
754                                 continue;
755                         }
756                         for (track = ob->adt->nla_tracks.first; track; track = track->next) {
757                                 for (strip = track->strips.first; strip; strip = strip->next) {
758                                         if (strip->type != NLASTRIP_TYPE_SOUND) {
759                                                 continue;
760                                         }
761                                         speaker = (Speaker *)ob->data;
762
763                                         if (AUD_removeSet(scene->speaker_handles, strip->speaker_handle)) {
764                                                 if (speaker->sound) {
765                                                         AUD_moveSequence(strip->speaker_handle, (double)strip->start / FPS, FLT_MAX, 0);
766                                                 }
767                                                 else {
768                                                         AUD_removeSequence(scene->sound_scene, strip->speaker_handle);
769                                                         strip->speaker_handle = NULL;
770                                                 }
771                                         }
772                                         else {
773                                                 if (speaker->sound) {
774                                                         strip->speaker_handle = AUD_addSequence(scene->sound_scene,
775                                                                                                 speaker->sound->playback_handle,
776                                                                                                 (double)strip->start / FPS, FLT_MAX, 0);
777                                                         AUD_setRelativeSequence(strip->speaker_handle, 0);
778                                                 }
779                                         }
780
781                                         if (strip->speaker_handle) {
782                                                 const bool mute = ((strip->flag & NLASTRIP_FLAG_MUTED) || (speaker->flag & SPK_MUTED));
783                                                 AUD_addSet(new_set, strip->speaker_handle);
784                                                 AUD_updateSequenceData(strip->speaker_handle, speaker->volume_max,
785                                                                        speaker->volume_min, speaker->distance_max,
786                                                                        speaker->distance_reference, speaker->attenuation,
787                                                                        speaker->cone_angle_outer, speaker->cone_angle_inner,
788                                                                        speaker->cone_volume_outer);
789
790                                                 mat4_to_quat(quat, ob->obmat);
791                                                 AUD_setSequenceAnimData(strip->speaker_handle, AUD_AP_LOCATION, CFRA, ob->obmat[3], 1);
792                                                 AUD_setSequenceAnimData(strip->speaker_handle, AUD_AP_ORIENTATION, CFRA, quat, 1);
793                                                 AUD_setSequenceAnimData(strip->speaker_handle, AUD_AP_VOLUME, CFRA, &speaker->volume, 1);
794                                                 AUD_setSequenceAnimData(strip->speaker_handle, AUD_AP_PITCH, CFRA, &speaker->pitch, 1);
795                                                 AUD_updateSequenceSound(strip->speaker_handle, speaker->sound->playback_handle);
796                                                 AUD_muteSequence(strip->speaker_handle, mute);
797                                         }
798                                 }
799                         }
800                 }
801         }
802
803         while ((handle = AUD_getSet(scene->speaker_handles))) {
804                 AUD_removeSequence(scene->sound_scene, handle);
805         }
806
807         if (scene->camera) {
808                 mat4_to_quat(quat, scene->camera->obmat);
809                 AUD_setSequencerAnimData(scene->sound_scene, AUD_AP_LOCATION, CFRA, scene->camera->obmat[3], 1);
810                 AUD_setSequencerAnimData(scene->sound_scene, AUD_AP_ORIENTATION, CFRA, quat, 1);
811         }
812
813         AUD_destroySet(scene->speaker_handles);
814         scene->speaker_handles = new_set;
815 }
816
817 void *BKE_sound_get_factory(void *sound)
818 {
819         return ((bSound *)sound)->playback_handle;
820 }
821
822 /* stupid wrapper because AUD_C-API.h includes Python.h which makesrna doesn't like */
823 float BKE_sound_get_length(bSound *sound)
824 {
825         AUD_SoundInfo info = AUD_getInfo(sound->playback_handle);
826
827         return info.length;
828 }
829
830 bool BKE_sound_is_jack_supported(void)
831 {
832 #ifdef WITH_EXTERNAL_AUDASPACE
833         return 1;
834 #else
835         return (bool)AUD_isJackSupported();
836 #endif
837 }
838
839 #else  /* WITH_AUDASPACE */
840
841 #include "BLI_utildefines.h"
842
843 int BKE_sound_define_from_str(const char *UNUSED(str)) { return -1; }
844 void BKE_sound_force_device(int UNUSED(device)) {}
845 void BKE_sound_init_once(void) {}
846 void BKE_sound_init(struct Main *UNUSED(bmain)) {}
847 void BKE_sound_exit(void) {}
848 void BKE_sound_exit_once(void) {}
849 void BKE_sound_cache(struct bSound *UNUSED(sound)) {}
850 void BKE_sound_delete_cache(struct bSound *UNUSED(sound)) {}
851 void BKE_sound_load(struct Main *UNUSED(bmain), struct bSound *UNUSED(sound)) {}
852 void BKE_sound_create_scene(struct Scene *UNUSED(scene)) {}
853 void BKE_sound_destroy_scene(struct Scene *UNUSED(scene)) {}
854 void BKE_sound_mute_scene(struct Scene *UNUSED(scene), int UNUSED(muted)) {}
855 void *BKE_sound_scene_add_scene_sound(struct Scene *UNUSED(scene), struct Sequence *UNUSED(sequence),
856                                       int UNUSED(startframe), int UNUSED(endframe), int UNUSED(frameskip)) { return NULL; }
857 void *BKE_sound_scene_add_scene_sound_defaults(struct Scene *UNUSED(scene),
858                                               struct Sequence *UNUSED(sequence)) { return NULL; }
859 void *BKE_sound_add_scene_sound(struct Scene *UNUSED(scene), struct Sequence *UNUSED(sequence), int UNUSED(startframe),
860                                 int UNUSED(endframe), int UNUSED(frameskip)) { return NULL; }
861 void *BKE_sound_add_scene_sound_defaults(struct Scene *UNUSED(scene), struct Sequence *UNUSED(sequence)) { return NULL; }
862 void BKE_sound_remove_scene_sound(struct Scene *UNUSED(scene), void *UNUSED(handle)) {}
863 void BKE_sound_mute_scene_sound(void *UNUSED(handle), char UNUSED(mute)) {}
864 void BKE_sound_move_scene_sound(struct Scene *UNUSED(scene), void *UNUSED(handle), int UNUSED(startframe),
865                                 int UNUSED(endframe), int UNUSED(frameskip)) {}
866 void BKE_sound_move_scene_sound_defaults(struct Scene *UNUSED(scene), struct Sequence *UNUSED(sequence)) {}
867 void BKE_sound_play_scene(struct Scene *UNUSED(scene)) {}
868 void BKE_sound_stop_scene(struct Scene *UNUSED(scene)) {}
869 void BKE_sound_seek_scene(struct Main *UNUSED(bmain), struct Scene *UNUSED(scene)) {}
870 float BKE_sound_sync_scene(struct Scene *UNUSED(scene)) { return NAN_FLT; }
871 int BKE_sound_scene_playing(struct Scene *UNUSED(scene)) { return -1; }
872 void BKE_sound_read_waveform(struct bSound *sound, short *stop) { UNUSED_VARS(sound, stop); }
873 void BKE_sound_init_main(struct Main *UNUSED(bmain)) {}
874 void BKE_sound_set_cfra(int UNUSED(cfra)) {}
875 void BKE_sound_update_sequencer(struct Main *UNUSED(main), struct bSound *UNUSED(sound)) {}
876 void BKE_sound_update_scene(struct Main *UNUSED(bmain), struct Scene *UNUSED(scene)) {}
877 void BKE_sound_update_scene_sound(void *UNUSED(handle), struct bSound *UNUSED(sound)) {}
878 void BKE_sound_update_scene_listener(struct Scene *UNUSED(scene)) {}
879 void BKE_sound_update_fps(struct Scene *UNUSED(scene)) {}
880 void BKE_sound_set_scene_sound_volume(void *UNUSED(handle), float UNUSED(volume), char UNUSED(animated)) {}
881 void BKE_sound_set_scene_sound_pan(void *UNUSED(handle), float UNUSED(pan), char UNUSED(animated)) {}
882 void BKE_sound_set_scene_volume(struct Scene *UNUSED(scene), float UNUSED(volume)) {}
883 void BKE_sound_set_scene_sound_pitch(void *UNUSED(handle), float UNUSED(pitch), char UNUSED(animated)) {}
884 float BKE_sound_get_length(struct bSound *UNUSED(sound)) { return 0; }
885 bool BKE_sound_is_jack_supported(void) { return false; }
886 #endif  /* WITH_AUDASPACE */