Blender V4.5
blenkernel/intern/sound.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <chrono>
10#include <condition_variable>
11#include <cstdlib>
12#include <cstring>
13#include <mutex>
14#include <optional>
15#include <thread>
16
17#include "MEM_guardedalloc.h"
18
19#include "BLI_build_config.h"
20#include "BLI_listbase.h"
21#include "BLI_math_base.h"
22#include "BLI_math_rotation.h"
23#include "BLI_path_utils.hh"
24#include "BLI_string.h"
25#include "BLI_threads.h"
26
27#include "BLT_translation.hh"
28
29/* Allow using deprecated functionality for .blend file I/O. */
30#define DNA_DEPRECATED_ALLOW
31
32#include "DNA_anim_types.h"
33#include "DNA_object_types.h"
35#include "DNA_scene_types.h"
36#include "DNA_screen_types.h"
37#include "DNA_sequence_types.h"
38#include "DNA_sound_types.h"
39#include "DNA_speaker_types.h"
40#include "DNA_userdef_types.h"
41
42#ifdef WITH_AUDASPACE
44# include <AUD_Handle.h>
45# include <AUD_Sequence.h>
46# include <AUD_Sound.h>
47# include <AUD_Special.h>
48#endif
49
50#include "BKE_bpath.hh"
51#include "BKE_global.hh"
52#include "BKE_idtype.hh"
53#include "BKE_lib_id.hh"
54#include "BKE_lib_query.hh"
55#include "BKE_library.hh"
56#include "BKE_main.hh"
57#include "BKE_packedFile.hh"
58#include "BKE_sound.h"
59
60#include "DEG_depsgraph.hh"
62
63#include "BLO_read_write.hh"
64
65#include "SEQ_sound.hh"
66#include "SEQ_time.hh"
67
68#include "CLG_log.h"
69
70static void sound_free_audio(bSound *sound);
71
72static void sound_copy_data(Main * /*bmain*/,
73 std::optional<Library *> /*owner_library*/,
74 ID *id_dst,
75 const ID *id_src,
76 const int /*flag*/)
77{
78 bSound *sound_dst = (bSound *)id_dst;
79 const bSound *sound_src = (const bSound *)id_src;
80
81 sound_dst->handle = nullptr;
82 sound_dst->cache = nullptr;
83 sound_dst->waveform = nullptr;
84 sound_dst->playback_handle = nullptr;
85 sound_dst->spinlock = (void *)MEM_mallocN<SpinLock>("sound_spinlock");
86 BLI_spin_init(static_cast<SpinLock *>(sound_dst->spinlock));
87
88 /* Just to be sure, should not have any value actually after reading time. */
89 sound_dst->ipo = nullptr;
90 sound_dst->newpackedfile = nullptr;
91
92 if (sound_src->packedfile != nullptr) {
93 sound_dst->packedfile = BKE_packedfile_duplicate(sound_src->packedfile);
94 }
95
96 BKE_sound_reset_runtime(sound_dst);
97}
98
99static void sound_free_data(ID *id)
100{
101 bSound *sound = (bSound *)id;
102
103 /* No animation-data here. */
104
105 if (sound->packedfile) {
107 sound->packedfile = nullptr;
108 }
109
110 sound_free_audio(sound);
112
113 if (sound->spinlock) {
114 BLI_spin_end(static_cast<SpinLock *>(sound->spinlock));
115 /* The void cast is needed when building without TBB. */
116 MEM_freeN((void *)static_cast<SpinLock *>(sound->spinlock));
117 sound->spinlock = nullptr;
118 }
119}
120
122{
123 bSound *sound = reinterpret_cast<bSound *>(id);
125
128 }
129}
130
131static void sound_foreach_cache(ID *id,
132 IDTypeForeachCacheFunctionCallback function_callback,
133 void *user_data)
134{
135 bSound *sound = (bSound *)id;
136 IDCacheKey key{};
137 key.id_session_uid = id->session_uid;
138 key.identifier = offsetof(bSound, waveform);
139
140 function_callback(id, &key, &sound->waveform, 0, user_data);
141}
142
143static void sound_foreach_path(ID *id, BPathForeachPathData *bpath_data)
144{
145 bSound *sound = (bSound *)id;
146 if (sound->packedfile != nullptr && (bpath_data->flag & BKE_BPATH_FOREACH_PATH_SKIP_PACKED) != 0)
147 {
148 return;
149 }
150
151 /* FIXME: This does not check for empty path... */
152 BKE_bpath_foreach_path_fixed_process(bpath_data, sound->filepath, sizeof(sound->filepath));
153}
154
155static void sound_blend_write(BlendWriter *writer, ID *id, const void *id_address)
156{
157 bSound *sound = (bSound *)id;
158 const bool is_undo = BLO_write_is_undo(writer);
159
160 /* Clean up, important in undo case to reduce false detection of changed datablocks. */
161 sound->tags = 0;
162 sound->handle = nullptr;
163 sound->playback_handle = nullptr;
164 sound->spinlock = nullptr;
165
166 /* Do not store packed files in case this is a library override ID. */
167 if (ID_IS_OVERRIDE_LIBRARY(sound) && !is_undo) {
168 sound->packedfile = nullptr;
169 }
170
171 /* write LibData */
172 BLO_write_id_struct(writer, bSound, id_address, &sound->id);
173 BKE_id_blend_write(writer, &sound->id);
174
176}
177
178static void sound_blend_read_data(BlendDataReader *reader, ID *id)
179{
180 bSound *sound = (bSound *)id;
181 sound->tags = 0;
182 sound->handle = nullptr;
183 sound->playback_handle = nullptr;
184
185 /* versioning stuff, if there was a cache, then we enable caching: */
186 if (sound->cache) {
187 sound->flags |= SOUND_FLAGS_CACHING;
188 sound->cache = nullptr;
189 }
190
191 if (BLO_read_data_is_undo(reader)) {
193 }
194
195 sound->spinlock = (void *)MEM_mallocN<SpinLock>("sound_spinlock");
196 BLI_spin_init(static_cast<SpinLock *>(sound->spinlock));
197
198 /* clear waveform loading flag */
200
201 BKE_packedfile_blend_read(reader, &sound->packedfile, sound->filepath);
202 BKE_packedfile_blend_read(reader, &sound->newpackedfile, sound->filepath);
203}
204
206 /*id_code*/ bSound::id_type,
207 /*id_filter*/ FILTER_ID_SO,
208 /*dependencies_id_types*/ 0,
209 /*main_listbase_index*/ INDEX_ID_SO,
210 /*struct_size*/ sizeof(bSound),
211 /*name*/ "Sound",
212 /*name_plural*/ N_("sounds"),
213 /*translation_context*/ BLT_I18NCONTEXT_ID_SOUND,
215 /*asset_type_info*/ nullptr,
216
217 /* A fuzzy case, think NULLified content is OK here... */
218 /*init_data*/ nullptr,
219 /*copy_data*/ sound_copy_data,
220 /*free_data*/ sound_free_data,
221 /*make_local*/ nullptr,
222 /*foreach_id*/ sound_foreach_id,
223 /*foreach_cache*/ sound_foreach_cache,
224 /*foreach_path*/ sound_foreach_path,
225 /*owner_pointer_get*/ nullptr,
226
227 /*blend_write*/ sound_blend_write,
228 /*blend_read_data*/ sound_blend_read_data,
229 /*blend_read_after_liblink*/ nullptr,
230
231 /*blend_read_undo_preserve*/ nullptr,
232
233 /*lib_override_apply_post*/ nullptr,
234};
235
236#ifdef WITH_AUDASPACE
237/* evil globals ;-) */
238static char **audio_device_names = nullptr;
239#endif
240
242{
244 /* This is a bit tricky and not quite reliable, but good enough check.
245 *
246 * We don't want audio system handles to be allocated on an original data-blocks, and only want
247 * them to be allocated on a data-blocks which are result of dependency graph evaluation.
248 *
249 * Data-blocks which are covered by a copy-on-evaluation system of dependency graph will have
250 * ID_TAG_COPIED_ON_EVAL tag set on them. But if some of data-blocks during its evaluation
251 * decides to re-allocate its nested one (for example, object evaluation could re-allocate mesh
252 * when evaluating modifier stack). Such data-blocks will have
253 * ID_TAG_COPIED_ON_EVAL_FINAL_RESULT tag set on them.
254 *
255 * Additionally, we also allow data-blocks outside of main database. Those can not be "original"
256 * and could be used as a temporary evaluated result during operations like baking.
257 *
258 * NOTE: We consider ID evaluated if ANY of those flags is set. We do NOT require ALL of them.
259 */
260 BLI_assert(id->tag &
262}
263
264bSound *BKE_sound_new_file(Main *bmain, const char *filepath)
265{
266 bSound *sound;
267 const char *blendfile_path = BKE_main_blendfile_path(bmain);
268 char filepath_abs[FILE_MAX];
269
270 STRNCPY(filepath_abs, filepath);
271 BLI_path_abs(filepath_abs, blendfile_path);
272
273 sound = static_cast<bSound *>(BKE_libblock_alloc(bmain, ID_SO, BLI_path_basename(filepath), 0));
274 STRNCPY(sound->filepath, filepath);
275 // sound->type = SOUND_TYPE_FILE; /* UNUSED. */
276
277 /* Extract sound specs for bSound */
278 SoundInfo info;
279 bool success = BKE_sound_info_get(bmain, sound, &info);
280 if (success) {
281 sound->samplerate = info.specs.samplerate;
282 sound->audio_channels = info.specs.channels;
283 }
284
285 sound->spinlock = (void *)MEM_mallocN<SpinLock>("sound_spinlock");
286 BLI_spin_init(static_cast<SpinLock *>(sound->spinlock));
287
289
290 return sound;
291}
292
293bSound *BKE_sound_new_file_exists_ex(Main *bmain, const char *filepath, bool *r_exists)
294{
295 bSound *sound;
296 char filepath_abs[FILE_MAX], filepath_test[FILE_MAX];
297
298 STRNCPY(filepath_abs, filepath);
299 BLI_path_abs(filepath_abs, BKE_main_blendfile_path(bmain));
300
301 /* first search an identical filepath */
302 for (sound = static_cast<bSound *>(bmain->sounds.first); sound;
303 sound = static_cast<bSound *>(sound->id.next))
304 {
305 STRNCPY(filepath_test, sound->filepath);
306 BLI_path_abs(filepath_test, ID_BLEND_PATH(bmain, &sound->id));
307
308 if (BLI_path_cmp(filepath_test, filepath_abs) == 0) {
309 id_us_plus(&sound->id); /* officially should not, it doesn't link here! */
310 if (r_exists) {
311 *r_exists = true;
312 }
313 return sound;
314 }
315 }
316
317 if (r_exists) {
318 *r_exists = false;
319 }
320 return BKE_sound_new_file(bmain, filepath);
321}
322
323bSound *BKE_sound_new_file_exists(Main *bmain, const char *filepath)
324{
325 return BKE_sound_new_file_exists_ex(bmain, filepath, nullptr);
326}
327
328static void sound_free_audio(bSound *sound)
329{
330#ifdef WITH_AUDASPACE
331 if (sound->handle) {
332 AUD_Sound_free(sound->handle);
333 sound->handle = nullptr;
334 sound->playback_handle = nullptr;
335 }
336
337 if (sound->cache) {
338 AUD_Sound_free(sound->cache);
339 sound->cache = nullptr;
340 }
341#else
342 UNUSED_VARS(sound);
343#endif /* WITH_AUDASPACE */
344}
345
346#ifdef WITH_AUDASPACE
347static CLG_LogRef LOG = {"bke.sound"};
348
349namespace {
350
351struct GlobalState {
352 const char *force_device = nullptr;
353
354 /* Parameters of the opened device */
355 const char *device_name = nullptr;
356 AUD_DeviceSpecs initialized_specs;
357
358 /* Device handle and its synchronization mutex. */
359 AUD_Device *sound_device = nullptr;
360 int buffer_size = 0;
361 std::mutex sound_device_mutex;
362
363 bool need_exit = false;
364 bool use_delayed_close = true;
365 std::thread delayed_close_thread;
366 std::condition_variable delayed_close_cv;
367
368 int num_device_users = 0;
369 std::chrono::time_point<std::chrono::steady_clock> last_user_disconnect_time_point;
370};
371
372GlobalState g_state;
373} // namespace
374
375static void sound_device_close_no_lock()
376{
377 if (g_state.sound_device) {
378 CLOG_INFO(&LOG, 3, "Closing audio device");
379 AUD_exit(g_state.sound_device);
380 g_state.sound_device = nullptr;
381 }
382}
383
384static void sound_device_open_no_lock(AUD_DeviceSpecs requested_specs)
385{
386 BLI_assert(!g_state.sound_device);
387
388 CLOG_INFO(&LOG, 3, "Opening audio device name:%s", g_state.device_name);
389
390 g_state.sound_device = AUD_init(
391 g_state.device_name, requested_specs, g_state.buffer_size, "Blender");
392 if (!g_state.sound_device) {
393 g_state.sound_device = AUD_init("None", requested_specs, g_state.buffer_size, "Blender");
394 }
395
396 g_state.initialized_specs.channels = AUD_Device_getChannels(g_state.sound_device);
397 g_state.initialized_specs.rate = AUD_Device_getRate(g_state.sound_device);
398 g_state.initialized_specs.format = AUD_Device_getFormat(g_state.sound_device);
399}
400
401static void sound_device_use_begin()
402{
403 ++g_state.num_device_users;
404
405 if (g_state.sound_device) {
406 return;
407 }
408
409 sound_device_open_no_lock(g_state.initialized_specs);
410}
411
412static void sound_device_use_end_after(const std::chrono::milliseconds after_ms)
413{
414 BLI_assert(g_state.num_device_users > 0);
415 if (g_state.num_device_users == 0) {
416 return;
417 }
418
419 --g_state.num_device_users;
420 if (g_state.num_device_users == 0) {
421 g_state.last_user_disconnect_time_point = std::chrono::steady_clock::now() + after_ms;
422 g_state.delayed_close_cv.notify_one();
423 }
424}
425
426static void sound_device_use_end()
427{
428 sound_device_use_end_after(std::chrono::milliseconds(0));
429}
430
431/* Return true if we need a thread which checks for device usage and closes it when it is inactive.
432 * Only runtime-invariant checks are done here, such as possible platform-specific requirements.
433 */
434static bool sound_use_close_thread()
435{
436# if OS_MAC
437 /* Closing audio device on macOS prior to 15.2 could lead to interference with other software.
438 * See #121911 for details. */
439 if (__builtin_available(macOS 15.2, *)) {
440 return true;
441 }
442 return false;
443# else
444 return true;
445# endif
446}
447
448static void delayed_close_thread_run()
449{
450 constexpr std::chrono::milliseconds device_close_delay{30000};
451
452 std::unique_lock lock(g_state.sound_device_mutex);
453
454 while (!g_state.need_exit) {
455 if (!g_state.use_delayed_close) {
456 CLOG_INFO(&LOG, 3, "Delayed device close is disabled");
457 /* Don't do anything here as delayed close is disabled.
458 * Wait so that we don't spin around in the while loop. */
459 g_state.delayed_close_cv.wait(lock);
460 continue;
461 }
462
463 if (g_state.num_device_users == 0) {
464 if (g_state.sound_device == nullptr) {
465 /* There are no device users, wait until there is device to be waited for to close. */
466 g_state.delayed_close_cv.wait(lock);
467 }
468 else {
469 g_state.delayed_close_cv.wait_until(
470 lock, g_state.last_user_disconnect_time_point + device_close_delay);
471 }
472 }
473 else {
474 /* If there are active device users wait indefinitely, until the system is requested to be
475 * closed or the user stops using device.
476 * It is not really guaranteed that the CV is notified for every user that stops using
477 * device, only the last one is guaranteed to notify the CV. */
478 g_state.delayed_close_cv.wait(lock);
479 }
480
481 if (g_state.need_exit) {
482 CLOG_INFO(&LOG, 3, "System exit requested");
483 break;
484 }
485
486 if (!g_state.use_delayed_close) {
487 /* Take into account corner case where you switch from a delayed close device while Blender
488 * is running and a delayed close has already been queued up. */
489 continue;
490 }
491
492 if (!g_state.sound_device) {
493 CLOG_INFO(&LOG, 3, "Device is not open, nothing to do");
494 continue;
495 }
496
497 CLOG_INFO(&LOG, 3, "Checking last device usage and timestamp");
498
499 if (g_state.num_device_users) {
500 CLOG_INFO(&LOG, 3, "Device is used by %d user(s)", g_state.num_device_users);
501 continue;
502 }
503
504 const std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
505 if ((now - g_state.last_user_disconnect_time_point) >= device_close_delay) {
506 sound_device_close_no_lock();
507 }
508 }
509
510 CLOG_INFO(&LOG, 3, "Delayed device close thread finished");
511}
512
514
515static void sound_sync_callback(void *data, int mode, float time)
516{
517 if (sound_jack_sync_callback == nullptr) {
518 return;
519 }
520 Main *bmain = (Main *)data;
521 sound_jack_sync_callback(bmain, mode, time);
522}
523
524void BKE_sound_force_device(const char *device)
525{
526 g_state.force_device = device;
527}
528
530{
531 AUD_initOnce();
532 if (sound_use_close_thread()) {
533 CLOG_INFO(&LOG, 2, "Using delayed device close thread");
534 g_state.delayed_close_thread = std::thread(delayed_close_thread_run);
535 }
536}
537
538void BKE_sound_exit()
539{
540 std::lock_guard lock(g_state.sound_device_mutex);
541 sound_device_close_no_lock();
542}
543
545{
546 {
547 std::unique_lock lock(g_state.sound_device_mutex);
548 g_state.need_exit = true;
549 }
550
551 if (g_state.delayed_close_thread.joinable()) {
552 g_state.delayed_close_cv.notify_one();
553 g_state.delayed_close_thread.join();
554 }
555
556 std::lock_guard lock(g_state.sound_device_mutex);
557 sound_device_close_no_lock();
558 AUD_exitOnce();
559
560 if (audio_device_names != nullptr) {
561 int i;
562 for (i = 0; audio_device_names[i]; i++) {
563 free(audio_device_names[i]);
564 }
565 free(audio_device_names);
566 audio_device_names = nullptr;
567 }
568}
569
570void BKE_sound_init(Main *bmain)
571{
572 std::lock_guard lock(g_state.sound_device_mutex);
573 /* Make sure no instance of the sound system is running, otherwise we get leaks. */
574 sound_device_close_no_lock();
575
576 AUD_DeviceSpecs requested_specs;
577 requested_specs.channels = AUD_Channels(U.audiochannels);
578 requested_specs.format = AUD_SampleFormat(U.audioformat);
579 requested_specs.rate = U.audiorate;
580
581 if (g_state.force_device == nullptr) {
582 char **names = BKE_sound_get_device_names();
583 g_state.device_name = names[0];
584
585 /* make sure device is within the bounds of the array */
586 for (int i = 0; names[i]; i++) {
587 if (i == U.audiodevice) {
588 g_state.device_name = names[i];
589 }
590 }
591 }
592 else {
593 g_state.device_name = g_state.force_device;
594 }
595
596 g_state.buffer_size = U.mixbufsize < 128 ? 1024 : U.mixbufsize;
597
598 if (requested_specs.rate < AUD_RATE_8000) {
599 requested_specs.rate = AUD_RATE_48000;
600 }
601
602 if (requested_specs.format <= AUD_FORMAT_INVALID) {
603 requested_specs.format = AUD_FORMAT_S16;
604 }
605
606 if (requested_specs.channels <= AUD_CHANNELS_INVALID) {
607 requested_specs.channels = AUD_CHANNELS_STEREO;
608 }
609
610 /* Make sure that we have our initalized_specs */
611 sound_device_open_no_lock(requested_specs);
612 if (STR_ELEM(g_state.device_name, "JACK", "PulseAudio", "PipeWire")) {
613 /* JACK:
614 * Do not close the device when using JACK. If we close it, we will not be able to
615 * respond to JACK audio bus commands.
616 *
617 * PulseAudio, PipeWire:
618 * These APIs are built around the idea that the program using them keeps the device open.
619 * Instead it uses audio streams to determine if something is playing back audio or not.
620 * These streams are only active when Audaspace is playing back, so we don't need to
621 * do anything manually.
622 * If we close these devices, it will become very hard and tedious for end users to
623 * control the volume or route audio from Blender.
624 */
625 g_state.use_delayed_close = false;
626 AUD_setSynchronizerCallback(sound_sync_callback, bmain);
627 }
628 else {
629 g_state.use_delayed_close = true;
630 sound_device_close_no_lock();
631 }
632}
633
635{
636 std::lock_guard lock(g_state.sound_device_mutex);
637 if (g_state.sound_device) {
638 AUD_setSynchronizerCallback(sound_sync_callback, bmain);
639 }
640}
641
642/* XXX unused currently */
643# if 0
644bSound *BKE_sound_new_buffer(Main *bmain, bSound *source)
645{
646 bSound *sound = nullptr;
647
648 char name[MAX_ID_NAME + 5];
649 BLI_string_join(name, sizeof(name), "buf_", source->id.name);
650
651 sound = BKE_libblock_alloc(bmain, ID_SO, name);
652
653 sound->child_sound = source;
654 sound->type = SOUND_TYPE_BUFFER;
655
656 sound_load(bmain, sound);
657
658 return sound;
659}
660
661bSound *BKE_sound_new_limiter(Main *bmain, bSound *source, float start, float end)
662{
663 bSound *sound = nullptr;
664
665 char name[MAX_ID_NAME + 5];
666 BLI_string_join(name, sizeof(name), "lim_", source->id.name);
667
668 sound = BKE_libblock_alloc(bmain, ID_SO, name);
669
670 sound->child_sound = source;
671 sound->start = start;
672 sound->end = end;
673 sound->type = SOUND_TYPE_LIMITER;
674
675 sound_load(bmain, sound);
676
677 return sound;
678}
679# endif
680
681void BKE_sound_cache(bSound *sound)
682{
684
685 if (sound->cache) {
686 AUD_Sound_free(sound->cache);
687 }
688
689 sound->cache = AUD_Sound_cache(sound->handle);
690 if (sound->cache) {
691 sound->playback_handle = sound->cache;
692 }
693 else {
694 sound->playback_handle = sound->handle;
695 }
696}
697
699{
700 if (sound->cache) {
701 AUD_Sound_free(sound->cache);
702 sound->cache = nullptr;
703 sound->playback_handle = sound->handle;
704 }
705}
706
707static void sound_load_audio(Main *bmain, bSound *sound, bool free_waveform)
708{
709
710 if (sound->cache) {
711 AUD_Sound_free(sound->cache);
712 sound->cache = nullptr;
713 }
714
715 if (sound->handle) {
716 AUD_Sound_free(sound->handle);
717 sound->handle = nullptr;
718 sound->playback_handle = nullptr;
719 }
720
721 if (free_waveform) {
723 }
724
725/* XXX unused currently */
726# if 0
727 switch (sound->type) {
728 case SOUND_TYPE_FILE:
729# endif
730 {
731 char fullpath[FILE_MAX];
732
733 /* load sound */
734 PackedFile *pf = sound->packedfile;
735
736 /* Don't modify `sound->filepath`, only change a copy. */
737 STRNCPY(fullpath, sound->filepath);
738 BLI_path_abs(fullpath, ID_BLEND_PATH(bmain, &sound->id));
739
740 /* but we need a packed file then */
741 if (pf) {
742 sound->handle = AUD_Sound_bufferFile((uchar *)pf->data, pf->size);
743 }
744 else {
745 /* or else load it from disk */
746 sound->handle = AUD_Sound_file(fullpath);
747 }
748 }
749/* XXX unused currently */
750# if 0
751 break;
752 }
753 case SOUND_TYPE_BUFFER:
754 if (sound->child_sound && sound->child_sound->handle) {
755 sound->handle = AUD_bufferSound(sound->child_sound->handle);
756 }
757 break;
758 case SOUND_TYPE_LIMITER:
759 if (sound->child_sound && sound->child_sound->handle) {
760 sound->handle = AUD_limitSound(sound->child_sound, sound->start, sound->end);
761 }
762 break;
763}
764# endif
765 if (sound->flags & SOUND_FLAGS_MONO) {
766 void *handle = AUD_Sound_rechannel(sound->handle, AUD_CHANNELS_MONO);
767 AUD_Sound_free(sound->handle);
768 sound->handle = handle;
769 }
770
771 if (sound->flags & SOUND_FLAGS_CACHING) {
772 sound->cache = AUD_Sound_cache(sound->handle);
773 }
774
775 if (sound->cache) {
776 sound->playback_handle = sound->cache;
777 }
778 else {
779 sound->playback_handle = sound->handle;
780 }
781}
782
783void BKE_sound_load(Main *bmain, bSound *sound)
784{
786 sound_load_audio(bmain, sound, true);
787}
788
789AUD_Device *BKE_sound_mixdown(const Scene *scene, AUD_DeviceSpecs specs, int start, float volume)
790{
792 return AUD_openMixdownDevice(
793 specs, scene->sound_scene, volume, AUD_RESAMPLE_QUALITY_MEDIUM, start / FPS);
794}
795
796void BKE_sound_create_scene(Scene *scene)
797{
799
800 /* should be done in version patch, but this gets called before */
801 if (scene->r.frs_sec_base == 0) {
802 scene->r.frs_sec_base = 1;
803 }
804
805 scene->sound_scene = AUD_Sequence_create(FPS, scene->audio.flag & AUDIO_MUTE);
806 AUD_Sequence_setSpeedOfSound(scene->sound_scene, scene->audio.speed_of_sound);
807 AUD_Sequence_setDopplerFactor(scene->sound_scene, scene->audio.doppler_factor);
808 AUD_Sequence_setDistanceModel(scene->sound_scene,
809 AUD_DistanceModel(scene->audio.distance_model));
810 scene->playback_handle = nullptr;
811 scene->sound_scrub_handle = nullptr;
812 scene->speaker_handles = nullptr;
813}
814
816{
817 if (scene->playback_handle) {
818 AUD_Handle_stop(scene->playback_handle);
819 }
820 if (scene->sound_scrub_handle) {
821 AUD_Handle_stop(scene->sound_scrub_handle);
822 }
823 if (scene->speaker_handles) {
824 void *handle;
825
826 while ((handle = AUD_getSet(scene->speaker_handles))) {
827 AUD_Sequence_remove(scene->sound_scene, handle);
828 }
829
831 }
832 if (scene->sound_scene) {
833 AUD_Sequence_free(scene->sound_scene);
834 }
835}
836
837void BKE_sound_lock()
838{
839 g_state.sound_device_mutex.lock();
840 if (g_state.sound_device == nullptr) {
841 return;
842 }
843 AUD_Device_lock(g_state.sound_device);
844}
845
846void BKE_sound_unlock()
847{
848 g_state.sound_device_mutex.unlock();
849 if (g_state.sound_device == nullptr) {
850 return;
851 }
852 AUD_Device_unlock(g_state.sound_device);
853}
854
856{
858
859 if (scene->sound_scene) {
860 AUD_Sequence_setSpecs(scene->sound_scene, g_state.initialized_specs.specs);
861 }
862}
863
864void BKE_sound_mute_scene(Scene *scene, int muted)
865{
867 if (scene->sound_scene) {
868 AUD_Sequence_setMuted(scene->sound_scene, muted);
869 }
870}
871
872void BKE_sound_update_fps(Main *bmain, Scene *scene)
873{
875
876 if (scene->sound_scene) {
877 AUD_Sequence_setFPS(scene->sound_scene, FPS);
878 }
879
881}
882
884{
886
887 AUD_Sequence_setSpeedOfSound(scene->sound_scene, scene->audio.speed_of_sound);
888 AUD_Sequence_setDopplerFactor(scene->sound_scene, scene->audio.doppler_factor);
889 AUD_Sequence_setDistanceModel(scene->sound_scene,
890 AUD_DistanceModel(scene->audio.distance_model));
891}
892
894 Scene *scene, Strip *sequence, int startframe, int endframe, int frameskip)
895{
897 if (sequence->scene && scene != sequence->scene) {
898 const double fps = FPS;
899 return AUD_Sequence_add(scene->sound_scene,
900 sequence->scene->sound_scene,
901 startframe / fps,
902 endframe / fps,
903 frameskip / fps);
904 }
905 return nullptr;
906}
907
909{
911 scene,
912 sequence,
915 sequence->startofs + sequence->anim_startofs);
916}
917
919 Scene *scene, Strip *sequence, int startframe, int endframe, int frameskip)
920{
922 /* Happens when sequence's sound data-block was removed. */
923 if (sequence->sound == nullptr) {
924 return nullptr;
925 }
927 const double fps = FPS;
928 const double offset_time = sequence->sound->offset_time + sequence->sound_offset -
929 frameskip / fps;
930 if (offset_time >= 0.0f) {
931 return AUD_Sequence_add(scene->sound_scene,
932 sequence->sound->playback_handle,
933 startframe / fps + offset_time,
934 endframe / fps,
935 0.0f);
936 }
937 return AUD_Sequence_add(scene->sound_scene,
938 sequence->sound->playback_handle,
939 startframe / fps,
940 endframe / fps,
941 -offset_time);
942}
943
944void *BKE_sound_add_scene_sound_defaults(Scene *scene, Strip *sequence)
945{
946 return BKE_sound_add_scene_sound(scene,
947 sequence,
950 sequence->startofs + sequence->anim_startofs);
951}
952
953void BKE_sound_remove_scene_sound(Scene *scene, void *handle)
954{
955 AUD_Sequence_remove(scene->sound_scene, handle);
956}
957
958void BKE_sound_mute_scene_sound(void *handle, bool mute)
959{
960 AUD_SequenceEntry_setMuted(handle, mute);
961}
962
963void BKE_sound_move_scene_sound(const Scene *scene,
964 void *handle,
965 int startframe,
966 int endframe,
967 int frameskip,
968 double audio_offset)
969{
971 const double fps = FPS;
972 const double offset_time = audio_offset - frameskip / fps;
973 if (offset_time >= 0.0f) {
974 AUD_SequenceEntry_move(handle, startframe / fps + offset_time, endframe / fps, 0.0f);
975 }
976 else {
977 AUD_SequenceEntry_move(handle, startframe / fps, endframe / fps, -offset_time);
978 }
979}
980
981void BKE_sound_move_scene_sound_defaults(Scene *scene, Strip *sequence)
982{
984 if (sequence->scene_sound) {
985 double offset_time = 0.0f;
986 if (sequence->sound != nullptr) {
987 offset_time = sequence->sound->offset_time + sequence->sound_offset;
988 }
990 sequence->scene_sound,
993 sequence->startofs + sequence->anim_startofs,
994 offset_time);
995 }
996}
997
998void BKE_sound_update_scene_sound(void *handle, bSound *sound)
999{
1000 AUD_SequenceEntry_setSound(handle, sound->playback_handle);
1001}
1002
1003#endif /* WITH_AUDASPACE */
1004
1005void BKE_sound_update_sequence_handle(void *handle, void *sound_handle)
1006{
1007#ifdef WITH_AUDASPACE
1008 AUD_SequenceEntry_setSound(handle, sound_handle);
1009#else
1010 UNUSED_VARS(handle, sound_handle);
1011#endif
1012}
1013
1014#ifdef WITH_AUDASPACE
1015
1016void BKE_sound_set_scene_volume(Scene *scene, float volume)
1017{
1019 if (scene->sound_scene == nullptr) {
1020 return;
1021 }
1022 AUD_Sequence_setAnimationData(scene->sound_scene,
1023 AUD_AP_VOLUME,
1024 scene->r.cfra,
1025 &volume,
1026 (scene->audio.flag & AUDIO_VOLUME_ANIMATED) != 0);
1027}
1028
1030 const int frame,
1031 float volume,
1032 const char animated)
1033{
1034 AUD_SequenceEntry_setAnimationData(handle, AUD_AP_VOLUME, frame, &volume, animated);
1035}
1036
1038 const int frame,
1039 float pitch,
1040 const char animated)
1041{
1042 AUD_SequenceEntry_setAnimationData(handle, AUD_AP_PITCH, frame, &pitch, animated);
1043}
1044
1046 int frame_start,
1047 int frame_end,
1048 float pitch)
1049{
1050 frame_start = max_ii(0, frame_start);
1051 frame_end = max_ii(0, frame_end);
1052 AUD_SequenceEntry_setConstantRangeAnimationData(
1053 handle, AUD_AP_PITCH, frame_start, frame_end, &pitch);
1054}
1055
1057 const int frame,
1058 float pan,
1059 const char animated)
1060{
1061 AUD_SequenceEntry_setAnimationData(handle, AUD_AP_PANNING, frame, &pan, animated);
1062}
1063
1065{
1066 BLI_assert_msg(0, "is not supposed to be used, is weird function.");
1067
1068 Scene *scene;
1069
1070 for (scene = static_cast<Scene *>(main->scenes.first); scene;
1071 scene = static_cast<Scene *>(scene->id.next))
1072 {
1073 blender::seq::sound_update(scene, sound);
1074 }
1075}
1076
1077/* This function assumes that you have already held the g_state.sound_device mutex. */
1078static void sound_start_play_scene(Scene *scene)
1079{
1081
1082 if (scene->playback_handle) {
1083 AUD_Handle_stop(scene->playback_handle);
1084 }
1085
1087
1088 scene->playback_handle = AUD_Device_play(g_state.sound_device, scene->sound_scene, 1);
1089 if (scene->playback_handle) {
1090 AUD_Handle_setLoopCount(scene->playback_handle, -1);
1091 }
1092}
1093
1094void BKE_sound_play_scene(Scene *scene)
1095{
1096 std::lock_guard lock(g_state.sound_device_mutex);
1097 sound_device_use_begin();
1099
1100 AUD_Status status;
1101 const double cur_time = FRA2TIME((scene->r.cfra + scene->r.subframe));
1102
1103 AUD_Device_lock(g_state.sound_device);
1104
1105 if (scene->sound_scrub_handle &&
1106 AUD_Handle_getStatus(scene->sound_scrub_handle) != AUD_STATUS_INVALID)
1107 {
1108 /* If the audio scrub handle is playing back, stop to make sure it is not active.
1109 * Otherwise, it will trigger a callback that will stop audio playback. */
1110 AUD_Handle_stop(scene->sound_scrub_handle);
1111 scene->sound_scrub_handle = nullptr;
1112 /* The scrub_handle started playback with playback_handle, stop it so we can
1113 * properly restart it. */
1114 AUD_Handle_pause(scene->playback_handle);
1115 }
1116
1117 status = scene->playback_handle ? AUD_Handle_getStatus(scene->playback_handle) :
1118 AUD_STATUS_INVALID;
1119
1120 if (status == AUD_STATUS_INVALID) {
1121 sound_start_play_scene(scene);
1122
1123 if (!scene->playback_handle) {
1124 AUD_Device_unlock(g_state.sound_device);
1125 return;
1126 }
1127 }
1128
1129 if (status != AUD_STATUS_PLAYING) {
1130 /* Seeking the synchronizer will also seek the playback handle.
1131 * Even if we don't have A/V sync on, keep the synchronizer and handle seek time in sync. */
1132 AUD_seekSynchronizer(cur_time);
1133 AUD_Handle_setPosition(scene->playback_handle, cur_time);
1134 AUD_Handle_resume(scene->playback_handle);
1135 }
1136
1137 if (scene->audio.flag & AUDIO_SYNC) {
1138 AUD_playSynchronizer();
1139 }
1140
1141 AUD_Device_unlock(g_state.sound_device);
1142}
1143
1144void BKE_sound_stop_scene(Scene *scene)
1145{
1146 std::lock_guard lock(g_state.sound_device_mutex);
1147 BLI_assert(g_state.sound_device);
1148 if (scene->playback_handle) {
1149 AUD_Handle_pause(scene->playback_handle);
1150
1151 if (scene->audio.flag & AUDIO_SYNC) {
1152 AUD_stopSynchronizer();
1153 }
1154 }
1155 sound_device_use_end();
1156}
1157
1158void BKE_sound_seek_scene(Main *bmain, Scene *scene)
1159{
1160 std::lock_guard lock(g_state.sound_device_mutex);
1161 bool animation_playing = false;
1162 for (bScreen *screen = static_cast<bScreen *>(bmain->screens.first); screen;
1163 screen = static_cast<bScreen *>(screen->id.next))
1164 {
1165 if (screen->animtimer) {
1166 animation_playing = true;
1167 break;
1168 }
1169 }
1170
1171 bool do_audio_scrub = scene->audio.flag & AUDIO_SCRUB && !animation_playing;
1172
1173 if (do_audio_scrub) {
1174 /* Make sure the sound device is open for scrubbing. */
1175 sound_device_use_begin();
1176 }
1177 else if (g_state.sound_device == nullptr) {
1178 /* Nothing to do if there is no sound device and we are not doing audio scrubbing. */
1179 return;
1180 }
1182
1183 AUD_Device_lock(g_state.sound_device);
1184
1185 AUD_Status status = scene->playback_handle ? AUD_Handle_getStatus(scene->playback_handle) :
1186 AUD_STATUS_INVALID;
1187 if (status == AUD_STATUS_INVALID) {
1188 sound_start_play_scene(scene);
1189
1190 if (!scene->playback_handle) {
1191 AUD_Device_unlock(g_state.sound_device);
1192 if (do_audio_scrub) {
1193 sound_device_use_end();
1194 }
1195 return;
1196 }
1197
1198 AUD_Handle_pause(scene->playback_handle);
1199 }
1200
1201 const double one_frame = 1.0 / FPS +
1202 (U.audiorate > 0 ? U.mixbufsize / double(U.audiorate) : 0.0);
1203 const double cur_time = FRA2TIME(scene->r.cfra);
1204
1205 if (do_audio_scrub) {
1206 /* Playback one frame of audio without advancing the timeline. */
1207 AUD_Handle_setPosition(scene->playback_handle, cur_time);
1208 AUD_Handle_resume(scene->playback_handle);
1209 if (scene->sound_scrub_handle &&
1210 AUD_Handle_getStatus(scene->sound_scrub_handle) != AUD_STATUS_INVALID)
1211 {
1212 AUD_Handle_setPosition(scene->sound_scrub_handle, 0);
1213 }
1214 else {
1215 if (scene->sound_scrub_handle) {
1216 AUD_Handle_stop(scene->sound_scrub_handle);
1217 }
1218 scene->sound_scrub_handle = AUD_pauseAfter(scene->playback_handle, one_frame);
1219 }
1220 sound_device_use_end_after(std::chrono::milliseconds(int(one_frame * 1000)));
1221 }
1222 else if (status == AUD_STATUS_PLAYING) {
1223 /* Seeking the synchronizer will also seek the playback handle.
1224 * Even if we don't have A/V sync on, keep the synchronizer and handle
1225 * seek time in sync.
1226 */
1227 AUD_seekSynchronizer(cur_time);
1228 AUD_Handle_setPosition(scene->playback_handle, cur_time);
1229 }
1230
1231 AUD_Device_unlock(g_state.sound_device);
1232}
1233
1234double BKE_sound_sync_scene(Scene *scene)
1235{
1237
1238 /* Ugly: Blender doesn't like it when the animation is played back during rendering */
1239 if (G.is_rendering) {
1240 return NAN_FLT;
1241 }
1242
1243 if (scene->playback_handle) {
1244 if (scene->audio.flag & AUDIO_SYNC) {
1245 return AUD_getSynchronizerPosition();
1246 }
1247
1248 return AUD_Handle_getPosition(scene->playback_handle);
1249 }
1250 return NAN_FLT;
1251}
1252
1254{
1255 if ((sound->tags & SOUND_TAGS_WAVEFORM_NO_RELOAD) == 0) {
1256 SoundWaveform *waveform = static_cast<SoundWaveform *>(sound->waveform);
1257 if (waveform) {
1258 if (waveform->data) {
1259 MEM_freeN(waveform->data);
1260 }
1261 MEM_freeN(waveform);
1262 }
1263
1264 sound->waveform = nullptr;
1265 }
1266 /* This tag is only valid once. */
1268}
1269
1270void BKE_sound_read_waveform(Main *bmain, bSound *sound, bool *stop)
1271{
1272 bool need_close_audio_handles = false;
1273 if (sound->playback_handle == nullptr) {
1274 /* TODO(sergey): Make it fully independent audio handle. */
1275 sound_load_audio(bmain, sound, true);
1276 need_close_audio_handles = true;
1277 }
1278
1279 AUD_SoundInfo info = AUD_getInfo(sound->playback_handle);
1280 SoundWaveform *waveform = MEM_mallocN<SoundWaveform>("SoundWaveform");
1281
1282 if (info.length > 0) {
1283 int length = info.length * SOUND_WAVE_SAMPLES_PER_SECOND;
1284
1285 waveform->data = MEM_malloc_arrayN<float>(3 * size_t(length), "SoundWaveform.samples");
1286 /* Ideally this would take a boolean argument. */
1287 short stop_i16 = *stop;
1288 waveform->length = AUD_readSound(
1289 sound->playback_handle, waveform->data, length, SOUND_WAVE_SAMPLES_PER_SECOND, &stop_i16);
1290 *stop = stop_i16 != 0;
1291 }
1292 else {
1293 /* Create an empty waveform here if the sound couldn't be
1294 * read. This indicates that reading the waveform is "done",
1295 * whereas just setting sound->waveform to nullptr causes other
1296 * code to think the waveform still needs to be created. */
1297 waveform->data = nullptr;
1298 waveform->length = 0;
1299 }
1300
1301 if (*stop) {
1302 if (waveform->data) {
1303 MEM_freeN(waveform->data);
1304 }
1305 MEM_freeN(waveform);
1306 BLI_spin_lock(static_cast<SpinLock *>(sound->spinlock));
1308 BLI_spin_unlock(static_cast<SpinLock *>(sound->spinlock));
1309 return;
1310 }
1311
1313
1314 BLI_spin_lock(static_cast<SpinLock *>(sound->spinlock));
1315 sound->waveform = waveform;
1317 BLI_spin_unlock(static_cast<SpinLock *>(sound->spinlock));
1318
1319 if (need_close_audio_handles) {
1320 sound_free_audio(sound);
1321 }
1322}
1323
1324static void sound_update_base(Scene *scene, Object *object, void *new_set)
1325{
1326 Speaker *speaker;
1327 float quat[4];
1328
1330 sound_verify_evaluated_id(&object->id);
1331
1332 if ((object->type != OB_SPEAKER) || !object->adt) {
1333 return;
1334 }
1335
1336 LISTBASE_FOREACH (NlaTrack *, track, &object->adt->nla_tracks) {
1337 LISTBASE_FOREACH (NlaStrip *, strip, &track->strips) {
1338 if (strip->type != NLASTRIP_TYPE_SOUND) {
1339 continue;
1340 }
1341 speaker = (Speaker *)object->data;
1342
1343 if (AUD_removeSet(scene->speaker_handles, strip->speaker_handle)) {
1344 if (speaker->sound) {
1345 AUD_SequenceEntry_move(strip->speaker_handle, double(strip->start) / FPS, FLT_MAX, 0);
1346 }
1347 else {
1348 AUD_Sequence_remove(scene->sound_scene, strip->speaker_handle);
1349 strip->speaker_handle = nullptr;
1350 }
1351 }
1352 else {
1353 if (speaker->sound) {
1354 strip->speaker_handle = AUD_Sequence_add(scene->sound_scene,
1355 speaker->sound->playback_handle,
1356 double(strip->start) / FPS,
1357 FLT_MAX,
1358 0);
1359 AUD_SequenceEntry_setRelative(strip->speaker_handle, 0);
1360 }
1361 }
1362
1363 if (strip->speaker_handle) {
1364 const bool mute = ((strip->flag & NLASTRIP_FLAG_MUTED) || (speaker->flag & SPK_MUTED));
1365 AUD_addSet(new_set, strip->speaker_handle);
1366 AUD_SequenceEntry_setVolumeMaximum(strip->speaker_handle, speaker->volume_max);
1367 AUD_SequenceEntry_setVolumeMinimum(strip->speaker_handle, speaker->volume_min);
1368 AUD_SequenceEntry_setDistanceMaximum(strip->speaker_handle, speaker->distance_max);
1369 AUD_SequenceEntry_setDistanceReference(strip->speaker_handle, speaker->distance_reference);
1370 AUD_SequenceEntry_setAttenuation(strip->speaker_handle, speaker->attenuation);
1371 AUD_SequenceEntry_setConeAngleOuter(strip->speaker_handle, speaker->cone_angle_outer);
1372 AUD_SequenceEntry_setConeAngleInner(strip->speaker_handle, speaker->cone_angle_inner);
1373 AUD_SequenceEntry_setConeVolumeOuter(strip->speaker_handle, speaker->cone_volume_outer);
1374
1375 mat4_to_quat(quat, object->object_to_world().ptr());
1376 blender::float3 location = object->object_to_world().location();
1377 AUD_SequenceEntry_setAnimationData(
1378 strip->speaker_handle, AUD_AP_LOCATION, scene->r.cfra, location, 1);
1379 AUD_SequenceEntry_setAnimationData(
1380 strip->speaker_handle, AUD_AP_ORIENTATION, scene->r.cfra, quat, 1);
1381 AUD_SequenceEntry_setAnimationData(
1382 strip->speaker_handle, AUD_AP_VOLUME, scene->r.cfra, &speaker->volume, 1);
1383 AUD_SequenceEntry_setAnimationData(
1384 strip->speaker_handle, AUD_AP_PITCH, scene->r.cfra, &speaker->pitch, 1);
1385 AUD_SequenceEntry_setSound(strip->speaker_handle, speaker->sound->playback_handle);
1386 AUD_SequenceEntry_setMuted(strip->speaker_handle, mute);
1387 }
1388 }
1389 }
1390}
1391
1392void BKE_sound_update_scene(Depsgraph *depsgraph, Scene *scene)
1393{
1395
1396 void *new_set = AUD_createSet();
1397 void *handle;
1398 float quat[4];
1399
1400 /* cheap test to skip looping over all objects (no speakers is a common case) */
1402 DEGObjectIterSettings deg_iter_settings = {nullptr};
1403 deg_iter_settings.depsgraph = depsgraph;
1404 deg_iter_settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY |
1407 DEG_OBJECT_ITER_BEGIN (&deg_iter_settings, object) {
1408 sound_update_base(scene, object, new_set);
1409 }
1411 }
1412
1413 while ((handle = AUD_getSet(scene->speaker_handles))) {
1414 AUD_Sequence_remove(scene->sound_scene, handle);
1415 }
1416
1417 if (scene->camera) {
1418 mat4_to_quat(quat, scene->camera->object_to_world().ptr());
1419 blender::float3 location = scene->camera->object_to_world().location();
1420 AUD_Sequence_setAnimationData(scene->sound_scene, AUD_AP_LOCATION, scene->r.cfra, location, 1);
1421 AUD_Sequence_setAnimationData(scene->sound_scene, AUD_AP_ORIENTATION, scene->r.cfra, quat, 1);
1422 }
1423
1425 scene->speaker_handles = new_set;
1426}
1427
1428void *BKE_sound_get_factory(void *sound)
1429{
1430 return ((bSound *)sound)->playback_handle;
1431}
1432
1433float BKE_sound_get_length(Main *bmain, bSound *sound)
1434{
1435 if (sound->playback_handle != nullptr) {
1436 AUD_SoundInfo info = AUD_getInfo(sound->playback_handle);
1437 return info.length;
1438 }
1439 SoundInfo info;
1440 if (!BKE_sound_info_get(bmain, sound, &info)) {
1441 return 0.0f;
1442 }
1443 return info.length;
1444}
1445
1447{
1448 if (audio_device_names == nullptr) {
1449 audio_device_names = AUD_getDeviceNames();
1450 }
1451
1452 return audio_device_names;
1453}
1454
1455static bool sound_info_from_playback_handle(void *playback_handle, SoundInfo *sound_info)
1456{
1457 if (playback_handle == nullptr) {
1458 return false;
1459 }
1460 AUD_SoundInfo info = AUD_getInfo(playback_handle);
1461 sound_info->specs.channels = (eSoundChannels)info.specs.channels;
1462 sound_info->length = info.length;
1463 sound_info->specs.samplerate = info.specs.rate;
1464 return true;
1465}
1466
1467bool BKE_sound_info_get(Main *main, bSound *sound, SoundInfo *sound_info)
1468{
1469 if (sound->playback_handle != nullptr) {
1470 return sound_info_from_playback_handle(sound->playback_handle, sound_info);
1471 }
1472 /* TODO(sergey): Make it fully independent audio handle. */
1473 /* Don't free waveforms during non-destructive queries.
1474 * This causes unnecessary recalculation - see #69921 */
1475 sound_load_audio(main, sound, false);
1476 const bool result = sound_info_from_playback_handle(sound->playback_handle, sound_info);
1477 sound_free_audio(sound);
1478 return result;
1479}
1480
1482 const char *filepath,
1483 int stream,
1484 SoundStreamInfo *sound_info)
1485{
1486 const char *blendfile_path = BKE_main_blendfile_path(main);
1487 char filepath_abs[FILE_MAX];
1488 AUD_Sound *sound;
1489 AUD_StreamInfo *stream_infos;
1490 int stream_count;
1491
1492 STRNCPY(filepath_abs, filepath);
1493 BLI_path_abs(filepath_abs, blendfile_path);
1494
1495 sound = AUD_Sound_file(filepath_abs);
1496 if (!sound) {
1497 return false;
1498 }
1499
1500 stream_count = AUD_Sound_getFileStreams(sound, &stream_infos);
1501
1502 AUD_Sound_free(sound);
1503
1504 if (!stream_infos) {
1505 return false;
1506 }
1507
1508 if ((stream < 0) || (stream >= stream_count)) {
1509 free(stream_infos);
1510 return false;
1511 }
1512
1513 sound_info->start = stream_infos[stream].start;
1514 sound_info->duration = stream_infos[stream].duration;
1515
1516 free(stream_infos);
1517
1518 return true;
1519}
1520
1521#else /* WITH_AUDASPACE */
1522
1523# include "BLI_utildefines.h"
1524
1525void BKE_sound_force_device(const char * /*device*/) {}
1527void BKE_sound_init(Main * /*bmain*/) {}
1530void BKE_sound_cache(bSound * /*sound*/) {}
1531void BKE_sound_delete_cache(bSound * /*sound*/) {}
1532void BKE_sound_load(Main * /*bmain*/, bSound * /*sound*/) {}
1533void BKE_sound_create_scene(Scene * /*scene*/) {}
1534void BKE_sound_destroy_scene(Scene * /*scene*/) {}
1539void BKE_sound_mute_scene(Scene * /*scene*/, int /*muted*/) {}
1541 Strip * /*sequence*/,
1542 int /*startframe*/,
1543 int /*endframe*/,
1544 int /*frameskip*/)
1545{
1546 return nullptr;
1547}
1548void *BKE_sound_scene_add_scene_sound_defaults(Scene * /*scene*/, Strip * /*sequence*/)
1549{
1550 return nullptr;
1551}
1553 Strip * /*sequence*/,
1554 int /*startframe*/,
1555 int /*endframe*/,
1556 int /*frameskip*/)
1557{
1558 return nullptr;
1559}
1560void *BKE_sound_add_scene_sound_defaults(Scene * /*scene*/, Strip * /*sequence*/)
1561{
1562 return nullptr;
1563}
1564void BKE_sound_remove_scene_sound(Scene * /*scene*/, void * /*handle*/) {}
1565void BKE_sound_mute_scene_sound(void * /*handle*/, bool /*mute*/) {}
1566void BKE_sound_move_scene_sound(const Scene * /*scene*/,
1567 void * /*handle*/,
1568 int /*startframe*/,
1569 int /*endframe*/,
1570 int /*frameskip*/,
1571 double /*audio_offset*/)
1572{
1573}
1574void BKE_sound_move_scene_sound_defaults(Scene * /*scene*/, Strip * /*sequence*/) {}
1575void BKE_sound_play_scene(Scene * /*scene*/) {}
1576void BKE_sound_stop_scene(Scene * /*scene*/) {}
1577void BKE_sound_seek_scene(Main * /*bmain*/, Scene * /*scene*/) {}
1578double BKE_sound_sync_scene(Scene * /*scene*/)
1579{
1580 return NAN_FLT;
1581}
1583 bSound *sound,
1584 /* NOLINTNEXTLINE: readability-non-const-parameter. */
1585 bool *stop)
1586{
1587 UNUSED_VARS(sound, stop, bmain);
1588}
1589void BKE_sound_update_sequencer(Main * /*main*/, bSound * /*sound*/) {}
1590void BKE_sound_update_scene(Depsgraph * /*depsgraph*/, Scene * /*scene*/) {}
1591void BKE_sound_update_scene_sound(void * /*handle*/, bSound * /*sound*/) {}
1593void BKE_sound_update_fps(Main * /*bmain*/, Scene * /*scene*/) {}
1595 int /*frame*/,
1596 float /*volume*/,
1597 char /*animated*/)
1598{
1599}
1601 int /*frame*/,
1602 float /*pan*/,
1603 char /*animated*/)
1604{
1605}
1606void BKE_sound_set_scene_volume(Scene * /*scene*/, float /*volume*/) {}
1608 int /*frame*/,
1609 float /*pitch*/,
1610 char /*animated*/)
1611{
1612}
1614 int /*frame_start*/,
1615 int /*frame_end*/,
1616 float /*pitch*/)
1617{
1618}
1619float BKE_sound_get_length(Main * /*bmain*/, bSound * /*sound*/)
1620{
1621 return 0;
1622}
1624{
1625 static char *names[1] = {nullptr};
1626 return names;
1627}
1628
1630
1631bool BKE_sound_info_get(Main * /*main*/, bSound * /*sound*/, SoundInfo * /*sound_info*/)
1632{
1633 return false;
1634}
1635
1637 const char * /*filepath*/,
1638 int /*stream*/,
1639 SoundStreamInfo * /*sound_info*/)
1640{
1641 return false;
1642}
1643
1644#endif /* WITH_AUDASPACE */
1645
1647{
1648 scene->sound_scene = nullptr;
1649 scene->playback_handle = nullptr;
1650 scene->sound_scrub_handle = nullptr;
1651 scene->speaker_handles = nullptr;
1652}
1653
1655{
1656 if (scene->sound_scene != nullptr) {
1657 return;
1658 }
1660}
1661
1663{
1664 sound->cache = nullptr;
1665 sound->playback_handle = nullptr;
1666}
1667
1669{
1670 if (sound->cache != nullptr) {
1671 return;
1672 }
1673 BKE_sound_load(bmain, sound);
1674}
1675
1677{
1678#if defined(WITH_AUDASPACE)
1679 sound_jack_sync_callback = callback;
1680#else
1681 UNUSED_VARS(callback);
1682#endif
1683}
1684
1685void BKE_sound_jack_scene_update(Scene *scene, int mode, double time)
1686{
1688
1689 /* Ugly: Blender doesn't like it when the animation is played back during rendering. */
1690 if (G.is_rendering) {
1691 return;
1692 }
1693#ifdef WITH_AUDASPACE
1694 AUD_Device_lock(g_state.sound_device);
1695
1696 if (mode) {
1697 BKE_sound_play_scene(scene);
1698 }
1699 else {
1700 BKE_sound_stop_scene(scene);
1701 }
1702 if (scene->playback_handle != nullptr) {
1703 AUD_Handle_setPosition(scene->playback_handle, time);
1704 }
1705 AUD_Device_unlock(g_state.sound_device);
1706#else
1707 UNUSED_VARS(mode, time);
1708#endif
1709}
1710
1711void BKE_sound_evaluate(Depsgraph *depsgraph, Main *bmain, bSound *sound)
1712{
1713 DEG_debug_print_eval(depsgraph, __func__, sound->id.name, sound);
1714 if (sound->id.recalc & ID_RECALC_SOURCE) {
1715 /* Sequencer checks this flag to see if the strip sound is to be updated from the Audaspace
1716 * side. */
1717 sound->id.recalc |= ID_RECALC_AUDIO;
1718 }
1719
1720 if (sound->id.recalc & ID_RECALC_AUDIO) {
1721 BKE_sound_load(bmain, sound);
1722 return;
1723 }
1724 BKE_sound_ensure_loaded(bmain, sound);
1725}
void * AUD_createSet()
Definition AUD_Set.cpp:13
void * AUD_getSet(void *set)
Definition AUD_Set.cpp:38
void AUD_addSet(void *set, void *entry)
Definition AUD_Set.cpp:31
void AUD_destroySet(void *set)
Definition AUD_Set.cpp:18
char AUD_removeSet(void *set, void *entry)
Definition AUD_Set.cpp:23
bool BKE_bpath_foreach_path_fixed_process(BPathForeachPathData *bpath_data, char *path, size_t path_maxncpy)
Definition bpath.cc:125
@ BKE_BPATH_FOREACH_PATH_SKIP_PACKED
Definition BKE_bpath.hh:37
void(*)(ID *id, const IDCacheKey *cache_key, void **cache_p, uint flags, void *user_data) IDTypeForeachCacheFunctionCallback
IDTypeInfo IDType_ID_SO
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:44
@ IDTYPE_FLAGS_NO_ANIMDATA
Definition BKE_idtype.hh:46
void * BKE_libblock_alloc(Main *bmain, short type, const char *name, int flag) ATTR_WARN_UNUSED_RESULT
Definition lib_id.cc:1428
void id_us_plus(ID *id)
Definition lib_id.cc:353
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2611
@ IDWALK_CB_USER
LibraryForeachIDFlag BKE_lib_query_foreachid_process_flags_get(const LibraryForeachIDData *data)
Definition lib_query.cc:129
@ IDWALK_DO_DEPRECATED_POINTERS
#define BKE_LIB_FOREACHID_PROCESS_ID_NOCHECK(data_, id_, cb_flag_)
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:877
PackedFile * BKE_packedfile_duplicate(const PackedFile *pf_src)
void BKE_packedfile_free(PackedFile *pf)
void BKE_packedfile_blend_write(BlendWriter *writer, const PackedFile *pf)
void BKE_packedfile_blend_read(BlendDataReader *reader, PackedFile **pf_p, blender::StringRefNull filepath)
void * BKE_sound_get_factory(void *sound)
#define SOUND_WAVE_SAMPLES_PER_SECOND
Definition BKE_sound.h:10
void(* SoundJackSyncCallback)(struct Main *bmain, int mode, double time)
Definition BKE_sound.h:197
eSoundChannels
Definition BKE_sound.h:62
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
#define BLI_INLINE
void BLI_kdtree_nd_ free(KDTree *tree)
#define LISTBASE_FOREACH(type, var, list)
MINLINE int max_ii(int a, int b)
#define NAN_FLT
void mat4_to_quat(float q[4], const float mat[4][4])
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
#define FILE_MAX
#define BLI_path_cmp
#define STR_ELEM(...)
Definition BLI_string.h:656
char * STRNCPY(char(&dst)[N], const char *src)
Definition BLI_string.h:688
#define BLI_string_join(...)
unsigned char uchar
pthread_spinlock_t SpinLock
void BLI_spin_init(SpinLock *spin)
Definition threads.cc:391
void BLI_spin_unlock(SpinLock *spin)
Definition threads.cc:430
void BLI_spin_lock(SpinLock *spin)
Definition threads.cc:405
void BLI_spin_end(SpinLock *spin)
Definition threads.cc:445
#define UNUSED_VARS(...)
#define UNUSED_VARS_NDEBUG(...)
bool BLO_read_data_is_undo(BlendDataReader *reader)
Definition readfile.cc:5485
#define BLO_write_id_struct(writer, struct_name, id_address, id)
bool BLO_write_is_undo(BlendWriter *writer)
#define BLT_I18NCONTEXT_ID_SOUND
#define CLOG_INFO(clg_ref, level,...)
Definition CLG_log.h:179
void DEG_debug_print_eval(Depsgraph *depsgraph, const char *function_name, const char *object_name, const void *object_address)
#define DEG_OBJECT_ITER_BEGIN(settings_, instance_)
#define DEG_OBJECT_ITER_END
bool DEG_id_type_any_exists(const Depsgraph *depsgraph, short id_type)
@ DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY
@ DEG_ITER_OBJECT_FLAG_LINKED_INDIRECTLY
@ DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET
@ ID_RECALC_AUDIO
Definition DNA_ID.h:1040
@ ID_RECALC_SOURCE
Definition DNA_ID.h:1051
@ ID_TAG_COPIED_ON_EVAL
Definition DNA_ID.h:905
@ ID_TAG_COPIED_ON_EVAL_FINAL_RESULT
Definition DNA_ID.h:915
@ ID_TAG_NO_MAIN
Definition DNA_ID.h:886
@ INDEX_ID_SO
Definition DNA_ID.h:1211
@ ID_SO
@ ID_SPK
@ NLASTRIP_FLAG_MUTED
@ NLASTRIP_TYPE_SOUND
Object is a sort of wrapper for general info.
@ OB_SPEAKER
@ AUDIO_MUTE
@ AUDIO_VOLUME_ANIMATED
@ AUDIO_SCRUB
@ AUDIO_SYNC
#define FPS
#define FRA2TIME(a)
@ SOUND_TAGS_WAVEFORM_LOADING
@ SOUND_TAGS_WAVEFORM_NO_RELOAD
@ SOUND_FLAGS_MONO
@ SOUND_FLAGS_CACHING
@ SPK_MUTED
Read Guarded memory(de)allocation.
bool stop
Definition WM_types.hh:1016
volatile int lock
#define U
void BKE_sound_jack_sync_callback_set(SoundJackSyncCallback callback)
void BKE_sound_refresh_callback_bmain(Main *)
void BKE_sound_ensure_scene(Scene *scene)
BLI_INLINE void sound_verify_evaluated_id(const ID *id)
void BKE_sound_exit()
double BKE_sound_sync_scene(Scene *)
void * BKE_sound_add_scene_sound_defaults(Scene *, Strip *)
void BKE_sound_destroy_scene(Scene *)
char ** BKE_sound_get_device_names()
void BKE_sound_init(Main *)
static void sound_free_audio(bSound *sound)
void BKE_sound_lock()
void BKE_sound_load(Main *, bSound *)
void BKE_sound_update_sequencer(Main *, bSound *)
static void sound_free_data(ID *id)
static void sound_blend_read_data(BlendDataReader *reader, ID *id)
void BKE_sound_mute_scene_sound(void *, bool)
void BKE_sound_seek_scene(Main *, Scene *)
void BKE_sound_play_scene(Scene *)
void BKE_sound_force_device(const char *)
void BKE_sound_jack_scene_update(Scene *scene, int mode, double time)
void BKE_sound_update_scene(Depsgraph *, Scene *)
bSound * BKE_sound_new_file_exists_ex(Main *bmain, const char *filepath, bool *r_exists)
static void sound_blend_write(BlendWriter *writer, ID *id, const void *id_address)
void BKE_sound_mute_scene(Scene *, int)
void BKE_sound_exit_once()
bSound * BKE_sound_new_file(Main *bmain, const char *filepath)
void BKE_sound_update_fps(Main *, Scene *)
void BKE_sound_delete_cache(bSound *)
static void sound_foreach_path(ID *id, BPathForeachPathData *bpath_data)
void BKE_sound_move_scene_sound_defaults(Scene *, Strip *)
void * BKE_sound_scene_add_scene_sound_defaults(Scene *, Strip *)
void BKE_sound_remove_scene_sound(Scene *, void *)
void BKE_sound_reset_runtime(bSound *sound)
bool BKE_sound_stream_info_get(Main *, const char *, int, SoundStreamInfo *)
float BKE_sound_get_length(Main *, bSound *)
void * BKE_sound_scene_add_scene_sound(Scene *, Strip *, int, int, int)
bool BKE_sound_info_get(Main *, bSound *, SoundInfo *)
static void sound_foreach_id(ID *id, LibraryForeachIDData *data)
void BKE_sound_set_scene_sound_pan_at_frame(void *, int, float, char)
static void sound_copy_data(Main *, std::optional< Library * >, ID *id_dst, const ID *id_src, const int)
void BKE_sound_init_once()
void * BKE_sound_add_scene_sound(Scene *, Strip *, int, int, int)
void BKE_sound_set_scene_sound_pitch_at_frame(void *, int, float, char)
void BKE_sound_create_scene(Scene *)
void BKE_sound_update_scene_sound(void *, bSound *)
void BKE_sound_free_waveform(bSound *)
void BKE_sound_set_scene_sound_pitch_constant_range(void *, int, int, float)
static void sound_foreach_cache(ID *id, IDTypeForeachCacheFunctionCallback function_callback, void *user_data)
void BKE_sound_set_scene_volume(Scene *, float)
bSound * BKE_sound_new_file_exists(Main *bmain, const char *filepath)
void BKE_sound_reset_scene_runtime(Scene *scene)
void BKE_sound_update_sequence_handle(void *handle, void *sound_handle)
void BKE_sound_stop_scene(Scene *)
void BKE_sound_reset_scene_specs(Scene *)
void BKE_sound_evaluate(Depsgraph *depsgraph, Main *bmain, bSound *sound)
void BKE_sound_move_scene_sound(const Scene *, void *, int, int, int, double)
void BKE_sound_read_waveform(Main *bmain, bSound *sound, bool *stop)
void BKE_sound_unlock()
void BKE_sound_cache(bSound *)
void BKE_sound_update_scene_listener(Scene *)
void BKE_sound_ensure_loaded(Main *bmain, bSound *sound)
void BKE_sound_set_scene_sound_volume_at_frame(void *, int, float, char)
BMesh const char void * data
BPy_StructRNA * depsgraph
#define offsetof(t, d)
#define pf(_x, _i)
Prefetch 64.
Definition gim_memory.h:48
#define main()
float length(VecOp< float, D >) RET
static GPUSelectNextState g_state
#define FILTER_ID_SO
#define MAX_ID_NAME
#define ID_BLEND_PATH(_bmain, _id)
#define ID_IS_OVERRIDE_LIBRARY(_id)
#define LOG(severity)
Definition log.h:32
void * MEM_mallocN(size_t len, const char *str)
Definition mallocn.cc:128
void * MEM_malloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:133
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
#define G(x, y, z)
int time_right_handle_frame_get(const Scene *scene, const Strip *strip)
void sound_update_length(Main *bmain, Scene *scene)
int time_left_handle_frame_get(const Scene *, const Strip *strip)
void sound_update(Scene *scene, bSound *sound)
VecBase< float, 3 > float3
#define FLT_MAX
Definition stdcycles.h:14
ListBase nla_tracks
float doppler_factor
float speed_of_sound
eBPathForeachFlag flag
Definition BKE_bpath.hh:94
unsigned int id_session_uid
Definition BKE_idtype.hh:74
size_t identifier
Definition BKE_idtype.hh:79
Definition DNA_ID.h:404
unsigned int recalc
Definition DNA_ID.h:427
int tag
Definition DNA_ID.h:424
void * next
Definition DNA_ID.h:407
char name[66]
Definition DNA_ID.h:415
void * first
ListBase sounds
Definition BKE_main.hh:266
ListBase screens
Definition BKE_main.hh:261
ListBase strips
struct AnimData * adt
void * sound_scrub_handle
void * sound_scene
struct RenderData r
void * speaker_handles
struct Object * camera
struct AudioData audio
void * playback_handle
int samplerate
Definition BKE_sound.h:77
eSoundChannels channels
Definition BKE_sound.h:76
float length
Definition BKE_sound.h:79
struct SoundInfo::@231102123130065173371367072324216376355364061242 specs
double duration
Definition BKE_sound.h:83
float * data
Definition BKE_sound.h:24
float cone_angle_outer
float distance_reference
float cone_volume_outer
float distance_max
float attenuation
struct bSound * sound
float cone_angle_inner
void * scene_sound
struct Scene * scene
struct bSound * sound
float sound_offset
void * playback_handle
struct PackedFile * packedfile
struct PackedFile * newpackedfile
char filepath[1024]
void * handle
struct Ipo * ipo
void * cache
void * spinlock
void * waveform
int audio_channels
short flags
double offset_time
i
Definition text_draw.cc:230
#define N_(msgid)
static void sound_jack_sync_callback(Main *bmain, int mode, double time)
uint8_t flag
Definition wm_window.cc:139