401 sound_runtime_generation_replace(sound,
nullptr);
404 runtime->
cache.reset();
416 const char *force_device =
nullptr;
419 const char *device_name =
nullptr;
420 aud::DeviceSpecs initialized_specs;
425 std::mutex sound_device_mutex;
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;
450 std::unique_lock
lock(sound_device_mutex);
454 if (delayed_close_thread.joinable()) {
455 delayed_close_cv.notify_all();
456 delayed_close_thread.join();
464static void sound_device_close_no_lock()
466 if (g_state.sound_device) {
468 bke::sound_device_exit();
469 g_state.sound_device =
nullptr;
473static void sound_device_open_no_lock(
const aud::DeviceSpecs &requested_specs)
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");
486 g_state.initialized_specs = g_state.sound_device->getSpecs();
489static void sound_device_use_begin()
491 ++g_state.num_device_users;
493 if (g_state.sound_device) {
497 sound_device_open_no_lock(g_state.initialized_specs);
500static void sound_device_use_end_after(
const std::chrono::milliseconds after_ms)
503 if (g_state.num_device_users == 0) {
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();
514static void sound_device_use_end()
516 sound_device_use_end_after(std::chrono::milliseconds(0));
522static bool sound_use_close_thread()
525 if (g_state.force_device &&
STREQ(g_state.force_device,
"None")) {
526# if defined(WITH_PYTHON_MODULE) || defined(WITH_HEADLESS)
537 if (__builtin_available(macOS 15.2, *)) {
546static void delayed_close_thread_run()
548 constexpr std::chrono::milliseconds device_close_delay{30000};
550 std::unique_lock
lock(g_state.sound_device_mutex);
552 while (!g_state.need_exit) {
553 if (!g_state.use_delayed_close) {
557 g_state.delayed_close_cv.wait(
lock);
561 if (g_state.num_device_users == 0) {
562 if (g_state.sound_device ==
nullptr) {
564 g_state.delayed_close_cv.wait(
lock);
567 g_state.delayed_close_cv.wait_until(
568 lock, g_state.last_user_disconnect_time_point + device_close_delay);
576 g_state.delayed_close_cv.wait(
lock);
579 if (g_state.need_exit) {
584 if (!g_state.use_delayed_close) {
590 if (!g_state.sound_device) {
595 CLOG_DEBUG(&
LOG,
"Checking last device usage and timestamp");
597 if (g_state.num_device_users) {
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();
613static void sound_sync_callback(
void *
data,
int mode,
float time)
615 if (sound_jack_sync_callback ==
nullptr) {
619 sound_jack_sync_callback(bmain, mode, time);
624 g_state.force_device = device;
629 bke::sound_system_initialize();
630 if (sound_use_close_thread()) {
632 g_state.delayed_close_thread = std::thread(delayed_close_thread_run);
638 g_state.exit_threads();
640 std::lock_guard
lock(g_state.sound_device_mutex);
641 sound_device_close_no_lock();
643 if (audio_device_names !=
nullptr) {
645 for (i = 0; audio_device_names[i]; i++) {
646 free(audio_device_names[i]);
648 free(audio_device_names);
649 audio_device_names =
nullptr;
655 std::lock_guard
lock(g_state.sound_device_mutex);
657 sound_device_close_no_lock();
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;
664 if (g_state.force_device ==
nullptr) {
666 g_state.device_name = names[0];
669 for (
int i = 0; names[i]; i++) {
670 if (i ==
U.audiodevice) {
671 g_state.device_name = names[i];
676 g_state.device_name = g_state.force_device;
679 g_state.buffer_size =
U.mixbufsize < 128 ? 1024 :
U.mixbufsize;
681 if (requested_specs.rate <
double(aud::RATE_8000)) {
682 requested_specs.rate = aud::RATE_48000;
685 if (requested_specs.format <= aud::FORMAT_INVALID) {
686 requested_specs.format = aud::FORMAT_S16;
689 if (requested_specs.channels <= aud::CHANNELS_INVALID) {
690 requested_specs.channels = aud::CHANNELS_STEREO;
694 sound_device_open_no_lock(requested_specs);
695 if (
STR_ELEM(g_state.device_name,
"JACK",
"PulseAudio",
"PipeWire")) {
708 g_state.use_delayed_close =
false;
709 aud::DeviceManager::getDevice()->setSyncCallback(sound_sync_callback, bmain);
712 g_state.use_delayed_close =
true;
713 sound_device_close_no_lock();
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);
725static void sound_load_audio(
Main *bmain,
bSound *sound,
bool free_waveform)
728 sound_runtime_generation_replace(sound,
nullptr);
729 runtime->cache.reset();
730 runtime->handle.reset();
731 runtime->playback_handle.reset();
743 STRNCPY(fullpath, sound->filepath);
748 runtime->handle =
AUD_Sound(
new aud::File((
uchar *)
pf->data,
pf->size, sound->stream_index));
752 runtime->handle =
AUD_Sound(
new aud::File(fullpath, sound->stream_index));
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));
765 runtime->cache =
AUD_Sound(
new aud::StreamBuffer(runtime->handle));
767 catch (aud::Exception &ex) {
772 if (runtime->cache) {
773 runtime->playback_handle = runtime->cache;
776 runtime->playback_handle = runtime->handle;
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));
788 sound_load_audio(bmain, sound,
true);
793 if (sound->packedfile !=
nullptr) {
807 const aud::DeviceSpecs &specs,
813 std::shared_ptr<aud::ReadDevice> device(
new aud::ReadDevice(specs));
814 device->setQuality(aud::ResampleQuality::MEDIUM);
815 device->setVolume(volume);
817 aud::Sequence *f =
dynamic_cast<aud::Sequence *
>(scene->runtime->audio.sound_scene.get());
818 f->setSpecs(specs.specs);
820 AUD_Handle handle = device->play(f->createQualityReader(aud::ResampleQuality::MEDIUM));
822 handle->seek(start / scene->frames_per_second());
827 catch (aud::Exception &) {
837 if (scene->r.frs_sec_base == 0) {
838 scene->r.frs_sec_base = 1;
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();
859 if (audio.playback_handle) {
860 audio.playback_handle->stop();
861 audio.playback_handle.reset();
863 if (audio.sound_scrub_handle) {
864 audio.sound_scrub_handle->stop();
865 audio.sound_scrub_handle.reset();
868 audio.sound_scene->remove(handle);
870 audio.speaker_handles.clear();
871 audio.sound_scene.
reset();
876 g_state.sound_device_mutex.lock();
877 if (g_state.sound_device !=
nullptr) {
878 g_state.sound_device->lock();
884 g_state.sound_device_mutex.unlock();
885 if (g_state.sound_device !=
nullptr) {
886 g_state.sound_device->unlock();
894 if (scene->runtime->audio.sound_scene) {
895 scene->runtime->audio.sound_scene->setSpecs(g_state.initialized_specs.specs);
902 if (scene->runtime->audio.sound_scene) {
903 scene->runtime->audio.sound_scene->mute(muted);
911 if (scene->runtime->audio.sound_scene) {
912 scene->runtime->audio.sound_scene->setFPS(scene->frames_per_second());
923 sound->setSpeedOfSound(scene->audio.speed_of_sound);
924 sound->setDopplerFactor(scene->audio.doppler_factor);
925 sound->setDistanceModel(aud::DistanceModel(scene->audio.distance_model));
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();
937 scene->runtime->audio.sound_scene->add(strip->scene->runtime->audio.sound_scene,
949 if (strip->sound ==
nullptr) {
954 int startframe = strip->left_handle();
955 int endframe = strip->right_handle(scene);
956 int frameskip = strip->startofs + strip->anim_startofs;
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) {
962 scene->runtime->audio.sound_scene->add(strip->sound->runtime->playback_handle,
963 startframe / fps + offset_time,
968 strip->sound->runtime->playback_handle, startframe / fps, endframe / fps, -offset_time));
973 scene->runtime->audio.sound_scene->remove(handle);
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);
995 handle->move(startframe / fps, endframe / fps, -offset_time);
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;
1008 strip->runtime->scene_sound,
1009 strip->left_handle(),
1010 strip->right_handle(scene),
1011 strip->startofs + strip->anim_startofs,
1018 handle->setSound(sound->runtime->playback_handle);
1025#ifdef WITH_AUDASPACE
1026 handle->setSound(sound_handle);
1032#ifdef WITH_AUDASPACE
1035static void set_audaspace_anim_property(std::shared_ptr<T> sound,
1036 aud::AnimateablePropertyType type,
1041 aud::AnimateableProperty *prop = sound->getAnimProperty(type);
1044 prop->write(value, frame, 1);
1055 if (scene->runtime->audio.sound_scene ==
nullptr) {
1059 const int frame = scene->r.cfra;
1060 set_audaspace_anim_property(
1061 scene->runtime->audio.sound_scene, aud::AP_VOLUME, frame, &volume, animated);
1069 set_audaspace_anim_property(handle, aud::AP_VOLUME, frame, &volume, animated);
1077 set_audaspace_anim_property(handle, aud::AP_PITCH, frame, &pitch, animated);
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);
1096 set_audaspace_anim_property(handle, aud::AP_PANNING, frame, &pan, animated);
1101 BLI_assert_msg(0,
"is not supposed to be used, is weird function.");
1105 for (scene =
main->scenes.first(); scene; scene =
static_cast<Scene *
>(scene->id.next)) {
1111static void sound_start_play_scene(
Scene *scene)
1116 if (audio.playback_handle) {
1117 audio.playback_handle->stop();
1118 audio.playback_handle.reset();
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);
1131 std::lock_guard
lock(g_state.sound_device_mutex);
1132 sound_device_use_begin();
1135 const double cur_time =
FRA2TIME(scene->r.cfra + scene->r.subframe);
1137 g_state.sound_device->lock();
1140 if (audio.sound_scrub_handle && audio.sound_scrub_handle->getStatus() != aud::STATUS_INVALID) {
1143 audio.sound_scrub_handle->stop();
1144 audio.sound_scrub_handle =
nullptr;
1147 audio.playback_handle->pause();
1150 aud::Status status = audio.playback_handle ? audio.playback_handle->getStatus() :
1151 aud::STATUS_INVALID;
1153 if (status == aud::STATUS_INVALID) {
1154 sound_start_play_scene(scene);
1156 if (!audio.playback_handle) {
1157 g_state.sound_device->unlock();
1162 if (status != aud::STATUS_PLAYING) {
1165 aud::DeviceManager::getDevice()->seekSynchronizer(cur_time);
1166 audio.playback_handle->seek(cur_time);
1167 audio.playback_handle->resume();
1171 aud::DeviceManager::getDevice()->playSynchronizer();
1174 g_state.sound_device->unlock();
1179 std::lock_guard
lock(g_state.sound_device_mutex);
1181 if (scene->runtime->audio.playback_handle) {
1182 scene->runtime->audio.playback_handle->pause();
1185 aud::DeviceManager::getDevice()->stopSynchronizer();
1188 sound_device_use_end();
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))
1198 if (screen->animtimer) {
1199 animation_playing =
true;
1204 bool do_audio_scrub = scene->audio.flag &
AUDIO_SCRUB && !animation_playing;
1206 if (do_audio_scrub) {
1208 sound_device_use_begin();
1210 else if (g_state.sound_device ==
nullptr) {
1216 g_state.sound_device->lock();
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);
1224 if (!audio.playback_handle) {
1225 g_state.sound_device->unlock();
1226 if (do_audio_scrub) {
1227 sound_device_use_end();
1232 audio.playback_handle->pause();
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);
1239 if (do_audio_scrub) {
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);
1247 if (audio.sound_scrub_handle) {
1248 audio.sound_scrub_handle->stop();
1250 audio.sound_scrub_handle = bke::sound_pause_after(audio.playback_handle, one_frame);
1252 sound_device_use_end_after(std::chrono::milliseconds(
int(one_frame * 1000)));
1254 else if (status == aud::STATUS_PLAYING) {
1259 aud::DeviceManager::getDevice()->seekSynchronizer(cur_time);
1260 audio.playback_handle->seek(cur_time);
1263 g_state.sound_device->unlock();
1271 if (
G.is_rendering) {
1275 if (scene->runtime->audio.playback_handle) {
1277 return aud::DeviceManager::getDevice()->getSynchronizerPosition();
1280 return scene->runtime->audio.playback_handle->getPosition();
1285static int sound_read(
1286 AUD_Sound sound,
float *buffer,
int length,
int samples_per_second,
const bool *interrupt)
1288 using namespace aud;
1293 specs.rate = RATE_INVALID;
1294 specs.channels = CHANNELS_MONO;
1295 specs.format = FORMAT_INVALID;
1297 std::shared_ptr<IReader> reader = ChannelMapper(sound, specs).createReader();
1299 specs.specs = reader->getSpecs();
1301 float samplejump = specs.rate / samples_per_second;
1307 for (
int i = 0; i <
length; i++) {
1314 aBuffer.assureSize(
len * AUD_SAMPLE_SIZE(specs));
1315 buf = aBuffer.getBuffer();
1317 reader->read(
len, eos, buf);
1320 power = *buf * *buf;
1321 for (
int j = 1; j <
len; j++) {
1328 power += buf[j] * buf[j];
1331 buffer[i * 3] =
min;
1332 buffer[i * 3 + 1] =
max;
1333 buffer[i * 3 + 2] = std::sqrt(
power /
len);
1335 if (overallmax <
max) {
1338 if (overallmax < -
min) {
1348 if (overallmax > 1.0f) {
1349 for (
int i = 0; i <
length * 3; i++) {
1350 buffer[i] /= overallmax;
1364 bool need_close_audio_handles =
false;
1366 if (runtime->playback_handle ==
nullptr) {
1368 sound_load_audio(bmain, sound,
true);
1369 need_close_audio_handles =
true;
1372 SoundInfo info = bke::sound_info_get(runtime->playback_handle);
1375 if (info.length > 0) {
1378 waveform->resize(3 *
length);
1381 waveform->resize(3 *
length);
1385 MEM_delete(waveform);
1386 MEM_SAFE_DELETE(runtime->waveform);
1388 runtime->tags &=
~bke::SoundTags::WaveformLoading;
1390 if (need_close_audio_handles) {
1399 runtime->waveform = waveform;
1400 runtime->tags &=
~bke::SoundTags::WaveformLoading;
1403 if (need_close_audio_handles) {
1429 if ((strip_runtime.speaker_handle !=
nullptr) &&
1430 scene->runtime->audio.speaker_handles.remove(strip_runtime.speaker_handle))
1432 if (speaker->sound) {
1433 strip_runtime.speaker_handle->move(
1434 double(strip.start) / scene->frames_per_second(),
FLT_MAX, 0);
1437 scene->runtime->audio.sound_scene->remove(strip_runtime.speaker_handle);
1438 strip_runtime.speaker_handle =
nullptr;
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(),
1448 strip_runtime.speaker_handle->setRelative(
false);
1452 if (strip_runtime.speaker_handle) {
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);
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);
1491 deg_iter_settings.
depsgraph = depsgraph;
1496 sound_update_base(scene,
object, new_set);
1503 audio.sound_scene->remove(handle);
1505 audio.speaker_handles.clear();
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);
1515 audio.speaker_handles = new_set;
1520 return ((
bSound *)sound)->runtime->playback_handle;
1525 if (sound->runtime->playback_handle !=
nullptr) {
1526 SoundInfo info = bke::sound_info_get(sound->runtime->playback_handle);
1538 if (audio_device_names ==
nullptr) {
1540 std::vector<std::string> v_names = aud::DeviceManager::getAvailableDeviceNames();
1541 char **names = (
char **)malloc(
sizeof(
char *) * (v_names.size() + 1));
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);
1549 names[v_names.size()] =
nullptr;
1550 audio_device_names = names;
1553 return audio_device_names;
1558 if (sound->runtime->playback_handle !=
nullptr) {
1559 *sound_info = bke::sound_info_get(sound->runtime->playback_handle);
1565 sound_load_audio(
main, sound,
false);
1566 const bool result = sound->runtime->playback_handle !=
nullptr;
1568 *sound_info = bke::sound_info_get(sound->runtime->playback_handle);
1575 const char *filepath,
1580 STRNCPY(filepath_abs, filepath);
1584 std::vector<aud::StreamInfo> streams;
1586 streams = aud::FileManager::queryStreams(filepath_abs);
1588 catch (aud::Exception &) {
1592 if (stream < 0 || stream >= streams.size()) {
1596 sound_info->start = streams[stream].start;
1597 sound_info->duration = streams[stream].duration;
1604 STRNCPY(filepath_abs, filepath);
1607 std::vector<aud::StreamInfo> streams;
1609 streams = aud::FileManager::queryStreams(filepath_abs);
1610 return streams.size();
1612 catch (aud::Exception &) {
1617# ifdef WITH_RUBBERBAND
1623 if (runtime.sound_time_stretch !=
nullptr && runtime.sound_time_stretch_fps == fps) {
1624 return runtime.sound_time_stretch;
1634 aud::StretcherQuality::HIGH,
1636 runtime.sound_time_stretch_fps = fps;
1637 return runtime.sound_time_stretch;
1645 std::shared_ptr<aud::AnimateableProperty> prop =
1646 std::dynamic_pointer_cast<aud::AnimateableTimeStretchPitchScale>(handle)->getAnimProperty(
1647 aud::AP_TIME_STRETCH);
1650 prop->write(&time_stretch, frame, 1);
1654 prop->write(&time_stretch);
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);
1677 res.specs.samplerate = 0;
1680 std::shared_ptr<aud::IReader> reader = sound->createReader();
1682 aud::Specs specs = reader->getSpecs();
1684 res.specs.samplerate = specs.rate;
1685 res.length = reader->getLength() /
float(specs.rate);
1688 catch (aud::Exception &ex) {
1694void bke::sound_system_initialize()
1696 aud::PluginManager::loadPlugins();
1697 aud::NULLDevice::registerPlugin();
1700AUD_Device bke::sound_device_init(
const char *device,
1701 const aud::DeviceSpecs &specs,
1705 using namespace aud;
1707 std::shared_ptr<IDeviceFactory> factory = device ? DeviceManager::getDeviceFactory(device) :
1708 DeviceManager::getDefaultDeviceFactory();
1711 factory->setName(name);
1712 factory->setBufferSize(buffersize);
1713 factory->setSpecs(specs);
1714 auto device = factory->openDevice();
1715 DeviceManager::setDevice(device);
1720 catch (Exception &ex) {
1726void bke::sound_device_exit()
1728 aud::DeviceManager::releaseDevice();
1734 device = aud::DeviceManager::getDevice();
1737 return device->play(sound,
true);
1739 catch (aud::Exception &ex) {
1748 auto read_device = std::dynamic_pointer_cast<aud::ReadDevice>(device);
1753 return read_device->read(buffer,
length);
1755 catch (aud::Exception &) {
1762 auto device = aud::DeviceManager::getDevice();
1767 std::lock_guard<aud::ILockable>
lock(*device);
1771 if (handle2.get()) {
1773 handle2->setStopCallback(
1783 catch (aud::Exception &ex) {
1789float *bke::sound_read_file_buffer(
const char *
filename,
1803 using namespace aud;
1806 specs.channels = CHANNELS_MONO;
1807 specs.rate = samplerate;
1815 std::shared_ptr<IReader> reader = file->createReader();
1816 SampleRate rate = reader->getSpecs().rate;
1821 sound =
AUD_Sound(
new Lowpass(sound, high));
1824 sound =
AUD_Sound(
new Highpass(sound, low));
1828 sound =
AUD_Sound(
new LinearResample(sound, specs));
1835 sound =
AUD_Sound(
new Accumulator(sound, additive));
1837 else if (additive) {
1841 reader = sound->createReader();
1851 buffer.resize((
position +
len) *
sizeof(
float),
true);
1852 reader->read(
len, eos, buffer.getBuffer() +
position);
1856 catch (Exception &) {
1871 const aud::DeviceSpecs &specs,
1872 aud::Container format,
1875 bool split_channels,
1876 std::string &r_error,
1877 bool (*progress_callback)(
float,
void *),
1878 void *progress_data)
1880 using namespace aud;
1882 sequence->setSpecs(specs.specs);
1883 std::shared_ptr<IReader> reader = sequence->createQualityReader(ResampleQuality::MEDIUM);
1884 reader->seek(start);
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);
1893 std::vector<std::shared_ptr<IWriter>> writers;
1894 int channels = specs.channels;
1896 aud::DeviceSpecs specs_mono = specs;
1897 specs_mono.channels = CHANNELS_MONO;
1898 for (
int i = 0; i < channels; i++) {
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(
'\\');
1905 if ((
index == std::string::npos) ||
1906 ((
index < index_slash) && (index_slash != std::string::npos)) ||
1907 ((
index < index_backslash) && (index_backslash != std::string::npos)))
1909 stream = fmt::format(
"{}_{}",
filename, i + 1);
1912 stream = fmt::format(
"{}_{}{}",
fn.substr(0,
index), i + 1,
fn.substr(
index));
1914 writers.push_back(FileWriter::createWriter(stream, specs_mono, format, codec, bitrate));
1916 FileWriter::writeReader(
1917 reader, writers,
length, buffersize, progress_callback, progress_data);
1921 catch (Exception &
e) {
1922 r_error =
e.getMessage();
2014 static char *names[1] = {
nullptr};
2038#if !defined(WITH_AUDASPACE) || !defined(WITH_RUBBERBAND)
2076#if defined(WITH_AUDASPACE)
2077 sound_jack_sync_callback = callback;
2088 if (
G.is_rendering) {
2091#ifdef WITH_AUDASPACE
2092 g_state.sound_device->lock();
2103 g_state.sound_device->unlock();
2131 *r_cache = runtime->
cache;
2134 runtime->
cache =
nullptr;
2145 runtime->
cache = cache;
2152 if (sound ==
nullptr) {
2162 runtime->
tags &=
~bke::SoundTags::WaveformLoading;
2171 if (runtime->
waveform ==
nullptr) {
2193#ifdef WITH_AUDASPACE
2194 const std::shared_ptr<SoundRuntimeGeneration> generation = sound_runtime_generation_get(sound);
2201 if (generation->samplers.lookup(accessor, key)) {
2202 return accessor->second;
2205 AUD_Sound sound_handle = generation->handle;
2206 if (!sound_handle) {
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) {
2220 accessor->second = std::shared_ptr<bSoundFrequencySampler>(
2223 return accessor->second;
2230#ifdef WITH_AUDASPACE
2238 weights[i] = 0.5f - 0.5f *
math::cos((2.0f * std::numbers::pi * i) / (size - 1));
2244 weights[i] = 0.54f - 0.46f *
math::cos((2.0f * std::numbers::pi * i) / (
size - 1));
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));
2260 const float sum = std::accumulate(weights.begin(), weights.end(), 0.0f);
2269 static Map<std::pair<bSoundFrequencySampler::WindowFunction, int>,
2270 std::unique_ptr<bSoundFrequencySampler::WindowWeights>>
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));
2280#ifdef WITH_AUDASPACE
2281bSoundFrequencySampler::bSoundFrequencySampler(
AUD_Sound sound,
2282 std::shared_ptr<SoundReaderCache> reader_cache,
2285 reader_cache_(std::move(reader_cache)),
2286 window_weights_(get_window_function_weights(key.window_function, key.fft_size))
2288 const SoundInfo info = bke::sound_info_get(sound);
2289 samples_per_second_ = info.specs.samplerate;
2292 window_cache_stride_ = key.fft_size / 8;
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);
2300std::optional<Array<float>> bSoundFrequencySampler::compute_fft(
const int start_sample)
const
2304 const int frequencies_num = key_.fft_size / 2;
2306#if defined(WITH_AUDASPACE) && defined(WITH_FFTW3)
2309 const int warmup_samples = std::min(2000, start_sample);
2312 Array<float> read_buffer_extra;
2313 bool is_end_of_stream =
false;
2314 int length = key_.fft_size + warmup_samples;
2316 SoundReaderLease reader = reader_cache_->acquire();
2318 return std::nullopt;
2320 channels_num = reader->getSpecs().channels;
2321 read_buffer_extra.reinitialize((key_.fft_size + warmup_samples) * channels_num);
2323 reader->seek(std::max(start_sample - warmup_samples, 0));
2324 reader->read(
length, is_end_of_stream, read_buffer_extra.data());
2331 const Span<float> read_buffer = read_buffer_extra
2334 .take_front(
length * channels_num)
2336 .drop_front(warmup_samples * channels_num);
2337 const int read_length = read_buffer.
size() / channels_num;
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;
2346 for (
const int i :
IndexRange(read_length)) {
2347 buffer[i] = read_buffer[i * channels_num + channel];
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];
2355 buffer[i] /= channels_num;
2360 for (
const int i :
IndexRange(read_length)) {
2361 buffer[i] *= window_weights_.weights[i];
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);
2370 fftwf_destroy_plan(plan);
2371 fftwf_free(fftwf_buffer);
2375 fftwf_execute(plan);
2378 Array<float> frequency_amplitudes(frequencies_num);
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];
2386 frequency_amplitudes[i] =
sqrt(
pow2f(c[0]) +
pow2f(c[1])) * scaling_factor;
2389 return frequency_amplitudes;
2392 return Array<float>(frequencies_num, 0.0f);
2397 const float p0,
const float p1,
const float p2,
const float p3,
const float t)
2399 const float t2 = t * t;
2400 const float t3 = t2 * t;
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);
2408 const float p0,
const float p1,
const float p2,
const float p3,
const float t)
2410 const float t2 = t * t;
2411 const float t3 = t2 * t;
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;
2418 return (b0 * p0 + b1 * p1 + b2 * p2 + b3 * p3);
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);
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);
2446 const int i_pre2 = i_pre - 1;
2447 const int i_post2 = i_post + 1;
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);
2462 const int i_pre2 = i_pre - 1;
2463 const int i_post2 = i_post + 1;
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);
2479float bSoundFrequencySampler::sample_frequency_range_in_window(
int window_i,
2482 InterpolationMethod method)
const
2484 const std::optional<Span<float>> window_opt = this->ensure_window_cache(window_i);
2485 if (!window_opt.has_value()) {
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;
2494float bSoundFrequencySampler::sample_cumulative_frequency(
const Span<float> window_values,
2496 const InterpolationMethod method)
const
2498 const int max_i = window_values.
size() - 1;
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);
2509 const int i_pre2 = std::max(i_pre - 1, 0);
2510 const int i_post2 = std::min(i_post + 1, max_i);
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];
2520 const int i_pre2 = std::max(i_pre - 1, 0);
2521 const int i_post2 = std::min(i_post + 1, max_i);
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];
2534std::optional<Span<float>> bSoundFrequencySampler::ensure_window_cache(
int window_i)
const
2536 if (window_caches_.is_empty()) {
2537 return std::nullopt;
2541 window_i = std::clamp<int>(window_i, 0, window_caches_.size() - 1);
2543 const WindowCache &window = window_caches_[window_i];
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()) {
2551 const Span<float> fft_values = fft_array->as_span();
2552 window.cumulative_amplitudes.emplace(fft_values.
size() + 1);
2554 for (
const int i : fft_array->index_range()) {
2555 const float value = std::abs(fft_values[i]);
2556 (*window.cumulative_amplitudes)[i] = sum;
2559 window.cumulative_amplitudes->last() = sum;
2561 return window.cumulative_amplitudes;