ID-Remap - Step one: core work (cleanup and rework of generic ID datablock handling).
[blender.git] / source / blender / editors / sound / sound_ops.c
index a2189f6237c0b30d060fa20f7bdaa7b0d61a2067..4931426d62e0e1150beba064483e59690df22942 100644 (file)
@@ -39,7 +39,6 @@
 #include "BLI_blenlib.h"
 #include "BLI_utildefines.h"
 
-#include "DNA_anim_types.h"
 #include "DNA_packedFile_types.h"
 #include "DNA_scene_types.h"
 #include "DNA_space_types.h"
@@ -52,6 +51,7 @@
 #include "BKE_global.h"
 #include "BKE_main.h"
 #include "BKE_report.h"
+#include "BKE_library.h"
 #include "BKE_packedFile.h"
 #include "BKE_scene.h"
 #include "BKE_sound.h"
 #include "WM_types.h"
 
 #ifdef WITH_AUDASPACE
-#  include "AUD_C-API.h"
+#  include AUD_SPECIAL_H
 #endif
 
 #include "ED_sound.h"
 #include "ED_util.h"
 
-#include "sound_intern.h"
 
 /******************** open sound operator ********************/
 
-static int sound_open_cancel(bContext *UNUSED(C), wmOperator *op)
+static void sound_open_cancel(bContext *UNUSED(C), wmOperator *op)
 {
        MEM_freeN(op->customdata);
        op->customdata = NULL;
-       return OPERATOR_CANCELLED;
 }
 
 static void sound_open_init(bContext *C, wmOperator *op)
@@ -89,7 +87,7 @@ static void sound_open_init(bContext *C, wmOperator *op)
        PropertyPointerRNA *pprop;
 
        op->customdata = pprop = MEM_callocN(sizeof(PropertyPointerRNA), "OpenPropertyPointerRNA");
-       uiIDContextProperty(C, &pprop->ptr, &pprop->prop);
+       UI_context_active_but_prop_get_templateID(C, &pprop->ptr, &pprop->prop);
 }
 
 #ifdef WITH_AUDASPACE
@@ -103,13 +101,14 @@ static int sound_open_exec(bContext *C, wmOperator *op)
        Main *bmain = CTX_data_main(C);
 
        RNA_string_get(op->ptr, "filepath", path);
-       sound = sound_new_file(bmain, path);
+       sound = BKE_sound_new_file(bmain, path);
 
        if (!op->customdata)
                sound_open_init(C, op);
 
-       if (sound == NULL || sound->playback_handle == NULL) {
+       if (sound->playback_handle == NULL) {
                if (op->customdata) MEM_freeN(op->customdata);
+               BKE_libblock_free(bmain, sound);
                BKE_report(op->reports, RPT_ERROR, "Unsupported audio format");
                return OPERATOR_CANCELLED;
        }
@@ -117,7 +116,7 @@ static int sound_open_exec(bContext *C, wmOperator *op)
        info = AUD_getInfo(sound->playback_handle);
 
        if (info.specs.channels == AUD_CHANNELS_INVALID) {
-               sound_delete(bmain, sound);
+               BKE_libblock_free(bmain, sound);
                if (op->customdata) MEM_freeN(op->customdata);
                BKE_report(op->reports, RPT_ERROR, "Unsupported audio format");
                return OPERATOR_CANCELLED;
@@ -125,11 +124,11 @@ static int sound_open_exec(bContext *C, wmOperator *op)
 
        if (RNA_boolean_get(op->ptr, "mono")) {
                sound->flags |= SOUND_FLAGS_MONO;
-               sound_load(bmain, sound);
+               BKE_sound_load(bmain, sound);
        }
 
        if (RNA_boolean_get(op->ptr, "cache")) {
-               sound_cache(sound);
+               BKE_sound_cache(sound);
        }
 
        /* hook into UI */
@@ -138,14 +137,14 @@ static int sound_open_exec(bContext *C, wmOperator *op)
        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--;
+               id_us_min(&sound->id);
 
                RNA_id_pointer_create(&sound->id, &idptr);
                RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr);
                RNA_property_update(C, &pprop->ptr, pprop->prop);
        }
 
