Blender V5.3
blenkernel/intern/sound.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 * SPDX-FileCopyrightText: 2025 Blender Authors
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later */
5
9
10#include <chrono>
11#include <condition_variable>
12#include <cstdlib>
13#include <cstring>
14#include <mutex>
15#include <numeric>
16#include <optional>
17#include <thread>
18#include <utility>
19
20#ifdef WITH_FFTW3
21# include <fftw3.h>
22#endif
27#include "BLI_enum_flags.hh"
28#include "BLI_listbase.hh"
32#include "BLI_string.hh"
33#include "BLI_threads.hh"
35#include "BLT_translation.hh"
36
37/* Allow using deprecated functionality for .blend file I/O. */
38#define DNA_DEPRECATED_ALLOW
39
47#include "DNA_speaker_types.h"
48#include "DNA_userdef_types.h"
50#ifdef WITH_AUDASPACE
51# include "BLI_set.hh"
52# include "BLI_vector.hh"
53
54# include <Exception.h>
55# include <IReader.h>
56# include <devices/DeviceManager.h>
57# include <devices/IDeviceFactory.h>
58# include <devices/IHandle.h>
59# include <devices/NULLDevice.h>
60# include <devices/ReadDevice.h>
61# include <file/File.h>
62# include <file/FileManager.h>
63# include <file/FileWriter.h>
64# include <fx/Accumulator.h>
65# include <fx/AnimateableTimeStretchPitchScale.h>
66# include <fx/Envelope.h>
67# include <fx/Highpass.h>
68# include <fx/Limiter.h>
69# include <fx/Lowpass.h>
70# include <fx/Sum.h>
71# include <fx/Threshold.h>
72# include <generator/Silence.h>
73# include <plugin/PluginManager.h>
74# include <respec/ChannelMapper.h>
75# include <respec/LinearResample.h>
76# include <sequence/Sequence.h>
77# include <sequence/SequenceEntry.h>
78# include <util/StreamBuffer.h>
79
80# include <fmt/format.h>
81
82#endif
83
84#include "BKE_bpath.hh"
85#include "BKE_global.hh"
86#include "BKE_idtype.hh"
87#include "BKE_lib_id.hh"
88#include "BKE_lib_query.hh"
89#include "BKE_library.hh"
90#include "BKE_main.hh"
91#include "BKE_packedFile.hh"
92#include "BKE_scene_runtime.hh"
93#include "BKE_sound.hh"
95
96#include "sound_reader_cache.hh"
97
98#include "DEG_depsgraph.hh"
103#include "SEQ_sequencer.hh"
104#include "SEQ_sound.hh"
105
108#include "CLG_log.h"
109
110namespace blender {
111
112namespace bke {
113struct SceneAudioRuntime;
115enum class SoundTags {
116 None = 0,
117 /* Do not free/reset waveform on sound load, only used by undo code. */
118 WaveformNoReload = 1 << 0,
124 ConcurrentMap<bSoundFrequencySampler::Key, std::shared_ptr<bSoundFrequencySampler>>;
125
126#ifdef WITH_AUDASPACE
127struct SoundRuntimeGeneration {
128 AUD_Sound handle;
129 bSoundFrequencySamplerMap samplers;
130 std::shared_ptr<SoundReaderCache> reader_cache;
131};
132#endif
133
137 /* The audaspace handle that should actually be played back.
138 * Should be cache if cache != NULL; otherwise its handle. */
140 /* Spin-lock for asynchronous loading of sounds. */
142
143 /* Note: not by-value since #sound_foreach_cache can only
144 * save/restore a pointer. */
147
148#ifdef WITH_AUDASPACE
149 std::mutex generation_mutex;
150 std::shared_ptr<SoundRuntimeGeneration> generation;
151#endif
152};
153
154} // namespace bke
156static void sound_free_audio(bSound *sound);
158static void sound_init_runtime(bSound *sound)
159{
160 sound->runtime = MEM_new<bke::SoundRuntime>(__func__);
162}
163
164#ifdef WITH_AUDASPACE
165static void sound_runtime_generation_replace(
166 bSound *sound, std::shared_ptr<bke::SoundRuntimeGeneration> generation)
167{
168 bke::SoundRuntime *runtime = sound->runtime;
169 std::shared_ptr<bke::SoundRuntimeGeneration> old_generation;
171 std::lock_guard lock{runtime->generation_mutex};
172 old_generation = std::move(runtime->generation);
173 runtime->generation = std::move(generation);
174 }
175}
177static std::shared_ptr<bke::SoundRuntimeGeneration> sound_runtime_generation_get(
178 const bSound &sound)
179{
180 bke::SoundRuntime *runtime = sound.runtime;
181 std::lock_guard lock{runtime->generation_mutex};
182 return runtime->generation;
183}
184#endif
185
186static void sound_free_waveform(bSound *sound)
187{
188 bke::SoundRuntime *runtime = sound->runtime;
189 if (!flag_is_set(runtime->tags, bke::SoundTags::WaveformNoReload)) {
190 MEM_SAFE_DELETE(runtime->waveform);
191 }
192 /* This tag is only valid once. */
193 runtime->tags &= ~bke::SoundTags::WaveformNoReload;
194}
195
196static void sound_copy_data(Main * /*bmain*/,
197 std::optional<Library *> /*owner_library*/,
198 ID *id_dst,
199 const ID *id_src,
200 const int /*flag*/)
201{
202 bSound *sound_dst = id_cast<bSound *>(id_dst);
203 const bSound *sound_src = id_cast<const bSound *>(id_src);
204
205 /* Just to be sure, should not have any value actually after reading time. */
206 sound_dst->newpackedfile = nullptr;
207
208 if (sound_src->packedfile != nullptr) {
209 sound_dst->packedfile = BKE_packedfile_duplicate(sound_src->packedfile);
210 }
212 sound_init_runtime(sound_dst);
213}
214
215static void sound_free_data(ID *id)
216{
217 bSound *sound = id_cast<bSound *>(id);
218
219 if (sound->packedfile) {
221 sound->packedfile = nullptr;
222 }
223
224 if (sound->runtime) {
225 sound_free_audio(sound);
226 sound_free_waveform(sound);
227 BLI_spin_end(&sound->runtime->spinlock);
228 MEM_delete(sound->runtime);
229 }
230}
231
232static void sound_foreach_cache(ID *id,
233 IDTypeForeachCacheFunctionCallback function_callback,
234 void *user_data)
236 bSound *sound = id_cast<bSound *>(id);
237 IDCacheKey key = {id->session_uid, 1};
238 function_callback(id, &key, reinterpret_cast<void **>(&sound->runtime->waveform), 0, user_data);
241static void sound_foreach_path(ID *id, BPathForeachPathData *bpath_data)
242{
243 bSound *sound = id_cast<bSound *>(id);
244 if (sound->packedfile != nullptr && (bpath_data->flag & BKE_BPATH_FOREACH_PATH_SKIP_PACKED) != 0)
245 {
246 return;
247 }
249 /* FIXME: This does not check for empty path... */
250 BKE_bpath_foreach_path_fixed_process(bpath_data, sound->filepath, sizeof(sound->filepath));
251}
252
253static void sound_blend_write(BlendWriter *writer, ID *id, const void *id_address)
254{
255 bSound *sound = id_cast<bSound *>(id);
256 const bool is_undo = writer->is_undo();
258 /* Clean up, important in undo case to reduce false detection of changed datablocks. */
259 sound->runtime = nullptr;
261 /* Do not store packed files in case this is a library override ID. */
262 if (ID_IS_OVERRIDE_LIBRARY(sound) && !is_undo) {
263 sound->packedfile = nullptr;
264 }
265
266 /* write LibData */
267 writer->write_id_struct(id_address, sound);
268 BKE_id_blend_write(writer, &sound->id);
271}
273static void sound_blend_read_data(BlendDataReader *reader, ID *id)
274{
275 bSound *sound = id_cast<bSound *>(id);
276 sound_init_runtime(sound);
277 if (BLO_read_data_is_undo(reader)) {
278 sound->runtime->tags |= bke::SoundTags::WaveformNoReload;
279 }
280
281 BKE_packedfile_blend_read(reader, &sound->packedfile, sound->filepath);
282 BKE_packedfile_blend_read(reader, &sound->newpackedfile, sound->filepath);
283}
286 .id_code = bSound::id_type,
287 .id_filter = FILTER_ID_SO,
288 .dependencies_id_types = 0,
289 .main_listbase_index = INDEX_ID_SO,
290 .struct_size = sizeof(bSound),
291 .name = "Sound",
292 .name_plural = N_("sounds"),
293 .translation_context = BLT_I18NCONTEXT_ID_SOUND,
295 .asset_type_info = nullptr,
297 /* A fuzzy case, think NULLified content is OK here... */
298 .init_data = nullptr,
299 .copy_data = sound_copy_data,
300 .free_data = sound_free_data,
301 .make_local = nullptr,
302 .foreach_id = nullptr,
303 .foreach_cache = sound_foreach_cache,
304 .foreach_path = sound_foreach_path,
305 .foreach_working_space_color = nullptr,
306 .foreach_asset_weak_reference = nullptr,
307 .owner_pointer_get = nullptr,
308
309 .blend_write = sound_blend_write,
310 .blend_read_data = sound_blend_read_data,
311 .blend_read_after_liblink = nullptr,
312
313 .blend_read_undo_preserve = nullptr,
314
315 .lib_override_apply_post = nullptr,
317
318#ifdef WITH_AUDASPACE
319/* evil globals ;-) */
320static char **audio_device_names = nullptr;
321#endif
326 /* This is a bit tricky and not quite reliable, but good enough check.
327 *
328 * We don't want audio system handles to be allocated on an original data-blocks, and only want
329 * them to be allocated on a data-blocks which are result of dependency graph evaluation.
330 *
331 * Data-blocks which are covered by a copy-on-evaluation system of dependency graph will have
332 * ID_TAG_COPIED_ON_EVAL tag set on them.
333 *
334 * Additionally, we also allow data-blocks outside of main database. Those can not be "original"
335 * and could be used as a temporary evaluated result during operations like baking.
336 *
337 * NOTE: We consider ID evaluated if ANY of those flags is set. We do NOT require ALL of them.
341
342bSound *BKE_sound_new_file(Main *bmain, const char *filepath, short stream_index)
343{
344 bSound *sound;
345 const char *blendfile_path = BKE_main_blendfile_path(bmain);
346 char filepath_abs[FILE_MAX];
348 STRNCPY(filepath_abs, filepath);
349 BLI_path_abs(filepath_abs, blendfile_path);
350
351 sound = static_cast<bSound *>(BKE_libblock_alloc(bmain, ID_SO, BLI_path_basename(filepath), 0));
352 STRNCPY(sound->filepath, filepath);
353 sound_init_runtime(sound);
354 sound->stream_index = stream_index;
355
356 /* Extract sound specs for bSound */
357 SoundInfo info;
358 bool success = BKE_sound_info_get(bmain, sound, &info);
359 if (success) {
360 sound->samplerate = info.specs.samplerate;
361 sound->audio_channels = info.specs.channels;
362 }
363
364 return sound;
365}
366
367static bSound *sound_new_file_exists_ex(Main *bmain, const char *filepath, short stream_index)
368{
369 bSound *sound;
370 char filepath_abs[FILE_MAX], filepath_test[FILE_MAX];
371
372 STRNCPY(filepath_abs, filepath);
373 BLI_path_abs(filepath_abs, BKE_main_blendfile_path(bmain));
374
375 /* Search for an existing sound matching both filepath and stream index. */
376 for (sound = bmain->sounds.first(); sound; sound = static_cast<bSound *>(sound->id.next)) {
377 if (sound->stream_index != stream_index) {
378 continue;
379 }
380 STRNCPY(filepath_test, sound->filepath);
381 BLI_path_abs(filepath_test, ID_BLEND_PATH(bmain, &sound->id));
382
383 if (BLI_path_cmp(filepath_test, filepath_abs) == 0) {
384 id_us_plus(&sound->id); /* officially should not, it doesn't link here! */
385 return sound;
386 }
387 }
388
389 return BKE_sound_new_file(bmain, filepath, stream_index);
390}
391
392bSound *BKE_sound_new_file_exists(Main *bmain, const char *filepath, short stream_index)
393{
394 return sound_new_file_exists_ex(bmain, filepath, stream_index);
395}
396
397static void sound_free_audio(bSound *sound)
398{
399#ifdef WITH_AUDASPACE
400 bke::SoundRuntime *runtime = sound->runtime;
401 sound_runtime_generation_replace(sound, nullptr);
402 runtime->handle.reset();
403 runtime->playback_handle.reset();
404 runtime->cache.reset();
405#else
406 UNUSED_VARS(sound);
407#endif /* WITH_AUDASPACE */
408}
410#ifdef WITH_AUDASPACE
411static CLG_LogRef LOG = {"sound"};
412
413namespace {
414
415struct GlobalState {
416 const char *force_device = nullptr;
418 /* Parameters of the opened device */
419 const char *device_name = nullptr;
420 aud::DeviceSpecs initialized_specs;
422 /* Device handle and its synchronization mutex. */
423 AUD_Device sound_device;
424 int buffer_size = 0;
425 std::mutex sound_device_mutex;
426
427 bool need_exit = false;
428 bool use_delayed_close = true;
429 std::thread delayed_close_thread;
430 std::condition_variable delayed_close_cv;
432 int num_device_users = 0;
433 std::chrono::time_point<std::chrono::steady_clock> last_user_disconnect_time_point;
434
435 ~GlobalState()
436 {
437 /* Ensure that we don't end up in a deadlock if the global state is being cleaned up
438 * before BKE_sound_exit_once has been called. (For example if someone called exit()
439 * to quickly close the program without cleaning up)
440 *
441 * If we don't do this, we could end up in a state where this destructor is waiting for
442 * other threads to let go of delayed_close_cv forever. See #146640.
443 */
444 exit_threads();
445 }
446
447 void exit_threads()
448 {
449 {
450 std::unique_lock lock(sound_device_mutex);
451 need_exit = true;
454 if (delayed_close_thread.joinable()) {
455 delayed_close_cv.notify_all();
456 delayed_close_thread.join();
457 }
458 }
460
461GlobalState g_state;
462} // namespace
464static void sound_device_close_no_lock()
465{
466 if (g_state.sound_device) {
467 CLOG_DEBUG(&LOG, "Closing audio device");
468 bke::sound_device_exit();
469 g_state.sound_device = nullptr;
470 }
472
473static void sound_device_open_no_lock(const aud::DeviceSpecs &requested_specs)
474{
475 BLI_assert(!g_state.sound_device);
476
477 CLOG_DEBUG(&LOG, "Opening audio device name:%s", g_state.device_name);
478
479 g_state.sound_device = bke::sound_device_init(
480 g_state.device_name, requested_specs, g_state.buffer_size, "Blender");
481 if (!g_state.sound_device) {
482 g_state.sound_device = bke::sound_device_init(
483 "None", requested_specs, g_state.buffer_size, "Blender");
484 }
485
486 g_state.initialized_specs = g_state.sound_device->getSpecs();
488
489static void sound_device_use_begin()
490{
491 ++g_state.num_device_users;
492
493 if (g_state.sound_device) {
494 return;
497 sound_device_open_no_lock(g_state.initialized_specs);
499
500static void sound_device_use_end_after(const std::chrono::milliseconds after_ms)
501{
502 BLI_assert(g_state.num_device_users > 0);
503 if (g_state.num_device_users == 0) {
504 return;
507 --g_state.num_device_users;
508 if (g_state.num_device_users == 0) {
509 g_state.last_user_disconnect_time_point = std::chrono::steady_clock::now() + after_ms;
510 g_state.delayed_close_cv.notify_one();
511 }
513
514static void sound_device_use_end()
516 sound_device_use_end_after(std::chrono::milliseconds(0));
517}
519/* Return true if we need a thread which checks for device usage and closes it when it is inactive.
520 * Only runtime-invariant checks are done here, such as possible platform-specific requirements.
521 */
522static bool sound_use_close_thread()
523{
524 /* No point starting a thread if sound is disabled and we're running headless. */
525 if (g_state.force_device && STREQ(g_state.force_device, "None")) {
526# if defined(WITH_PYTHON_MODULE) || defined(WITH_HEADLESS)
527 return false;
528# endif
529 if (G.background) {
530 return false;
531 }
532 }
533
534# if OS_MAC
535 /* Closing audio device on macOS prior to 15.2 could lead to interference with other software.
536 * See #121911 for details. */
537 if (__builtin_available(macOS 15.2, *)) {
538 return true;
539 }
540 return false;
541# else
542 return true;
543# endif
544}
546static void delayed_close_thread_run()
548 constexpr std::chrono::milliseconds device_close_delay{30000};
549
550 std::unique_lock lock(g_state.sound_device_mutex);
551
552 while (!g_state.need_exit) {
553 if (!g_state.use_delayed_close) {
554 CLOG_DEBUG(&LOG, "Delayed device close is disabled");
555 /* Don't do anything here as delayed close is disabled.
556 * Wait so that we don't spin around in the while loop. */
557 g_state.delayed_close_cv.wait(lock);
558 continue;
559 }
561 if (g_state.num_device_users == 0) {
562 if (g_state.sound_device == nullptr) {
563 /* There are no device users, wait until there is device to be waited for to close. */
564 g_state.delayed_close_cv.wait(lock);
566 else {
567 g_state.delayed_close_cv.wait_until(
568 lock, g_state.last_user_disconnect_time_point + device_close_delay);
569 }
571 else {
572 /* If there are active device users wait indefinitely, until the system is requested to be
573 * closed or the user stops using device.
574 * It is not really guaranteed that the CV is notified for every user that stops using
575 * device, only the last one is guaranteed to notify the CV. */
576 g_state.delayed_close_cv.wait(lock);
577 }
579 if (g_state.need_exit) {
580 CLOG_DEBUG(&LOG, "System exit requested");
581 break;
583
584 if (!g_state.use_delayed_close) {
585 /* Take into account corner case where you switch from a delayed close device while Blender
586 * is running and a delayed close has already been queued up. */
587 continue;
588 }
590 if (!g_state.sound_device) {
591 CLOG_DEBUG(&LOG, "Device is not open, nothing to do");
592 continue;
593 }
595 CLOG_DEBUG(&LOG, "Checking last device usage and timestamp");
597 if (g_state.num_device_users) {
598 CLOG_DEBUG(&LOG, "Device is used by %d user(s)", g_state.num_device_users);
599 continue;
600 }
601
602 const std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
603 if ((now - g_state.last_user_disconnect_time_point) >= device_close_delay) {
604 sound_device_close_no_lock();
605 }
606 }
607
608 CLOG_DEBUG(&LOG, "Delayed device close thread finished");
609}
611static SoundJackSyncCallback sound_jack_sync_callback = nullptr;
612
613static void sound_sync_callback(void *data, int mode, float time)
614{
615 if (sound_jack_sync_callback == nullptr) {
616 return;
618 Main *bmain = (Main *)data;
619 sound_jack_sync_callback(bmain, mode, time);
620}
621
622void BKE_sound_force_device(const char *device)
623{
624 g_state.force_device = device;
625}
628{
629 bke::sound_system_initialize();
630 if (sound_use_close_thread()) {
631 CLOG_DEBUG(&LOG, "Using delayed device close thread");
632 g_state.delayed_close_thread = std::thread(delayed_close_thread_run);
633 }
634}
635
637{
638 g_state.exit_threads();
639
640 std::lock_guard lock(g_state.sound_device_mutex);
641 sound_device_close_no_lock();
642
643 if (audio_device_names != nullptr) {
644 int i;
645 for (i = 0; audio_device_names[i]; i++) {
646 free(audio_device_names[i]);
647 }
648 free(audio_device_names);
649 audio_device_names = nullptr;
650 }
651}
653void BKE_sound_init(Main *bmain)
654{
655 std::lock_guard lock(g_state.sound_device_mutex);
656 /* Make sure no instance of the sound system is running, otherwise we get leaks. */
657 sound_device_close_no_lock();
658
659 aud::DeviceSpecs requested_specs;
660 requested_specs.channels = aud::Channels(U.audiochannels);
661 requested_specs.format = aud::SampleFormat(U.audioformat);
662 requested_specs.rate = U.audiorate;
663
664 if (g_state.force_device == nullptr) {
665 char **names = BKE_sound_get_device_names();
666 g_state.device_name = names[0];
667
668 /* make sure device is within the bounds of the array */
669 for (int i = 0; names[i]; i++) {
670 if (i == U.audiodevice) {
671 g_state.device_name = names[i];
672 }
674 }
675 else {
676 g_state.device_name = g_state.force_device;
677 }
678
679 g_state.buffer_size = U.mixbufsize < 128 ? 1024 : U.mixbufsize;
680
681 if (requested_specs.rate < double(aud::RATE_8000)) {
682 requested_specs.rate = aud::RATE_48000;
683 }
684
685 if (requested_specs.format <= aud::FORMAT_INVALID) {
686 requested_specs.format = aud::FORMAT_S16;
687 }
688
689 if (requested_specs.channels <= aud::CHANNELS_INVALID) {
690 requested_specs.channels = aud::CHANNELS_STEREO;
691 }
692
693 /* Make sure that we have our initalized_specs */
694 sound_device_open_no_lock(requested_specs);
695 if (STR_ELEM(g_state.device_name, "JACK", "PulseAudio", "PipeWire")) {
696 /* JACK:
697 * Do not close the device when using JACK. If we close it, we will not be able to
698 * respond to JACK audio bus commands.
699 *
700 * PulseAudio, PipeWire:
701 * These APIs are built around the idea that the program using them keeps the device open.
702 * Instead it uses audio streams to determine if something is playing back audio or not.
703 * These streams are only active when Audaspace is playing back, so we don't need to
704 * do anything manually.
705 * If we close these devices, it will become very hard and tedious for end users to
706 * control the volume or route audio from Blender.
707 */
708 g_state.use_delayed_close = false;
709 aud::DeviceManager::getDevice()->setSyncCallback(sound_sync_callback, bmain);
710 }
711 else {
712 g_state.use_delayed_close = true;
713 sound_device_close_no_lock();
714 }
715}
716
718{
719 std::lock_guard lock(g_state.sound_device_mutex);
720 if (g_state.sound_device) {
721 aud::DeviceManager::getDevice()->setSyncCallback(sound_sync_callback, bmain);
722 }
723}
724
725static void sound_load_audio(Main *bmain, bSound *sound, bool free_waveform)
726{
727 bke::SoundRuntime *runtime = sound->runtime;
728 sound_runtime_generation_replace(sound, nullptr);
729 runtime->cache.reset();
730 runtime->handle.reset();
731 runtime->playback_handle.reset();
732 if (free_waveform) {
733 sound_free_waveform(sound);
734 }
735
736 {
737 char fullpath[FILE_MAX];
738
739 /* load sound */
740 PackedFile *pf = sound->packedfile;
741
742 /* Don't modify `sound->filepath`, only change a copy. */
743 STRNCPY(fullpath, sound->filepath);
744 BLI_path_abs(fullpath, ID_BLEND_PATH(bmain, &sound->id));
745
746 /* but we need a packed file then */
747 if (pf) {
748 runtime->handle = AUD_Sound(new aud::File((uchar *)pf->data, pf->size, sound->stream_index));
749 }
750 else {
751 /* or else load it from disk */
752 runtime->handle = AUD_Sound(new aud::File(fullpath, sound->stream_index));
753 }
754 }
755 if (sound->flags & SOUND_FLAGS_MONO) {
756 aud::DeviceSpecs specs;
757 specs.channels = aud::CHANNELS_MONO;
758 specs.rate = aud::RATE_INVALID;
759 specs.format = aud::FORMAT_INVALID;
760 runtime->handle = AUD_Sound(new aud::ChannelMapper(runtime->handle, specs));
761 }
762
763 if (sound->flags & SOUND_FLAGS_CACHING) {
764 try {
765 runtime->cache = AUD_Sound(new aud::StreamBuffer(runtime->handle));
766 }
767 catch (aud::Exception &ex) {
768 (void)ex;
769 }
770 }
771
772 if (runtime->cache) {
773 runtime->playback_handle = runtime->cache;
774 }
775 else {
776 runtime->playback_handle = runtime->handle;
777 }
778
779 auto generation = std::make_shared<bke::SoundRuntimeGeneration>();
780 generation->handle = runtime->handle;
781 generation->reader_cache = std::make_shared<bke::SoundReaderCache>(generation->handle);
782 sound_runtime_generation_replace(sound, std::move(generation));
783}
784
785void BKE_sound_load(Main *bmain, bSound *sound)
786{
787 sound_verify_evaluated_id(&sound->id);
788 sound_load_audio(bmain, sound, true);
789}
790
791void BKE_sound_packfile_ensure(Main *bmain, bSound *sound, ReportList *reports)
792{
793 if (sound->packedfile != nullptr) {
794 /* Sound is already packed and considered unmodified, do not attempt to repack it, since its
795 * original file may not be available anymore on the current FS.
796 *
797 * See #152638.
798 */
799 return;
800 }
801
802 sound->packedfile = BKE_packedfile_new(
803 reports, sound->filepath, ID_BLEND_PATH(bmain, &sound->id));
804}
805
806AUD_Device BKE_sound_mixdown(const Scene *scene,
807 const aud::DeviceSpecs &specs,
808 int start,
809 float volume)
810{
811 sound_verify_evaluated_id(&scene->id);
812 try {
813 std::shared_ptr<aud::ReadDevice> device(new aud::ReadDevice(specs));
814 device->setQuality(aud::ResampleQuality::MEDIUM);
815 device->setVolume(volume);
816
817 aud::Sequence *f = dynamic_cast<aud::Sequence *>(scene->runtime->audio.sound_scene.get());
818 f->setSpecs(specs.specs);
819
820 AUD_Handle handle = device->play(f->createQualityReader(aud::ResampleQuality::MEDIUM));
821 if (handle.get()) {
822 handle->seek(start / scene->frames_per_second());
823 }
824
825 return device;
826 }
827 catch (aud::Exception &) {
828 return nullptr;
829 }
830}
831
832void BKE_sound_create_scene(Scene *scene)
833{
834 sound_verify_evaluated_id(&scene->id);
835
836 /* should be done in version patch, but this gets called before */
837 if (scene->r.frs_sec_base == 0) {
838 scene->r.frs_sec_base = 1;
839 }
840
841 bke::SceneAudioRuntime &audio = scene->runtime->audio;
842
843 aud::Specs specs;
844 specs.channels = aud::CHANNELS_STEREO;
845 specs.rate = aud::RATE_48000;
846 audio.sound_scene = std::make_shared<aud::Sequence>(
847 specs, scene->frames_per_second(), scene->audio.flag & AUDIO_MUTE);
848 audio.sound_scene->setSpeedOfSound(scene->audio.speed_of_sound);
849 audio.sound_scene->setDopplerFactor(scene->audio.doppler_factor);
850 audio.sound_scene->setDistanceModel(aud::DistanceModel(scene->audio.distance_model));
851 audio.playback_handle = nullptr;
852 audio.sound_scrub_handle = nullptr;
853 audio.speaker_handles.clear();
854}
855
857{
858 bke::SceneAudioRuntime &audio = scene->runtime->audio;
859 if (audio.playback_handle) {
860 audio.playback_handle->stop();
861 audio.playback_handle.reset();
862 }
863 if (audio.sound_scrub_handle) {
864 audio.sound_scrub_handle->stop();
865 audio.sound_scrub_handle.reset();
866 }
867 for (AUD_SequenceEntry handle : audio.speaker_handles) {
868 audio.sound_scene->remove(handle);
869 }
870 audio.speaker_handles.clear();
871 audio.sound_scene.reset();
872}
873
874void BKE_sound_lock()
875{
876 g_state.sound_device_mutex.lock();
877 if (g_state.sound_device != nullptr) {
878 g_state.sound_device->lock();
879 }
880}
881
882void BKE_sound_unlock()
883{
884 g_state.sound_device_mutex.unlock();
885 if (g_state.sound_device != nullptr) {
886 g_state.sound_device->unlock();
887 }
888}
889
891{
892 sound_verify_evaluated_id(&scene->id);
893
894 if (scene->runtime->audio.sound_scene) {
895 scene->runtime->audio.sound_scene->setSpecs(g_state.initialized_specs.specs);
896 }
897}
898
899void BKE_sound_mute_scene(Scene *scene, bool muted)
900{
901 sound_verify_evaluated_id(&scene->id);
902 if (scene->runtime->audio.sound_scene) {
903 scene->runtime->audio.sound_scene->mute(muted);
904 }
905}
906
907void BKE_sound_update_fps(Main *bmain, Scene *scene)
908{
909 sound_verify_evaluated_id(&scene->id);
910
911 if (scene->runtime->audio.sound_scene) {
912 scene->runtime->audio.sound_scene->setFPS(scene->frames_per_second());
913 }
914
915 seq::sound_update_length(bmain, scene);
916}
917
919{
920 sound_verify_evaluated_id(&scene->id);
921
922 AUD_Sequence sound = scene->runtime->audio.sound_scene;
923 sound->setSpeedOfSound(scene->audio.speed_of_sound);
924 sound->setDopplerFactor(scene->audio.doppler_factor);
925 sound->setDistanceModel(aud::DistanceModel(scene->audio.distance_model));
926}
927
929{
930 sound_verify_evaluated_id(&scene->id);
931 if (strip->scene && scene != strip->scene) {
932 int startframe = strip->left_handle();
933 int endframe = strip->right_handle(scene);
934 int frameskip = strip->startofs + strip->anim_startofs;
935 const double fps = scene->frames_per_second();
936 return AUD_SequenceEntry(
937 scene->runtime->audio.sound_scene->add(strip->scene->runtime->audio.sound_scene,
938 startframe / fps,
939 endframe / fps,
940 frameskip / fps));
941 }
942 return nullptr;
943}
944
946{
947 sound_verify_evaluated_id(&scene->id);
948 /* Happens when sequence's sound data-block was removed. */
949 if (strip->sound == nullptr) {
950 return nullptr;
951 }
952 sound_verify_evaluated_id(&strip->sound->id);
953
954 int startframe = strip->left_handle();
955 int endframe = strip->right_handle(scene);
956 int frameskip = strip->startofs + strip->anim_startofs;
957
958 const double fps = scene->frames_per_second();
959 const double offset_time = strip->sound->offset_time + strip->sound_offset - frameskip / fps;
960 if (offset_time >= 0.0f) {
961 return AUD_SequenceEntry(
962 scene->runtime->audio.sound_scene->add(strip->sound->runtime->playback_handle,
963 startframe / fps + offset_time,
964 endframe / fps,
965 0.0f));
966 }
967 return AUD_SequenceEntry(scene->runtime->audio.sound_scene->add(
968 strip->sound->runtime->playback_handle, startframe / fps, endframe / fps, -offset_time));
969}
970
972{
973 scene->runtime->audio.sound_scene->remove(handle);
974}
975
976void BKE_sound_mute_scene_sound(AUD_SequenceEntry handle, bool mute)
977{
978 handle->mute(mute);
979}
980
981void BKE_sound_move_scene_sound(const Scene *scene,
982 AUD_SequenceEntry handle,
983 int startframe,
984 int endframe,
985 int frameskip,
986 double audio_offset)
987{
988 sound_verify_evaluated_id(&scene->id);
989 const double fps = scene->frames_per_second();
990 const double offset_time = audio_offset - frameskip / fps;
991 if (offset_time >= 0.0f) {
992 handle->move(startframe / fps + offset_time, endframe / fps, 0.0f);
993 }
994 else {
995 handle->move(startframe / fps, endframe / fps, -offset_time);
996 }
997}
998
1000{
1001 sound_verify_evaluated_id(&scene->id);
1002 if (strip->runtime->scene_sound) {
1003 double offset_time = 0.0f;
1004 if (strip->sound != nullptr) {
1005 offset_time = strip->sound->offset_time + strip->sound_offset;
1006 }
1008 strip->runtime->scene_sound,
1009 strip->left_handle(),
1010 strip->right_handle(scene),
1011 strip->startofs + strip->anim_startofs,
1012 offset_time);
1013 }
1014}
1015
1017{
1018 handle->setSound(sound->runtime->playback_handle);
1019}
1020
1021#endif /* WITH_AUDASPACE */
1022
1024{
1025#ifdef WITH_AUDASPACE
1026 handle->setSound(sound_handle);
1027#else
1028 UNUSED_VARS(handle, sound_handle);
1029#endif
1030}
1031
1032#ifdef WITH_AUDASPACE
1033
1034template<typename T>
1035static void set_audaspace_anim_property(std::shared_ptr<T> sound,
1036 aud::AnimateablePropertyType type,
1037 const int frame,
1038 float *value,
1039 bool animated)
1040{
1041 aud::AnimateableProperty *prop = sound->getAnimProperty(type);
1042 if (animated) {
1043 if (frame >= 0) {
1044 prop->write(value, frame, 1);
1045 }
1046 }
1047 else {
1048 prop->write(value);
1049 }
1050}
1051
1052void BKE_sound_set_scene_volume(Scene *scene, float volume)
1053{
1054 sound_verify_evaluated_id(&scene->id);
1055 if (scene->runtime->audio.sound_scene == nullptr) {
1056 return;
1057 }
1058 const bool animated = (scene->audio.flag & AUDIO_VOLUME_ANIMATED) != 0;
1059 const int frame = scene->r.cfra;
1060 set_audaspace_anim_property(
1061 scene->runtime->audio.sound_scene, aud::AP_VOLUME, frame, &volume, animated);
1062}
1063
1065 const int frame,
1066 float volume,
1067 bool animated)
1068{
1069 set_audaspace_anim_property(handle, aud::AP_VOLUME, frame, &volume, animated);
1070}
1071
1073 const int frame,
1074 float pitch,
1075 bool animated)
1076{
1077 set_audaspace_anim_property(handle, aud::AP_PITCH, frame, &pitch, animated);
1078}
1079
1081 int frame_start,
1082 int frame_end,
1083 float pitch)
1084{
1085 frame_start = max_ii(0, frame_start);
1086 frame_end = max_ii(0, frame_end);
1087 aud::AnimateableProperty *prop = handle->getAnimProperty(aud::AP_PITCH);
1088 prop->writeConstantRange(&pitch, frame_start, frame_end);
1089}
1090
1092 const int frame,
1093 float pan,
1094 bool animated)
1095{
1096 set_audaspace_anim_property(handle, aud::AP_PANNING, frame, &pan, animated);
1097}
1098
1100{
1101 BLI_assert_msg(0, "is not supposed to be used, is weird function.");
1102
1103 Scene *scene;
1104
1105 for (scene = main->scenes.first(); scene; scene = static_cast<Scene *>(scene->id.next)) {
1106 seq::sound_update(scene, sound);
1107 }
1108}
1109
1110/* This function assumes that you have already held the g_state.sound_device mutex. */
1111static void sound_start_play_scene(Scene *scene)
1112{
1113 sound_verify_evaluated_id(&scene->id);
1114
1115 bke::SceneAudioRuntime &audio = scene->runtime->audio;
1116 if (audio.playback_handle) {
1117 audio.playback_handle->stop();
1118 audio.playback_handle.reset();
1119 }
1120
1122
1123 audio.playback_handle = bke::sound_device_play(g_state.sound_device, audio.sound_scene);
1124 if (audio.playback_handle) {
1125 audio.playback_handle->setLoopCount(-1);
1126 }
1127}
1128
1129void BKE_sound_play_scene(Scene *scene)
1130{
1131 std::lock_guard lock(g_state.sound_device_mutex);
1132 sound_device_use_begin();
1133 sound_verify_evaluated_id(&scene->id);
1134
1135 const double cur_time = FRA2TIME(scene->r.cfra + scene->r.subframe);
1136
1137 g_state.sound_device->lock();
1138
1139 bke::SceneAudioRuntime &audio = scene->runtime->audio;
1140 if (audio.sound_scrub_handle && audio.sound_scrub_handle->getStatus() != aud::STATUS_INVALID) {
1141 /* If the audio scrub handle is playing back, stop to make sure it is not active.
1142 * Otherwise, it will trigger a callback that will stop audio playback. */
1143 audio.sound_scrub_handle->stop();
1144 audio.sound_scrub_handle = nullptr;
1145 /* The scrub_handle started playback with playback_handle, stop it so we can
1146 * properly restart it. */
1147 audio.playback_handle->pause();
1148 }
1149
1150 aud::Status status = audio.playback_handle ? audio.playback_handle->getStatus() :
1151 aud::STATUS_INVALID;
1152
1153 if (status == aud::STATUS_INVALID) {
1154 sound_start_play_scene(scene);
1155
1156 if (!audio.playback_handle) {
1157 g_state.sound_device->unlock();
1158 return;
1159 }
1160 }
1161
1162 if (status != aud::STATUS_PLAYING) {
1163 /* Seeking the synchronizer will also seek the playback handle.
1164 * Even if we don't have A/V sync on, keep the synchronizer and handle seek time in sync. */
1165 aud::DeviceManager::getDevice()->seekSynchronizer(cur_time);
1166 audio.playback_handle->seek(cur_time);
1167 audio.playback_handle->resume();
1168 }
1169
1170 if (scene->audio.flag & AUDIO_SYNC) {
1171 aud::DeviceManager::getDevice()->playSynchronizer();
1172 }
1173
1174 g_state.sound_device->unlock();
1175}
1176
1177void BKE_sound_stop_scene(Scene *scene)
1178{
1179 std::lock_guard lock(g_state.sound_device_mutex);
1180 BLI_assert(g_state.sound_device);
1181 if (scene->runtime->audio.playback_handle) {
1182 scene->runtime->audio.playback_handle->pause();
1183
1184 if (scene->audio.flag & AUDIO_SYNC) {
1185 aud::DeviceManager::getDevice()->stopSynchronizer();
1186 }
1187 }
1188 sound_device_use_end();
1189}
1190
1191void BKE_sound_seek_scene(Main *bmain, Scene *scene)
1192{
1193 std::lock_guard lock(g_state.sound_device_mutex);
1194 bool animation_playing = false;
1195 for (bScreen *screen = bmain->screens.first(); screen;
1196 screen = static_cast<bScreen *>(screen->id.next))
1197 {
1198 if (screen->animtimer) {
1199 animation_playing = true;
1200 break;
1201 }
1202 }
1203
1204 bool do_audio_scrub = scene->audio.flag & AUDIO_SCRUB && !animation_playing;
1205
1206 if (do_audio_scrub) {
1207 /* Make sure the sound device is open for scrubbing. */
1208 sound_device_use_begin();
1209 }
1210 else if (g_state.sound_device == nullptr) {
1211 /* Nothing to do if there is no sound device and we are not doing audio scrubbing. */
1212 return;
1213 }
1214 sound_verify_evaluated_id(&scene->id);
1215
1216 g_state.sound_device->lock();
1217
1218 bke::SceneAudioRuntime &audio = scene->runtime->audio;
1219 aud::Status status = audio.playback_handle ? audio.playback_handle->getStatus() :
1220 aud::STATUS_INVALID;
1221 if (status == aud::STATUS_INVALID) {
1222 sound_start_play_scene(scene);
1223
1224 if (!audio.playback_handle) {
1225 g_state.sound_device->unlock();
1226 if (do_audio_scrub) {
1227 sound_device_use_end();
1228 }
1229 return;
1230 }
1231
1232 audio.playback_handle->pause();
1233 }
1234
1235 const double one_frame = 1.0 / scene->frames_per_second() +
1236 (U.audiorate > 0 ? U.mixbufsize / double(U.audiorate) : 0.0);
1237 const double cur_time = FRA2TIME(scene->r.cfra);
1238
1239 if (do_audio_scrub) {
1240 /* Playback one frame of audio without advancing the timeline. */
1241 audio.playback_handle->seek(cur_time);
1242 audio.playback_handle->resume();
1243 if (audio.sound_scrub_handle && audio.sound_scrub_handle->getStatus() != aud::STATUS_INVALID) {
1244 audio.sound_scrub_handle->seek(0);
1245 }
1246 else {
1247 if (audio.sound_scrub_handle) {
1248 audio.sound_scrub_handle->stop();
1249 }
1250 audio.sound_scrub_handle = bke::sound_pause_after(audio.playback_handle, one_frame);
1251 }
1252 sound_device_use_end_after(std::chrono::milliseconds(int(one_frame * 1000)));
1253 }
1254 else if (status == aud::STATUS_PLAYING) {
1255 /* Seeking the synchronizer will also seek the playback handle.
1256 * Even if we don't have A/V sync on, keep the synchronizer and handle
1257 * seek time in sync.
1258 */
1259 aud::DeviceManager::getDevice()->seekSynchronizer(cur_time);
1260 audio.playback_handle->seek(cur_time);
1261 }
1262
1263 g_state.sound_device->unlock();
1264}
1265
1266double BKE_sound_sync_scene(Scene *scene)
1267{
1268 sound_verify_evaluated_id(&scene->id);
1269
1270 /* Ugly: Blender doesn't like it when the animation is played back during rendering */
1271 if (G.is_rendering) {
1272 return NAN_FLT;
1273 }
1274
1275 if (scene->runtime->audio.playback_handle) {
1276 if (scene->audio.flag & AUDIO_SYNC) {
1277 return aud::DeviceManager::getDevice()->getSynchronizerPosition();
1278 }
1279
1280 return scene->runtime->audio.playback_handle->getPosition();
1281 }
1282 return NAN_FLT;
1283}
1284
1285static int sound_read(
1286 AUD_Sound sound, float *buffer, int length, int samples_per_second, const bool *interrupt)
1287{
1288 using namespace aud;
1289 DeviceSpecs specs;
1290 float *buf;
1291 Buffer aBuffer;
1292
1293 specs.rate = RATE_INVALID;
1294 specs.channels = CHANNELS_MONO;
1295 specs.format = FORMAT_INVALID;
1296
1297 std::shared_ptr<IReader> reader = ChannelMapper(sound, specs).createReader();
1298
1299 specs.specs = reader->getSpecs();
1300 int len;
1301 float samplejump = specs.rate / samples_per_second;
1302 float min, max, power, overallmax;
1303 bool eos;
1304
1305 overallmax = 0;
1306
1307 for (int i = 0; i < length; i++) {
1308 len = floor(samplejump * (i + 1)) - floor(samplejump * i);
1309
1310 if (*interrupt) {
1311 return 0;
1312 }
1313
1314 aBuffer.assureSize(len * AUD_SAMPLE_SIZE(specs));
1315 buf = aBuffer.getBuffer();
1316
1317 reader->read(len, eos, buf);
1318
1319 max = min = *buf;
1320 power = *buf * *buf;
1321 for (int j = 1; j < len; j++) {
1322 if (buf[j] < min) {
1323 min = buf[j];
1324 }
1325 if (buf[j] > max) {
1326 max = buf[j];
1327 }
1328 power += buf[j] * buf[j];
1329 }
1330
1331 buffer[i * 3] = min;
1332 buffer[i * 3 + 1] = max;
1333 buffer[i * 3 + 2] = std::sqrt(power / len);
1334
1335 if (overallmax < max) {
1336 overallmax = max;
1337 }
1338 if (overallmax < -min) {
1339 overallmax = -min;
1340 }
1341
1342 if (eos) {
1343 length = i;
1344 break;
1345 }
1346 }
1347
1348 if (overallmax > 1.0f) {
1349 for (int i = 0; i < length * 3; i++) {
1350 buffer[i] /= overallmax;
1351 }
1352 }
1353
1354 return length;
1355}
1356
1357void BKE_sound_read_waveform(Main *bmain, bSound *sound, bool *stop)
1358{
1359 if (*stop) {
1361 return;
1362 }
1363
1364 bool need_close_audio_handles = false;
1365 bke::SoundRuntime *runtime = sound->runtime;
1366 if (runtime->playback_handle == nullptr) {
1367 /* TODO(sergey): Make it fully independent audio handle. */
1368 sound_load_audio(bmain, sound, true);
1369 need_close_audio_handles = true;
1370 }
1371
1372 SoundInfo info = bke::sound_info_get(runtime->playback_handle);
1373
1374 Vector<float> *waveform = MEM_new<Vector<float>>(__func__);
1375 if (info.length > 0) {
1376 int length = info.length * SOUND_WAVE_SAMPLES_PER_SECOND;
1377
1378 waveform->resize(3 * length);
1379 length = sound_read(
1380 runtime->playback_handle, waveform->data(), length, SOUND_WAVE_SAMPLES_PER_SECOND, stop);
1381 waveform->resize(3 * length);
1382 }
1383
1384 if (*stop) {
1385 MEM_delete(waveform);
1386 MEM_SAFE_DELETE(runtime->waveform);
1387 BLI_spin_lock(&runtime->spinlock);
1388 runtime->tags &= ~bke::SoundTags::WaveformLoading;
1389 BLI_spin_unlock(&runtime->spinlock);
1390 if (need_close_audio_handles) {
1391 sound_free_audio(sound);
1392 }
1393 return;
1394 }
1395
1396 sound_free_waveform(sound);
1397
1398 BLI_spin_lock(&runtime->spinlock);
1399 runtime->waveform = waveform;
1400 runtime->tags &= ~bke::SoundTags::WaveformLoading;
1401 BLI_spin_unlock(&runtime->spinlock);
1402
1403 if (need_close_audio_handles) {
1404 sound_free_audio(sound);
1405 }
1406}
1407
1408static void sound_update_base(Scene *scene, Object *object, Set<AUD_SequenceEntry> &new_set)
1409{
1410 Speaker *speaker;
1411 float quat[4];
1412
1413 sound_verify_evaluated_id(&scene->id);
1415
1416 if ((object->type != OB_SPEAKER) || !object->adt) {
1417 return;
1418 }
1419
1420 for (NlaTrack &track : object->adt->nla_tracks) {
1421 for (NlaStrip &strip : track.strips) {
1422 if (strip.type != NLASTRIP_TYPE_SOUND) {
1423 continue;
1424 }
1425 speaker = (Speaker *)object->data;
1426
1427 bke::NlaStripRuntime &strip_runtime = strip.runtime_get();
1428
1429 if ((strip_runtime.speaker_handle != nullptr) &&
1430 scene->runtime->audio.speaker_handles.remove(strip_runtime.speaker_handle))
1431 {
1432 if (speaker->sound) {
1433 strip_runtime.speaker_handle->move(
1434 double(strip.start) / scene->frames_per_second(), FLT_MAX, 0);
1435 }
1436 else {
1437 scene->runtime->audio.sound_scene->remove(strip_runtime.speaker_handle);
1438 strip_runtime.speaker_handle = nullptr;
1439 }
1440 }
1441 else {
1442 if (speaker->sound) {
1443 strip_runtime.speaker_handle = AUD_SequenceEntry(scene->runtime->audio.sound_scene->add(
1444 speaker->sound->runtime->playback_handle,
1445 double(strip.start) / scene->frames_per_second(),
1446 FLT_MAX,
1447 0));
1448 strip_runtime.speaker_handle->setRelative(false);
1449 }
1450 }
1451
1452 if (strip_runtime.speaker_handle) {
1453 const bool mute = ((strip.flag & NLASTRIP_FLAG_MUTED) || (speaker->flag & SPK_MUTED));
1454 new_set.add(strip_runtime.speaker_handle);
1455 strip_runtime.speaker_handle->setVolumeMaximum(speaker->volume_max);
1456 strip_runtime.speaker_handle->setVolumeMinimum(speaker->volume_min);
1457 strip_runtime.speaker_handle->setDistanceMaximum(speaker->distance_max);
1458 strip_runtime.speaker_handle->setDistanceReference(speaker->distance_reference);
1459 strip_runtime.speaker_handle->setAttenuation(speaker->attenuation);
1460 strip_runtime.speaker_handle->setConeAngleOuter(speaker->cone_angle_outer);
1461 strip_runtime.speaker_handle->setConeAngleInner(speaker->cone_angle_inner);
1462 strip_runtime.speaker_handle->setConeVolumeOuter(speaker->cone_volume_outer);
1463
1464 mat4_to_quat(quat, object->object_to_world().ptr());
1465 float3 location = object->object_to_world().location();
1466 set_audaspace_anim_property(
1467 strip_runtime.speaker_handle, aud::AP_LOCATION, scene->r.cfra, location, true);
1468 set_audaspace_anim_property(
1469 strip_runtime.speaker_handle, aud::AP_ORIENTATION, scene->r.cfra, quat, true);
1470 set_audaspace_anim_property(
1471 strip_runtime.speaker_handle, aud::AP_VOLUME, scene->r.cfra, &speaker->volume, true);
1472 set_audaspace_anim_property(
1473 strip_runtime.speaker_handle, aud::AP_PITCH, scene->r.cfra, &speaker->pitch, true);
1474 strip_runtime.speaker_handle->setSound(speaker->sound->runtime->playback_handle);
1475 strip_runtime.speaker_handle->mute(mute);
1476 }
1477 }
1478 }
1479}
1480
1481void BKE_sound_update_scene(Depsgraph *depsgraph, Scene *scene)
1482{
1483 sound_verify_evaluated_id(&scene->id);
1484
1485 Set<AUD_SequenceEntry> new_set;
1486 float quat[4];
1487
1488 /* cheap test to skip looping over all objects (no speakers is a common case) */
1489 if (DEG_id_type_any_exists(depsgraph, ID_SPK)) {
1490 DEGObjectIterSettings deg_iter_settings = {nullptr};
1491 deg_iter_settings.depsgraph = depsgraph;
1492 deg_iter_settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY |
1495 DEG_OBJECT_ITER_BEGIN (&deg_iter_settings, object) {
1496 sound_update_base(scene, object, new_set);
1497 }
1499 }
1500
1501 bke::SceneAudioRuntime &audio = scene->runtime->audio;
1502 for (AUD_SequenceEntry handle : audio.speaker_handles) {
1503 audio.sound_scene->remove(handle);
1504 }
1505 audio.speaker_handles.clear();
1506
1507 if (scene->camera) {
1508 mat4_to_quat(quat, scene->camera->object_to_world().ptr());
1509 float3 location = scene->camera->object_to_world().location();
1510 set_audaspace_anim_property(
1511 audio.sound_scene, aud::AP_LOCATION, scene->r.cfra, location, true);
1512 set_audaspace_anim_property(audio.sound_scene, aud::AP_ORIENTATION, scene->r.cfra, quat, true);
1513 }
1514
1515 audio.speaker_handles = new_set;
1516}
1517
1519{
1520 return ((bSound *)sound)->runtime->playback_handle;
1521}
1522
1523float BKE_sound_get_length(Main *bmain, bSound *sound)
1524{
1525 if (sound->runtime->playback_handle != nullptr) {
1526 SoundInfo info = bke::sound_info_get(sound->runtime->playback_handle);
1527 return info.length;
1528 }
1529 SoundInfo info;
1530 if (!BKE_sound_info_get(bmain, sound, &info)) {
1531 return 0.0f;
1532 }
1533 return info.length;
1534}
1535
1537{
1538 if (audio_device_names == nullptr) {
1539
1540 std::vector<std::string> v_names = aud::DeviceManager::getAvailableDeviceNames();
1541 char **names = (char **)malloc(sizeof(char *) * (v_names.size() + 1));
1542
1543 for (int i = 0; i < v_names.size(); i++) {
1544 std::string name = v_names[i];
1545 const size_t name_size = sizeof(char) * (name.length() + 1);
1546 names[i] = (char *)malloc(name_size);
1547 memcpy(names[i], name.c_str(), name_size);
1548 }
1549 names[v_names.size()] = nullptr;
1550 audio_device_names = names;
1551 }
1552
1553 return audio_device_names;
1554}
1555
1556bool BKE_sound_info_get(Main *main, bSound *sound, SoundInfo *sound_info)
1557{
1558 if (sound->runtime->playback_handle != nullptr) {
1559 *sound_info = bke::sound_info_get(sound->runtime->playback_handle);
1560 return true;
1561 }
1562 /* TODO(sergey): Make it fully independent audio handle. */
1563 /* Don't free waveforms during non-destructive queries.
1564 * This causes unnecessary recalculation - see #69921 */
1565 sound_load_audio(main, sound, false);
1566 const bool result = sound->runtime->playback_handle != nullptr;
1567 if (result) {
1568 *sound_info = bke::sound_info_get(sound->runtime->playback_handle);
1569 }
1570 sound_free_audio(sound);
1571 return result;
1572}
1573
1575 const char *filepath,
1576 int stream,
1577 SoundStreamInfo *sound_info)
1578{
1579 char filepath_abs[FILE_MAX];
1580 STRNCPY(filepath_abs, filepath);
1581 const char *blendfile_path = BKE_main_blendfile_path(main);
1582 BLI_path_abs(filepath_abs, blendfile_path);
1583
1584 std::vector<aud::StreamInfo> streams;
1585 try {
1586 streams = aud::FileManager::queryStreams(filepath_abs);
1587 }
1588 catch (aud::Exception &) {
1589 return false;
1590 }
1591
1592 if (stream < 0 || stream >= streams.size()) {
1593 return false;
1594 }
1595
1596 sound_info->start = streams[stream].start;
1597 sound_info->duration = streams[stream].duration;
1598 return true;
1599}
1600
1601int BKE_sound_stream_count(Main *main, const char *filepath)
1602{
1603 char filepath_abs[FILE_MAX];
1604 STRNCPY(filepath_abs, filepath);
1606
1607 std::vector<aud::StreamInfo> streams;
1608 try {
1609 streams = aud::FileManager::queryStreams(filepath_abs);
1610 return streams.size();
1611 }
1612 catch (aud::Exception &) {
1613 return 0;
1614 }
1615}
1616
1617# ifdef WITH_RUBBERBAND
1619{
1620 seq::StripRuntime &runtime = *strip->runtime;
1621
1622 /* If we already have a time stretch effect with the same frame-rate, use that. */
1623 if (runtime.sound_time_stretch != nullptr && runtime.sound_time_stretch_fps == fps) {
1624 return runtime.sound_time_stretch;
1625 }
1626
1627 /* Otherwise create the time stretch effect. */
1628 runtime.clear_sound_time_stretch();
1629 runtime.sound_time_stretch = AUD_Sound(
1630 new aud::AnimateableTimeStretchPitchScale(BKE_sound_playback_handle_get(strip->sound),
1631 fps,
1632 1.0f,
1633 1.0f,
1634 aud::StretcherQuality::HIGH,
1635 false));
1636 runtime.sound_time_stretch_fps = fps;
1637 return runtime.sound_time_stretch;
1638}
1639
1641 int frame,
1642 float time_stretch,
1643 bool animated)
1644{
1645 std::shared_ptr<aud::AnimateableProperty> prop =
1646 std::dynamic_pointer_cast<aud::AnimateableTimeStretchPitchScale>(handle)->getAnimProperty(
1647 aud::AP_TIME_STRETCH);
1648 if (animated) {
1649 if (frame >= 0) {
1650 prop->write(&time_stretch, frame, 1);
1651 }
1652 }
1653 else {
1654 prop->write(&time_stretch);
1655 }
1656}
1657
1659 int frame_start,
1660 int frame_end,
1661 float time_stretch)
1662{
1663 frame_start = max_ii(0, frame_start);
1664 frame_end = max_ii(0, frame_end);
1665 std::shared_ptr<aud::AnimateableProperty> prop =
1666 std::dynamic_pointer_cast<aud::AnimateableTimeStretchPitchScale>(handle)->getAnimProperty(
1667 aud::AP_TIME_STRETCH);
1668 prop->writeConstantRange(&time_stretch, frame_start, frame_end);
1669}
1670# endif /* WITH_RUBBERBAND */
1671
1672SoundInfo bke::sound_info_get(AUD_Sound sound)
1673{
1674 SoundInfo res;
1675 res.length = 0.0f;
1676 res.specs.channels = SOUND_CHANNELS_INVALID;
1677 res.specs.samplerate = 0;
1678
1679 try {
1680 std::shared_ptr<aud::IReader> reader = sound->createReader();
1681 if (reader) {
1682 aud::Specs specs = reader->getSpecs();
1683 res.specs.channels = eSoundChannels(specs.channels);
1684 res.specs.samplerate = specs.rate;
1685 res.length = reader->getLength() / float(specs.rate);
1686 }
1687 }
1688 catch (aud::Exception &ex) {
1689 (void)ex;
1690 }
1691 return res;
1692}
1693
1694void bke::sound_system_initialize()
1695{
1696 aud::PluginManager::loadPlugins();
1697 aud::NULLDevice::registerPlugin();
1698}
1699
1700AUD_Device bke::sound_device_init(const char *device,
1701 const aud::DeviceSpecs &specs,
1702 int buffersize,
1703 const char *name)
1704{
1705 using namespace aud;
1706 try {
1707 std::shared_ptr<IDeviceFactory> factory = device ? DeviceManager::getDeviceFactory(device) :
1708 DeviceManager::getDefaultDeviceFactory();
1709
1710 if (factory) {
1711 factory->setName(name);
1712 factory->setBufferSize(buffersize);
1713 factory->setSpecs(specs);
1714 auto device = factory->openDevice();
1715 DeviceManager::setDevice(device);
1716
1717 return AUD_Device(device);
1718 }
1719 }
1720 catch (Exception &ex) {
1721 (void)ex;
1722 }
1723 return nullptr;
1724}
1725
1726void bke::sound_device_exit()
1727{
1728 aud::DeviceManager::releaseDevice();
1729}
1730
1731AUD_Handle bke::sound_device_play(AUD_Device device, AUD_Sound sound)
1732{
1733 if (!device) {
1734 device = aud::DeviceManager::getDevice();
1735 }
1736 try {
1737 return device->play(sound, true);
1738 }
1739 catch (aud::Exception &ex) {
1740 (void)ex;
1741 }
1742 return nullptr;
1743}
1744
1745bool bke::sound_device_read(AUD_Device device, uchar *buffer, int length)
1746{
1747 BLI_assert(buffer);
1748 auto read_device = std::dynamic_pointer_cast<aud::ReadDevice>(device);
1749 if (!read_device) {
1750 return false;
1751 }
1752 try {
1753 return read_device->read(buffer, length);
1754 }
1755 catch (aud::Exception &) {
1756 return false;
1757 }
1758}
1759
1760AUD_Handle bke::sound_pause_after(AUD_Handle handle, double seconds)
1761{
1762 auto device = aud::DeviceManager::getDevice();
1763
1764 AUD_Sound silence = AUD_Sound(new aud::Silence(device->getSpecs().rate));
1765 AUD_Sound limiter = AUD_Sound(new aud::Limiter(silence, 0, seconds));
1766
1767 std::lock_guard<aud::ILockable> lock(*device);
1768
1769 try {
1770 AUD_Handle handle2 = device->play(limiter);
1771 if (handle2.get()) {
1772 AUD_Handle *data = new AUD_Handle(handle);
1773 handle2->setStopCallback(
1774 [](void *data) {
1775 AUD_Handle *handle = (AUD_Handle *)data;
1776 (*handle)->pause();
1777 delete handle;
1778 },
1779 data);
1780 return handle2;
1781 }
1782 }
1783 catch (aud::Exception &ex) {
1784 (void)ex;
1785 }
1786 return nullptr;
1787}
1788
1789float *bke::sound_read_file_buffer(const char *filename,
1790 float low,
1791 float high,
1792 float attack,
1793 float release,
1794 float threshold,
1795 bool accumulate,
1796 bool additive,
1797 bool square,
1798 float sthreshold,
1799 double samplerate,
1800 int stream,
1801 int *length)
1802{
1803 using namespace aud;
1804
1805 DeviceSpecs specs;
1806 specs.channels = CHANNELS_MONO;
1807 specs.rate = samplerate;
1808
1809 AUD_Sound file = AUD_Sound(new File(filename, stream));
1810 int position = 0;
1811
1812 Buffer buffer;
1813
1814 try {
1815 std::shared_ptr<IReader> reader = file->createReader();
1816 SampleRate rate = reader->getSpecs().rate;
1817
1818 AUD_Sound sound = AUD_Sound(new ChannelMapper(file, specs));
1819
1820 if (high < rate) {
1821 sound = AUD_Sound(new Lowpass(sound, high));
1822 }
1823 if (low > 0) {
1824 sound = AUD_Sound(new Highpass(sound, low));
1825 }
1826
1827 sound = AUD_Sound(new Envelope(sound, attack, release, threshold, 0.1f));
1828 sound = AUD_Sound(new LinearResample(sound, specs));
1829
1830 if (square) {
1831 sound = AUD_Sound(new Threshold(sound, sthreshold));
1832 }
1833
1834 if (accumulate) {
1835 sound = AUD_Sound(new Accumulator(sound, additive));
1836 }
1837 else if (additive) {
1838 sound = AUD_Sound(new Sum(sound));
1839 }
1840
1841 reader = sound->createReader();
1842
1843 if (!reader) {
1844 return nullptr;
1845 }
1846
1847 int len;
1848 bool eos;
1849 do {
1850 len = samplerate;
1851 buffer.resize((position + len) * sizeof(float), true);
1852 reader->read(len, eos, buffer.getBuffer() + position);
1853 position += len;
1854 } while (!eos);
1855 }
1856 catch (Exception &) {
1857 return nullptr;
1858 }
1859
1861 memcpy(result, buffer.getBuffer(), position * sizeof(float));
1862 *length = position;
1863 return result;
1864}
1865
1866bool bke::sound_mixdown(AUD_Sequence sequence,
1867 uint start,
1868 uint length,
1869 uint buffersize,
1870 const char *filename,
1871 const aud::DeviceSpecs &specs,
1872 aud::Container format,
1873 aud::Codec codec,
1874 uint bitrate,
1875 bool split_channels,
1876 std::string &r_error,
1877 bool (*progress_callback)(float, void *),
1878 void *progress_data)
1879{
1880 using namespace aud;
1881 try {
1882 sequence->setSpecs(specs.specs);
1883 std::shared_ptr<IReader> reader = sequence->createQualityReader(ResampleQuality::MEDIUM);
1884 reader->seek(start);
1885
1886 if (!split_channels) {
1887 std::shared_ptr<IWriter> writer = FileWriter::createWriter(
1888 filename, specs, format, codec, bitrate);
1889 FileWriter::writeReader(
1890 reader, writer, length, buffersize, progress_callback, progress_data);
1891 }
1892 else {
1893 std::vector<std::shared_ptr<IWriter>> writers;
1894 int channels = specs.channels;
1895
1896 aud::DeviceSpecs specs_mono = specs;
1897 specs_mono.channels = CHANNELS_MONO;
1898 for (int i = 0; i < channels; i++) {
1899 std::string stream;
1900 std::string fn = filename;
1901 size_t index = fn.find_last_of('.');
1902 size_t index_slash = fn.find_last_of('/');
1903 size_t index_backslash = fn.find_last_of('\\');
1904
1905 if ((index == std::string::npos) ||
1906 ((index < index_slash) && (index_slash != std::string::npos)) ||
1907 ((index < index_backslash) && (index_backslash != std::string::npos)))
1908 {
1909 stream = fmt::format("{}_{}", filename, i + 1);
1910 }
1911 else {
1912 stream = fmt::format("{}_{}{}", fn.substr(0, index), i + 1, fn.substr(index));
1913 }
1914 writers.push_back(FileWriter::createWriter(stream, specs_mono, format, codec, bitrate));
1915 }
1916 FileWriter::writeReader(
1917 reader, writers, length, buffersize, progress_callback, progress_data);
1918 }
1919 return true;
1920 }
1921 catch (Exception &e) {
1922 r_error = e.getMessage();
1923 return false;
1924 }
1925}
1926
1927#else /* WITH_AUDASPACE */
1928
1929# include "BLI_utildefines.hh"
1930
1931void BKE_sound_force_device(const char * /*device*/) {}
1933void BKE_sound_init(Main * /*bmain*/) {}
1935void BKE_sound_load(Main * /*bmain*/, bSound * /*sound*/) {}
1936void BKE_sound_packfile_ensure(Main * /*bmain*/, bSound * /*sound*/, ReportList * /*reports*/) {}
1937void BKE_sound_create_scene(Scene * /*scene*/) {}
1938void BKE_sound_destroy_scene(Scene * /*scene*/) {}
1943void BKE_sound_mute_scene(Scene * /*scene*/, bool /*muted*/) {}
1945{
1946 return nullptr;
1947}
1949{
1950 return nullptr;
1951}
1953void BKE_sound_mute_scene_sound(AUD_SequenceEntry /*handle*/, bool /*mute*/) {}
1954void BKE_sound_move_scene_sound(const Scene * /*scene*/,
1955 AUD_SequenceEntry /*handle*/,
1956 int /*startframe*/,
1957 int /*endframe*/,
1958 int /*frameskip*/,
1959 double /*audio_offset*/)
1960{
1961}
1962void BKE_sound_move_scene_sound_defaults(Scene * /*scene*/, Strip * /*strip*/) {}
1963void BKE_sound_play_scene(Scene * /*scene*/) {}
1964void BKE_sound_stop_scene(Scene * /*scene*/) {}
1965void BKE_sound_seek_scene(Main * /*bmain*/, Scene * /*scene*/) {}
1966double BKE_sound_sync_scene(Scene * /*scene*/)
1967{
1968 return NAN_FLT;
1969}
1971 bSound *sound,
1972 /* NOLINTNEXTLINE: readability-non-const-parameter. */
1973 bool *stop)
1974{
1975 UNUSED_VARS(sound, stop, bmain);
1976}
1977
1978void BKE_sound_update_sequencer(Main * /*main*/, bSound * /*sound*/) {}
1979void BKE_sound_update_scene(Depsgraph * /*depsgraph*/, Scene * /*scene*/) {}
1982void BKE_sound_update_fps(Main * /*bmain*/, Scene * /*scene*/) {}
1984 int /*frame*/,
1985 float /*volume*/,
1986 bool /*animated*/)
1987{
1988}
1990 int /*frame*/,
1991 float /*pan*/,
1992 bool /*animated*/)
1993{
1994}
1995void BKE_sound_set_scene_volume(Scene * /*scene*/, float /*volume*/) {}
1997 int /*frame*/,
1998 float /*pitch*/,
1999 bool /*animated*/)
2000{
2001}
2003 int /*frame_start*/,
2004 int /*frame_end*/,
2005 float /*pitch*/)
2006{
2007}
2008float BKE_sound_get_length(Main * /*bmain*/, bSound * /*sound*/)
2009{
2010 return 0;
2011}
2013{
2014 static char *names[1] = {nullptr};
2015 return names;
2016}
2017
2018bool BKE_sound_info_get(Main * /*main*/, bSound * /*sound*/, SoundInfo * /*sound_info*/)
2019{
2020 return false;
2021}
2022
2024 const char * /*filepath*/,
2025 int /*stream*/,
2026 SoundStreamInfo * /*sound_info*/)
2027{
2028 return false;
2029}
2030
2031int BKE_sound_stream_count(Main * /*main*/, const char * /*filepath*/)
2032{
2033 return 0;
2034}
2035
2036#endif /* WITH_AUDASPACE */
2037
2038#if !defined(WITH_AUDASPACE) || !defined(WITH_RUBBERBAND)
2040{
2041 return nullptr;
2042}
2043
2045 int /*frame*/,
2046 float /*time_stretch*/,
2047 bool /*animated*/)
2048{
2049}
2051 int /*frame_start*/,
2052 int /*frame_end*/,
2053 float /*time_stretch*/)
2054{
2055}
2056#endif
2057
2059{
2060 if (scene->runtime->audio.sound_scene != nullptr) {
2061 return;
2062 }
2064}
2065
2066static void sound_ensure_loaded(Main *bmain, bSound *sound)
2067{
2068 if (sound->runtime->cache != nullptr) {
2069 return;
2070 }
2071 BKE_sound_load(bmain, sound);
2072}
2073
2075{
2076#if defined(WITH_AUDASPACE)
2077 sound_jack_sync_callback = callback;
2078#else
2079 UNUSED_VARS(callback);
2080#endif
2081}
2082
2083void BKE_sound_jack_scene_update(Scene *scene, int mode, double time)
2084{
2086
2087 /* Ugly: Blender doesn't like it when the animation is played back during rendering. */
2088 if (G.is_rendering) {
2089 return;
2090 }
2091#ifdef WITH_AUDASPACE
2092 g_state.sound_device->lock();
2093
2094 if (mode) {
2095 BKE_sound_play_scene(scene);
2096 }
2097 else {
2098 BKE_sound_stop_scene(scene);
2099 }
2100 if (scene->runtime->audio.playback_handle != nullptr) {
2101 scene->runtime->audio.playback_handle->seek(time);
2102 }
2103 g_state.sound_device->unlock();
2104#else
2105 UNUSED_VARS(mode, time);
2106#endif
2107}
2108
2109void BKE_sound_evaluate(Depsgraph *depsgraph, Main *bmain, bSound *sound)
2110{
2111 DEG_debug_print_eval(depsgraph, __func__, sound->id.name, sound);
2112 if (sound->id.recalc & ID_RECALC_SOURCE) {
2113 /* Sequencer checks this flag to see if the strip sound is to be updated from the Audaspace
2114 * side. */
2115 sound->id.recalc |= ID_RECALC_AUDIO;
2116 }
2117
2118 if (sound->id.recalc & ID_RECALC_AUDIO) {
2119 BKE_sound_load(bmain, sound);
2120 return;
2121 }
2122 sound_ensure_loaded(bmain, sound);
2123}
2124
2126 AUD_Sound *r_cache,
2127 AUD_Sound *r_playback_handle,
2128 Vector<float> **r_waveform)
2129{
2130 bke::SoundRuntime *runtime = sound->runtime;
2131 *r_cache = runtime->cache;
2132 *r_playback_handle = runtime->playback_handle;
2133 *r_waveform = runtime->waveform;
2134 runtime->cache = nullptr;
2135 runtime->playback_handle = nullptr;
2136 runtime->waveform = nullptr;
2137}
2138
2140 AUD_Sound cache,
2141 AUD_Sound playback_handle,
2142 Vector<float> *waveform)
2143{
2144 bke::SoundRuntime *runtime = sound->runtime;
2145 runtime->cache = cache;
2146 runtime->playback_handle = playback_handle;
2147 runtime->waveform = waveform;
2148}
2149
2151{
2152 if (sound == nullptr) {
2153 return nullptr;
2154 }
2155 return sound->runtime->playback_handle;
2156}
2157
2159{
2160 bke::SoundRuntime *runtime = sound->runtime;
2161 BLI_spin_lock(&runtime->spinlock);
2162 runtime->tags &= ~bke::SoundTags::WaveformLoading;
2163 BLI_spin_unlock(&runtime->spinlock);
2164}
2165
2167{
2168 bke::SoundRuntime *runtime = sound->runtime;
2169 bool result = false;
2170 BLI_spin_lock(&runtime->spinlock);
2171 if (runtime->waveform == nullptr) {
2172 /* Load the waveform data if it hasn't been loaded and cached already. */
2173 if (!flag_is_set(runtime->tags, bke::SoundTags::WaveformLoading)) {
2174 /* Prevent sounds from reloading. */
2176 result = true;
2177 }
2178 }
2179 BLI_spin_unlock(&runtime->spinlock);
2180 return result;
2181}
2182
2184{
2185 return sound->runtime->waveform;
2186}
2187
2188namespace bke {
2189
2190std::shared_ptr<const bSoundFrequencySampler> bSoundFrequencySampler::get_cached(
2191 const bSound &sound, const Key &key)
2192{
2193#ifdef WITH_AUDASPACE
2194 const std::shared_ptr<SoundRuntimeGeneration> generation = sound_runtime_generation_get(sound);
2195 if (!generation) {
2196 return nullptr;
2197 }
2198 {
2199 /* Fast common case when the sampler has been created already. */
2201 if (generation->samplers.lookup(accessor, key)) {
2202 return accessor->second;
2203 }
2204 }
2205 AUD_Sound sound_handle = generation->handle;
2206 if (!sound_handle) {
2207 /* Maybe try to load the sound in this case instead of relying on cache. */
2208 return nullptr;
2209 }
2210 /* Slower case when the sampler is newly created. */
2212 if (generation->samplers.add(accessor, key)) {
2213 if (key.channel.has_value()) {
2214 const SoundInfo info = sound_info_get(sound_handle);
2215 const int channel = *key.channel;
2216 if (channel < 0 || channel >= info.specs.channels) {
2217 return nullptr;
2218 }
2219 }
2220 accessor->second = std::shared_ptr<bSoundFrequencySampler>(
2221 new bSoundFrequencySampler(sound_handle, generation->reader_cache, key));
2222 }
2223 return accessor->second;
2224#else
2225 UNUSED_VARS(sound, key);
2226 return nullptr;
2227#endif
2228}
2229
2230#ifdef WITH_AUDASPACE
2231static bSoundFrequencySampler::WindowWeights compute_window_function_weights(
2232 const bSoundFrequencySampler::WindowFunction window, const int size)
2233{
2234 Array<float> weights(size);
2235 switch (window) {
2237 for (const int i : IndexRange(size)) {
2238 weights[i] = 0.5f - 0.5f * math::cos((2.0f * std::numbers::pi * i) / (size - 1));
2239 }
2240 break;
2241 }
2243 for (const int i : IndexRange(size)) {
2244 weights[i] = 0.54f - 0.46f * math::cos((2.0f * std::numbers::pi * i) / (size - 1));
2245 }
2246 break;
2247 }
2249 for (const int i : IndexRange(size)) {
2250 weights[i] = 0.42f - 0.5f * math::cos((2.0f * std::numbers::pi * i) / (size - 1)) +
2251 0.08f * math::cos((4.0f * std::numbers::pi * i) / (size - 1));
2252 }
2253 break;
2254 }
2256 weights.fill(1.0f);
2257 break;
2258 }
2259 }
2260 const float sum = std::accumulate(weights.begin(), weights.end(), 0.0f);
2261 return bSoundFrequencySampler::WindowWeights{std::move(weights), sum};
2262}
2263
2265static const bSoundFrequencySampler::WindowWeights &get_window_function_weights(
2266 const bSoundFrequencySampler::WindowFunction window, const int size)
2267{
2268 static Mutex mutex;
2269 static Map<std::pair<bSoundFrequencySampler::WindowFunction, int>,
2270 std::unique_ptr<bSoundFrequencySampler::WindowWeights>>
2271 map;
2272 std::lock_guard lock{mutex};
2273 return *map.lookup_or_add_cb({window, size}, [&]() {
2274 return std::make_unique<bSoundFrequencySampler::WindowWeights>(
2275 compute_window_function_weights(window, size));
2276 });
2277}
2278#endif
2279
2280#ifdef WITH_AUDASPACE
2281bSoundFrequencySampler::bSoundFrequencySampler(AUD_Sound sound,
2282 std::shared_ptr<SoundReaderCache> reader_cache,
2283 const Key &key)
2284 : key_(key),
2285 reader_cache_(std::move(reader_cache)),
2286 window_weights_(get_window_function_weights(key.window_function, key.fft_size))
2287{
2288 const SoundInfo info = bke::sound_info_get(sound);
2289 samples_per_second_ = info.specs.samplerate;
2290 /* This could be a parameter but a single fixed value seems fine for now and makes caching much
2291 * simpler. */
2292 window_cache_stride_ = key.fft_size / 8;
2293 /* The length is negative for sounds of unknown duration. */
2294 const int window_caches_num = std::max<int>(
2295 std::ceil(info.length * info.specs.samplerate / window_cache_stride_), 0);
2296 window_caches_.reinitialize(window_caches_num);
2297}
2298#endif
2299
2300std::optional<Array<float>> bSoundFrequencySampler::compute_fft(const int start_sample) const
2301{
2302 /* Since the result of the dft algorithm is symmetric in this case, only the first half is
2303 * computed. */
2304 const int frequencies_num = key_.fft_size / 2;
2305
2306#if defined(WITH_AUDASPACE) && defined(WITH_FFTW3)
2307 /* Read some extra samples before the ones we are actually interested in here. This is done
2308 * because the #read function may sometimes give invalid data for the first samples. */
2309 const int warmup_samples = std::min(2000, start_sample);
2310
2311 int channels_num;
2312 Array<float> read_buffer_extra;
2313 bool is_end_of_stream = false;
2314 int length = key_.fft_size + warmup_samples;
2315 {
2316 SoundReaderLease reader = reader_cache_->acquire();
2317 if (!reader) {
2318 return std::nullopt;
2319 }
2320 channels_num = reader->getSpecs().channels;
2321 read_buffer_extra.reinitialize((key_.fft_size + warmup_samples) * channels_num);
2322 try {
2323 reader->seek(std::max(start_sample - warmup_samples, 0));
2324 reader->read(length, is_end_of_stream, read_buffer_extra.data());
2325 }
2326 catch (...) {
2327 reader.discard();
2328 throw;
2329 }
2330 }
2331 const Span<float> read_buffer = read_buffer_extra
2332 .as_span()
2333 /* Limit to actually read values. */
2334 .take_front(length * channels_num)
2335 /* Drop the warmup samples. */
2336 .drop_front(warmup_samples * channels_num);
2337 const int read_length = read_buffer.size() / channels_num;
2338
2339 /* Pull out the samples for the requested channel(s). */
2340 Array<float> buffer(key_.fft_size, 0.0f);
2341 if (key_.channel.has_value()) {
2342 const int channel = *key_.channel;
2343 if (channel < 0 || channel >= channels_num) {
2344 return std::nullopt;
2345 }
2346 for (const int i : IndexRange(read_length)) {
2347 buffer[i] = read_buffer[i * channels_num + channel];
2348 }
2349 }
2350 else {
2351 for (const int i : IndexRange(read_length)) {
2352 for (const int c : IndexRange(channels_num)) {
2353 buffer[i] += read_buffer[i * channels_num + c];
2354 }
2355 buffer[i] /= channels_num;
2356 }
2357 }
2358
2359 /* Apply window function which avoids spectral leakage (depending on the function). */
2360 for (const int i : IndexRange(read_length)) {
2361 buffer[i] *= window_weights_.weights[i];
2362 }
2363
2364 /* Set up the fftw plan. */
2365 fftwf_complex *fftwf_buffer = static_cast<fftwf_complex *>(
2366 fftwf_malloc(sizeof(fftwf_complex) * (frequencies_num + 1)));
2367 fftwf_plan plan = fftwf_plan_dft_r2c_1d(
2368 key_.fft_size, buffer.data(), fftwf_buffer, FFTW_ESTIMATE);
2369 BLI_SCOPED_DEFER([&]() {
2370 fftwf_destroy_plan(plan);
2371 fftwf_free(fftwf_buffer);
2372 });
2373
2374 /* Actually perform the fourier transform. */
2375 fftwf_execute(plan);
2376
2377 /* Compute the amplitudes of the frequencies. */
2378 Array<float> frequency_amplitudes(frequencies_num);
2379 /* The scaling factor is applied so that changing the fft size or window function does not affect
2380 * the magnitude of the result. */
2381 const float scaling_factor = 1.0f / window_weights_.weights_sum;
2382 for (const int i : IndexRange(frequencies_num)) {
2383 const fftwf_complex &c = fftwf_buffer[i];
2384 /* Take real and imaginary parts into account which correspond to the sin and cos component of
2385 * the frequency. */
2386 frequency_amplitudes[i] = sqrt(pow2f(c[0]) + pow2f(c[1])) * scaling_factor;
2387 }
2388
2389 return frequency_amplitudes;
2390#else
2391 UNUSED_VARS(start_sample);
2392 return Array<float>(frequencies_num, 0.0f);
2393#endif
2394}
2395
2397 const float p0, const float p1, const float p2, const float p3, const float t)
2398{
2399 const float t2 = t * t;
2400 const float t3 = t2 * t;
2401
2402 const float value = 0.5 * ((2 * p1) + (-p0 + p2) * t + (2 * p0 - 5 * p1 + 4 * p2 - p3) * t2 +
2403 (-p0 + 3 * p1 - 3 * p2 + p3) * t3);
2404 return value;
2405}
2406
2408 const float p0, const float p1, const float p2, const float p3, const float t)
2409{
2410 const float t2 = t * t;
2411 const float t3 = t2 * t;
2412
2413 const float b0 = (1.0f - t) * (1.0f - t) * (1.0f - t) / 6.0f;
2414 const float b1 = (3.0f * t3 - 6.0f * t2 + 4.0f) / 6.0f;
2415 const float b2 = (-3.0f * t3 + 3.0f * t2 + 3.0f * t + 1.0f) / 6.0f;
2416 const float b3 = t3 / 6.0f;
2417
2418 return (b0 * p0 + b1 * p1 + b2 * p2 + b3 * p3);
2419}
2420
2421float bSoundFrequencySampler::sample(const float time,
2422 const float low,
2423 const float high,
2424 const InterpolationMethod time_interpolation,
2425 const InterpolationMethod frequency_interpolation) const
2426{
2427 if (low >= high) {
2428 return 0.0f;
2429 }
2430 const float i_float = std::max(
2431 0.0f, (time * samples_per_second_ - key_.fft_size / 2) / window_cache_stride_);
2432 const int i_pre = floorf(i_float);
2433 const int i_post = i_pre + 1;
2434 const float t = fractf(i_float);
2435
2436 switch (time_interpolation) {
2438 const float v_prev = this->sample_frequency_range_in_window(
2439 i_pre, low, high, frequency_interpolation);
2440 const float v_next = this->sample_frequency_range_in_window(
2441 i_post, low, high, frequency_interpolation);
2442
2443 return math::interpolate(v_prev, v_next, t);
2444 }
2446 const int i_pre2 = i_pre - 1;
2447 const int i_post2 = i_post + 1;
2448
2449 const float p0 = this->sample_frequency_range_in_window(
2450 i_pre2, low, high, frequency_interpolation);
2451 const float p1 = this->sample_frequency_range_in_window(
2452 i_pre, low, high, frequency_interpolation);
2453 const float p2 = this->sample_frequency_range_in_window(
2454 i_post, low, high, frequency_interpolation);
2455 const float p3 = this->sample_frequency_range_in_window(
2456 i_post2, low, high, frequency_interpolation);
2457
2458 return catmul_rom_interpolation(p0, p1, p2, p3, t);
2459 }
2460 default:
2462 const int i_pre2 = i_pre - 1;
2463 const int i_post2 = i_post + 1;
2464
2465 const float p0 = this->sample_frequency_range_in_window(
2466 i_pre2, low, high, frequency_interpolation);
2467 const float p1 = this->sample_frequency_range_in_window(
2468 i_pre, low, high, frequency_interpolation);
2469 const float p2 = this->sample_frequency_range_in_window(
2470 i_post, low, high, frequency_interpolation);
2471 const float p3 = this->sample_frequency_range_in_window(
2472 i_post2, low, high, frequency_interpolation);
2473
2474 return bspline_interpolation(p0, p1, p2, p3, t);
2475 }
2476 }
2477}
2478
2479float bSoundFrequencySampler::sample_frequency_range_in_window(int window_i,
2480 float low,
2481 float high,
2482 InterpolationMethod method) const
2483{
2484 const std::optional<Span<float>> window_opt = this->ensure_window_cache(window_i);
2485 if (!window_opt.has_value()) {
2486 return 0.0f;
2487 }
2488 const Span<float> window_values = *window_opt;
2489 const float cumulative_low = this->sample_cumulative_frequency(window_values, low, method);
2490 const float cumulative_high = this->sample_cumulative_frequency(window_values, high, method);
2491 return cumulative_high - cumulative_low;
2492}
2493
2494float bSoundFrequencySampler::sample_cumulative_frequency(const Span<float> window_values,
2495 const float frequency,
2496 const InterpolationMethod method) const
2497{
2498 const int max_i = window_values.size() - 1;
2499 const float i_float = frequency * key_.fft_size / samples_per_second_;
2500 const int i_pre = std::clamp<int>(std::floor(i_float), 0, max_i);
2501 const int i_post = std::min(i_pre + 1, max_i);
2502 const float t = fractf(i_float);
2503
2504 switch (method) {
2506 return math::interpolate(window_values[i_pre], window_values[i_post], t);
2507 }
2509 const int i_pre2 = std::max(i_pre - 1, 0);
2510 const int i_post2 = std::min(i_post + 1, max_i);
2511
2512 const float p0 = window_values[i_pre2];
2513 const float p1 = window_values[i_pre];
2514 const float p2 = window_values[i_post];
2515 const float p3 = window_values[i_post2];
2516
2517 return catmul_rom_interpolation(p0, p1, p2, p3, t);
2518 }
2520 const int i_pre2 = std::max(i_pre - 1, 0);
2521 const int i_post2 = std::min(i_post + 1, max_i);
2522
2523 const float p0 = window_values[i_pre2];
2524 const float p1 = window_values[i_pre];
2525 const float p2 = window_values[i_post];
2526 const float p3 = window_values[i_post2];
2527
2528 return bspline_interpolation(p0, p1, p2, p3, t);
2529 }
2530 }
2531 return 0.0f;
2532}
2533
2534std::optional<Span<float>> bSoundFrequencySampler::ensure_window_cache(int window_i) const
2535{
2536 if (window_caches_.is_empty()) {
2537 return std::nullopt;
2538 }
2539
2540 /* Clamp window so that too low or high indices get mapped to the closest valid index. */
2541 window_i = std::clamp<int>(window_i, 0, window_caches_.size() - 1);
2542
2543 const WindowCache &window = window_caches_[window_i];
2544 /* Compute the FFT of that window if that wasn't done already. */
2545 window.mutex.ensure([&]() {
2546 std::optional<Array<float>> fft_array = this->compute_fft(window_i * window_cache_stride_);
2547 if (!fft_array.has_value()) {
2548 return;
2549 }
2550 /* Compute prefix sum for the fft values. */
2551 const Span<float> fft_values = fft_array->as_span();
2552 window.cumulative_amplitudes.emplace(fft_values.size() + 1);
2553 float sum = 0.0f;
2554 for (const int i : fft_array->index_range()) {
2555 const float value = std::abs(fft_values[i]);
2556 (*window.cumulative_amplitudes)[i] = sum;
2557 sum += value;
2558 }
2559 window.cumulative_amplitudes->last() = sum;
2560 });
2561 return window.cumulative_amplitudes;
2562}
2563
2564} // namespace bke
2565
2566} // namespace blender
#define SOUND_WAVE_SAMPLES_PER_SECOND
Definition BKE_sound.hh:21
std::shared_ptr< void > AUD_Sequence
std::shared_ptr< void > AUD_SequenceEntry
std::shared_ptr< void > AUD_Sound
std::shared_ptr< void > AUD_Handle
std::shared_ptr< void > AUD_Device
#define NAN_FLT
#define BLI_SCOPED_DEFER(function_to_defer)
#define FILE_MAX
#define BLI_path_cmp
#define STR_ELEM(...)
#define BLT_I18NCONTEXT_ID_SOUND
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define DEG_OBJECT_ITER_BEGIN(settings_, instance_)
#define DEG_OBJECT_ITER_END
#define FILTER_ID_SO
Definition DNA_ID.h:1221
#define ID_BLEND_PATH(_bmain, _id)
Definition DNA_ID.h:688
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
Object is a sort of wrapper for general info.
#define FRA2TIME(a)
Singleton to manage exceptions.
Read Blender memory allocation and freeing.
volatile int lock
#define U
#define UNUSED_VARS(...)
#define UNUSED_VARS_NDEBUG(...)
#define BLI_INLINE
#define BLI_assert(a)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
btScalar max
SIMD_FORCE_INLINE btScalar length() const
Return the length of the vector.
Definition btVector3.h:257
Camera * camera
Definition scene.h:145
void reset()
Definition scene.cpp:502
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange()=default
static std::shared_ptr< const bSoundFrequencySampler > get_cached(const bSound &sound, const Key &key)
float sample(float time, float low, float high, InterpolationMethod time_interpolation, InterpolationMethod frequency_interpolation) const
#define pf(_x, _i)
Prefetch 64.
Definition gim_memory.h:48
#define FLT_MAX
#define main()
#define position
#define frequency(freq)
#define floor
#define sqrt
float square(float v)
#define LOG(level)
Definition log.h:97
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
ccl_device_inline float2 power(const float2 v, const float e)
#define G(x, y, z)
void free(Subdiv *subdiv)
Definition subdiv.cc:192
ConcurrentMap< bSoundFrequencySampler::Key, std::shared_ptr< bSoundFrequencySampler > > bSoundFrequencySamplerMap
static float bspline_interpolation(const float p0, const float p1, const float p2, const float p3, const float t)
static float catmul_rom_interpolation(const float p0, const float p1, const float p2, const float p3, const float t)
ENUM_OPERATORS(ShaderNodeAncestorFlags)
int size(const AssetLibraryReference *library_reference)
std::string filename(const std::string_view &str, size_t pos)
T cos(const AngleRadianBase< T > &a)
T interpolate(const T &a, const T &b, const FactorT &t)
void sound_update_length(Main *bmain, Scene *scene)
void sound_update(Scene *scene, bSound *sound)
BMesh Iterators.
void BKE_sound_force_device(const char *device)
void BKE_sound_update_scene(Depsgraph *depsgraph, Scene *scene)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_packedfile_blend_write(BlendWriter *writer, const PackedFile *pf)
char * STRNCPY(char(&dst)[N], const char *src)
@ SOUND_CHANNELS_INVALID
Definition BKE_sound.hh:60
@ SOUND_FLAGS_CACHING
AUD_Sound BKE_sound_playback_handle_get(const bSound *sound)
void BKE_sound_move_scene_sound_defaults(Scene *scene, Strip *strip)
void BKE_sound_mute_scene_sound(AUD_SequenceEntry handle, bool mute)
PackedFile * BKE_packedfile_duplicate(const PackedFile *pf_src)
void BKE_sound_ensure_scene(Scene *scene)
unsigned int uint
Definition BKE_text.h:55
void mat4_to_quat(float q[4], const float mat[4][4])
pthread_spinlock_t SpinLock
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
void BKE_sound_jack_scene_update(Scene *scene, int mode, double time)
static bSound * sound_new_file_exists_ex(Main *bmain, const char *filepath, short stream_index)
void BKE_sound_refresh_callback_bmain(Main *bmain)
void id_us_plus(ID *id)
Definition lib_id.cc:433
@ NLASTRIP_FLAG_MUTED
bool BKE_sound_stream_info_get(Main *main, const char *filepath, int stream, SoundStreamInfo *sound_info)
void BKE_sound_move_scene_sound(const Scene *scene, AUD_SequenceEntry handle, int startframe, int endframe, int frameskip, double audio_offset)
void(*)(ID *id, const IDCacheKey *cache_key, void **cache_p, uint flags, void *user_data) IDTypeForeachCacheFunctionCallback
static void sound_free_waveform(bSound *sound)
void DEG_debug_print_eval(Depsgraph *depsgraph, const char *function_name, const char *object_name, const void *object_address)
MINLINE float fractf(float a)
void BKE_sound_reset_scene_specs(Scene *scene)
void BKE_packedfile_free(PackedFile *pf)
PackedFile * BKE_packedfile_new(ReportList *reports, const char *filepath_rel, const char *basepath)
MINLINE int max_ii(int a, int b)
bSound * BKE_sound_new_file_exists(Main *bmain, const char *filepath, short stream_index=0)
bool BKE_sound_info_get(Main *main, bSound *sound, SoundInfo *sound_info)
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2890
AUD_SequenceEntry BKE_sound_add_scene_sound(Scene *scene, Strip *strip)
static void sound_foreach_cache(ID *id, IDTypeForeachCacheFunctionCallback function_callback, void *user_data)
void BKE_sound_set_scene_sound_time_stretch_at_frame(AUD_Sound handle, int frame, float time_stretch, bool animated)
double BKE_sound_sync_scene(Scene *scene)
MINLINE float pow2f(float x)
void BKE_sound_update_scene_listener(Scene *scene)
const Vector< float > * BKE_sound_runtime_get_waveform(const bSound *sound)
Global G
Definition blender.cc:57
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:1058
void BKE_sound_update_sequencer(Main *main, bSound *sound)
void BKE_sound_runtime_state_set(const bSound *sound, AUD_Sound cache, AUD_Sound playback_handle, Vector< float > *waveform)
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
bool BLO_read_data_is_undo(BlendDataReader *reader)
Definition readfile.cc:6819
bool DEG_id_type_any_exists(const Depsgraph *depsgraph, short id_type)
void BLI_spin_end(SpinLock *spin)
Definition threads.cc:448
IDTypeInfo IDType_ID_SO
@ AUDIO_VOLUME_ANIMATED
unsigned char uchar
Definition BKE_text.h:58
static void sound_copy_data(Main *, std::optional< Library * >, ID *id_dst, const ID *id_src, const int)
static void sound_free_data(ID *id)
static void sound_blend_write(BlendWriter *writer, ID *id, const void *id_address)
BMesh const char void * data
void BKE_sound_play_scene(Scene *scene)
void BKE_sound_set_scene_sound_pitch_constant_range(AUD_SequenceEntry handle, int frame_start, int frame_end, float pitch)
int BKE_sound_stream_count(Main *main, const char *filepath)
float BKE_sound_get_length(Main *bmain, bSound *sound)
@ NLASTRIP_TYPE_SOUND
void(*) SoundJackSyncCallback(Main *bmain, int mode, double time)
Definition BKE_sound.hh:207
void BKE_sound_packfile_ensure(Main *bmain, bSound *sound, ReportList *reports)
void BKE_sound_runtime_state_get_and_clear(const bSound *sound, AUD_Sound *r_cache, AUD_Sound *r_playback_handle, Vector< float > **r_waveform)
void BKE_sound_set_scene_volume(Scene *scene, float volume)
@ DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET
@ DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY
@ DEG_ITER_OBJECT_FLAG_LINKED_INDIRECTLY
char ** BKE_sound_get_device_names()
bSound * BKE_sound_new_file(Main *bmain, const char *filepath, short stream_index=0)
static void sound_init_runtime(bSound *sound)
void BKE_sound_create_scene(Scene *scene)
void BKE_sound_set_scene_sound_time_stretch_constant_range(AUD_Sound handle, int frame_start, int frame_end, float time_stretch)
void BKE_sound_init(Main *bmain)
void BKE_sound_mute_scene(Scene *scene, bool muted)
void BKE_sound_update_fps(Main *bmain, Scene *scene)
static void sound_ensure_loaded(Main *bmain, bSound *sound)
void BKE_sound_runtime_clear_waveform_loading_tag(bSound *sound)
void BLI_spin_unlock(SpinLock *spin)
Definition threads.cc:433
@ BKE_BPATH_FOREACH_PATH_SKIP_PACKED
Definition BKE_bpath.hh:49
BLI_INLINE void sound_verify_evaluated_id(const ID *id)
void BKE_sound_update_scene_sound(AUD_SequenceEntry handle, bSound *sound)
void BLI_spin_init(SpinLock *spin)
Definition threads.cc:394
void BKE_sound_destroy_scene(Scene *scene)
bool BKE_bpath_foreach_path_fixed_process(BPathForeachPathData *bpath_data, char *path, size_t path_maxncpy)
Definition bpath.cc:130
void BKE_sound_read_waveform(Main *bmain, bSound *sound, bool *stop)
@ INDEX_ID_SO
Definition DNA_ID.h:1304
static void sound_blend_read_data(BlendDataReader *reader, ID *id)
void BKE_sound_set_scene_sound_pitch_at_frame(AUD_SequenceEntry handle, int frame, float pitch, bool animated)
std::mutex Mutex
Definition BLI_mutex.hh:51
@ ID_RECALC_SOURCE
Definition DNA_ID.h:1143
@ ID_RECALC_AUDIO
Definition DNA_ID.h:1132
void BKE_sound_load(Main *bmain, bSound *sound)
void BKE_sound_remove_scene_sound(Scene *scene, AUD_SequenceEntry handle)
@ ID_TAG_NO_MAIN
Definition DNA_ID.h:981
@ ID_TAG_COPIED_ON_EVAL
Definition DNA_ID.h:1000
static void sound_free_audio(bSound *sound)
void BKE_sound_stop_scene(Scene *scene)
void BKE_packedfile_blend_read(BlendDataReader *reader, PackedFile **pf_p, StringRefNull filepath)
void BKE_sound_seek_scene(Main *bmain, Scene *scene)
static void sound_foreach_path(ID *id, BPathForeachPathData *bpath_data)
VecBase< float, 3 > float3
AUD_SequenceEntry BKE_sound_scene_add_scene_sound(Scene *scene, Strip *strip)
void BKE_sound_set_scene_sound_volume_at_frame(AUD_SequenceEntry handle, int frame, float volume, bool animated)
void BLI_spin_lock(SpinLock *spin)
Definition threads.cc:408
AUD_Sound BKE_sound_get_factory(void *sound)
void * BKE_libblock_alloc(Main *bmain, short type, const char *name, int flag) ATTR_WARN_UNUSED_RESULT
Definition lib_id.cc:1545
AUD_Sound BKE_sound_ensure_time_stretch_effect(const Strip *strip, float fps)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
void BKE_sound_evaluate(Depsgraph *depsgraph, Main *bmain, bSound *sound)
void BKE_sound_jack_sync_callback_set(SoundJackSyncCallback callback)
void BKE_sound_set_scene_sound_pan_at_frame(AUD_SequenceEntry handle, int frame, float pan, bool animated)
@ IDTYPE_FLAGS_NO_ANIMDATA
Definition BKE_idtype.hh:52
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:50
void BKE_sound_update_sequence_handle(AUD_SequenceEntry handle, AUD_Sound sound_handle)
bool BKE_sound_runtime_start_waveform_loading(bSound *sound)
#define floorf
#define fractf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
void write_id_struct(const void *id_address, const T *id, BlendStructWriterTypedFn< T > fn=nullptr)
bool is_undo() const
char name[258]
Definition DNA_ID.h:433
void * next
Definition DNA_ID.h:418
unsigned int recalc
Definition DNA_ID.h:446
ListBaseT< bSound > sounds
Definition BKE_main.hh:405
ListBaseT< NlaStrip > strips
bke::SceneRuntime * runtime
struct blender::SoundInfo::@227301210053207103375114031212057322370226174015 specs
char filepath[1024]
bke::SoundRuntime * runtime
struct PackedFile * packedfile
#define N_(msgid)