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