-       if (op->customdata) MEM_freeN(op->customdata);
+       MEM_freeN(op->customdata);
        return OPERATOR_FINISHED;
 }
 
@@ -186,10 +185,11 @@ static void SOUND_OT_open(wmOperatorType *ot)
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
 
        /* properties */
-       WM_operator_properties_filesel(ot, FOLDERFILE | SOUNDFILE | MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE,
-                                      WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY);
-       RNA_def_boolean(ot->srna, "cache", FALSE, "Cache", "Cache the sound in memory");
-       RNA_def_boolean(ot->srna, "mono", FALSE, "Mono", "Mixdown the sound to mono");
+       WM_operator_properties_filesel(
+               ot, FILE_TYPE_FOLDER | FILE_TYPE_SOUND | FILE_TYPE_MOVIE, FILE_SPECIAL, FILE_OPENFILE,
+               WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA);
+       RNA_def_boolean(ot->srna, "cache", false, "Cache", "Cache the sound in memory");
+       RNA_def_boolean(ot->srna, "mono", false, "Mono", "Merge all the sound's channels into one");
 }
 
 static void SOUND_OT_open_mono(wmOperatorType *ot)
@@ -208,10 +208,11 @@ static void SOUND_OT_open_mono(wmOperatorType *ot)
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
 
        /* properties */
-       WM_operator_properties_filesel(ot, FOLDERFILE | SOUNDFILE | MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE,
-                                      WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY);
-       RNA_def_boolean(ot->srna, "cache", FALSE, "Cache", "Cache the sound in memory");
-       RNA_def_boolean(ot->srna, "mono", TRUE, "Mono", "Mixdown the sound to mono");
+       WM_operator_properties_filesel(
+               ot, FILE_TYPE_FOLDER | FILE_TYPE_SOUND | FILE_TYPE_MOVIE, FILE_SPECIAL, FILE_OPENFILE,
+               WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA);
+       RNA_def_boolean(ot->srna, "cache", false, "Cache", "Cache the sound in memory");
+       RNA_def_boolean(ot->srna, "mono", true, "Mono", "Mixdown the sound to mono");
 }
 
 /* ******************************************************* */
@@ -245,7 +246,7 @@ static int sound_update_animation_flags_exec(bContext *C, wmOperator *UNUSED(op)
        }
        SEQ_END
 
-           fcu = id_data_find_fcurve(&scene->id, scene, &RNA_Scene, "audio_volume", 0, &driven);
+       fcu = id_data_find_fcurve(&scene->id, scene, &RNA_Scene, "audio_volume", 0, &driven);
        if (fcu || driven)
                scene->audio.flag |= AUDIO_VOLUME_ANIMATED;
        else
@@ -288,11 +289,11 @@ static int sound_bake_animation_exec(bContext *C, wmOperator *UNUSED(op))
 
        for (cfra = (scene->r.sfra > 0) ? (scene->r.sfra - 1) : 0; cfra <= scene->r.efra + 1; cfra++) {
                scene->r.cfra = cfra;
-               BKE_scene_update_for_newframe(bmain, scene, scene->lay);
+               BKE_scene_update_for_newframe(bmain->eval_ctx, bmain, scene, scene->lay);
        }
 
        scene->r.cfra = oldfra;
-       BKE_scene_update_for_newframe(bmain, scene, scene->lay);
+       BKE_scene_update_for_newframe(bmain->eval_ctx, bmain, scene, scene->lay);
 
        return OPERATOR_FINISHED;
 }
