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