From 17c5e9eba32112c7670792d28bcfbe9caa05765a Mon Sep 17 00:00:00 2001 From: hj kim Date: Tue, 26 May 2020 17:02:40 +0900 Subject: [PATCH] Unify Getting and Setting metadata related code for MP3 and Wav MP3 and Wav could have metadata in ID3tag. but Wav only can save metadata in ID3v2. Change-Id: I4da4d7cec1a94222d2bcf11f3d688e8c4a32ff79 --- src/metadata_editor.cpp | 105 ++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 68 deletions(-) diff --git a/src/metadata_editor.cpp b/src/metadata_editor.cpp index 93d79a4..69b798c 100755 --- a/src/metadata_editor.cpp +++ b/src/metadata_editor.cpp @@ -648,19 +648,9 @@ extern "C" int metadata_editor_set_path(metadata_editor_h metadata, const char * return METADATA_EDITOR_ERROR_NONE; } -static int __metadata_editor_get_mp3_metadata(metadata_editor_s *metadata, metadata_editor_attr_e attribute, char **value) +static int __get_ID3_tag(metadata_editor_attr_e attribute, ID3v1::Tag *tag1, ID3v2::Tag *tag2, char **value) { - int ret = METADATA_EDITOR_ERROR_NONE; - - ret = __check_metadata_get_parameter(metadata, value); - metadata_editor_retvm_if(ret != METADATA_EDITOR_ERROR_NONE, ret, "fail to __check_metadata_get_parameter() [%d]", ret); - - // Bring the pointer to actual file type and make tag pointers - auto _file = dynamic_cast(metadata->file); - auto tag1 = _file->ID3v1Tag(); - auto tag2 = _file->ID3v2Tag(); - - switch (attribute) { // Check which one of frame types was given to the function for processing + switch (attribute) { case METADATA_EDITOR_ATTR_ARTIST: return __ID3_getTwixFrameByName(tag1, tag2, "TPE1", value); case METADATA_EDITOR_ATTR_TITLE: return __ID3_getTwixFrameByName(tag1, tag2, "TIT2", value); case METADATA_EDITOR_ATTR_ALBUM: return __ID3_getTwixFrameByName(tag1, tag2, "TALB", value); @@ -680,6 +670,19 @@ static int __metadata_editor_get_mp3_metadata(metadata_editor_s *metadata, metad } } +static int __metadata_editor_get_mp3_metadata(metadata_editor_s *metadata, metadata_editor_attr_e attribute, char **value) +{ + int ret = METADATA_EDITOR_ERROR_NONE; + + ret = __check_metadata_get_parameter(metadata, value); + metadata_editor_retvm_if(ret != METADATA_EDITOR_ERROR_NONE, ret, "fail to __check_metadata_get_parameter() [%d]", ret); + + auto _file = dynamic_cast(metadata->file); + metadata_editor_retvm_if(!_file, METADATA_EDITOR_ERROR_OPERATION_FAILED, "fail to dynamic_cast"); + + return __get_ID3_tag(attribute, _file->ID3v1Tag(), _file->ID3v2Tag(), value); +} + static int __metadata_editor_get_mp4_metadata(metadata_editor_s *metadata, metadata_editor_attr_e attribute, char **value) { int ret = METADATA_EDITOR_ERROR_NONE; @@ -828,24 +831,7 @@ static int __metadata_editor_get_wav_metadata(metadata_editor_s *metadata, metad return METADATA_EDITOR_ERROR_OPERATION_FAILED; } - switch (attribute) { // Check which one of frame types was given to the function for processing - case METADATA_EDITOR_ATTR_ARTIST: return __ID3_getTwixFrameByName(NULL, tag2, "TPE1", value); - case METADATA_EDITOR_ATTR_TITLE: return __ID3_getTwixFrameByName(NULL, tag2, "TIT2", value); - case METADATA_EDITOR_ATTR_ALBUM: return __ID3_getTwixFrameByName(NULL, tag2, "TALB", value); - case METADATA_EDITOR_ATTR_GENRE: return __ID3_getTwixFrameByName(NULL, tag2, "TCON", value); - case METADATA_EDITOR_ATTR_AUTHOR: return __ID3_getTwixFrameByName(NULL, tag2, "TCOM", value); - case METADATA_EDITOR_ATTR_COPYRIGHT: return __ID3_getTwixFrameByName(NULL, tag2, "TCOP", value); - case METADATA_EDITOR_ATTR_DATE: return __ID3_getTwixFrameByName(NULL, tag2, "TDRC", value); - case METADATA_EDITOR_ATTR_DESCRIPTION: return __ID3_getTwixFrameByName(NULL, tag2, "TIT3", value); - case METADATA_EDITOR_ATTR_COMMENT: return __ID3_getTwixFrameByName(NULL, tag2, "COMM", value); - case METADATA_EDITOR_ATTR_TRACK_NUM: return __ID3_getTwixFrameByName(NULL, tag2, "TRCK", value); - case METADATA_EDITOR_ATTR_CONDUCTOR: return __ID3_getTwixFrameByName(NULL, tag2, "TPE3", value); - case METADATA_EDITOR_ATTR_PICTURE_NUM: return __ID3_getNumberOfPictures(tag2, value); - case METADATA_EDITOR_ATTR_UNSYNCLYRICS: return __ID3_getLyricsFrame(tag2, value); - default: - metadata_editor_error("Invalid attribute [%d]", attribute); - return METADATA_EDITOR_ERROR_INVALID_PARAMETER; - } + return __get_ID3_tag(attribute, NULL, tag2, value); } #endif @@ -882,21 +868,11 @@ extern "C" int metadata_editor_get_metadata(metadata_editor_h metadata, metadata } } -static int __metadata_editor_set_mp3_metadata(metadata_editor_s *metadata, metadata_editor_attr_e attribute, const char *value) +static int __set_ID3_tag(metadata_editor_attr_e attribute, ID3v1::Tag *tag1, ID3v2::Tag *tag2, const char *value) { - int ret = METADATA_EDITOR_ERROR_NONE; - - ret = __check_metadata_set_parameter(metadata); - metadata_editor_retvm_if(ret != METADATA_EDITOR_ERROR_NONE, ret, "fail to __check_metadata_set_parameter() [%d]", ret); - - // Bring the pointer to actual file type and make tags pointers - auto _file = (MPEG::File*)metadata->file; - auto tag1 = _file->ID3v1Tag(); - auto tag2 = _file->ID3v2Tag(true); - - metadata_editor_retvm_if(tag2 == NULL, METADATA_EDITOR_ERROR_OPERATION_FAILED, "Error. ID3v2 tag was not created. Can not proceed metadata updating"); + metadata_editor_retvm_if(!tag2, METADATA_EDITOR_ERROR_OPERATION_FAILED, "Error. ID3v2 tag was not created. Can not proceed metadata updating"); - switch (attribute) { // Check which one of frame type was given for processing + switch (attribute) { case METADATA_EDITOR_ATTR_ARTIST: return __ID3_setTwixFrameByName(tag1, tag2, "TPE1", value); case METADATA_EDITOR_ATTR_TITLE: return __ID3_setTwixFrameByName(tag1, tag2, "TIT2", value); case METADATA_EDITOR_ATTR_ALBUM: return __ID3_setTwixFrameByName(tag1, tag2, "TALB", value); @@ -913,6 +889,21 @@ static int __metadata_editor_set_mp3_metadata(metadata_editor_s *metadata, metad metadata_editor_error("Invalid attribute [%d]", attribute); return METADATA_EDITOR_ERROR_INVALID_PARAMETER; } + + return METADATA_EDITOR_ERROR_NONE; +} + +static int __metadata_editor_set_mp3_metadata(metadata_editor_s *metadata, metadata_editor_attr_e attribute, const char *value) +{ + int ret = METADATA_EDITOR_ERROR_NONE; + + ret = __check_metadata_set_parameter(metadata); + metadata_editor_retvm_if(ret != METADATA_EDITOR_ERROR_NONE, ret, "fail to __check_metadata_set_parameter() [%d]", ret); + + auto _file = dynamic_cast(metadata->file); + metadata_editor_retvm_if(!_file, METADATA_EDITOR_ERROR_OPERATION_FAILED, "fail to dynamic_cast"); + + return __set_ID3_tag(attribute, _file->ID3v1Tag(), _file->ID3v2Tag(true), value); } static int __metadata_editor_set_mp4_metadata(metadata_editor_s *metadata, metadata_editor_attr_e attribute, const char *value) @@ -1048,32 +1039,10 @@ static int __metadata_editor_set_wav_metadata(metadata_editor_s *metadata, metad ret = __check_metadata_set_parameter(metadata); metadata_editor_retvm_if(ret != METADATA_EDITOR_ERROR_NONE, ret, "fail to __check_metadata_set_parameter() [%d]", ret); - // Bring the pointer to actual file type and make tags pointers - auto _file = (RIFF::WAV::File*)metadata->file; - auto tag2 = _file->tag(); - // Check if the valid tag pointer exist - if (!tag2) { - metadata_editor_error("Error. ID3v2 tag was not created. Can not proceed metadata updating"); - return METADATA_EDITOR_ERROR_OPERATION_FAILED; - } + auto _file = dynamic_cast(metadata->file); + metadata_editor_retvm_if(!_file, METADATA_EDITOR_ERROR_OPERATION_FAILED, "fail to dynamic_cast"); - switch (attribute) { // Check which one of frame type was given for processing - case METADATA_EDITOR_ATTR_ARTIST: return __ID3v2_setFrameByName(tag2, "TPE1", value); - case METADATA_EDITOR_ATTR_TITLE: return __ID3v2_setFrameByName(tag2, "TIT2", value); - case METADATA_EDITOR_ATTR_ALBUM: return __ID3v2_setFrameByName(tag2, "TALB", value); - case METADATA_EDITOR_ATTR_GENRE: return __ID3v2_setFrameByName(tag2, "TCON", value); - case METADATA_EDITOR_ATTR_AUTHOR: return __ID3v2_setFrameByName(tag2, "TCOM", value); - case METADATA_EDITOR_ATTR_COPYRIGHT: return __ID3v2_setFrameByName(tag2, "TCOP", value); - case METADATA_EDITOR_ATTR_DATE: return __ID3v2_setFrameByName(tag2, "TDRC", value); - case METADATA_EDITOR_ATTR_DESCRIPTION: return __ID3v2_setFrameByName(tag2, "TIT3", value); - case METADATA_EDITOR_ATTR_TRACK_NUM: return __ID3v2_setFrameByName(tag2, "TRCK", value); - case METADATA_EDITOR_ATTR_CONDUCTOR: return __ID3v2_setFrameByName(tag2, "TPE3", value); - case METADATA_EDITOR_ATTR_COMMENT: return __ID3_setTwixCommentFrame(NULL, tag2, value); - case METADATA_EDITOR_ATTR_UNSYNCLYRICS: return __ID3_setLyricsFrame(tag2, value); - default: - metadata_editor_error("Invalid attribute [%d]", attribute); - return METADATA_EDITOR_ERROR_INVALID_PARAMETER; - } + return __set_ID3_tag(attribute, NULL, _file->tag(), value); } #endif -- 2.34.1