From c334bf69a7282254bb80bb2896bd8716930a4adf Mon Sep 17 00:00:00 2001 From: Joerg Mueller Date: Sat, 6 Aug 2011 17:57:20 +0000 Subject: 3D Audio GSoC: Mixdown functionality. * Mixdown possible via libsndfile and ffmpeg! * Fixed some ffmpeg deprecation warnings * Mixdown UI only shows working Container, Codec and Format combinations! * Minor bugs and warnings fixed --- source/blender/editors/sound/sound_ops.c | 287 ++++++++++++++++++++++++++++++- 1 file changed, 279 insertions(+), 8 deletions(-) (limited to 'source/blender/editors/sound/sound_ops.c') diff --git a/source/blender/editors/sound/sound_ops.c b/source/blender/editors/sound/sound_ops.c index 8744ec5dfd6..c6a3663d512 100644 --- a/source/blender/editors/sound/sound_ops.c +++ b/source/blender/editors/sound/sound_ops.c @@ -82,7 +82,7 @@ static void open_init(bContext *C, wmOperator *op) { PropertyPointerRNA *pprop; - + op->customdata= pprop= MEM_callocN(sizeof(PropertyPointerRNA), "OpenPropertyPointerRNA"); uiIDContextProperty(C, &pprop->ptr, &pprop->prop); } @@ -101,7 +101,7 @@ static int open_exec(bContext *C, wmOperator *op) if(!op->customdata) open_init(C, op); - + if (sound==NULL || sound->playback_handle == NULL) { if(op->customdata) MEM_freeN(op->customdata); BKE_report(op->reports, RPT_ERROR, "Unsupported audio format"); @@ -120,15 +120,15 @@ static int open_exec(bContext *C, wmOperator *op) if (RNA_boolean_get(op->ptr, "cache")) { sound_cache(sound, 0); } - + /* hook into UI */ pprop= op->customdata; - + if(pprop->prop) { /* when creating new ID blocks, use is already 1, but RNA * pointer se also increases user, so this compensates it */ sound->id.us--; - + RNA_id_pointer_create(&sound->id, &idptr); RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr); RNA_property_update(C, &pprop->ptr, pprop->prop); @@ -153,12 +153,12 @@ static int open_invoke(bContext *C, wmOperator *op, wmEvent *event) { if(!RNA_property_is_set(op->ptr, "relative_path")) RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS); - + if(RNA_property_is_set(op->ptr, "filepath")) return open_exec(C, op); - + open_init(C, op); - + return WM_operator_filesel(C, op, event); } @@ -181,6 +181,276 @@ void SOUND_OT_open(wmOperatorType *ot) RNA_def_boolean(ot->srna, "cache", FALSE, "Cache", "Cache the sound in memory."); } +/******************** mixdown operator ********************/ + +static int mixdown_exec(bContext *C, wmOperator *op) +{ + char path[FILE_MAX]; + char filename[FILE_MAX]; + Scene *scene; + Main *bmain; + + int bitrate, accuracy; + AUD_DeviceSpecs specs; + AUD_Container container; + AUD_Codec codec; + const char* result; + + RNA_string_get(op->ptr, "filepath", path); + bitrate = RNA_int_get(op->ptr, "bitrate") * 1000; + accuracy = RNA_int_get(op->ptr, "accuracy"); + specs.format = RNA_enum_get(op->ptr, "format"); + container = RNA_enum_get(op->ptr, "container"); + codec = RNA_enum_get(op->ptr, "codec"); + scene = CTX_data_scene(C); + bmain = CTX_data_main(C); + specs.channels = scene->r.ffcodecdata.audio_channels; + specs.rate = scene->r.ffcodecdata.audio_mixrate; + + BLI_strncpy(filename, path, sizeof(filename)); + BLI_path_abs(filename, bmain->name); + + result = AUD_mixdown(scene->sound_scene, SFRA * specs.rate / FPS, (EFRA - SFRA) * specs.rate / FPS, + accuracy, filename, specs, container, codec, bitrate); + + if(result) + { + BKE_report(op->reports, RPT_ERROR, result); + return OPERATOR_CANCELLED; + } + + return OPERATOR_FINISHED; +} + +static int mixdown_invoke(bContext *C, wmOperator *op, wmEvent *event) +{ + if(!RNA_property_is_set(op->ptr, "relative_path")) + RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS); + + if(RNA_property_is_set(op->ptr, "filepath")) + return mixdown_exec(C, op); + + return WM_operator_filesel(C, op, event); +} + +static int mixdown_draw_check_prop(PropertyRNA *prop) +{ + const char *prop_id= RNA_property_identifier(prop); + return !( strcmp(prop_id, "filepath") == 0 || + strcmp(prop_id, "directory") == 0 || + strcmp(prop_id, "filename") == 0 + ); +} + +static void mixdown_draw(bContext *C, wmOperator *op) +{ + static EnumPropertyItem pcm_format_items[] = { + {AUD_FORMAT_U8, "U8", 0, "U8", "8 bit unsigned"}, + {AUD_FORMAT_S16, "S16", 0, "S16", "16 bit signed"}, +#ifdef WITH_SNDFILE + {AUD_FORMAT_S24, "S24", 0, "S24", "24 bit signed"}, +#endif + {AUD_FORMAT_S32, "S32", 0, "S32", "32 bit signed"}, + {AUD_FORMAT_FLOAT32, "F32", 0, "F32", "32 bit floating point"}, + {AUD_FORMAT_FLOAT64, "F64", 0, "F64", "64 bit floating point"}, + {0, NULL, 0, NULL, NULL}}; + + static EnumPropertyItem mp3_format_items[] = { + {AUD_FORMAT_S16, "S16", 0, "S16", "16 bit signed"}, + {AUD_FORMAT_S32, "S32", 0, "S32", "32 bit signed"}, + {0, NULL, 0, NULL, NULL}}; + + static EnumPropertyItem ac3_format_items[] = { + {AUD_FORMAT_S16, "S16", 0, "S16", "16 bit signed"}, + {AUD_FORMAT_FLOAT32, "F32", 0, "F32", "32 bit floating point"}, + {0, NULL, 0, NULL, NULL}}; + +#ifdef WITH_SNDFILE + static EnumPropertyItem flac_format_items[] = { + {AUD_FORMAT_S16, "S16", 0, "S16", "16 bit signed"}, + {AUD_FORMAT_S24, "S24", 0, "S24", "24 bit signed"}, + {0, NULL, 0, NULL, NULL}}; +#endif + + static EnumPropertyItem all_codec_items[] = { + {AUD_CODEC_AAC, "AAC", 0, "AAC", "Advanced Audio Coding"}, + {AUD_CODEC_AC3, "AC3", 0, "AC3", "Dolby Digital ATRAC 3"}, + {AUD_CODEC_FLAC, "FLAC", 0, "FLAC", "Free Lossless Audio Codec"}, + {AUD_CODEC_MP2, "MP2", 0, "MP2", "MPEG-1 Audio Layer II"}, + {AUD_CODEC_MP3, "MP3", 0, "MP3", "MPEG-2 Audio Layer III"}, + {AUD_CODEC_PCM, "PCM", 0, "PCM", "Pulse Code Modulation (RAW)"}, + {AUD_CODEC_VORBIS, "VORBIS", 0, "Vorbis", "Xiph.Org Vorbis Codec"}, + {0, NULL, 0, NULL, NULL}}; + + static EnumPropertyItem ogg_codec_items[] = { + {AUD_CODEC_FLAC, "FLAC", 0, "FLAC", "Free Lossless Audio Codec"}, + {AUD_CODEC_VORBIS, "VORBIS", 0, "Vorbis", "Xiph.Org Vorbis Codec"}, + {0, NULL, 0, NULL, NULL}}; + + uiLayout *layout = op->layout; + wmWindowManager *wm= CTX_wm_manager(C); + PointerRNA ptr; + PropertyRNA *prop_format; + PropertyRNA *prop_codec; + PropertyRNA *prop_bitrate; + + AUD_Container container = RNA_enum_get(op->ptr, "container"); + AUD_Codec codec = RNA_enum_get(op->ptr, "codec"); + + prop_format = RNA_struct_find_property(op->ptr, "format"); + prop_codec = RNA_struct_find_property(op->ptr, "codec"); + prop_bitrate = RNA_struct_find_property(op->ptr, "bitrate"); + + RNA_def_property_clear_flag(prop_bitrate, PROP_HIDDEN); + RNA_def_property_flag(prop_codec, PROP_HIDDEN); + RNA_def_property_flag(prop_format, PROP_HIDDEN); + + switch(container) + { + case AUD_CONTAINER_AC3: + RNA_def_property_clear_flag(prop_format, PROP_HIDDEN); + RNA_def_property_enum_items(prop_format, ac3_format_items); + RNA_def_property_enum_items(prop_codec, all_codec_items); + RNA_enum_set(op->ptr, "codec", AUD_CODEC_AC3); + break; + case AUD_CONTAINER_FLAC: + RNA_def_property_flag(prop_bitrate, PROP_HIDDEN); + RNA_def_property_enum_items(prop_codec, all_codec_items); + RNA_enum_set(op->ptr, "codec", AUD_CODEC_FLAC); +#ifdef WITH_SNDFILE + RNA_def_property_clear_flag(prop_format, PROP_HIDDEN); + RNA_def_property_enum_items(prop_format, flac_format_items); +#else + RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16); +#endif + break; + case AUD_CONTAINER_MATROSKA: + RNA_def_property_clear_flag(prop_codec, PROP_HIDDEN); + RNA_def_property_enum_items(prop_codec, all_codec_items); + + switch(codec) + { + case AUD_CODEC_AAC: + RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16); + break; + case AUD_CODEC_AC3: + RNA_def_property_enum_items(prop_format, ac3_format_items); + RNA_def_property_clear_flag(prop_format, PROP_HIDDEN); + break; + case AUD_CODEC_FLAC: + RNA_def_property_flag(prop_bitrate, PROP_HIDDEN); + RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16); + break; + case AUD_CODEC_MP2: + RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16); + break; + case AUD_CODEC_MP3: + RNA_def_property_enum_items(prop_format, mp3_format_items); + RNA_def_property_clear_flag(prop_format, PROP_HIDDEN); + break; + case AUD_CODEC_PCM: + RNA_def_property_flag(prop_bitrate, PROP_HIDDEN); + RNA_def_property_enum_items(prop_format, pcm_format_items); + RNA_def_property_clear_flag(prop_format, PROP_HIDDEN); + break; + case AUD_CODEC_VORBIS: + RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16); + break; + } + + break; + case AUD_CONTAINER_MP2: + RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16); + RNA_enum_set(op->ptr, "codec", AUD_CODEC_MP2); + RNA_def_property_enum_items(prop_codec, all_codec_items); + break; + case AUD_CONTAINER_MP3: + RNA_def_property_clear_flag(prop_format, PROP_HIDDEN); + RNA_def_property_enum_items(prop_format, mp3_format_items); + RNA_def_property_enum_items(prop_codec, all_codec_items); + RNA_enum_set(op->ptr, "codec", AUD_CODEC_MP3); + break; + case AUD_CONTAINER_OGG: + RNA_def_property_clear_flag(prop_codec, PROP_HIDDEN); + RNA_def_property_enum_items(prop_codec, ogg_codec_items); + RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16); + break; + case AUD_CONTAINER_WAV: + RNA_def_property_flag(prop_bitrate, PROP_HIDDEN); + RNA_def_property_clear_flag(prop_format, PROP_HIDDEN); + RNA_def_property_enum_items(prop_format, pcm_format_items); + RNA_def_property_enum_items(prop_codec, all_codec_items); + RNA_enum_set(op->ptr, "codec", AUD_CODEC_PCM); + break; + } + + RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr); + + /* main draw call */ + uiDefAutoButsRNA(layout, &ptr, mixdown_draw_check_prop, '\0'); +} + +void SOUND_OT_mixdown(wmOperatorType *ot) +{ + static EnumPropertyItem format_items[] = { + {AUD_FORMAT_U8, "U8", 0, "U8", "8 bit unsigned"}, + {AUD_FORMAT_S16, "S16", 0, "S16", "16 bit signed"}, + {AUD_FORMAT_S24, "S24", 0, "S24", "24 bit signed"}, + {AUD_FORMAT_S32, "S32", 0, "S32", "32 bit signed"}, + {AUD_FORMAT_FLOAT32, "F32", 0, "F32", "32 bit floating point"}, + {AUD_FORMAT_FLOAT64, "F64", 0, "F64", "64 bit floating point"}, + {0, NULL, 0, NULL, NULL}}; + + static EnumPropertyItem container_items[] = { +#ifdef WITH_FFMPEG + {AUD_CONTAINER_AC3, "AC3", 0, "ac3", "Dolby Digital ATRAC 3"}, +#endif + {AUD_CONTAINER_FLAC, "FLAC", 0, "flac", "Free Lossless Audio Codec"}, +#ifdef WITH_FFMPEG + {AUD_CONTAINER_MATROSKA, "MATROSKA", 0, "mkv", "Matroska"}, + {AUD_CONTAINER_MP2, "MP2", 0, "mp2", "MPEG-1 Audio Layer II"}, + {AUD_CONTAINER_MP3, "MP3", 0, "mp3", "MPEG-2 Audio Layer III"}, +#endif + {AUD_CONTAINER_OGG, "OGG", 0, "ogg", "Xiph.Org Ogg Container"}, + {AUD_CONTAINER_WAV, "WAV", 0, "wav", "Waveform Audio File Format"}, + {0, NULL, 0, NULL, NULL}}; + + static EnumPropertyItem codec_items[] = { +#ifdef WITH_FFMPEG + {AUD_CODEC_AAC, "AAC", 0, "AAC", "Advanced Audio Coding"}, + {AUD_CODEC_AC3, "AC3", 0, "AC3", "Dolby Digital ATRAC 3"}, +#endif + {AUD_CODEC_FLAC, "FLAC", 0, "FLAC", "Free Lossless Audio Codec"}, +#ifdef WITH_FFMPEG + {AUD_CODEC_MP2, "MP2", 0, "MP2", "MPEG-1 Audio Layer II"}, + {AUD_CODEC_MP3, "MP3", 0, "MP3", "MPEG-2 Audio Layer III"}, +#endif + {AUD_CODEC_PCM, "PCM", 0, "PCM", "Pulse Code Modulation (RAW)"}, + {AUD_CODEC_VORBIS, "VORBIS", 0, "Vorbis", "Xiph.Org Vorbis Codec"}, + {0, NULL, 0, NULL, NULL}}; + + /* identifiers */ + ot->name= "Mixdown"; + ot->description= "Mixes the scene's audio to a sound file"; + ot->idname= "SOUND_OT_mixdown"; + + /* api callbacks */ + ot->exec= mixdown_exec; + ot->invoke= mixdown_invoke; + ot->ui= mixdown_draw; + + /* flags */ + ot->flag= OPTYPE_REGISTER; + + /* properties */ + WM_operator_properties_filesel(ot, FOLDERFILE|SOUNDFILE, FILE_SPECIAL, FILE_SAVE, WM_FILESEL_FILEPATH); + RNA_def_int(ot->srna, "accuracy", 1024, 1, 16777216, "Accuracy", "Sample accuracy. Important for animation data. The lower the value, the more accurate.", 1, 16777216); + RNA_def_enum(ot->srna, "container", container_items, AUD_CONTAINER_FLAC, "Container", "File format"); + RNA_def_enum(ot->srna, "codec", codec_items, AUD_CODEC_FLAC, "Codec", "Audio Codec"); + RNA_def_enum(ot->srna, "format", format_items, AUD_FORMAT_S16, "Format", "Sample format"); + RNA_def_int(ot->srna, "bitrate", 192, 32, 512, "Bitrate", "Bitrate in kbit/s", 32, 512); +} + /* ******************************************************* */ static int sound_poll(bContext *C) @@ -393,6 +663,7 @@ void SOUND_OT_bake_animation(wmOperatorType *ot) void ED_operatortypes_sound(void) { WM_operatortype_append(SOUND_OT_open); + WM_operatortype_append(SOUND_OT_mixdown); WM_operatortype_append(SOUND_OT_pack); WM_operatortype_append(SOUND_OT_unpack); WM_operatortype_append(SOUND_OT_update_animation_flags); -- cgit v1.2.3