Blender V5.3
sound_ops.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2007 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstddef>
10#include <cstdlib>
11#include <cstring>
12
13#include "MEM_guardedalloc.h"
14
15#include "BLI_listbase.hh"
16#include "BLI_path_utils.hh"
17#include "BLI_string.hh"
18#include "BLI_utildefines.hh"
19
20#include "DNA_scene_types.h"
21#include "DNA_sequence_types.h"
22#include "DNA_sound_types.h"
23#include "DNA_space_types.h"
24
25#include "BKE_context.hh"
26#include "BKE_fcurve.hh"
27#include "BKE_global.hh"
28#include "BKE_lib_id.hh"
29#include "BKE_library.hh"
30#include "BKE_main.hh"
31#include "BKE_packedFile.hh"
32#include "BKE_report.hh"
33#include "BKE_scene.hh"
34#include "BKE_scene_runtime.hh"
35#include "BKE_sound.hh"
36
37#include "RNA_access.hh"
38#include "RNA_define.hh"
39#include "RNA_enum_types.hh"
40#include "RNA_prototypes.hh"
41
42#include "SEQ_iterator.hh"
43#include "SEQ_sequencer.hh"
44
45#include "UI_interface.hh"
47
48#include "WM_api.hh"
49#include "WM_types.hh"
50
51#ifdef WITH_AUDASPACE
52# include <file/IWriter.h>
53#endif
54
56
57#include "ED_sound.hh"
58#include "ED_util.hh"
59
60namespace blender {
61
62/******************** open sound operator ********************/
63
64static void sound_open_cancel(bContext * /*C*/, wmOperator *op)
65{
66 MEM_delete(static_cast<PropertyPointerRNA *>(op->customdata));
67 op->customdata = nullptr;
68}
69
71{
72 PropertyPointerRNA *pprop;
73
74 op->customdata = pprop = MEM_new<PropertyPointerRNA>(__func__);
76}
77
78#ifdef WITH_AUDASPACE
79static wmOperatorStatus sound_open_exec(bContext *C, wmOperator *op)
80{
81 char filepath[FILE_MAX];
82 bSound *sound;
83 PropertyPointerRNA *pprop;
84 Main *bmain = CTX_data_main(C);
85
86 RNA_string_get(op->ptr, "filepath", filepath);
87 sound = BKE_sound_new_file_exists(bmain, filepath);
88
89 if (!op->customdata) {
90 sound_open_init(C, op);
91 }
92
93 if (RNA_boolean_get(op->ptr, "mono")) {
94 sound->flags |= SOUND_FLAGS_MONO;
95 }
96
97 if (RNA_boolean_get(op->ptr, "cache")) {
98 sound->flags |= SOUND_FLAGS_CACHING;
99 }
100
101 /* hook into UI */
102 pprop = static_cast<PropertyPointerRNA *>(op->customdata);
103
104 if (pprop->prop) {
105 /* when creating new ID blocks, use is already 1, but RNA
106 * pointer use also increases user, so this compensates it */
107 id_us_min(&sound->id);
108
109 PointerRNA idptr = RNA_id_pointer_create(&sound->id);
110 RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr, nullptr);
111 RNA_property_update(C, &pprop->ptr, pprop->prop);
112 }
113
115
116 MEM_delete(pprop);
117 return OPERATOR_FINISHED;
118}
119
120#else /* WITH_AUDASPACE */
121
123{
124 BKE_report(op->reports, RPT_ERROR, "Compiled without sound support");
125
126 return OPERATOR_CANCELLED;
127}
128
129#endif
130
132{
133 if (RNA_struct_property_is_set(op->ptr, "filepath")) {
134 return sound_open_exec(C, op);
135 }
136
137 sound_open_init(C, op);
138
139 return WM_operator_filesel(C, op, event);
140}
141
143{
144 /* identifiers */
145 ot->name = "Open Sound";
146 ot->description = "Load a sound file";
147 ot->idname = "SOUND_OT_open";
148
149 /* API callbacks. */
150 ot->exec = sound_open_exec;
153
154 /* flags */
156
157 /* properties */
165 RNA_def_boolean(ot->srna, "cache", false, "Cache", "Cache the sound in memory");
166 RNA_def_boolean(ot->srna, "mono", false, "Mono", "Merge all the sound's channels into one");
167}
168
170{
171 /* identifiers */
172 ot->name = "Open Sound Mono";
173 ot->description = "Load a sound file as mono";
174 ot->idname = "SOUND_OT_open_mono";
175
176 /* API callbacks. */
177 ot->exec = sound_open_exec;
180
181 /* flags */
183
184 /* properties */
192 RNA_def_boolean(ot->srna, "cache", false, "Cache", "Cache the sound in memory");
193 RNA_def_boolean(ot->srna, "mono", true, "Mono", "Mixdown the sound to mono");
194}
195
196/* ******************************************************* */
197
198static void sound_update_animation_flags(Scene *scene);
199
200static bool sound_update_animation_flags_fn(Strip *strip, void *user_data)
201{
202 const FCurve *fcu;
203 Scene *scene = static_cast<Scene *>(user_data);
204 bool driven;
205
206 fcu = id_data_find_fcurve(&scene->id, strip, RNA_Strip, "volume", 0, &driven);
207 if (fcu || driven) {
208 strip->flag |= SEQ_AUDIO_VOLUME_ANIMATED;
209 }
210 else {
211 strip->flag &= ~SEQ_AUDIO_VOLUME_ANIMATED;
212 }
213
214 fcu = id_data_find_fcurve(&scene->id, strip, RNA_Strip, "pitch", 0, &driven);
215 if (fcu || driven) {
216 strip->flag |= SEQ_AUDIO_PITCH_ANIMATED;
217 }
218 else {
219 strip->flag &= ~SEQ_AUDIO_PITCH_ANIMATED;
220 }
221
222 fcu = id_data_find_fcurve(&scene->id, strip, RNA_Strip, "pan", 0, &driven);
223 if (fcu || driven) {
224 strip->flag |= SEQ_AUDIO_PAN_ANIMATED;
225 }
226 else {
227 strip->flag &= ~SEQ_AUDIO_PAN_ANIMATED;
228 }
229
230 if (strip->type == STRIP_TYPE_SCENE) {
231 /* TODO(sergey): For now we do manual recursion into the scene strips,
232 * but perhaps it should be covered by recursive_apply?
233 */
234 sound_update_animation_flags(strip->scene);
235 }
236
237 return true;
238}
239
241{
242 const FCurve *fcu;
243 bool driven;
244
245 if (scene->id.tag & ID_TAG_DOIT) {
246 return;
247 }
248 scene->id.tag |= ID_TAG_DOIT;
249
250 if (scene->ed != nullptr) {
252 }
253
254 fcu = id_data_find_fcurve(&scene->id, scene, RNA_Scene, "audio_volume", 0, &driven);
255 if (fcu || driven) {
257 }
258 else {
260 }
261}
262
272
274{
275 /*
276 * This operator is needed to set a correct state of the sound animation
277 * System. Unfortunately there's no really correct place to call the exec
278 * function, that's why I made it an operator that's only visible in the
279 * search menu. Apart from that the bake animation operator calls it too.
280 */
281
282 /* identifiers */
283 ot->name = "Update Animation";
284 ot->description = "Update animation flags";
285 ot->idname = "SOUND_OT_update_animation_flags";
286
287 /* API callbacks. */
289
290 /* flags */
291 ot->flag = OPTYPE_REGISTER;
292}
293
294/* ******************************************************* */
295
297{
298 Scene *scene = CTX_data_scene(C);
299 /* NOTE: We will be forcefully evaluating dependency graph at every frame, so no need to ensure
300 * current scene state is evaluated as it will be lost anyway. */
301 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
302 int oldfra = scene->r.cfra;
303 int cfra;
304
306
307 for (cfra = (scene->r.sfra > 0) ? (scene->r.sfra - 1) : 0; cfra <= scene->r.efra + 1; cfra++) {
308 scene->r.cfra = cfra;
309 /* Make sure the camera is updated, since it will affect stereo/surround output. */
312 }
313
314 scene->r.cfra = oldfra;
316
317 return OPERATOR_FINISHED;
318}
319
321{
322 /* identifiers */
323 ot->name = "Update Animation Cache";
324 ot->description = "Update the audio animation cache";
325 ot->idname = "SOUND_OT_bake_animation";
326
327 /* API callbacks. */
329
330 /* flags */
331 ot->flag = OPTYPE_REGISTER;
332}
333
334/******************** mixdown operator ********************/
335
336#ifdef WITH_AUDASPACE
337static bool sound_mixdown_progress(float progress, void *data)
338{
339 wmJobWorkerStatus *worker_status = static_cast<wmJobWorkerStatus *>(data);
340 worker_status->progress = progress;
341 worker_status->do_update = true;
342 return G.is_break == false && worker_status->stop == false;
343}
344
345struct SoundMixdownJobData {
346 wmWindowManager *wm = nullptr;
347 bool interface_locked = false;
348
349 Scene *scene_eval = nullptr;
350 std::string filepath;
351 aud::Container container{};
352 aud::Codec codec{};
353 aud::DeviceSpecs specs{};
354 int bitrate = 0;
355 int accuracy = 0;
356 bool split = false;
357
358 bool succeeded = false;
359 std::string error_message;
360};
361
362static void sound_mixdown_startjob(void *customdata, wmJobWorkerStatus *worker_status)
363{
364 SoundMixdownJobData *mixdown_job_data = static_cast<SoundMixdownJobData *>(customdata);
365
366 Scene *scene_eval = mixdown_job_data->scene_eval;
367 const char *filepath = mixdown_job_data->filepath.c_str();
368 aud::Container container = mixdown_job_data->container;
369 aud::Codec codec = mixdown_job_data->codec;
370 int bitrate = mixdown_job_data->bitrate;
371 int accuracy = mixdown_job_data->accuracy;
372 aud::DeviceSpecs specs = mixdown_job_data->specs;
373 bool split = mixdown_job_data->split;
374
375 const double fps = scene_eval->frames_per_second();
376 const int start_frame = scene_eval->r.sfra;
377 const int end_frame = scene_eval->r.efra;
378
379 std::string error_message;
380 bool result = bke::sound_mixdown(scene_eval->runtime->audio.sound_scene,
381 start_frame * specs.rate / fps,
382 (end_frame - start_frame + 1) * specs.rate / fps,
383 accuracy,
384 filepath,
385 specs,
386 container,
387 codec,
388 bitrate,
389 split,
390 error_message,
391 sound_mixdown_progress,
392 worker_status);
393
394 BKE_sound_reset_scene_specs(scene_eval);
395
396 mixdown_job_data->succeeded = result;
397 mixdown_job_data->error_message = error_message;
398}
399
400static void sound_mixdown_endjob_ex(SoundMixdownJobData *mixdown_job_data, ReportList *reports)
401{
402 if (mixdown_job_data->interface_locked) {
403 WM_locked_interface_set(mixdown_job_data->wm, false);
404 }
405
406 if (!mixdown_job_data->succeeded) {
407 if (reports) {
408 BKE_report(reports, RPT_ERROR, mixdown_job_data->error_message.c_str());
409 }
410 else {
411 WM_global_report(RPT_ERROR, mixdown_job_data->error_message.c_str());
412 }
413 }
414}
415
416static void sound_mixdown_endjob(void *customdata)
417{
418 SoundMixdownJobData *mixdown_job_data = static_cast<SoundMixdownJobData *>(customdata);
419 sound_mixdown_endjob_ex(mixdown_job_data, nullptr);
420}
421#endif
422
424{
425#ifdef WITH_AUDASPACE
426 if (!RNA_struct_property_is_set_ex(op->ptr, "filepath", false)) {
427 BKE_report(op->reports, RPT_ERROR, "No filepath given");
428 return OPERATOR_CANCELLED;
429 }
430
431 char filepath[FILE_MAX];
432 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
433 Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
434 Main *bmain = CTX_data_main(C);
436
437 int bitrate, accuracy;
438 aud::DeviceSpecs specs;
439
441
442 RNA_string_get(op->ptr, "filepath", filepath);
443 bitrate = RNA_int_get(op->ptr, "bitrate") * 1000;
444 accuracy = RNA_int_get(op->ptr, "accuracy");
445 specs.format = aud::SampleFormat(RNA_enum_get(op->ptr, "format"));
446 aud::Container container = aud::Container(RNA_enum_get(op->ptr, "container"));
447 aud::Codec codec = aud::Codec(RNA_enum_get(op->ptr, "codec"));
448 bool split = RNA_boolean_get(op->ptr, "split_channels");
449 specs.channels = aud::Channels(RNA_enum_get(op->ptr, "channels"));
450 specs.rate = RNA_int_get(op->ptr, "mixrate");
451
452 BLI_path_abs(filepath, BKE_main_blendfile_path(bmain));
453
454 SoundMixdownJobData *mixdown_job_data = MEM_new<SoundMixdownJobData>(__func__);
455 mixdown_job_data->wm = wm;
456 mixdown_job_data->scene_eval = scene_eval;
457 mixdown_job_data->filepath = filepath;
458 mixdown_job_data->container = container;
459 mixdown_job_data->codec = codec;
460 mixdown_job_data->specs = specs;
461 mixdown_job_data->bitrate = bitrate;
462 mixdown_job_data->accuracy = accuracy;
463 mixdown_job_data->split = split;
464
465 if (scene_eval->r.use_lock_interface) {
466 WM_locked_interface_set(mixdown_job_data->wm, true);
467 /* Save the state in the main thread to avoid any issues if the user messes with the setting
468 * during rendering. */
469 mixdown_job_data->interface_locked = true;
470 }
471
472 if (op->flag & OP_IS_INVOKE) {
473 /* Non blocking call. For when the operator has been called from the GUI. */
474 wmJob *wm_job = WM_jobs_get(wm,
477 "Rendering Audio...",
480
481 WM_jobs_customdata_set(wm_job, mixdown_job_data, [](void *j) {
482 MEM_delete(static_cast<SoundMixdownJobData *>(j));
483 });
485 WM_jobs_callbacks(wm_job, sound_mixdown_startjob, nullptr, nullptr, sound_mixdown_endjob);
486
487 WM_jobs_start(wm, wm_job);
488 }
489 else {
490 /* This is called directly from the exec operator, this operation is now blocking. */
491 wmJobWorkerStatus worker_status = {};
492
493 sound_mixdown_startjob(mixdown_job_data, &worker_status);
494 sound_mixdown_endjob_ex(mixdown_job_data, op->reports);
495 const bool succeeded = mixdown_job_data->succeeded;
496
497 MEM_delete(mixdown_job_data);
498
499 if (!succeeded) {
500 return OPERATOR_CANCELLED;
501 }
502 }
503
504#else /* WITH_AUDASPACE */
505 (void)C;
506 (void)op;
507#endif /* WITH_AUDASPACE */
508 return OPERATOR_FINISHED;
509}
510
511#ifdef WITH_AUDASPACE
512static const EnumPropertyItem container_items[] = {
513# ifdef WITH_FFMPEG
514 {aud::CONTAINER_AAC, "AAC", 0, "AAC", "Advanced Audio Coding"},
515 {aud::CONTAINER_AC3, "AC3", 0, "AC3", "Dolby Digital ATRAC 3"},
516# endif
517 {aud::CONTAINER_FLAC, "FLAC", 0, "FLAC", "Free Lossless Audio Codec"},
518# ifdef WITH_FFMPEG
519 {aud::CONTAINER_MATROSKA, "MATROSKA", 0, "MKV", "Matroska"},
520 {aud::CONTAINER_MP2, "MP2", 0, "MP2", "MPEG-1 Audio Layer II"},
521 {aud::CONTAINER_MP3, "MP3", 0, "MP3", "MPEG-2 Audio Layer III"},
522# endif
523 {aud::CONTAINER_OGG, "OGG", 0, "OGG", "Xiph.Org Ogg Container"},
524 {aud::CONTAINER_WAV, "WAV", 0, "WAV", "Waveform Audio File Format"},
525 {0, nullptr, 0, nullptr, nullptr},
526};
527
528static const char *snd_ext_sound[] = {
529 ".ac3",
530 ".flac",
531 ".mkv",
532 ".mp2",
533 ".mp3",
534 ".ogg",
535 ".wav",
536 ".aac",
537 nullptr,
538};
539
540static bool sound_mixdown_check(bContext * /*C*/, wmOperator *op)
541{
542 aud::Container container = aud::Container(RNA_enum_get(op->ptr, "container"));
543
544 const char *extension = nullptr;
545
546 const EnumPropertyItem *item = container_items;
547 while (item->identifier != nullptr) {
548 if (item->value == container) {
549 const char **ext = snd_ext_sound;
550 while (*ext != nullptr) {
551 if (STRCASEEQ(*ext + 1, item->name)) {
552 extension = *ext;
553 break;
554 }
555
556 ext++;
557 }
558 }
559 item++;
560 }
561
562 if (extension) {
563 PropertyRNA *prop;
564 char filepath[FILE_MAX];
565
566 int check;
567
568 prop = RNA_struct_find_property(op->ptr, "filepath");
569 RNA_property_string_get(op->ptr, prop, filepath);
570
571 if (BLI_path_extension_check_array(filepath, snd_ext_sound)) {
572 check = BLI_path_extension_replace(filepath, FILE_MAX, extension);
573 }
574 else {
575 check = BLI_path_extension_ensure(filepath, FILE_MAX, extension);
576 }
577
578 if (!check) {
579 return check;
580 }
581
582 RNA_property_string_set(op->ptr, prop, filepath);
583 return true;
584 }
585
586 return false;
587}
588
589#endif /* WITH_AUDASPACE */
590
592{
593 if (RNA_struct_property_is_set(op->ptr, "filepath")) {
594 return sound_mixdown_exec(C, op);
595 }
596
597 return WM_operator_filesel(C, op, event);
598}
599
600#ifdef WITH_AUDASPACE
601
602static bool sound_mixdown_draw_check_prop(PointerRNA * /*ptr*/,
603 PropertyRNA *prop,
604 void * /*user_data*/)
605{
606 const char *prop_id = RNA_property_identifier(prop);
607 return !STR_ELEM(prop_id, "filepath", "directory", "filename");
608}
609
610static void sound_mixdown_draw(bContext *C, wmOperator *op)
611{
612 static const EnumPropertyItem pcm_format_items[] = {
613 {aud::FORMAT_U8, "U8", 0, "U8", "8-bit unsigned"},
614 {aud::FORMAT_S16, "S16", 0, "S16", "16-bit signed"},
615# ifdef WITH_SNDFILE
616 {aud::FORMAT_S24, "S24", 0, "S24", "24-bit signed"},
617# endif
618 {aud::FORMAT_S32, "S32", 0, "S32", "32-bit signed"},
619 {aud::FORMAT_FLOAT32, "F32", 0, "F32", "32-bit floating-point"},
620 {aud::FORMAT_FLOAT64, "F64", 0, "F64", "64-bit floating-point"},
621 {0, nullptr, 0, nullptr, nullptr},
622 };
623
624 static const EnumPropertyItem mp3_format_items[] = {
625 {aud::FORMAT_S16, "S16", 0, "S16", "16-bit signed"},
626 {aud::FORMAT_S32, "S32", 0, "S32", "32-bit signed"},
627 {0, nullptr, 0, nullptr, nullptr},
628 };
629
630# ifdef WITH_SNDFILE
631 static const EnumPropertyItem flac_format_items[] = {
632 {aud::FORMAT_S16, "S16", 0, "S16", "16-bit signed"},
633 {aud::FORMAT_S24, "S24", 0, "S24", "24-bit signed"},
634 {0, nullptr, 0, nullptr, nullptr},
635 };
636# endif
637
638 static const EnumPropertyItem all_codec_items[] = {
639 {aud::CODEC_AAC, "AAC", 0, "AAC", "Advanced Audio Coding"},
640 {aud::CODEC_AC3, "AC3", 0, "AC3", "Dolby Digital ATRAC 3"},
641 {aud::CODEC_FLAC, "FLAC", 0, "FLAC", "Free Lossless Audio Codec"},
642 {aud::CODEC_MP2, "MP2", 0, "MP2", "MPEG-1 Audio Layer II"},
643 {aud::CODEC_MP3, "MP3", 0, "MP3", "MPEG-2 Audio Layer III"},
644 {aud::CODEC_PCM, "PCM", 0, "PCM", "Pulse Code Modulation (RAW)"},
645 {aud::CODEC_OPUS, "OPUS", 0, "Opus", "Opus Interactive Audio Codec"},
646 {aud::CODEC_VORBIS, "VORBIS", 0, "Vorbis", "Xiph.Org Vorbis Codec"},
647 {0, nullptr, 0, nullptr, nullptr},
648 };
649
650 static const EnumPropertyItem ogg_codec_items[] = {
651 {aud::CODEC_FLAC, "FLAC", 0, "FLAC", "Free Lossless Audio Codec"},
652 {aud::CODEC_OPUS, "OPUS", 0, "Opus", "Opus Interactive Audio Codec"},
653 {aud::CODEC_VORBIS, "VORBIS", 0, "Vorbis", "Xiph.Org Vorbis Codec"},
654 {0, nullptr, 0, nullptr, nullptr},
655 };
656
657 ui::Layout &layout = *op->layout;
659 PropertyRNA *prop_format;
660 PropertyRNA *prop_codec;
661 PropertyRNA *prop_bitrate;
662
663 layout.use_property_split_set(true);
664 layout.use_property_decorate_set(false);
665
666 aud::Container container = aud::Container(RNA_enum_get(op->ptr, "container"));
667 aud::Codec codec = aud::Codec(RNA_enum_get(op->ptr, "codec"));
668
669 prop_format = RNA_struct_find_property(op->ptr, "format");
670 prop_codec = RNA_struct_find_property(op->ptr, "codec");
671 prop_bitrate = RNA_struct_find_property(op->ptr, "bitrate");
672
676
677 switch (container) {
678 case aud::CONTAINER_AAC:
679 RNA_def_property_enum_items(prop_codec, all_codec_items);
680 RNA_enum_set(op->ptr, "codec", aud::CODEC_AAC);
681 RNA_enum_set(op->ptr, "format", aud::FORMAT_FLOAT32);
682 break;
683 case aud::CONTAINER_AC3:
684 RNA_def_property_enum_items(prop_codec, all_codec_items);
685 RNA_enum_set(op->ptr, "codec", aud::CODEC_AC3);
686 RNA_enum_set(op->ptr, "format", aud::FORMAT_FLOAT32);
687 break;
688 case aud::CONTAINER_FLAC:
689 RNA_def_property_flag(prop_bitrate, PROP_HIDDEN);
690 RNA_def_property_enum_items(prop_codec, all_codec_items);
691 RNA_enum_set(op->ptr, "codec", aud::CODEC_FLAC);
692# ifdef WITH_SNDFILE
694 RNA_def_property_enum_items(prop_format, flac_format_items);
695# else
696 RNA_enum_set(op->ptr, "format", aud::FORMAT_S16);
697# endif
698 break;
699 case aud::CONTAINER_MATROSKA:
701 RNA_def_property_enum_items(prop_codec, all_codec_items);
702
703 switch (codec) {
704 case aud::CODEC_AAC:
705 RNA_enum_set(op->ptr, "format", aud::FORMAT_S16);
706 break;
707 case aud::CODEC_AC3:
708 RNA_enum_set(op->ptr, "format", aud::FORMAT_FLOAT32);
709 break;
710 case aud::CODEC_FLAC:
711 RNA_def_property_flag(prop_bitrate, PROP_HIDDEN);
712 RNA_enum_set(op->ptr, "format", aud::FORMAT_S16);
713 break;
714 case aud::CODEC_MP2:
715 RNA_enum_set(op->ptr, "format", aud::FORMAT_S16);
716 break;
717 case aud::CODEC_MP3:
718 RNA_def_property_enum_items(prop_format, mp3_format_items);
720 break;
721 case aud::CODEC_PCM:
722 RNA_def_property_flag(prop_bitrate, PROP_HIDDEN);
723 RNA_def_property_enum_items(prop_format, pcm_format_items);
725 break;
726 case aud::CODEC_VORBIS:
727 RNA_enum_set(op->ptr, "format", aud::FORMAT_S16);
728 break;
729 default:
730 break;
731 }
732
733 break;
734 case aud::CONTAINER_MP2:
735 RNA_enum_set(op->ptr, "format", aud::FORMAT_S16);
736 RNA_enum_set(op->ptr, "codec", aud::CODEC_MP2);
737 RNA_def_property_enum_items(prop_codec, all_codec_items);
738 break;
739 case aud::CONTAINER_MP3:
741 RNA_def_property_enum_items(prop_format, mp3_format_items);
742 RNA_def_property_enum_items(prop_codec, all_codec_items);
743 RNA_enum_set(op->ptr, "codec", aud::CODEC_MP3);
744 break;
745 case aud::CONTAINER_OGG:
747 RNA_def_property_enum_items(prop_codec, ogg_codec_items);
748 RNA_enum_set(op->ptr, "format", aud::FORMAT_S16);
749 break;
750 case aud::CONTAINER_WAV:
751 RNA_def_property_flag(prop_bitrate, PROP_HIDDEN);
753 RNA_def_property_enum_items(prop_format, pcm_format_items);
754 RNA_def_property_enum_items(prop_codec, all_codec_items);
755 RNA_enum_set(op->ptr, "codec", aud::CODEC_PCM);
756 break;
757 default:
758 break;
759 }
760
761 PointerRNA ptr = RNA_pointer_create_discrete(&wm->id, op->type->srna, op->properties);
762
763 /* main draw call */
764 uiDefAutoButsRNA(&layout,
765 &ptr,
766 sound_mixdown_draw_check_prop,
767 nullptr,
768 nullptr,
770 false);
771}
772#endif /* WITH_AUDASPACE */
773
775{
776#ifdef WITH_AUDASPACE
777 static const EnumPropertyItem format_items[] = {
778 {aud::FORMAT_U8, "U8", 0, "U8", "8-bit unsigned"},
779 {aud::FORMAT_S16, "S16", 0, "S16", "16-bit signed"},
780 {aud::FORMAT_S24, "S24", 0, "S24", "24-bit signed"},
781 {aud::FORMAT_S32, "S32", 0, "S32", "32-bit signed"},
782 {aud::FORMAT_FLOAT32, "F32", 0, "F32", "32-bit floating-point"},
783 {aud::FORMAT_FLOAT64, "F64", 0, "F64", "64-bit floating-point"},
784 {0, nullptr, 0, nullptr, nullptr},
785 };
786
787 static const EnumPropertyItem codec_items[] = {
788# ifdef WITH_FFMPEG
789 {aud::CODEC_AAC, "AAC", 0, "AAC", "Advanced Audio Coding"},
790 {aud::CODEC_AC3, "AC3", 0, "AC3", "Dolby Digital ATRAC 3"},
791# endif
792 {aud::CODEC_FLAC, "FLAC", 0, "FLAC", "Free Lossless Audio Codec"},
793# ifdef WITH_FFMPEG
794 {aud::CODEC_MP2, "MP2", 0, "MP2", "MPEG-1 Audio Layer II"},
795 {aud::CODEC_MP3, "MP3", 0, "MP3", "MPEG-2 Audio Layer III"},
796# endif
797 {aud::CODEC_PCM, "PCM", 0, "PCM", "Pulse Code Modulation (RAW)"},
798 {aud::CODEC_VORBIS, "VORBIS", 0, "Vorbis", "Xiph.Org Vorbis Codec"},
799 {0, nullptr, 0, nullptr, nullptr},
800 };
801
802 static const EnumPropertyItem channel_items[] = {
803 {aud::CHANNELS_MONO, "MONO", 0, "Mono", "Single audio channel"},
804 {aud::CHANNELS_STEREO, "STEREO", 0, "Stereo", "Stereo audio channels"},
805 {aud::CHANNELS_STEREO_LFE, "STEREO_LFE", 0, "Stereo LFE", "Stereo with LFE channel"},
806 {aud::CHANNELS_SURROUND4, "SURROUND4", 0, "4 Channels", "4 channel surround sound"},
807 {aud::CHANNELS_SURROUND5, "SURROUND5", 0, "5 Channels", "5 channel surround sound"},
808 {aud::CHANNELS_SURROUND51, "SURROUND51", 0, "5.1 Surround", "5.1 surround sound"},
809 {aud::CHANNELS_SURROUND61, "SURROUND61", 0, "6.1 Surround", "6.1 surround sound"},
810 {aud::CHANNELS_SURROUND71, "SURROUND71", 0, "7.1 Surround", "7.1 surround sound"},
811 {0, nullptr, 0, nullptr, nullptr}};
812
813#endif /* WITH_AUDASPACE */
814
815 /* identifiers */
816 ot->name = "Mixdown";
817 ot->description = "Mix the scene's audio to a sound file";
818 ot->idname = "SOUND_OT_mixdown";
819
820 /* API callbacks. */
823
824#ifdef WITH_AUDASPACE
825 ot->check = sound_mixdown_check;
826 ot->ui = sound_mixdown_draw;
827#endif
828 /* flags */
829 ot->flag = OPTYPE_REGISTER;
830
831 /* properties */
835 FILE_SAVE,
839#ifdef WITH_AUDASPACE
841 ot->srna,
842 "accuracy",
843 1024,
844 1,
845 16777216,
846 "Accuracy",
847 "Sample accuracy, important for animation data (the lower the value, the more accurate)",
848 1,
849 16777216);
851 ot->srna, "container", container_items, aud::CONTAINER_FLAC, "Container", "File format");
852 RNA_def_enum(ot->srna, "codec", codec_items, aud::CODEC_FLAC, "Codec", "Audio Codec");
853 RNA_def_enum(ot->srna,
854 "channels",
855 channel_items,
856 aud::CHANNELS_STEREO,
857 "Channels",
858 "Audio channel count");
859 RNA_def_enum(ot->srna, "format", format_items, aud::FORMAT_S16, "Format", "Sample format");
860 RNA_def_int(ot->srna,
861 "mixrate",
862 48000,
863 8000,
864 192000,
865 "Sample Rate",
866 "Sample rate in samples/s",
867 8000,
868 192000);
869 RNA_def_int(ot->srna, "bitrate", 192, 32, 512, "Bitrate", "Bitrate in kbit/s", 32, 512);
871 "split_channels",
872 false,
873 "Split channels",
874 "Each channel will be rendered into a mono file");
875#endif /* WITH_AUDASPACE */
876}
877
878/* ******************************************************* */
879
880static bool sound_poll(bContext *C)
881{
883
884 if (!ed || !ed->act_strip || ed->act_strip->type != STRIP_TYPE_SOUND) {
885 return false;
886 }
887
888 return true;
889}
890/********************* pack operator *********************/
891
893{
894 Main *bmain = CTX_data_main(C);
896 bSound *sound;
897
898 if (!ed || !ed->act_strip || ed->act_strip->type != STRIP_TYPE_SOUND) {
899 return OPERATOR_CANCELLED;
900 }
901
902 sound = ed->act_strip->sound;
903
904 if (!sound || sound->packedfile) {
905 return OPERATOR_CANCELLED;
906 }
907
909 op->reports, sound->filepath, ID_BLEND_PATH(bmain, &sound->id));
910
911 DEG_id_tag_update_ex(bmain, &sound->id, ID_RECALC_AUDIO);
912
913 return OPERATOR_FINISHED;
914}
915
917{
918 /* identifiers */
919 ot->name = "Pack Sound";
920 ot->description = "Pack the sound into the current blend file";
921 ot->idname = "SOUND_OT_pack";
922
923 /* API callbacks. */
924 ot->exec = sound_pack_exec;
925 ot->poll = sound_poll;
926
927 /* flags */
929}
930
931/********************* unpack operator *********************/
932
934{
935 Main *bmain = CTX_data_main(C);
936 int method = RNA_enum_get(op->ptr, "method");
937 bSound *sound = nullptr;
938
939 /* find the supplied image by name */
940 if (RNA_struct_property_is_set(op->ptr, "id")) {
941 char sndname[MAX_ID_NAME - 2];
942 RNA_string_get(op->ptr, "id", sndname);
943 sound = static_cast<bSound *>(BLI_findstring(&bmain->sounds, sndname, offsetof(ID, name) + 2));
944 }
945
946 if (!sound || !sound->packedfile) {
947 return OPERATOR_CANCELLED;
948 }
949
950 if (!ID_IS_EDITABLE(&sound->id)) {
951 BKE_report(op->reports, RPT_ERROR, "Sound is not editable");
952 return OPERATOR_CANCELLED;
953 }
954
955 if (G.fileflags & G_FILE_AUTOPACK) {
958 "AutoPack is enabled, so image will be packed again on file save");
959 }
960
961 BKE_packedfile_unpack_sound(bmain, op->reports, sound, ePF_FileStatus(method));
962
963 return OPERATOR_FINISHED;
964}
965
967{
969 bSound *sound;
970
971 if (RNA_struct_property_is_set(op->ptr, "id")) {
972 return sound_unpack_exec(C, op);
973 }
974
975 if (!ed || !ed->act_strip || ed->act_strip->type != STRIP_TYPE_SOUND) {
976 return OPERATOR_CANCELLED;
977 }
978
979 sound = ed->act_strip->sound;
980
981 if (!sound || !sound->packedfile) {
982 return OPERATOR_CANCELLED;
983 }
984
985 if (!ID_IS_EDITABLE(&sound->id)) {
986 BKE_report(op->reports, RPT_ERROR, "Sound is not editable");
987 return OPERATOR_CANCELLED;
988 }
989
990 if (G.fileflags & G_FILE_AUTOPACK) {
993 "AutoPack is enabled, so image will be packed again on file save");
994 }
995
997 C, "SOUND_OT_unpack", sound->id.name + 2, sound->filepath, "sounds", sound->packedfile);
998
999 return OPERATOR_FINISHED;
1000}
1001
1003{
1004 /* identifiers */
1005 ot->name = "Unpack Sound";
1006 ot->description = "Unpack the sound to the samples filename";
1007 ot->idname = "SOUND_OT_unpack";
1008
1009 /* API callbacks. */
1010 ot->exec = sound_unpack_exec;
1012 ot->poll = sound_poll;
1013
1014 /* flags */
1016
1017 /* properties */
1019 ot->srna, "method", rna_enum_unpack_method_items, PF_USE_LOCAL, "Method", "How to unpack");
1020 /* XXX: weak!, will fail with library, name collisions */
1022 ot->srna, "id", nullptr, MAX_ID_NAME - 2, "Sound Name", "Sound data-block name to unpack");
1023}
1024
1025/* ******************************************************* */
1026
1037
1038} // namespace blender
#define FILE_MAX
#define STR_ELEM(...)
#define MAX_ID_NAME
Definition DNA_ID.h:367
#define ID_BLEND_PATH(_bmain, _id)
Definition DNA_ID.h:688
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define ND_SEQUENCER
Definition WM_types.hh:456
#define NC_SCENE
Definition WM_types.hh:391
#define STRCASEEQ(a, b)
Definition scene.h:133
#define offsetof(t, d)
#define G(x, y, z)
void split(const std::string &s, const char delim, std::vector< std::string > &tokens)
Definition abc_util.cc:68
void foreach_strip(ListBaseT< Strip > *seqbase, ForEachFunc callback, void *user_data)
Definition iterator.cc:60
Editing * editing_get(const Scene *scene)
Definition sequencer.cc:304
AutoPropButsReturn uiDefAutoButsRNA(Layout *layout, PointerRNA *ptr, bool(*check_prop)(PointerRNA *ptr, PropertyRNA *prop, void *user_data), void *user_data, PropertyRNA *prop_activate_init, eButLabelAlign label_align, bool compact)
void context_active_but_prop_get_templateID(const bContext *C, PointerRNA *r_ptr, PropertyRNA **r_prop)
BMesh Iterators.
void ED_operatortypes_sound()
void WM_operatortype_append(void(*opfunc)(wmOperatorType *))
static void sound_open_init(bContext *C, wmOperator *op)
Definition sound_ops.cc:70
void BKE_scene_graph_update_for_newframe(Depsgraph *depsgraph)
Definition scene.cc:2953
int RNA_enum_get(PointerRNA *ptr, const char *name)
@ SOUND_FLAGS_CACHING
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
@ RPT_WARNING
Definition BKE_report.hh:40
Scene * CTX_data_sequencer_scene(const bContext *C)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
void WM_jobs_callbacks(wmJob *wm_job, wm_jobs_start_callback startjob, void(*initjob)(void *), void(*update)(void *), void(*endjob)(void *))
Definition wm_jobs.cc:407
void RNA_property_pointer_set(PointerRNA *ptr, PropertyRNA *prop, PointerRNA ptr_value, ReportList *reports)
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
static void sound_update_animation_flags(Scene *scene)
Definition sound_ops.cc:240
wmOperatorStatus WM_operator_filesel(bContext *C, wmOperator *op, const wmEvent *)
void unpack_menu(bContext *C, const char *opname, const char *id_name, const char *abs_name, const char *folder, PackedFile *pf)
Definition ed_util.cc:367
static bool sound_update_animation_flags_fn(Strip *strip, void *user_data)
Definition sound_ops.cc:200
PointerRNA RNA_id_pointer_create(ID *id)
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
const char * RNA_property_identifier(const PropertyRNA *prop)
static void SOUND_OT_open_mono(wmOperatorType *ot)
Definition sound_ops.cc:169
@ WM_JOB_TYPE_SOUND_MIXDOWN
Definition WM_api.hh:2053
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
bool RNA_struct_property_is_set_ex(PointerRNA *ptr, const char *identifier, bool use_ghost)
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:615
std::string RNA_property_string_get(PointerRNA *ptr, PropertyRNA *prop)
void BKE_sound_reset_scene_specs(Scene *scene)
int RNA_int_get(PointerRNA *ptr, const char *name)
wmWindow * CTX_wm_window(const bContext *C)
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
PackedFile * BKE_packedfile_new(ReportList *reports, const char *filepath_rel, const char *basepath)
void WM_jobs_customdata_set(wmJob *wm_job, void *customdata, void(*free)(void *customdata))
Definition wm_jobs.cc:377
bSound * BKE_sound_new_file_exists(Main *bmain, const char *filepath, short stream_index=0)
static void sound_open_cancel(bContext *, wmOperator *op)
Definition sound_ops.cc:64
@ WM_JOB_PROGRESS
Definition WM_api.hh:1997
static wmOperatorStatus sound_pack_exec(bContext *C, wmOperator *op)
Definition sound_ops.cc:892
static wmOperatorStatus sound_unpack_invoke(bContext *C, wmOperator *op, const wmEvent *)
Definition sound_ops.cc:966
void BKE_main_id_tag_idcode(Main *mainvar, short type, int tag, bool value)
Definition lib_id.cc:1307
void WM_operator_properties_filesel(wmOperatorType *ot, const int filter, const short type, const eFileSel_Action action, const eFileSel_Flag flag, const short display, const short sort)
static void SOUND_OT_bake_animation(wmOperatorType *ot)
Definition sound_ops.cc:320
void WM_jobs_start(wmWindowManager *wm, wmJob *wm_job)
Definition wm_jobs.cc:498
Global G
Definition blender.cc:57
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:1058
static wmOperatorStatus sound_mixdown_invoke(bContext *C, wmOperator *op, const wmEvent *event)
Definition sound_ops.cc:591
void WM_locked_interface_set(wmWindowManager *wm, bool lock)
bool BLI_path_extension_ensure(char *path, size_t path_maxncpy, const char *ext) ATTR_NONNULL(1
@ G_FILE_AUTOPACK
void RNA_def_property_enum_items(PropertyRNA *prop, const EnumPropertyItem *item)
void DEG_id_tag_update_ex(Main *bmain, ID *id, unsigned int flags)
void RNA_property_string_set(PointerRNA *ptr, PropertyRNA *prop, const char *value)
@ AUDIO_VOLUME_ANIMATED
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
BMesh const char void * data
static wmOperatorStatus sound_open_exec(bContext *, wmOperator *op)
Definition sound_ops.cc:122
@ WM_FILESEL_RELPATH
Definition WM_api.hh:1329
@ WM_FILESEL_SHOW_PROPS
Definition WM_api.hh:1335
@ WM_FILESEL_FILEPATH
Definition WM_api.hh:1332
wmWindowManager * CTX_wm_manager(const bContext *C)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
static void SOUND_OT_update_animation_flags(wmOperatorType *ot)
Definition sound_ops.cc:273
static wmOperatorStatus sound_unpack_exec(bContext *C, wmOperator *op)
Definition sound_ops.cc:933
static wmOperatorStatus sound_mixdown_exec(bContext *C, wmOperator *op)
Definition sound_ops.cc:423
@ SEQ_AUDIO_VOLUME_ANIMATED
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
const EnumPropertyItem rna_enum_unpack_method_items[]
void WM_global_report(eReportType type, const char *message)
static void SOUND_OT_mixdown(wmOperatorType *ot)
Definition sound_ops.cc:774
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
static bool sound_poll(bContext *C)
Definition sound_ops.cc:880
bool BLI_path_extension_replace(char *path, size_t path_maxncpy, const char *ext) ATTR_NONNULL(1
void RNA_def_property_clear_flag(PropertyRNA *prop, PropertyFlag flag)
static wmOperatorStatus sound_bake_animation_exec(bContext *C, wmOperator *)
Definition sound_ops.cc:296
static void SOUND_OT_pack(wmOperatorType *ot)
Definition sound_ops.cc:916
static wmOperatorStatus sound_open_invoke(bContext *C, wmOperator *op, const wmEvent *event)
Definition sound_ops.cc:131
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
@ FILE_SAVE
Definition WM_api.hh:1342
@ FILE_OPENFILE
Definition WM_api.hh:1341
Main * CTX_data_main(const bContext *C)
wmJob * WM_jobs_get(wmWindowManager *wm, wmWindow *win, const void *owner, const char *name, const eWM_JobFlag flag, const eWM_JobType job_type)
Definition wm_jobs.cc:228
bool BLI_path_extension_check_array(const char *path, const char **ext_array) ATTR_NONNULL(1
bool BKE_scene_camera_switch_update(Scene *scene)
Definition scene.cc:2480
@ ID_RECALC_AUDIO
Definition DNA_ID.h:1132
@ ID_RECALC_SEQUENCER_STRIPS
Definition DNA_ID.h:1122
@ PROP_HIDDEN
Definition RNA_types.hh:408
@ ID_TAG_DOIT
Definition DNA_ID.h:1029
FCurve * id_data_find_fcurve(ID *id, void *data, StructRNA *type, const char *prop_name, int index, bool *r_driven)
static void SOUND_OT_open(wmOperatorType *ot)
Definition sound_ops.cc:142
void DEG_relations_tag_update(Main *bmain)
Scene * CTX_data_scene(const bContext *C)
static void SOUND_OT_unpack(wmOperatorType *ot)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
std::string RNA_string_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
Scene * DEG_get_evaluated_scene(const Depsgraph *graph)
void id_us_min(ID *id)
Definition lib_id.cc:441
void WM_jobs_timer(wmJob *wm_job, double time_step, uint note, uint endnote, void(*timer_step)(void *))
Definition wm_jobs.cc:393
int BKE_packedfile_unpack_sound(Main *bmain, ReportList *reports, bSound *sound, enum ePF_FileStatus how)
static wmOperatorStatus sound_update_animation_flags_exec(bContext *C, wmOperator *)
Definition sound_ops.cc:263
ListBaseT< Strip > seqbase
char name[258]
Definition DNA_ID.h:433
ListBaseT< bSound > sounds
Definition BKE_main.hh:405
struct Editing * ed
struct RenderData r
struct AudioData audio
char filepath[1024]
struct PackedFile * packedfile
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
bool(*) check(bContext *C, wmOperator *op)
Definition WM_types.hh:1133
const char * description
Definition WM_types.hh:1115
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
void(*) ui(bContext *C, wmOperator *op)
Definition WM_types.hh:1176
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
struct ReportList * reports