Cleanup unintentional change in rB8a2270efc52a
[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
21  * \ingroup bke
22  */
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "MEM_guardedalloc.h"
28
29 #include "BLI_blenlib.h"
30 #include "BLI_iterator.h"
31 #include "BLI_math.h"
32 #include "BLI_threads.h"
33
34 #include "BLT_translation.h"
35
36 /* Allow using deprecated functionality for .blend file I/O. */
37 #define DNA_DEPRECATED_ALLOW
38
39 #include "DNA_anim_types.h"
40 #include "DNA_object_types.h"
41 #include "DNA_packedFile_types.h"
42 #include "DNA_scene_types.h"
43 #include "DNA_screen_types.h"
44 #include "DNA_sequence_types.h"
45 #include "DNA_sound_types.h"
46 #include "DNA_speaker_types.h"
47 #include "DNA_windowmanager_types.h"
48
49 #ifdef WITH_AUDASPACE
50 #  include "../../../intern/audaspace/intern/AUD_Set.h"
51 #  include <AUD_Handle.h>
52 #  include <AUD_Sequence.h>
53 #  include <AUD_Sound.h>
54 #  include <AUD_Special.h>
55 #endif
56
57 #include "BKE_global.h"
58 #include "BKE_idtype.h"
59 #include "BKE_lib_id.h"
60 #include "BKE_main.h"
61 #include "BKE_packedFile.h"
62 #include "BKE_scene.h"
63 #include "BKE_sound.h"
64
65 #include "DEG_depsgraph.h"
66 #include "DEG_depsgraph_query.h"
67
68 #include "BLO_read_write.h"
69
70 #include "SEQ_sequencer.h"
71
72 static void sound_free_audio(bSound *sound);
73
74 static void sound_copy_data(Main *UNUSED(bmain),
75                             ID *id_dst,
76                             const ID *id_src,
77                             const int UNUSED(flag))
78 {
79   bSound *sound_dst = (bSound *)id_dst;
80   const bSound *sound_src = (const bSound *)id_src;
81
82   sound_dst->handle = NULL;
83   sound_dst->cache = NULL;
84   sound_dst->waveform = NULL;
85   sound_dst->playback_handle = NULL;
86   sound_dst->spinlock = MEM_mallocN(sizeof(SpinLock), "sound_spinlock");
87   BLI_spin_init(sound_dst->spinlock);
88
89   /* Just to be sure, should not have any value actually after reading time. */
90   sound_dst->ipo = NULL;
91   sound_dst->newpackedfile = NULL;
92
93   if (sound_src->packedfile != NULL) {
94     sound_dst->packedfile = BKE_packedfile_duplicate(sound_src->packedfile);
95   }
96
97   BKE_sound_reset_runtime(sound_dst);
98 }
99
100 static void sound_free_data(ID *id)
101 {
102   bSound *sound = (bSound *)id;
103
104   /* No animdata here. */
105
106   if (sound->packedfile) {
107     BKE_packedfile_free(sound->packedfile);
108     sound->packedfile = NULL;
109   }
110
111   sound_free_audio(sound);
112   BKE_sound_free_waveform(sound);
113
114   if (sound->spinlock) {
115     BLI_spin_end(sound->spinlock);
116     MEM_freeN(sound->spinlock);
117     sound->spinlock = NULL;
118   }
119 }
120
121 static void sound_foreach_cache(ID *id,
122                                 IDTypeForeachCacheFunctionCallback function_callback,
123                                 void *user_data)
124 {
125   bSound *sound = (bSound *)id;
126   IDCacheKey key = {
127       .id_session_uuid = id->session_uuid,
128       .offset_in_ID = offsetof(bSound, waveform),
129       .cache_v = sound->waveform,
130   };
131
132   function_callback(id, &key, &sound->waveform, 0, user_data);
133 }
134
135 static void sound_blend_write(BlendWriter *writer, ID *id, const void *id_address)
136 {
137   bSound *sound = (bSound *)id;
138   if (sound->id.us > 0 || BLO_write_is_undo(writer)) {
139     /* Clean up, important in undo case to reduce false detection of changed datablocks. */
140     sound->tags = 0;
141     sound->handle = NULL;
142     sound->playback_handle = NULL;
143     sound->spinlock = NULL;
144
145     /* write LibData */
146     BLO_write_id_struct(writer, bSound, id_address, &sound->id);
147     BKE_id_blend_write(writer, &sound->id);
148
149     BKE_packedfile_blend_write(writer, sound->packedfile);
150   }
151 }
152
153 static void sound_blend_read_data(BlendDataReader *reader, ID *id)
154 {
155   bSound *sound = (bSound *)id;
156   sound->tags = 0;
157   sound->handle = NULL;
158   sound->playback_handle = NULL;
159
160   /* versioning stuff, if there was a cache, then we enable caching: */
161   if (sound->cache) {
162     sound->flags |= SOUND_FLAGS_CACHING;
163     sound->cache = NULL;
164   }
165
166   if (BLO_read_data_is_undo(reader)) {
167     sound->tags |= SOUND_TAGS_WAVEFORM_NO_RELOAD;
168   }
169
170   sound->spinlock = MEM_mallocN(sizeof(SpinLock), "sound_spinlock");
171   BLI_spin_init(sound->spinlock);
172
173   /* clear waveform loading flag */
174   sound->tags &= ~SOUND_TAGS_WAVEFORM_LOADING;
175
176   BKE_packedfile_blend_read(reader, &sound->packedfile);
177   BKE_packedfile_blend_read(reader, &sound->newpackedfile);
178 }
179
180 static void sound_blend_read_lib(BlendLibReader *reader, ID *id)
181 {
182   bSound *sound = (bSound *)id;
183   BLO_read_id_address(
184       reader, sound->id.lib, &sound->ipo); /* XXX deprecated - old animation system */
185 }
186
187 static void sound_blend_read_expand(BlendExpander *expander, ID *id)
188 {
189   bSound *snd = (bSound *)id;
190   BLO_expand(expander, snd->ipo); /* XXX deprecated - old animation system */
191 }
192
193 IDTypeInfo IDType_ID_SO = {
194     .id_code = ID_SO,
195     .id_filter = FILTER_ID_SO,
196     .main_listbase_index = INDEX_ID_SO,
197     .struct_size = sizeof(bSound),
198     .name = "Sound",
199     .name_plural = "sounds",
200     .translation_context = BLT_I18NCONTEXT_ID_SOUND,
201     .flags = IDTYPE_FLAGS_NO_ANIMDATA,
202
203     /* A fuzzy case, think NULLified content is OK here... */
204     .init_data = NULL,
205     .copy_data = sound_copy_data,
206     .free_data = sound_free_data,
207     .make_local = NULL,
208     .foreach_id = NULL,
209     .foreach_cache = sound_foreach_cache,
210
211     .blend_write = sound_blend_write,
212     .blend_read_data = sound_blend_read_data,
213     .blend_read_lib = sound_blend_read_lib,
214     .blend_read_expand = sound_blend_read_expand,
215
216     .blend_read_undo_preserve = NULL,
217 };
218
219 #ifdef WITH_AUDASPACE
220 /* evil globals ;-) */
221 static int sound_cfra;
222 static char **audio_device_names = NULL;
223 #endif
224
225 BLI_INLINE void sound_verify_evaluated_id(const ID *id)
226 {
227   UNUSED_VARS_NDEBUG(id);
228   /* This is a bit tricky and not quite reliable, but good enough check.
229    *
230    * We don't want audio system handles to be allocated on an original data-blocks, and only want
231    * them to be allocated on a data-blocks which are result of dependency graph evaluation.
232    *
233    * Data-blocks which are covered by a copy-on-write system of dependency graph will have
234    * LIB_TAG_COPIED_ON_WRITE tag set on them. But if some of data-blocks during its evaluation
235    * decides to re-allocate its nested one (for example, object evaluation could re-allocate mesh
236    * when evaluating modifier stack). Such data-blocks will have
237    * LIB_TAG_COPIED_ON_WRITE_EVAL_RESULT tag set on them.
238    *
239    * Additionally, we also allow data-blocks outside of main database. Those can not be "original"
240    * and could be used as a temporary evaluated result during operations like baking.
241    *
242    * NOTE: We consider ID evaluated if ANY of those flags is set. We do NOT require ALL of them.
243    */
244   BLI_assert(id->tag &
245              (LIB_TAG_COPIED_ON_WRITE | LIB_TAG_COPIED_ON_WRITE_EVAL_RESULT | LIB_TAG_NO_MAIN));
246 }
247
248 bSound *BKE_sound_new_file(Main *bmain, const char *filepath)
249 {
250   bSound *sound;
251   const char *path;
252   char str[FILE_MAX];
253
254   BLI_strncpy(str, filepath, sizeof(str));
255
256   path = BKE_main_blendfile_path(bmain);
257
258   BLI_path_abs(str, path);
259
260   sound = BKE_libblock_alloc(bmain, ID_SO, BLI_path_basename(filepath), 0);
261   BLI_strncpy(sound->filepath, filepath, FILE_MAX);
262   /* sound->type = SOUND_TYPE_FILE; */ /* XXX unused currently */
263
264   sound->spinlock = MEM_mallocN(sizeof(SpinLock), "sound_spinlock");
265   BLI_spin_init(sound->spinlock);
266
267   BKE_sound_reset_runtime(sound);
268
269   return sound;
270 }
271
272 bSound *BKE_sound_new_file_exists_ex(Main *bmain, const char *filepath, bool *r_exists)
273 {
274   bSound *sound;
275   char str[FILE_MAX], strtest[FILE_MAX];
276
277   BLI_strncpy(str, filepath, sizeof(str));
278   BLI_path_abs(str, BKE_main_blendfile_path(bmain));
279
280   /* first search an identical filepath */
281   for (sound = bmain->sounds.first; sound; sound = sound->id.next) {
282     BLI_strncpy(strtest, sound->filepath, sizeof(sound->filepath));
283     BLI_path_abs(strtest, ID_BLEND_PATH(bmain, &sound->id));
284
285     if (BLI_path_cmp(strtest, str) == 0) {
286       id_us_plus(&sound->id); /* officially should not, it doesn't link here! */
287       if (r_exists) {
288         *r_exists = true;
289       }
290       return sound;
291     }
292   }
293
294   if (r_exists) {
295     *r_exists = false;
296   }
297   return BKE_sound_new_file(bmain, filepath);
298 }
299
300 bSound *BKE_sound_new_file_exists(Main *bmain, const char *filepath)
301 {
302   return BKE_sound_new_file_exists_ex(bmain, filepath, NULL);
303 }
304
305 static void sound_free_audio(bSound *sound)
306 {
307 #ifdef WITH_AUDASPACE
308   if (sound->handle) {
309     AUD_Sound_free(sound->handle);
310     sound->handle = NULL;
311     sound->playback_handle = NULL;
312   }
313
314   if (sound->cache) {
315     AUD_Sound_free(sound->cache);
316     sound->cache = NULL;
317   }
318 #else
319   UNUSED_VARS(sound);
320 #endif /* WITH_AUDASPACE */
321 }
322
323 #ifdef WITH_AUDASPACE
324
325 static const char *force_device = NULL;
326
327 #  ifdef WITH_JACK
328 static SoundJackSyncCallback sound_jack_sync_callback = NULL;
329
330 static void sound_sync_callback(void *data, int mode, float time)
331 {
332   if (sound_jack_sync_callback == NULL) {
333     return;
334   }
335   Main *bmain = (Main *)data;
336   sound_jack_sync_callback(bmain, mode, time);
337 }
338 #  endif
339
340 void BKE_sound_force_device(const char *device)
341 {
342   force_device = device;
343 }
344
345 void BKE_sound_init_once(void)
346 {
347   AUD_initOnce();
348   atexit(BKE_sound_exit_once);
349 }
350
351 static AUD_Device *sound_device = NULL;
352
353 void *BKE_sound_get_device(void)
354 {
355   return sound_device;
356 }
357
358 void BKE_sound_init(Main *bmain)
359 {
360   /* Make sure no instance of the sound system is running, otherwise we get leaks. */
361   BKE_sound_exit();
362
363   AUD_DeviceSpecs specs;
364   int device, buffersize;
365   const char *device_name;
366
367   device = U.audiodevice;
368   buffersize = U.mixbufsize;
369   specs.channels = U.audiochannels;
370   specs.format = U.audioformat;
371   specs.rate = U.audiorate;
372
373   if (force_device == NULL) {
374     int i;
375     char **names = BKE_sound_get_device_names();
376     device_name = names[0];
377
378     /* make sure device is within the bounds of the array */
379     for (i = 0; names[i]; i++) {
380       if (i == device) {
381         device_name = names[i];
382       }
383     }
384   }
385   else {
386     device_name = force_device;
387   }
388
389   if (buffersize < 128) {
390     buffersize = 1024;
391   }
392
393   if (specs.rate < AUD_RATE_8000) {
394     specs.rate = AUD_RATE_48000;
395   }
396
397   if (specs.format <= AUD_FORMAT_INVALID) {
398     specs.format = AUD_FORMAT_S16;
399   }
400
401   if (specs.channels <= AUD_CHANNELS_INVALID) {
402     specs.channels = AUD_CHANNELS_STEREO;
403   }
404
405   if (!(sound_device = AUD_init(device_name, specs, buffersize, "Blender"))) {
406     sound_device = AUD_init("Null", specs, buffersize, "Blender");
407   }
408
409   BKE_sound_init_main(bmain);
410 }
411
412 void BKE_sound_init_main(Main *bmain)
413 {
414 #  ifdef WITH_JACK
415   if (sound_device) {
416     AUD_setSynchronizerCallback(sound_sync_callback, bmain);
417   }
418 #  else
419   UNUSED_VARS(bmain);
420 #  endif
421 }
422
423 void BKE_sound_exit(void)
424 {
425   AUD_exit(sound_device);
426   sound_device = NULL;
427 }
428
429 void BKE_sound_exit_once(void)
430 {
431   AUD_exit(sound_device);
432   sound_device = NULL;
433   AUD_exitOnce();
434
435   if (audio_device_names != NULL) {
436     int i;
437     for (i = 0; audio_device_names[i]; i++) {
438       free(audio_device_names[i]);
439     }
440     free(audio_device_names);
441     audio_device_names = NULL;
442   }
443 }
444
445 /* XXX unused currently */
446 #  if 0
447 bSound *BKE_sound_new_buffer(Main *bmain, bSound *source)
448 {
449   bSound *sound = NULL;
450
451   char name[MAX_ID_NAME + 5];
452   strcpy(name, "buf_");
453   strcpy(name + 4, source->id.name);
454
455   sound = BKE_libblock_alloc(bmain, ID_SO, name);
456
457   sound->child_sound = source;
458   sound->type = SOUND_TYPE_BUFFER;
459
460   sound_load(bmain, sound);
461
462   return sound;
463 }
464
465 bSound *BKE_sound_new_limiter(Main *bmain, bSound *source, float start, float end)
466 {
467   bSound *sound = NULL;
468
469   char name[MAX_ID_NAME + 5];
470   strcpy(name, "lim_");
471   strcpy(name + 4, source->id.name);
472
473   sound = BKE_libblock_alloc(bmain, ID_SO, name);
474
475   sound->child_sound = source;
476   sound->start = start;
477   sound->end = end;
478   sound->type = SOUND_TYPE_LIMITER;
479
480   sound_load(bmain, sound);
481
482   return sound;
483 }
484 #  endif
485
486 void BKE_sound_cache(bSound *sound)
487 {
488   sound_verify_evaluated_id(&sound->id);
489
490   if (sound->cache) {
491     AUD_Sound_free(sound->cache);
492   }
493
494   sound->cache = AUD_Sound_cache(sound->handle);
495   if (sound->cache) {
496     sound->playback_handle = sound->cache;
497   }
498   else {
499     sound->playback_handle = sound->handle;
500   }
501 }
502
503 void BKE_sound_delete_cache(bSound *sound)
504 {
505   if (sound->cache) {
506     AUD_Sound_free(sound->cache);
507     sound->cache = NULL;
508     sound->playback_handle = sound->handle;
509   }
510 }
511
512 static void sound_load_audio(Main *bmain, bSound *sound, bool free_waveform)
513 {
514
515   if (sound->cache) {
516     AUD_Sound_free(sound->cache);
517     sound->cache = NULL;
518   }
519
520   if (sound->handle) {
521     AUD_Sound_free(sound->handle);
522     sound->handle = NULL;
523     sound->playback_handle = NULL;
524   }
525
526   if (free_waveform) {
527     BKE_sound_free_waveform(sound);
528   }
529
530 /* XXX unused currently */
531 #  if 0
532     switch (sound->type) {
533       case SOUND_TYPE_FILE:
534 #  endif
535   {
536     char fullpath[FILE_MAX];
537
538     /* load sound */
539     PackedFile *pf = sound->packedfile;
540
541     /* don't modify soundact->sound->filepath, only change a copy */
542     BLI_strncpy(fullpath, sound->filepath, sizeof(fullpath));
543     BLI_path_abs(fullpath, ID_BLEND_PATH(bmain, &sound->id));
544
545     /* but we need a packed file then */
546     if (pf) {
547       sound->handle = AUD_Sound_bufferFile((unsigned char *)pf->data, pf->size);
548     }
549     else {
550       /* or else load it from disk */
551       sound->handle = AUD_Sound_file(fullpath);
552     }
553   }
554 /* XXX unused currently */
555 #  if 0
556       break;
557     }
558     case SOUND_TYPE_BUFFER:
559       if (sound->child_sound && sound->child_sound->handle) {
560         sound->handle = AUD_bufferSound(sound->child_sound->handle);
561       }
562       break;
563     case SOUND_TYPE_LIMITER:
564       if (sound->child_sound && sound->child_sound->handle) {
565         sound->handle = AUD_limitSound(sound->child_sound, sound->start, sound->end);
566       }
567       break;
568   }
569 #  endif
570   if (sound->flags & SOUND_FLAGS_MONO) {
571     void *handle = AUD_Sound_rechannel(sound->handle, AUD_CHANNELS_MONO);
572     AUD_Sound_free(sound->handle);
573     sound->handle = handle;
574   }
575
576   if (sound->flags & SOUND_FLAGS_CACHING) {
577     sound->cache = AUD_Sound_cache(sound->handle);
578   }
579
580   if (sound->cache) {
581     sound->playback_handle = sound->cache;
582   }
583   else {
584     sound->playback_handle = sound->handle;
585   }
586 }
587
588 void BKE_sound_load(Main *bmain, bSound *sound)
589 {
590   sound_verify_evaluated_id(&sound->id);
591   sound_load_audio(bmain, sound, true);
592 }
593
594 AUD_Device *BKE_sound_mixdown(const Scene *scene, AUD_DeviceSpecs specs, int start, float volume)
595 {
596   sound_verify_evaluated_id(&scene->id);
597   return AUD_openMixdownDevice(specs, scene->sound_scene, volume, start / FPS);
598 }
599
600 void BKE_sound_create_scene(Scene *scene)
601 {
602   sound_verify_evaluated_id(&scene->id);
603
604   /* should be done in version patch, but this gets called before */
605   if (scene->r.frs_sec_base == 0) {
606     scene->r.frs_sec_base = 1;
607   }
608
609   scene->sound_scene = AUD_Sequence_create(FPS, scene->audio.flag & AUDIO_MUTE);
610   AUD_Sequence_setSpeedOfSound(scene->sound_scene, scene->audio.speed_of_sound);
611   AUD_Sequence_setDopplerFactor(scene->sound_scene, scene->audio.doppler_factor);
612   AUD_Sequence_setDistanceModel(scene->sound_scene, scene->audio.distance_model);
613   scene->playback_handle = NULL;
614   scene->sound_scrub_handle = NULL;
615   scene->speaker_handles = NULL;
616 }
617
618 void BKE_sound_destroy_scene(Scene *scene)
619 {
620   if (scene->playback_handle) {
621     AUD_Handle_stop(scene->playback_handle);
622   }
623   if (scene->sound_scrub_handle) {
624     AUD_Handle_stop(scene->sound_scrub_handle);
625   }
626   if (scene->speaker_handles) {
627     void *handle;
628
629     while ((handle = AUD_getSet(scene->speaker_handles))) {
630       AUD_Sequence_remove(scene->sound_scene, handle);
631     }
632
633     AUD_destroySet(scene->speaker_handles);
634   }
635   if (scene->sound_scene) {
636     AUD_Sequence_free(scene->sound_scene);
637   }
638 }
639
640 void BKE_sound_lock()
641 {
642   AUD_Device_lock(sound_device);
643 }
644
645 void BKE_sound_unlock()
646 {
647   AUD_Device_unlock(sound_device);
648 }
649
650 void BKE_sound_reset_scene_specs(Scene *scene)
651 {
652   sound_verify_evaluated_id(&scene->id);
653
654   if (scene->sound_scene) {
655     AUD_Specs specs;
656
657     specs.channels = AUD_Device_getChannels(sound_device);
658     specs.rate = AUD_Device_getRate(sound_device);
659
660     AUD_Sequence_setSpecs(scene->sound_scene, specs);
661   }
662 }
663
664 void BKE_sound_mute_scene(Scene *scene, int muted)
665 {
666   sound_verify_evaluated_id(&scene->id);
667   if (scene->sound_scene) {
668     AUD_Sequence_setMuted(scene->sound_scene, muted);
669   }
670 }
671
672 void BKE_sound_update_fps(Main *bmain, Scene *scene)
673 {
674   sound_verify_evaluated_id(&scene->id);
675
676   if (scene->sound_scene) {
677     AUD_Sequence_setFPS(scene->sound_scene, FPS);
678   }
679
680   BKE_sequencer_refresh_sound_length(bmain, scene);
681 }
682
683 void BKE_sound_update_scene_listener(Scene *scene)
684 {
685   sound_verify_evaluated_id(&scene->id);
686
687   AUD_Sequence_setSpeedOfSound(scene->sound_scene, scene->audio.speed_of_sound);
688   AUD_Sequence_setDopplerFactor(scene->sound_scene, scene->audio.doppler_factor);
689   AUD_Sequence_setDistanceModel(scene->sound_scene, scene->audio.distance_model);
690 }
691
692 void *BKE_sound_scene_add_scene_sound(
693     Scene *scene, Sequence *sequence, int startframe, int endframe, int frameskip)
694 {
695   sound_verify_evaluated_id(&scene->id);
696   if (sequence->scene && scene != sequence->scene) {
697     const double fps = FPS;
698     return AUD_Sequence_add(scene->sound_scene,
699                             sequence->scene->sound_scene,
700                             startframe / fps,
701                             endframe / fps,
702                             frameskip / fps);
703   }
704   return NULL;
705 }
706
707 void *BKE_sound_scene_add_scene_sound_defaults(Scene *scene, Sequence *sequence)
708 {
709   return BKE_sound_scene_add_scene_sound(scene,
710                                          sequence,
711                                          sequence->startdisp,
712                                          sequence->enddisp,
713                                          sequence->startofs + sequence->anim_startofs);
714 }
715
716 void *BKE_sound_add_scene_sound(
717     Scene *scene, Sequence *sequence, int startframe, int endframe, int frameskip)
718 {
719   sound_verify_evaluated_id(&scene->id);
720   /* Happens when sequence's sound data-block was removed. */
721   if (sequence->sound == NULL) {
722     return NULL;
723   }
724   sound_verify_evaluated_id(&sequence->sound->id);
725   const double fps = FPS;
726   void *handle = AUD_Sequence_add(scene->sound_scene,
727                                   sequence->sound->playback_handle,
728                                   startframe / fps,
729                                   endframe / fps,
730                                   frameskip / fps);
731   AUD_SequenceEntry_setMuted(handle, (sequence->flag & SEQ_MUTE) != 0);
732   AUD_SequenceEntry_setAnimationData(handle, AUD_AP_VOLUME, CFRA, &sequence->volume, 0);
733   AUD_SequenceEntry_setAnimationData(handle, AUD_AP_PITCH, CFRA, &sequence->pitch, 0);
734   AUD_SequenceEntry_setAnimationData(handle, AUD_AP_PANNING, CFRA, &sequence->pan, 0);
735   return handle;
736 }
737
738 void *BKE_sound_add_scene_sound_defaults(Scene *scene, Sequence *sequence)
739 {
740   return BKE_sound_add_scene_sound(scene,
741                                    sequence,
742                                    sequence->startdisp,
743                                    sequence->enddisp,
744                                    sequence->startofs + sequence->anim_startofs);
745 }
746
747 void BKE_sound_remove_scene_sound(Scene *scene, void *handle)
748 {
749   AUD_Sequence_remove(scene->sound_scene, handle);
750 }
751
752 void BKE_sound_mute_scene_sound(void *handle, char mute)
753 {
754   AUD_SequenceEntry_setMuted(handle, mute);
755 }
756
757 void BKE_sound_move_scene_sound(
758     Scene *scene, void *handle, int startframe, int endframe, int frameskip)
759 {
760   sound_verify_evaluated_id(&scene->id);
761   const double fps = FPS;
762   AUD_SequenceEntry_move(handle, startframe / fps, endframe / fps, frameskip / fps);
763 }
764
765 void BKE_sound_move_scene_sound_defaults(Scene *scene, Sequence *sequence)
766 {
767   sound_verify_evaluated_id(&scene->id);
768   if (sequence->scene_sound) {
769     BKE_sound_move_scene_sound(scene,
770                                sequence->scene_sound,
771                                sequence->startdisp,
772                                sequence->enddisp,
773                                sequence->startofs + sequence->anim_startofs);
774   }
775 }
776
777 void BKE_sound_update_scene_sound(void *handle, bSound *sound)
778 {
779   AUD_SequenceEntry_setSound(handle, sound->playback_handle);
780 }
781
782 void BKE_sound_set_cfra(int cfra)
783 {
784   sound_cfra = cfra;
785 }
786
787 void BKE_sound_set_scene_volume(Scene *scene, float volume)
788 {
789   sound_verify_evaluated_id(&scene->id);
790   if (scene->sound_scene == NULL) {
791     return;
792   }
793   AUD_Sequence_setAnimationData(scene->sound_scene,
794                                 AUD_AP_VOLUME,
795                                 CFRA,
796                                 &volume,
797                                 (scene->audio.flag & AUDIO_VOLUME_ANIMATED) != 0);
798 }
799
800 void BKE_sound_set_scene_sound_volume(void *handle, float volume, char animated)
801 {
802   AUD_SequenceEntry_setAnimationData(handle, AUD_AP_VOLUME, sound_cfra, &volume, animated);
803 }
804
805 void BKE_sound_set_scene_sound_pitch(void *handle, float pitch, char animated)
806 {
807   AUD_SequenceEntry_setAnimationData(handle, AUD_AP_PITCH, sound_cfra, &pitch, animated);
808 }
809
810 void BKE_sound_set_scene_sound_pan(void *handle, float pan, char animated)
811 {
812   AUD_SequenceEntry_setAnimationData(handle, AUD_AP_PANNING, sound_cfra, &pan, animated);
813 }
814
815 void BKE_sound_update_sequencer(Main *main, bSound *sound)
816 {
817   BLI_assert(!"is not supposed to be used, is weird function.");
818
819   Scene *scene;
820
821   for (scene = main->scenes.first; scene; scene = scene->id.next) {
822     BKE_sequencer_update_sound(scene, sound);
823   }
824 }
825
826 static void sound_start_play_scene(Scene *scene)
827 {
828   sound_verify_evaluated_id(&scene->id);
829
830   if (scene->playback_handle) {
831     AUD_Handle_stop(scene->playback_handle);
832   }
833
834   BKE_sound_reset_scene_specs(scene);
835
836   if ((scene->playback_handle = AUD_Device_play(sound_device, scene->sound_scene, 1))) {
837     AUD_Handle_setLoopCount(scene->playback_handle, -1);
838   }
839 }
840
841 static double get_cur_time(Scene *scene)
842 {
843   /* We divide by the current framelen to take into account time remapping.
844    * Otherwise we will get the wrong starting time which will break A/V sync.
845    * See T74111 for further details. */
846   return FRA2TIME((CFRA + SUBFRA) / (double)scene->r.framelen);
847 }
848
849 void BKE_sound_play_scene(Scene *scene)
850 {
851   sound_verify_evaluated_id(&scene->id);
852
853   AUD_Status status;
854   const double cur_time = get_cur_time(scene);
855
856   AUD_Device_lock(sound_device);
857
858   status = scene->playback_handle ? AUD_Handle_getStatus(scene->playback_handle) :
859                                     AUD_STATUS_INVALID;
860
861   if (status == AUD_STATUS_INVALID) {
862     sound_start_play_scene(scene);
863
864     if (!scene->playback_handle) {
865       AUD_Device_unlock(sound_device);
866       return;
867     }
868   }
869
870   if (status != AUD_STATUS_PLAYING) {
871     AUD_Handle_setPosition(scene->playback_handle, cur_time);
872     AUD_Handle_resume(scene->playback_handle);
873   }
874
875   if (scene->audio.flag & AUDIO_SYNC) {
876     AUD_playSynchronizer();
877   }
878
879   AUD_Device_unlock(sound_device);
880 }
881
882 void BKE_sound_stop_scene(Scene *scene)
883 {
884   if (scene->playback_handle) {
885     AUD_Handle_pause(scene->playback_handle);
886
887     if (scene->audio.flag & AUDIO_SYNC) {
888       AUD_stopSynchronizer();
889     }
890   }
891 }
892
893 void BKE_sound_seek_scene(Main *bmain, Scene *scene)
894 {
895   sound_verify_evaluated_id(&scene->id);
896
897   AUD_Status status;
898   bScreen *screen;
899   int animation_playing;
900
901   const double one_frame = 1.0 / FPS;
902   const double cur_time = FRA2TIME(CFRA);
903
904   AUD_Device_lock(sound_device);
905
906   status = scene->playback_handle ? AUD_Handle_getStatus(scene->playback_handle) :
907                                     AUD_STATUS_INVALID;
908
909   if (status == AUD_STATUS_INVALID) {
910     sound_start_play_scene(scene);
911
912     if (!scene->playback_handle) {
913       AUD_Device_unlock(sound_device);
914       return;
915     }
916
917     AUD_Handle_pause(scene->playback_handle);
918   }
919
920   animation_playing = 0;
921   for (screen = bmain->screens.first; screen; screen = screen->id.next) {
922     if (screen->animtimer) {
923       animation_playing = 1;
924       break;
925     }
926   }
927
928   if (scene->audio.flag & AUDIO_SCRUB && !animation_playing) {
929     AUD_Handle_setPosition(scene->playback_handle, cur_time);
930     if (scene->audio.flag & AUDIO_SYNC) {
931       AUD_seekSynchronizer(scene->playback_handle, cur_time);
932     }
933     AUD_Handle_resume(scene->playback_handle);
934     if (scene->sound_scrub_handle &&
935         AUD_Handle_getStatus(scene->sound_scrub_handle) != AUD_STATUS_INVALID) {
936       AUD_Handle_setPosition(scene->sound_scrub_handle, 0);
937     }
938     else {
939       if (scene->sound_scrub_handle) {
940         AUD_Handle_stop(scene->sound_scrub_handle);
941       }
942       scene->sound_scrub_handle = AUD_pauseAfter(scene->playback_handle, one_frame);
943     }
944   }
945   else {
946     if (scene->audio.flag & AUDIO_SYNC) {
947       AUD_seekSynchronizer(scene->playback_handle, cur_time);
948     }
949     else {
950       if (status == AUD_STATUS_PLAYING) {
951         AUD_Handle_setPosition(scene->playback_handle, cur_time);
952       }
953     }
954   }
955
956   AUD_Device_unlock(sound_device);
957 }
958
959 double BKE_sound_sync_scene(Scene *scene)
960 {
961   sound_verify_evaluated_id(&scene->id);
962
963   /* Ugly: Blender doesn't like it when the animation is played back during rendering */
964   if (G.is_rendering) {
965     return NAN_FLT;
966   }
967
968   if (scene->playback_handle) {
969     if (scene->audio.flag & AUDIO_SYNC) {
970       return AUD_getSynchronizerPosition(scene->playback_handle);
971     }
972
973     return AUD_Handle_getPosition(scene->playback_handle);
974   }
975   return NAN_FLT;
976 }
977
978 int BKE_sound_scene_playing(Scene *scene)
979 {
980   sound_verify_evaluated_id(&scene->id);
981
982   /* Ugly: Blender doesn't like it when the animation is played back during rendering */
983   if (G.is_rendering) {
984     return -1;
985   }
986
987   /* In case of a "Null" audio device, we have no playback information. */
988   if (AUD_Device_getRate(sound_device) == AUD_RATE_INVALID) {
989     return -1;
990   }
991
992   if (scene->audio.flag & AUDIO_SYNC) {
993     return AUD_isSynchronizerPlaying();
994   }
995
996   return -1;
997 }
998
999 void BKE_sound_free_waveform(bSound *sound)
1000 {
1001   if ((sound->tags & SOUND_TAGS_WAVEFORM_NO_RELOAD) == 0) {
1002     SoundWaveform *waveform = sound->waveform;
1003     if (waveform) {
1004       if (waveform->data) {
1005         MEM_freeN(waveform->data);
1006       }
1007       MEM_freeN(waveform);
1008     }
1009
1010     sound->waveform = NULL;
1011   }
1012   /* This tag is only valid once. */
1013   sound->tags &= ~SOUND_TAGS_WAVEFORM_NO_RELOAD;
1014 }
1015
1016 void BKE_sound_read_waveform(Main *bmain, bSound *sound, short *stop)
1017 {
1018   bool need_close_audio_handles = false;
1019   if (sound->playback_handle == NULL) {
1020     /* TODO(sergey): Make it fully independent audio handle. */
1021     sound_load_audio(bmain, sound, true);
1022     need_close_audio_handles = true;
1023   }
1024
1025   AUD_SoundInfo info = AUD_getInfo(sound->playback_handle);
1026   SoundWaveform *waveform = MEM_mallocN(sizeof(SoundWaveform), "SoundWaveform");
1027
1028   if (info.length > 0) {
1029     int length = info.length * SOUND_WAVE_SAMPLES_PER_SECOND;
1030
1031     waveform->data = MEM_mallocN(sizeof(float[3]) * length, "SoundWaveform.samples");
1032     waveform->length = AUD_readSound(
1033         sound->playback_handle, waveform->data, length, SOUND_WAVE_SAMPLES_PER_SECOND, stop);
1034   }
1035   else {
1036     /* Create an empty waveform here if the sound couldn't be
1037      * read. This indicates that reading the waveform is "done",
1038      * whereas just setting sound->waveform to NULL causes other
1039      * code to think the waveform still needs to be created. */
1040     waveform->data = NULL;
1041     waveform->length = 0;
1042   }
1043
1044   if (*stop) {
1045     if (waveform->data) {
1046       MEM_freeN(waveform->data);
1047     }
1048     MEM_freeN(waveform);
1049     BLI_spin_lock(sound->spinlock);
1050     sound->tags &= ~SOUND_TAGS_WAVEFORM_LOADING;
1051     BLI_spin_unlock(sound->spinlock);
1052     return;
1053   }
1054
1055   BKE_sound_free_waveform(sound);
1056
1057   BLI_spin_lock(sound->spinlock);
1058   sound->waveform = waveform;
1059   sound->tags &= ~SOUND_TAGS_WAVEFORM_LOADING;
1060   BLI_spin_unlock(sound->spinlock);
1061
1062   if (need_close_audio_handles) {
1063     sound_free_audio(sound);
1064   }
1065 }
1066
1067 static void sound_update_base(Scene *scene, Object *object, void *new_set)
1068 {
1069   NlaTrack *track;
1070   NlaStrip *strip;
1071   Speaker *speaker;
1072   float quat[4];
1073
1074   sound_verify_evaluated_id(&scene->id);
1075   sound_verify_evaluated_id(&object->id);
1076
1077   if ((object->type != OB_SPEAKER) || !object->adt) {
1078     return;
1079   }
1080
1081   for (track = object->adt->nla_tracks.first; track; track = track->next) {
1082     for (strip = track->strips.first; strip; strip = strip->next) {
1083       if (strip->type != NLASTRIP_TYPE_SOUND) {
1084         continue;
1085       }
1086       speaker = (Speaker *)object->data;
1087
1088       if (AUD_removeSet(scene->speaker_handles, strip->speaker_handle)) {
1089         if (speaker->sound) {
1090           AUD_SequenceEntry_move(strip->speaker_handle, (double)strip->start / FPS, FLT_MAX, 0);
1091         }
1092         else {
1093           AUD_Sequence_remove(scene->sound_scene, strip->speaker_handle);
1094           strip->speaker_handle = NULL;
1095         }
1096       }
1097       else {
1098         if (speaker->sound) {
1099           strip->speaker_handle = AUD_Sequence_add(scene->sound_scene,
1100                                                    speaker->sound->playback_handle,
1101                                                    (double)strip->start / FPS,
1102                                                    FLT_MAX,
1103                                                    0);
1104           AUD_SequenceEntry_setRelative(strip->speaker_handle, 0);
1105         }
1106       }
1107
1108       if (strip->speaker_handle) {
1109         const bool mute = ((strip->flag & NLASTRIP_FLAG_MUTED) || (speaker->flag & SPK_MUTED));
1110         AUD_addSet(new_set, strip->speaker_handle);
1111         AUD_SequenceEntry_setVolumeMaximum(strip->speaker_handle, speaker->volume_max);
1112         AUD_SequenceEntry_setVolumeMinimum(strip->speaker_handle, speaker->volume_min);
1113         AUD_SequenceEntry_setDistanceMaximum(strip->speaker_handle, speaker->distance_max);
1114         AUD_SequenceEntry_setDistanceReference(strip->speaker_handle, speaker->distance_reference);
1115         AUD_SequenceEntry_setAttenuation(strip->speaker_handle, speaker->attenuation);
1116         AUD_SequenceEntry_setConeAngleOuter(strip->speaker_handle, speaker->cone_angle_outer);
1117         AUD_SequenceEntry_setConeAngleInner(strip->speaker_handle, speaker->cone_angle_inner);
1118         AUD_SequenceEntry_setConeVolumeOuter(strip->speaker_handle, speaker->cone_volume_outer);
1119
1120         mat4_to_quat(quat, object->obmat);
1121         AUD_SequenceEntry_setAnimationData(
1122             strip->speaker_handle, AUD_AP_LOCATION, CFRA, object->obmat[3], 1);
1123         AUD_SequenceEntry_setAnimationData(
1124             strip->speaker_handle, AUD_AP_ORIENTATION, CFRA, quat, 1);
1125         AUD_SequenceEntry_setAnimationData(
1126             strip->speaker_handle, AUD_AP_VOLUME, CFRA, &speaker->volume, 1);
1127         AUD_SequenceEntry_setAnimationData(
1128             strip->speaker_handle, AUD_AP_PITCH, CFRA, &speaker->pitch, 1);
1129         AUD_SequenceEntry_setSound(strip->speaker_handle, speaker->sound->playback_handle);
1130         AUD_SequenceEntry_setMuted(strip->speaker_handle, mute);
1131       }
1132     }
1133   }
1134 }
1135
1136 void BKE_sound_update_scene(Depsgraph *depsgraph, Scene *scene)
1137 {
1138   sound_verify_evaluated_id(&scene->id);
1139
1140   void *new_set = AUD_createSet();
1141   void *handle;
1142   float quat[4];
1143
1144   /* cheap test to skip looping over all objects (no speakers is a common case) */
1145   if (DEG_id_type_any_exists(depsgraph, ID_SPK)) {
1146     DEG_OBJECT_ITER_BEGIN (depsgraph,
1147                            object,
1148                            (DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY |
1149                             DEG_ITER_OBJECT_FLAG_LINKED_INDIRECTLY |
1150                             DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET)) {
1151       sound_update_base(scene, object, new_set);
1152     }
1153     DEG_OBJECT_ITER_END;
1154   }
1155
1156   while ((handle = AUD_getSet(scene->speaker_handles))) {
1157     AUD_Sequence_remove(scene->sound_scene, handle);
1158   }
1159
1160   if (scene->camera) {
1161     mat4_to_quat(quat, scene->camera->obmat);
1162     AUD_Sequence_setAnimationData(
1163         scene->sound_scene, AUD_AP_LOCATION, CFRA, scene->camera->obmat[3], 1);
1164     AUD_Sequence_setAnimationData(scene->sound_scene, AUD_AP_ORIENTATION, CFRA, quat, 1);
1165   }
1166
1167   AUD_destroySet(scene->speaker_handles);
1168   scene->speaker_handles = new_set;
1169 }
1170
1171 void *BKE_sound_get_factory(void *sound)
1172 {
1173   return ((bSound *)sound)->playback_handle;
1174 }
1175
1176 float BKE_sound_get_length(Main *bmain, bSound *sound)
1177 {
1178   if (sound->playback_handle != NULL) {
1179     AUD_SoundInfo info = AUD_getInfo(sound->playback_handle);
1180     return info.length;
1181   }
1182   SoundInfo info;
1183   if (!BKE_sound_info_get(bmain, sound, &info)) {
1184     return 0.0f;
1185   }
1186   return info.length;
1187 }
1188
1189 char **BKE_sound_get_device_names(void)
1190 {
1191   if (audio_device_names == NULL) {
1192     audio_device_names = AUD_getDeviceNames();
1193   }
1194
1195   return audio_device_names;
1196 }
1197
1198 static bool sound_info_from_playback_handle(void *playback_handle, SoundInfo *sound_info)
1199 {
1200   if (playback_handle == NULL) {
1201     return false;
1202   }
1203   AUD_SoundInfo info = AUD_getInfo(playback_handle);
1204   sound_info->specs.channels = (eSoundChannels)info.specs.channels;
1205   sound_info->length = info.length;
1206   return true;
1207 }
1208
1209 bool BKE_sound_info_get(struct Main *main, struct bSound *sound, SoundInfo *sound_info)
1210 {
1211   if (sound->playback_handle != NULL) {
1212     return sound_info_from_playback_handle(sound->playback_handle, sound_info);
1213   }
1214   /* TODO(sergey): Make it fully independent audio handle. */
1215   /* Don't free waveforms during non-destructive queries.
1216    * This causes unnecessary recalculation - see T69921 */
1217   sound_load_audio(main, sound, false);
1218   const bool result = sound_info_from_playback_handle(sound->playback_handle, sound_info);
1219   sound_free_audio(sound);
1220   return result;
1221 }
1222
1223 #else /* WITH_AUDASPACE */
1224
1225 #  include "BLI_utildefines.h"
1226
1227 void BKE_sound_force_device(const char *UNUSED(device))
1228 {
1229 }
1230 void BKE_sound_init_once(void)
1231 {
1232 }
1233 void BKE_sound_init(Main *UNUSED(bmain))
1234 {
1235 }
1236 void BKE_sound_exit(void)
1237 {
1238 }
1239 void BKE_sound_exit_once(void)
1240 {
1241 }
1242 void BKE_sound_cache(bSound *UNUSED(sound))
1243 {
1244 }
1245 void BKE_sound_delete_cache(bSound *UNUSED(sound))
1246 {
1247 }
1248 void BKE_sound_load(Main *UNUSED(bmain), bSound *UNUSED(sound))
1249 {
1250 }
1251 void BKE_sound_create_scene(Scene *UNUSED(scene))
1252 {
1253 }
1254 void BKE_sound_destroy_scene(Scene *UNUSED(scene))
1255 {
1256 }
1257 void BKE_sound_lock(void)
1258 {
1259 }
1260 void BKE_sound_unlock(void)
1261 {
1262 }
1263 void BKE_sound_reset_scene_specs(Scene *UNUSED(scene))
1264 {
1265 }
1266 void BKE_sound_mute_scene(Scene *UNUSED(scene), int UNUSED(muted))
1267 {
1268 }
1269 void *BKE_sound_scene_add_scene_sound(Scene *UNUSED(scene),
1270                                       Sequence *UNUSED(sequence),
1271                                       int UNUSED(startframe),
1272                                       int UNUSED(endframe),
1273                                       int UNUSED(frameskip))
1274 {
1275   return NULL;
1276 }
1277 void *BKE_sound_scene_add_scene_sound_defaults(Scene *UNUSED(scene), Sequence *UNUSED(sequence))
1278 {
1279   return NULL;
1280 }
1281 void *BKE_sound_add_scene_sound(Scene *UNUSED(scene),
1282                                 Sequence *UNUSED(sequence),
1283                                 int UNUSED(startframe),
1284                                 int UNUSED(endframe),
1285                                 int UNUSED(frameskip))
1286 {
1287   return NULL;
1288 }
1289 void *BKE_sound_add_scene_sound_defaults(Scene *UNUSED(scene), Sequence *UNUSED(sequence))
1290 {
1291   return NULL;
1292 }
1293 void BKE_sound_remove_scene_sound(Scene *UNUSED(scene), void *UNUSED(handle))
1294 {
1295 }
1296 void BKE_sound_mute_scene_sound(void *UNUSED(handle), char UNUSED(mute))
1297 {
1298 }
1299 void BKE_sound_move_scene_sound(Scene *UNUSED(scene),
1300                                 void *UNUSED(handle),
1301                                 int UNUSED(startframe),
1302                                 int UNUSED(endframe),
1303                                 int UNUSED(frameskip))
1304 {
1305 }
1306 void BKE_sound_move_scene_sound_defaults(Scene *UNUSED(scene), Sequence *UNUSED(sequence))
1307 {
1308 }
1309 void BKE_sound_play_scene(Scene *UNUSED(scene))
1310 {
1311 }
1312 void BKE_sound_stop_scene(Scene *UNUSED(scene))
1313 {
1314 }
1315 void BKE_sound_seek_scene(Main *UNUSED(bmain), Scene *UNUSED(scene))
1316 {
1317 }
1318 double BKE_sound_sync_scene(Scene *UNUSED(scene))
1319 {
1320   return NAN_FLT;
1321 }
1322 int BKE_sound_scene_playing(Scene *UNUSED(scene))
1323 {
1324   return -1;
1325 }
1326 void BKE_sound_read_waveform(Main *bmain,
1327                              bSound *sound,
1328                              /* NOLINTNEXTLINE: readability-non-const-parameter. */
1329                              short *stop)
1330 {
1331   UNUSED_VARS(sound, stop, bmain);
1332 }
1333 void BKE_sound_init_main(Main *UNUSED(bmain))
1334 {
1335 }
1336 void BKE_sound_set_cfra(int UNUSED(cfra))
1337 {
1338 }
1339 void BKE_sound_update_sequencer(Main *UNUSED(main), bSound *UNUSED(sound))
1340 {
1341 }
1342 void BKE_sound_update_scene(Depsgraph *UNUSED(depsgraph), Scene *UNUSED(scene))
1343 {
1344 }
1345 void BKE_sound_update_scene_sound(void *UNUSED(handle), bSound *UNUSED(sound))
1346 {
1347 }
1348 void BKE_sound_update_scene_listener(Scene *UNUSED(scene))
1349 {
1350 }
1351 void BKE_sound_update_fps(Main *UNUSED(bmain), Scene *UNUSED(scene))
1352 {
1353 }
1354 void BKE_sound_set_scene_sound_volume(void *UNUSED(handle),
1355                                       float UNUSED(volume),
1356                                       char UNUSED(animated))
1357 {
1358 }
1359 void BKE_sound_set_scene_sound_pan(void *UNUSED(handle), float UNUSED(pan), char UNUSED(animated))
1360 {
1361 }
1362 void BKE_sound_set_scene_volume(Scene *UNUSED(scene), float UNUSED(volume))
1363 {
1364 }
1365 void BKE_sound_set_scene_sound_pitch(void *UNUSED(handle),
1366                                      float UNUSED(pitch),
1367                                      char UNUSED(animated))
1368 {
1369 }
1370 float BKE_sound_get_length(struct Main *UNUSED(bmain), bSound *UNUSED(sound))
1371 {
1372   return 0;
1373 }
1374 char **BKE_sound_get_device_names(void)
1375 {
1376   static char *names[1] = {NULL};
1377   return names;
1378 }
1379
1380 void BKE_sound_free_waveform(bSound *UNUSED(sound))
1381 {
1382 }
1383
1384 bool BKE_sound_info_get(struct Main *UNUSED(main),
1385                         struct bSound *UNUSED(sound),
1386                         SoundInfo *UNUSED(sound_info))
1387 {
1388   return false;
1389 }
1390
1391 #endif /* WITH_AUDASPACE */
1392
1393 void BKE_sound_reset_scene_runtime(Scene *scene)
1394 {
1395   scene->sound_scene = NULL;
1396   scene->playback_handle = NULL;
1397   scene->sound_scrub_handle = NULL;
1398   scene->speaker_handles = NULL;
1399 }
1400
1401 void BKE_sound_ensure_scene(struct Scene *scene)
1402 {
1403   if (scene->sound_scene != NULL) {
1404     return;
1405   }
1406   BKE_sound_create_scene(scene);
1407 }
1408
1409 void BKE_sound_reset_runtime(bSound *sound)
1410 {
1411   sound->cache = NULL;
1412   sound->playback_handle = NULL;
1413 }
1414
1415 void BKE_sound_ensure_loaded(Main *bmain, bSound *sound)
1416 {
1417   if (sound->cache != NULL) {
1418     return;
1419   }
1420   BKE_sound_load(bmain, sound);
1421 }
1422
1423 void BKE_sound_jack_sync_callback_set(SoundJackSyncCallback callback)
1424 {
1425 #if defined(WITH_AUDASPACE) && defined(WITH_JACK)
1426   sound_jack_sync_callback = callback;
1427 #else
1428   UNUSED_VARS(callback);
1429 #endif
1430 }
1431
1432 void BKE_sound_jack_scene_update(Scene *scene, int mode, double time)
1433 {
1434   sound_verify_evaluated_id(&scene->id);
1435
1436   /* Ugly: Blender doesn't like it when the animation is played back during rendering. */
1437   if (G.is_rendering) {
1438     return;
1439   }
1440
1441   if (mode) {
1442     BKE_sound_play_scene(scene);
1443   }
1444   else {
1445     BKE_sound_stop_scene(scene);
1446   }
1447 #ifdef WITH_AUDASPACE
1448   if (scene->playback_handle != NULL) {
1449     AUD_Handle_setPosition(scene->playback_handle, time);
1450   }
1451 #else
1452   UNUSED_VARS(time);
1453 #endif
1454 }
1455
1456 void BKE_sound_evaluate(Depsgraph *depsgraph, Main *bmain, bSound *sound)
1457 {
1458   DEG_debug_print_eval(depsgraph, __func__, sound->id.name, sound);
1459   if (sound->id.recalc & ID_RECALC_AUDIO) {
1460     BKE_sound_load(bmain, sound);
1461     return;
1462   }
1463   BKE_sound_ensure_loaded(bmain, sound);
1464 }