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 --g_state.num_device_users;
415 if (g_state.num_device_users == 0) {
416 g_state.last_user_disconnect_time_point = std::chrono::steady_clock::now() + after_ms;
417 g_state.delayed_close_cv.notify_one();
418 }
419}
420
421static void sound_device_use_end()
422{
423 sound_device_use_end_after(std::chrono::milliseconds(0));
424}
425
426/* Return true if we need a thread which checks for device usage and closes it when it is inactive.
427 * Only runtime-invariant checks are done here, such as possible platform-specific requirements.
428 */
429static bool sound_use_close_thread()
430{
431# if OS_MAC
432 /* Closing audio device on macOS prior to 15.2 could lead to interference with other software.
433 * See #121911 for details. */
434 if (__builtin_available(macOS 15.2, *)) {
435 return true;
436 }
437 return false;
438# else
439 return true;
440# endif
441}
442
443static void delayed_close_thread_run()
444{
445 constexpr std::chrono::milliseconds device_close_delay{30000};
446
447 std::unique_lock lock(g_state.sound_device_mutex);
448
449 while (!g_state.need_exit) {
450 if (!g_state.use_delayed_close) {
451 CLOG_INFO(&LOG, 3, "Delayed device close is disabled");
452 /* Don't do anything here as delayed close is disabled.
453 * Wait so that we don't spin around in the while loop. */
454 g_state.delayed_close_cv.wait(lock);
455 continue;
456 }
457
458 if (g_state.num_device_users == 0) {
459 if (g_state.sound_device == nullptr) {
460 /* There are no device users, wait until there is device to be waited for to close. */
461 g_state.delayed_close_cv.wait(lock);
462 }
463 else {
464 g_state.delayed_close_cv.wait_until(
465 lock, g_state.last_user_disconnect_time_point + device_close_delay);
466 }
467 }
468 else {
469 /* If there are active device users wait indefinitely, until the system is requested to be
470 * closed or the user stops using device.
471 * It is not really guaranteed that the CV is notified for every user that stops using
472 * device, only the last one is guaranteed to notify the CV. */
473 g_state.delayed_close_cv.wait(lock);
474 }
475
476 if (g_state.need_exit) {
477 CLOG_INFO(&LOG, 3, "System exit requested");
478 break;
479 }
480
481 if (!g_state.use_delayed_close) {
482 /* Take into account corner case where you switch from a delayed close device while Blender
483 * is running and a delayed close has already been queued up. */
484 continue;
485 }
486
487 if (!g_state.sound_device) {
488 CLOG_INFO(&LOG, 3, "Device is not open, nothing to do");
489 continue;
490 }
491
492 CLOG_INFO(&LOG, 3, "Checking last device usage and timestamp");
493
494 if (g_state.num_device_users) {
495 CLOG_INFO(&LOG, 3, "Device is used by %d user(s)", g_state.num_device_users);
496 continue;
497 }
498
499 const std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
500 if ((now - g_state.last_user_disconnect_time_point) >= device_close_delay) {
501 sound_device_close_no_lock();
502 }
503 }
504
505 CLOG_INFO(&LOG, 3, "Delayed device close thread finished");
506}
507
509
510static void sound_sync_callback(void *data, int mode, float time)
511{
512 if (sound_jack_sync_callback == nullptr) {
513 return;
514 }
515 Main *bmain = (Main *)data;
516 sound_jack_sync_callback(bmain, mode, time);
517}
518
519void BKE_sound_force_device(const char *device)
520{
521 g_state.force_device = device;
522}
523
525{
526 AUD_initOnce();
527 if (sound_use_close_thread()) {
528 CLOG_INFO(&LOG, 2, "Using delayed device close thread");
529 g_state.delayed_close_thread = std::thread(delayed_close_thread_run);
530 }
531}
532
533void BKE_sound_exit()
534{
535 std::lock_guard lock(g_state.sound_device_mutex);
536 sound_device_close_no_lock();
537}
538
540{
541 {
542 std::unique_lock lock(g_state.sound_device_mutex);
543 g_state.need_exit = true;
544 }
545
546 if (g_state.delayed_close_thread.joinable()) {
547 g_state.delayed_close_cv.notify_one();
548 g_state.delayed_close_thread.join();
549 }
550
551 std::lock_guard lock(g_state.sound_device_mutex);
552 sound_device_close_no_lock();
553 AUD_exitOnce();
554
555 if (audio_device_names != nullptr) {
556 int i;
557 for (i = 0; audio_device_names[i]; i++) {
558 free(audio_device_names[i]);
559 }
560 free(audio_device_names);
561 audio_device_names = nullptr;
562 }
563}
564
565void BKE_sound_init(Main *bmain)
566{
567 std::lock_guard lock(g_state.sound_device_mutex);
568 /* Make sure no instance of the sound system is running, otherwise we get leaks. */
569 sound_device_close_no_lock();
570
571 AUD_DeviceSpecs requested_specs;
572 requested_specs.channels = AUD_Channels(U.audiochannels);
573 requested_specs.format = AUD_SampleFormat(U.audioformat);
574 requested_specs.rate = U.audiorate;
575
576 if (g_state.force_device == nullptr) {
577 char **names = BKE_sound_get_device_names();
578 g_state.device_name = names[0];
579
580 /* make sure device is within the bounds of the array */
581 for (int i = 0; names[i]; i++) {
582 if (i == U.audiodevice) {
583 g_state.device_name = names[i];
584 }
585 }
586 }
587 else {
588 g_state.device_name = g_state.force_device;
589 }
590
591 g_state.buffer_size = U.mixbufsize < 128 ? 1024 : U.mixbufsize;
592
593 if (requested_specs.rate < AUD_RATE_8000) {
594 requested_specs.rate = AUD_RATE_48000;
595 }
596
597 if (requested_specs.format <= AUD_FORMAT_INVALID) {
598 requested_specs.format = AUD_FORMAT_S16;
599 }
600
601 if (requested_specs.channels <= AUD_CHANNELS_INVALID) {
602 requested_specs.channels = AUD_CHANNELS_STEREO;
603 }
604
605 /* Make sure that we have our initalized_specs */
606 sound_device_open_no_lock(requested_specs);
607 if (STR_ELEM(g_state.device_name, "JACK", "PulseAudio", "PipeWire")) {
608 /* JACK:
609 * Do not close the device when using JACK. If we close it, we will not be able to
610 * respond to JACK audio bus commands.
611 *
612 * PulseAudio, PipeWire:
613 * These APIs are built around the idea that the program using them keeps the device open.
614 * Instead it uses audio streams to determine if something is playing back audio or not.
615 * These streams are only active when Audaspace is playing back, so we don't need to
616 * do anything manually.
617 * If we close these devices, it will become very hard and tedious for end users to
618 * control the volume or route audio from Blender.
619 */
620 g_state.use_delayed_close = false;
621 AUD_setSynchronizerCallback(sound_sync_callback, bmain);
622 }
623 else {
624 g_state.use_delayed_close = true;
625 sound_device_close_no_lock();
626 }
627}
628
630{
631 std::lock_guard lock(g_state.sound_device_mutex);
632 if (g_state.sound_device) {
633 AUD_setSynchronizerCallback(sound_sync_callback, bmain);
634 }
635}
636
637/* XXX unused currently */
638# if 0
639bSound *BKE_sound_new_buffer(Main *bmain, bSound *source)
640{
641 bSound *sound = nullptr;
642
643 char name[MAX_ID_NAME + 5];
644 BLI_string_join(name, sizeof(name), "buf_", source->id.name);
645
646 sound = BKE_libblock_alloc(bmain, ID_SO, name);
647
648 sound->child_sound = source;
649 sound->type = SOUND_TYPE_BUFFER;
650
651 sound_load(bmain, sound);
652
653 return sound;
654}
655
656bSound *BKE_sound_new_limiter(Main *bmain, bSound *source, float start, float end)
657{
658 bSound *sound = nullptr;
659
660 char name[MAX_ID_NAME + 5];
661 BLI_string_join(name, sizeof(name), "lim_", source->id.name);
662
663 sound = BKE_libblock_alloc(bmain, ID_SO, name);
664
665 sound->child_sound = source;
666 sound->start = start;
667 sound->end = end;
668 sound->type = SOUND_TYPE_LIMITER;
669
670 sound_load(bmain, sound);
671
672 return sound;
673}
674# endif
675
676void BKE_sound_cache(bSound *sound)
677{
679
680 if (sound->cache) {
681 AUD_Sound_free(sound->cache);
682 }
683
684 sound->cache = AUD_Sound_cache(sound->handle);
685 if (sound->cache) {
686 sound->playback_handle = sound->cache;
687 }
688 else {
689 sound->playback_handle = sound->handle;
690 }
691}
692
694{
695 if (sound->cache) {
696 AUD_Sound_free(sound->cache);
697 sound->cache = nullptr;
698 sound->playback_handle = sound->handle;
699 }
700}
701
702static void sound_load_audio(Main *bmain, bSound *sound, bool free_waveform)
703{
704
705 if (sound->cache) {
706 AUD_Sound_free(sound->cache);
707 sound->cache = nullptr;
708 }
709
710 if (sound->handle) {
711 AUD_Sound_free(sound->handle);
712 sound->handle = nullptr;
713 sound->playback_handle = nullptr;
714 }
715
716 if (free_waveform) {
718 }
719
720/* XXX unused currently */
721# if 0
722 switch (sound->type) {
723 case SOUND_TYPE_FILE:
724# endif
725 {
726 char fullpath[FILE_MAX];
727
728 /* load sound */
729 PackedFile *pf = sound->packedfile;
730
731 /* Don't modify `sound->filepath`, only change a copy. */
732 STRNCPY(fullpath, sound->filepath);
733 BLI_path_abs(fullpath, ID_BLEND_PATH(bmain, &sound->id));
734
735 /* but we need a packed file then */
736 if (pf) {
737 sound->handle = AUD_Sound_bufferFile((uchar *)pf->data, pf->size);
738 }
739 else {
740 /* or else load it from disk */
741 sound->handle = AUD_Sound_file(fullpath);
742 }
743 }
744/* XXX unused currently */
745# if 0
746 break;
747 }
748 case SOUND_TYPE_BUFFER:
749 if (sound->child_sound && sound->child_sound->handle) {
750 sound->handle = AUD_bufferSound(sound->child_sound->handle);
751 }
752 break;
753 case SOUND_TYPE_LIMITER:
754 if (sound->child_sound && sound->child_sound->handle) {
755 sound->handle = AUD_limitSound(sound->child_sound, sound->start, sound->end);
756 }
757 break;
758}
759# endif
760 if (sound->flags & SOUND_FLAGS_MONO) {
761 void *handle = AUD_Sound_rechannel(sound->handle, AUD_CHANNELS_MONO);
762 AUD_Sound_free(sound->handle);
763 sound->handle = handle;
764 }
765
766 if (sound->flags & SOUND_FLAGS_CACHING) {
767 sound->cache = AUD_Sound_cache(sound->handle);
768 }
769
770 if (sound->cache) {
771 sound->playback_handle = sound->cache;
772 }
773 else {
774 sound->playback_handle = sound->handle;
775 }
776}
777
778void BKE_sound_load(Main *bmain, bSound *sound)
779{
781 sound_load_audio(bmain, sound, true);
782}
783
784AUD_Device *BKE_sound_mixdown(const Scene *scene, AUD_DeviceSpecs specs, int start, float volume)
785{
787 return AUD_openMixdownDevice(
788 specs, scene->sound_scene, volume, AUD_RESAMPLE_QUALITY_MEDIUM, start / FPS);
789}
790
791void BKE_sound_create_scene(Scene *scene)
792{
794
795 /* should be done in version patch, but this gets called before */
796 if (scene->r.frs_sec_base == 0) {
797 scene->r.frs_sec_base = 1;
798 }
799
800 scene->sound_scene = AUD_Sequence_create(FPS, scene->audio.flag & AUDIO_MUTE);
801 AUD_Sequence_setSpeedOfSound(scene->sound_scene, scene->audio.speed_of_sound);
802 AUD_Sequence_setDopplerFactor(scene->sound_scene, scene->audio.doppler_factor);
803 AUD_Sequence_setDistanceModel(scene->sound_scene,
804 AUD_DistanceModel(scene->audio.distance_model));
805 scene->playback_handle = nullptr;
806 scene->sound_scrub_handle = nullptr;
807 scene->speaker_handles = nullptr;
808}
809
811{
812 if (scene->playback_handle) {
813 AUD_Handle_stop(scene->playback_handle);
814 }
815 if (scene->sound_scrub_handle) {
816 AUD_Handle_stop(scene->sound_scrub_handle);
817 }
818 if (scene->speaker_handles) {
819 void *handle;
820
821 while ((handle = AUD_getSet(scene->speaker_handles))) {
822 AUD_Sequence_remove(scene->sound_scene, handle);
823 }
824
826 }
827 if (scene->sound_scene) {
828 AUD_Sequence_free(scene->sound_scene);
829 }
830}
831
832void BKE_sound_lock()
833{
834 g_state.sound_device_mutex.lock();
835 if (g_state.sound_device == nullptr) {
836 return;
837 }
838 AUD_Device_lock(g_state.sound_device);
839}
840
841void BKE_sound_unlock()
842{
843 g_state.sound_device_mutex.unlock();
844 if (g_state.sound_device == nullptr) {
845 return;
846 }
847 AUD_Device_unlock(g_state.sound_device);
848}
849
851{
853
854 if (scene->sound_scene) {
855 AUD_Sequence_setSpecs(scene->sound_scene, g_state.initialized_specs.specs);
856 }
857}
858
859void BKE_sound_mute_scene(Scene *scene, int muted)
860{
862 if (scene->sound_scene) {
863 AUD_Sequence_setMuted(scene->sound_scene, muted);
864 }
865}
866
867void BKE_sound_update_fps(Main *bmain, Scene *scene)
868{
870
871 if (scene->sound_scene) {
872 AUD_Sequence_setFPS(scene->sound_scene, FPS);
873 }
874
876}
877
879{
881
882 AUD_Sequence_setSpeedOfSound(scene->sound_scene, scene->audio.speed_of_sound);
883 AUD_Sequence_setDopplerFactor(scene->sound_scene, scene->audio.doppler_factor);
884 AUD_Sequence_setDistanceModel(scene->sound_scene,
885 AUD_DistanceModel(scene->audio.distance_model));
886}
887
889 Scene *scene, Strip *sequence, int startframe, int endframe, int frameskip)
890{
892 if (sequence->scene && scene != sequence->scene) {
893 const double fps = FPS;
894 return AUD_Sequence_add(scene->sound_scene,
895 sequence->scene->sound_scene,
896 startframe / fps,
897 endframe / fps,
898 frameskip / fps);
899 }
900 return nullptr;
901}
902
904{
906 scene,
907 sequence,
910 sequence->startofs + sequence->anim_startofs);
911}
912
914 Scene *scene, Strip *sequence, int startframe, int endframe, int frameskip)
915{
917 /* Happens when sequence's sound data-block was removed. */
918 if (sequence->sound == nullptr) {
919 return nullptr;
920 }
922 const double fps = FPS;
923 const double offset_time = sequence->sound->offset_time + sequence->sound_offset -
924 frameskip / fps;
925 if (offset_time >= 0.0f) {
926 return AUD_Sequence_add(scene->sound_scene,
927 sequence->sound->playback_handle,
928 startframe / fps + offset_time,
929 endframe / fps,
930 0.0f);
931 }
932 return AUD_Sequence_add(scene->sound_scene,
933 sequence->sound->playback_handle,
934 startframe / fps,
935 endframe / fps,
936 -offset_time);
937}
938
939void *BKE_sound_add_scene_sound_defaults(Scene *scene, Strip *sequence)
940{
941 return BKE_sound_add_scene_sound(scene,
942 sequence,
945 sequence->startofs + sequence->anim_startofs);
946}
947
948void BKE_sound_remove_scene_sound(Scene *scene, void *handle)
949{
950 AUD_Sequence_remove(scene->sound_scene, handle);
951}
952
953void BKE_sound_mute_scene_sound(void *handle, bool mute)
954{
955 AUD_SequenceEntry_setMuted(handle, mute);
956}
957
958void BKE_sound_move_scene_sound(const Scene *scene,
959 void *handle,
960 int startframe,
961 int endframe,
962 int frameskip,
963 double audio_offset)
964{
966 const double fps = FPS;
967 const double offset_time = audio_offset - frameskip / fps;
968 if (offset_time >= 0.0f) {
969 AUD_SequenceEntry_move(handle, startframe / fps + offset_time, endframe / fps, 0.0f);
970 }
971 else {
972 AUD_SequenceEntry_move(handle, startframe / fps, endframe / fps, -offset_time);
973 }
974}
975
976void BKE_sound_move_scene_sound_defaults(Scene *scene, Strip *sequence)
977{
979 if (sequence->scene_sound) {
980 double offset_time = 0.0f;
981 if (sequence->sound != nullptr) {
982 offset_time = sequence->sound->offset_time + sequence->sound_offset;
983 }
985 sequence->scene_sound,
988 sequence->startofs + sequence->anim_startofs,
989 offset_time);
990 }
991}
992
993void BKE_sound_update_scene_sound(void *handle, bSound *sound)
994{
995 AUD_SequenceEntry_setSound(handle, sound->playback_handle);
996}
997
998#endif /* WITH_AUDASPACE */
999
1000void BKE_sound_update_sequence_handle(void *handle, void *sound_handle)
1001{
1002#ifdef WITH_AUDASPACE
1003 AUD_SequenceEntry_setSound(handle, sound_handle);
1004#else
1005 UNUSED_VARS(handle, sound_handle);
1006#endif
1007}
1008
1009#ifdef WITH_AUDASPACE
1010
1011void BKE_sound_set_scene_volume(Scene *scene, float volume)
1012{
1014 if (scene->sound_scene == nullptr) {
1015 return;
1016 }
1017 AUD_Sequence_setAnimationData(scene->sound_scene,
1018 AUD_AP_VOLUME,
1019 scene->r.cfra,
1020 &volume,
1021 (scene->audio.flag & AUDIO_VOLUME_ANIMATED) != 0);
1022}
1023
1025 const int frame,
1026 float volume,
1027 const char animated)
1028{
1029 AUD_SequenceEntry_setAnimationData(handle, AUD_AP_VOLUME, frame, &volume, animated);
1030}
1031
1033 const int frame,
1034 float pitch,
1035 const char animated)
1036{
1037 AUD_SequenceEntry_setAnimationData(handle, AUD_AP_PITCH, frame, &pitch, animated);
1038}
1039
1041 int frame_start,
1042 int frame_end,
1043 float pitch)
1044{
1045 frame_start = max_ii(0, frame_start);
1046 frame_end = max_ii(0, frame_end);
1047 AUD_SequenceEntry_setConstantRangeAnimationData(
1048 handle, AUD_AP_PITCH, frame_start, frame_end, &pitch);
1049}
1050
1052 const int frame,
1053 float pan,
1054 const char animated)
1055{
1056 AUD_SequenceEntry_setAnimationData(handle, AUD_AP_PANNING, frame, &pan, animated);
1057}
1058
1060{
1061 BLI_assert_msg(0, "is not supposed to be used, is weird function.");
1062
1063 Scene *scene;
1064
1065 for (scene = static_cast<Scene *>(main->scenes.first); scene;
1066 scene = static_cast<Scene *>(scene->id.next))
1067 {
1068 blender::seq::sound_update(scene, sound);
1069 }
1070}
1071
1072/* This function assumes that you have already held the g_state.sound_device mutex. */
1073static void sound_start_play_scene(Scene *scene)
1074{
1076
1077 if (scene->playback_handle) {
1078 AUD_Handle_stop(scene->playback_handle);
1079 }
1080
1082
1083 scene->playback_handle = AUD_Device_play(g_state.sound_device, scene->sound_scene, 1);
1084 if (scene->playback_handle) {
1085 AUD_Handle_setLoopCount(scene->playback_handle, -1);
1086 }
1087}
1088
1089void BKE_sound_play_scene(Scene *scene)
1090{
1091 std::lock_guard lock(g_state.sound_device_mutex);
1092 sound_device_use_begin();
1094
1095 AUD_Status status;
1096 const double cur_time = FRA2TIME((scene->r.cfra + scene->r.subframe));
1097
1098 AUD_Device_lock(g_state.sound_device);
1099
1100 if (scene->sound_scrub_handle &&
1101 AUD_Handle_getStatus(scene->sound_scrub_handle) != AUD_STATUS_INVALID)
1102 {
1103 /* If the audio scrub handle is playing back, stop to make sure it is not active.
1104 * Otherwise, it will trigger a callback that will stop audio playback. */
1105 AUD_Handle_stop(scene->sound_scrub_handle);
1106 scene->sound_scrub_handle = nullptr;
1107 /* The scrub_handle started playback with playback_handle, stop it so we can
1108 * properly restart it. */
1109 AUD_Handle_pause(scene->playback_handle);
1110 }
1111
1112 status = scene->playback_handle ? AUD_Handle_getStatus(scene->playback_handle) :
1113 AUD_STATUS_INVALID;
1114
1115 if (status == AUD_STATUS_INVALID) {
1116 sound_start_play_scene(scene);
1117
1118 if (!scene->playback_handle) {
1119 AUD_Device_unlock(g_state.sound_device);
1120 return;
1121 }
1122 }
1123
1124 if (status != AUD_STATUS_PLAYING) {
1125 /* Seeking the synchronizer will also seek the playback handle.
1126 * Even if we don't have A/V sync on, keep the synchronizer and handle seek time in sync. */
1127 AUD_seekSynchronizer(cur_time);
1128 AUD_Handle_setPosition(scene->playback_handle, cur_time);
1129 AUD_Handle_resume(scene->playback_handle);
1130 }
1131
1132 if (scene->audio.flag & AUDIO_SYNC) {
1133 AUD_playSynchronizer();
1134 }
1135
1136 AUD_Device_unlock(g_state.sound_device);
1137}
1138
1139void BKE_sound_stop_scene(Scene *scene)
1140{
1141 std::lock_guard lock(g_state.sound_device_mutex);
1142 BLI_assert(g_state.sound_device);
1143 if (scene->playback_handle) {
1144 AUD_Handle_pause(scene->playback_handle);
1145
1146 if (scene->audio.flag & AUDIO_SYNC) {
1147 AUD_stopSynchronizer();
1148 }
1149 }
1150 sound_device_use_end();
1151}
1152
1153void BKE_sound_seek_scene(Main *bmain, Scene *scene)
1154{
1155 std::lock_guard lock(g_state.sound_device_mutex);
1156 bool animation_playing = false;
1157 for (bScreen *screen = static_cast<bScreen *>(bmain->screens.first); screen;
1158 screen = static_cast<bScreen *>(screen->id.next))
1159 {
1160 if (screen->animtimer) {
1161 animation_playing = true;
1162 break;
1163 }
1164 }
1165
1166 bool do_audio_scrub = scene->audio.flag & AUDIO_SCRUB && !animation_playing;
1167
1168 if (do_audio_scrub) {
1169 /* Make sure the sound device is open for scrubbing. */
1170 sound_device_use_begin();
1171 }
1172 else if (g_state.sound_device == nullptr) {
1173 /* Nothing to do if there is no sound device and we are not doing audio scrubbing. */
1174 return;
1175 }
1177
1178 AUD_Device_lock(g_state.sound_device);
1179
1180 AUD_Status status = scene->playback_handle ? AUD_Handle_getStatus(scene->playback_handle) :
1181 AUD_STATUS_INVALID;
1182 if (status == AUD_STATUS_INVALID) {
1183 sound_start_play_scene(scene);
1184
1185 if (!scene->playback_handle) {
1186 AUD_Device_unlock(g_state.sound_device);
1187 if (do_audio_scrub) {
1188 sound_device_use_end();
1189 }
1190 return;
1191 }
1192
1193 AUD_Handle_pause(scene->playback_handle);
1194 }
1195
1196 const double one_frame = 1.0 / FPS +
1197 (U.audiorate > 0 ? U.mixbufsize / double(U.audiorate) : 0.0);
1198 const double cur_time = FRA2TIME(scene->r.cfra);
1199
1200 if (do_audio_scrub) {
1201 /* Playback one frame of audio without advancing the timeline. */
1202 AUD_Handle_setPosition(scene->playback_handle, cur_time);
1203 AUD_Handle_resume(scene->playback_handle);
1204 if (scene->sound_scrub_handle &&
1205 AUD_Handle_getStatus(scene->sound_scrub_handle) != AUD_STATUS_INVALID)
1206 {
1207 AUD_Handle_setPosition(scene->sound_scrub_handle, 0);
1208 }
1209 else {
1210 if (scene->sound_scrub_handle) {
1211 AUD_Handle_stop(scene->sound_scrub_handle);
1212 }
1213 scene->sound_scrub_handle = AUD_pauseAfter(scene->playback_handle, one_frame);
1214 }
1215 sound_device_use_end_after(std::chrono::milliseconds(int(one_frame * 1000)));
1216 }
1217 else if (status == AUD_STATUS_PLAYING) {
1218 /* Seeking the synchronizer will also seek the playback handle.
1219 * Even if we don't have A/V sync on, keep the synchronizer and handle
1220 * seek time in sync.
1221 */
1222 AUD_seekSynchronizer(cur_time);
1223 AUD_Handle_setPosition(scene->playback_handle, cur_time);
1224 }
1225
1226 AUD_Device_unlock(g_state.sound_device);
1227}
1228
1229double BKE_sound_sync_scene(Scene *scene)
1230{
1232
1233 /* Ugly: Blender doesn't like it when the animation is played back during rendering */
1234 if (G.is_rendering) {
1235 return NAN_FLT;
1236 }
1237
1238 if (scene->playback_handle) {
1239 if (scene->audio.flag & AUDIO_SYNC) {
1240 return AUD_getSynchronizerPosition();
1241 }
1242
1243 return AUD_Handle_getPosition(scene->playback_handle);
1244 }
1245 return NAN_FLT;
1246}
1247
1249{
1250 if ((sound->tags & SOUND_TAGS_WAVEFORM_NO_RELOAD) == 0) {
1251 SoundWaveform *waveform = static_cast<SoundWaveform *>(sound->waveform);
1252 if (waveform) {
1253 if (waveform->data) {
1254 MEM_freeN(waveform->data);
1255 }
1256 MEM_freeN(waveform);
1257 }
1258
1259 sound->waveform = nullptr;
1260 }
1261 /* This tag is only valid once. */
1263}
1264
1265void BKE_sound_read_waveform(Main *bmain, bSound *sound, bool *stop)
1266{
1267 bool need_close_audio_handles = false;
1268 if (sound->playback_handle == nullptr) {
1269 /* TODO(sergey): Make it fully independent audio handle. */
1270 sound_load_audio(bmain, sound, true);
1271 need_close_audio_handles = true;
1272 }
1273
1274 AUD_SoundInfo info = AUD_getInfo(sound->playback_handle);
1275 SoundWaveform *waveform = MEM_mallocN<SoundWaveform>("SoundWaveform");
1276
1277 if (info.length > 0) {
1278 int length = info.length * SOUND_WAVE_SAMPLES_PER_SECOND;
1279
1280 waveform->data = MEM_malloc_arrayN<float>(3 * size_t(length), "SoundWaveform.samples");
1281 /* Ideally this would take a boolean argument. */
1282 short stop_i16 = *stop;
1283 waveform->length = AUD_readSound(
1284 sound->playback_handle, waveform->data, length, SOUND_WAVE_SAMPLES_PER_SECOND, &stop_i16);
1285 *stop = stop_i16 != 0;
1286 }
1287 else {
1288 /* Create an empty waveform here if the sound couldn't be
1289 * read. This indicates that reading the waveform is "done",
1290 * whereas just setting sound->waveform to nullptr causes other
1291 * code to think the waveform still needs to be created. */
1292 waveform->data = nullptr;
1293 waveform->length = 0;
1294 }
1295
1296 if (*stop) {
1297 if (waveform->data) {
1298 MEM_freeN(waveform->data);
1299 }
1300 MEM_freeN(waveform);
1301 BLI_spin_lock(static_cast<SpinLock *>(sound->spinlock));
1303 BLI_spin_unlock(static_cast<SpinLock *>(sound->spinlock));
1304 return;
1305 }
1306
1308
1309 BLI_spin_lock(static_cast<SpinLock *>(sound->spinlock));
1310 sound->waveform = waveform;
1312 BLI_spin_unlock(static_cast<SpinLock *>(sound->spinlock));
1313
1314 if (need_close_audio_handles) {
1315 sound_free_audio(sound);
1316 }
1317}
1318
1319static void sound_update_base(Scene *scene, Object *object, void *new_set)
1320{
1321 Speaker *speaker;
1322 float quat[4];
1323
1325 sound_verify_evaluated_id(&object->id);
1326
1327 if ((object->type != OB_SPEAKER) || !object->adt) {
1328 return;
1329 }
1330
1331 LISTBASE_FOREACH (NlaTrack *, track, &object->adt->nla_tracks) {
1332 LISTBASE_FOREACH (NlaStrip *, strip, &track->strips) {
1333 if (strip->type != NLASTRIP_TYPE_SOUND) {
1334 continue;
1335 }
1336 speaker = (Speaker *)object->data;
1337
1338 if (AUD_removeSet(scene->speaker_handles, strip->speaker_handle)) {
1339 if (speaker->sound) {
1340 AUD_SequenceEntry_move(strip->speaker_handle, double(strip->start) / FPS, FLT_MAX, 0);
1341 }
1342 else {
1343 AUD_Sequence_remove(scene->sound_scene, strip->speaker_handle);
1344 strip->speaker_handle = nullptr;
1345 }
1346 }
1347 else {
1348 if (speaker->sound) {
1349 strip->speaker_handle = AUD_Sequence_add(scene->sound_scene,
1350 speaker->sound->playback_handle,
1351 double(strip->start) / FPS,
1352 FLT_MAX,
1353 0);
1354 AUD_SequenceEntry_setRelative(strip->speaker_handle, 0);
1355 }
1356 }
1357
1358 if (strip->speaker_handle) {
1359 const bool mute = ((strip->flag & NLASTRIP_FLAG_MUTED) || (speaker->flag & SPK_MUTED));
1360 AUD_addSet(new_set, strip->speaker_handle);
1361 AUD_SequenceEntry_setVolumeMaximum(strip->speaker_handle, speaker->volume_max);
1362 AUD_SequenceEntry_setVolumeMinimum(strip->speaker_handle, speaker->volume_min);
1363 AUD_SequenceEntry_setDistanceMaximum(strip->speaker_handle, speaker->distance_max);
1364 AUD_SequenceEntry_setDistanceReference(strip->speaker_handle, speaker->distance_reference);
1365 AUD_SequenceEntry_setAttenuation(strip->speaker_handle, speaker->attenuation);
1366 AUD_SequenceEntry_setConeAngleOuter(strip->speaker_handle, speaker->cone_angle_outer);
1367 AUD_SequenceEntry_setConeAngleInner(strip->speaker_handle, speaker->cone_angle_inner);
1368 AUD_SequenceEntry_setConeVolumeOuter(strip->speaker_handle, speaker->cone_volume_outer);
1369
1370 mat4_to_quat(quat, object->object_to_world().ptr());
1371 blender::float3 location = object->object_to_world().location();
1372 AUD_SequenceEntry_setAnimationData(
1373 strip->speaker_handle, AUD_AP_LOCATION, scene->r.cfra, location, 1);
1374 AUD_SequenceEntry_setAnimationData(
1375 strip->speaker_handle, AUD_AP_ORIENTATION, scene->r.cfra, quat, 1);
1376 AUD_SequenceEntry_setAnimationData(
1377 strip->speaker_handle, AUD_AP_VOLUME, scene->r.cfra, &speaker->volume, 1);
1378 AUD_SequenceEntry_setAnimationData(
1379 strip->speaker_handle, AUD_AP_PITCH, scene->r.cfra, &speaker->pitch, 1);
1380 AUD_SequenceEntry_setSound(strip->speaker_handle, speaker->sound->playback_handle);
1381 AUD_SequenceEntry_setMuted(strip->speaker_handle, mute);
1382 }
1383 }
1384 }
1385}
1386
1387void BKE_sound_update_scene(Depsgraph *depsgraph, Scene *scene)
1388{
1390
1391 void *new_set = AUD_createSet();
1392 void *handle;
1393 float quat[4];
1394
1395 /* cheap test to skip looping over all objects (no speakers is a common case) */
1397 DEGObjectIterSettings deg_iter_settings = {nullptr};
1398 deg_iter_settings.depsgraph = depsgraph;
1399 deg_iter_settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY |
1402 DEG_OBJECT_ITER_BEGIN (&deg_iter_settings, object) {
1403 sound_update_base(scene, object, new_set);
1404 }
1406 }
1407
1408 while ((handle = AUD_getSet(scene->speaker_handles))) {
1409 AUD_Sequence_remove(scene->sound_scene, handle);
1410 }
1411
1412 if (scene->camera) {
1413 mat4_to_quat(quat, scene->camera->object_to_world().ptr());
1414 blender::float3 location = scene->camera->object_to_world().location();
1415 AUD_Sequence_setAnimationData(scene->sound_scene, AUD_AP_LOCATION, scene->r.cfra, location, 1);
1416 AUD_Sequence_setAnimationData(scene->sound_scene, AUD_AP_ORIENTATION, scene->r.cfra, quat, 1);
1417 }
1418
1420 scene->speaker_handles = new_set;
1421}
1422
1423void *BKE_sound_get_factory(void *sound)
1424{
1425 return ((bSound *)sound)->playback_handle;
1426}
1427
1428float BKE_sound_get_length(Main *bmain, bSound *sound)
1429{
1430 if (sound->playback_handle != nullptr) {
1431 AUD_SoundInfo info = AUD_getInfo(sound->playback_handle);
1432 return info.length;
1433 }
1434 SoundInfo info;
1435 if (!BKE_sound_info_get(bmain, sound, &info)) {
1436 return 0.0f;
1437 }
1438 return info.length;
1439}
1440
1442{
1443 if (audio_device_names == nullptr) {
1444 audio_device_names = AUD_getDeviceNames();
1445 }
1446
1447 return audio_device_names;
1448}
1449
1450static bool sound_info_from_playback_handle(void *playback_handle, SoundInfo *sound_info)
1451{
1452 if (playback_handle == nullptr) {
1453 return false;
1454 }
1455 AUD_SoundInfo info = AUD_getInfo(playback_handle);
1456 sound_info->specs.channels = (eSoundChannels)info.specs.channels;
1457 sound_info->length = info.length;
1458 sound_info->specs.samplerate = info.specs.rate;
1459 return true;
1460}
1461
1462bool BKE_sound_info_get(Main *main, bSound *sound, SoundInfo *sound_info)
1463{
1464 if (sound->playback_handle != nullptr) {
1465 return sound_info_from_playback_handle(sound->playback_handle, sound_info);
1466 }
1467 /* TODO(sergey): Make it fully independent audio handle. */
1468 /* Don't free waveforms during non-destructive queries.
1469 * This causes unnecessary recalculation - see #69921 */
1470 sound_load_audio(main, sound, false);
1471 const bool result = sound_info_from_playback_handle(sound->playback_handle, sound_info);
1472 sound_free_audio(sound);
1473 return result;
1474}
1475
1477 const char *filepath,
1478 int stream,
1479 SoundStreamInfo *sound_info)
1480{
1481 const char *blendfile_path = BKE_main_blendfile_path(main);
1482 char filepath_abs[FILE_MAX];
1483 AUD_Sound *sound;
1484 AUD_StreamInfo *stream_infos;
1485 int stream_count;
1486
1487 STRNCPY(filepath_abs, filepath);
1488 BLI_path_abs(filepath_abs, blendfile_path);
1489
1490 sound = AUD_Sound_file(filepath_abs);
1491 if (!sound) {
1492 return false;
1493 }
1494
1495 stream_count = AUD_Sound_getFileStreams(sound, &stream_infos);
1496
1497 AUD_Sound_free(sound);
1498
1499 if (!stream_infos) {
1500 return false;
1501 }
1502
1503 if ((stream < 0) || (stream >= stream_count)) {
1504 free(stream_infos);
1505 return false;
1506 }
1507
1508 sound_info->start = stream_infos[stream].start;
1509 sound_info->duration = stream_infos[stream].duration;
1510
1511 free(stream_infos);
1512
1513 return true;
1514}
1515
1516#else /* WITH_AUDASPACE */
1517
1518# include "BLI_utildefines.h"
1519
1520void BKE_sound_force_device(const char * /*device*/) {}
1522void BKE_sound_init(Main * /*bmain*/) {}
1525void BKE_sound_cache(bSound * /*sound*/) {}
1526void BKE_sound_delete_cache(bSound * /*sound*/) {}
1527void BKE_sound_load(Main * /*bmain*/, bSound * /*sound*/) {}
1528void BKE_sound_create_scene(Scene * /*scene*/) {}
1529void BKE_sound_destroy_scene(Scene * /*scene*/) {}
1534void BKE_sound_mute_scene(Scene * /*scene*/, int /*muted*/) {}
1536 Strip * /*sequence*/,
1537 int /*startframe*/,
1538 int /*endframe*/,
1539 int /*frameskip*/)
1540{
1541 return nullptr;
1542}
1543void *BKE_sound_scene_add_scene_sound_defaults(Scene * /*scene*/, Strip * /*sequence*/)
1544{
1545 return nullptr;
1546}
1548 Strip * /*sequence*/,
1549 int /*startframe*/,
1550 int /*endframe*/,
1551 int /*frameskip*/)
1552{
1553 return nullptr;
1554}
1555void *BKE_sound_add_scene_sound_defaults(Scene * /*scene*/, Strip * /*sequence*/)
1556{
1557 return nullptr;
1558}
1559void BKE_sound_remove_scene_sound(Scene * /*scene*/, void * /*handle*/) {}
1560void BKE_sound_mute_scene_sound(void * /*handle*/, bool /*mute*/) {}
1561void BKE_sound_move_scene_sound(const Scene * /*scene*/,
1562 void * /*handle*/,
1563 int /*startframe*/,
1564 int /*endframe*/,
1565 int /*frameskip*/,
1566 double /*audio_offset*/)
1567{
1568}
1569void BKE_sound_move_scene_sound_defaults(Scene * /*scene*/, Strip * /*sequence*/) {}
1570void BKE_sound_play_scene(Scene * /*scene*/) {}
1571void BKE_sound_stop_scene(Scene * /*scene*/) {}
1572void BKE_sound_seek_scene(Main * /*bmain*/, Scene * /*scene*/) {}
1573double BKE_sound_sync_scene(Scene * /*scene*/)
1574{
1575 return NAN_FLT;
1576}
1578 bSound *sound,
1579 /* NOLINTNEXTLINE: readability-non-const-parameter. */
1580 bool *stop)
1581{
1582 UNUSED_VARS(sound, stop, bmain);
1583}
1584void BKE_sound_update_sequencer(Main * /*main*/, bSound * /*sound*/) {}
1585void BKE_sound_update_scene(Depsgraph * /*depsgraph*/, Scene * /*scene*/) {}
1586void BKE_sound_update_scene_sound(void * /*handle*/, bSound * /*sound*/) {}
1588void BKE_sound_update_fps(Main * /*bmain*/, Scene * /*scene*/) {}
1590 int /*frame*/,
1591 float /*volume*/,
1592 char /*animated*/)
1593{
1594}
1596 int /*frame*/,
1597 float /*pan*/,
1598 char /*animated*/)
1599{
1600}
1601void BKE_sound_set_scene_volume(Scene * /*scene*/, float /*volume*/) {}
1603 int /*frame*/,
1604 float /*pitch*/,
1605 char /*animated*/)
1606{
1607}
1609 int /*frame_start*/,
1610 int /*frame_end*/,
1611 float /*pitch*/)
1612{
1613}
1614float BKE_sound_get_length(Main * /*bmain*/, bSound * /*sound*/)
1615{
1616 return 0;
1617}
1619{
1620 static char *names[1] = {nullptr};
1621 return names;
1622}
1623
1625
1626bool BKE_sound_info_get(Main * /*main*/, bSound * /*sound*/, SoundInfo * /*sound_info*/)
1627{
1628 return false;
1629}
1630
1632 const char * /*filepath*/,
1633 int /*stream*/,
1634 SoundStreamInfo * /*sound_info*/)
1635{
1636 return false;
1637}
1638
1639#endif /* WITH_AUDASPACE */
1640
1642{
1643 scene->sound_scene = nullptr;
1644 scene->playback_handle = nullptr;
1645 scene->sound_scrub_handle = nullptr;
1646 scene->speaker_handles = nullptr;
1647}
1648
1650{
1651 if (scene->sound_scene != nullptr) {
1652 return;
1653 }
1655}
1656
1658{
1659 sound->cache = nullptr;
1660 sound->playback_handle = nullptr;
1661}
1662
1664{
1665 if (sound->cache != nullptr) {
1666 return;
1667 }
1668 BKE_sound_load(bmain, sound);
1669}
1670
1672{
1673#if defined(WITH_AUDASPACE)
1674 sound_jack_sync_callback = callback;
1675#else
1676 UNUSED_VARS(callback);
1677#endif
1678}
1679
1680void BKE_sound_jack_scene_update(Scene *scene, int mode, double time)
1681{
1683
1684 /* Ugly: Blender doesn't like it when the animation is played back during rendering. */
1685 if (G.is_rendering) {
1686 return;
1687 }
1688#ifdef WITH_AUDASPACE
1689 AUD_Device_lock(g_state.sound_device);
1690
1691 if (mode) {
1692 BKE_sound_play_scene(scene);
1693 }
1694 else {
1695 BKE_sound_stop_scene(scene);
1696 }
1697 if (scene->playback_handle != nullptr) {
1698 AUD_Handle_setPosition(scene->playback_handle, time);
1699 }
1700 AUD_Device_unlock(g_state.sound_device);
1701#else
1702 UNUSED_VARS(mode, time);
1703#endif
1704}
1705
1706void BKE_sound_evaluate(Depsgraph *depsgraph, Main *bmain, bSound *sound)
1707{
1708 DEG_debug_print_eval(depsgraph, __func__, sound->id.name, sound);
1709 if (sound->id.recalc & ID_RECALC_SOURCE) {
1710 /* Sequencer checks this flag to see if the strip sound is to be updated from the Audaspace
1711 * side. */
1712 sound->id.recalc |= ID_RECALC_AUDIO;
1713 }
1714
1715 if (sound->id.recalc & ID_RECALC_AUDIO) {
1716 BKE_sound_load(bmain, sound);
1717 return;
1718 }
1719 BKE_sound_ensure_loaded(bmain, sound);
1720}
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
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:44
@ IDTYPE_FLAGS_NO_ANIMDATA
Definition BKE_idtype.hh:46
void(*)(ID *id, const IDCacheKey *cache_key, void **cache_p, uint flags, void *user_data) IDTypeForeachCacheFunctionCallback
IDTypeInfo IDType_ID_SO
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:872
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
@ INDEX_ID_SO
Definition DNA_ID.h:1211
@ 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
@ 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_FLAGS_MONO
@ SOUND_FLAGS_CACHING
@ SOUND_TAGS_WAVEFORM_LOADING
@ SOUND_TAGS_WAVEFORM_NO_RELOAD
@ 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
struct SoundInfo::@044326216360345075252245153320322143020002364111 specs
int samplerate
Definition BKE_sound.h:77
eSoundChannels channels
Definition BKE_sound.h:76
float length
Definition BKE_sound.h:79
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