@@ -402,7 +403,7 @@ static bool sound_mixdown_check(bContext *UNUSED(C), wmOperator *op)
                if (item->value == container) {
                        const char **ext = snd_ext_sound;
                        while (*ext != NULL) {
-                               if (!strcmp(*ext + 1, item->name)) {
+                               if (STREQ(*ext + 1, item->name)) {
                                        extension = *ext;
                                        break;
                                }
@@ -431,10 +432,10 @@ static bool sound_mixdown_check(bContext *UNUSED(C), wmOperator *op)
                        return check;
 
                RNA_property_string_set(op->ptr, prop, filepath);
-               return TRUE;
+               return true;
        }
 
-       return FALSE;
+       return false;
 }
 
 #endif // WITH_AUDASPACE
@@ -452,9 +453,9 @@ static int sound_mixdown_invoke(bContext *C, wmOperator *op, const wmEvent *even
 static bool sound_mixdown_draw_check_prop(PointerRNA *UNUSED(ptr), 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);
+       return !(STREQ(prop_id, "filepath") ||
+                STREQ(prop_id, "directory") ||
+                STREQ(prop_id, "filename"));
 }
 
 static void sound_mixdown_draw(bContext *C, wmOperator *op)
@@ -477,12 +478,6 @@ static void sound_mixdown_draw(bContext *C, wmOperator *op)
                {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"},
@@ -528,10 +523,9 @@ static void sound_mixdown_draw(bContext *C, wmOperator *op)
 
        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);
+                       RNA_enum_set(op->ptr, "format", AUD_FORMAT_FLOAT32);
                        break;
                case AUD_CONTAINER_FLAC:
                        RNA_def_property_flag(prop_bitrate, PROP_HIDDEN);
@@ -553,8 +547,7 @@ static void sound_mixdown_draw(bContext *C, wmOperator *op)
                                        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);
+                                       RNA_enum_set(op->ptr, "format", AUD_FORMAT_FLOAT32);
                                        break;
                                case AUD_CODEC_FLAC:
                                        RNA_def_property_flag(prop_bitrate, PROP_HIDDEN);
@@ -661,10 +654,13 @@ static void SOUND_OT_mixdown(wmOperatorType *ot)
        ot->flag = OPTYPE_REGISTER;
 
        /* properties */
-       WM_operator_properties_filesel(ot, FOLDERFILE | SOUNDFILE, FILE_SPECIAL, FILE_SAVE,
-                                      WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY);
+       WM_operator_properties_filesel(
+               ot, FILE_TYPE_FOLDER | FILE_TYPE_SOUND, FILE_SPECIAL, FILE_SAVE,
+               WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA);
 #ifdef WITH_AUDASPACE
-       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_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");
@@ -701,7 +697,7 @@ static int sound_pack_exec(bContext *C, wmOperator *op)
                return OPERATOR_CANCELLED;
 
        sound->packedfile = newPackedFile(op->reports, sound->name, ID_BLEND_PATH(bmain, &sound->id));
-       sound_load(bmain, sound);
+       BKE_sound_load(bmain, sound);
 
        return OPERATOR_FINISHED;
 }
@@ -787,8 +783,8 @@ static void SOUND_OT_unpack(wmOperatorType *ot)
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
 
        /* properties */
-       RNA_def_enum(ot->srna, "method", unpack_method_items, PF_USE_LOCAL, "Method", "How to unpack");
-       RNA_def_string(ot->srna, "id", "", MAX_ID_NAME - 2, "Sound Name", "Sound datablock name to unpack"); /* XXX, weark!, will fail with library, name collisions */
+       RNA_def_enum(ot->srna, "method", rna_enum_unpack_method_items, PF_USE_LOCAL, "Method", "How to unpack");
+       RNA_def_string(ot->srna, "id", NULL, MAX_ID_NAME - 2, "Sound Name", "Sound datablock name to unpack"); /* XXX, weark!, will fail with library, name collisions */
 }
 
 /* ******************************************************* */