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