From 829c8248897b6cd2e4ad061f4fd7eed208e689e9 Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Sat, 17 Nov 2012 12:56:23 +0100 Subject: [PATCH 3/5] Moved get_songinfo from himdcli to libhimd So we can reuse it later in the GUI. --- himdcli/himdcli.c | 48 ++-------------------------------- libhimd/himd.h | 6 ++++- libhimd/libhimd.pro | 2 +- libhimd/mp3tools.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 48 deletions(-) create mode 100644 libhimd/mp3tools.c diff --git a/himdcli/himdcli.c b/himdcli/himdcli.c index e677667..3b15678 100644 --- a/himdcli/himdcli.c +++ b/himdcli/himdcli.c @@ -332,51 +332,6 @@ void himd_dumpholes(struct himd * h) printf("%d: %05u-%05u\n", i, holes.holes[i].firstblock, holes.holes[i].lastblock); } -/* - * gets artist, title and album info from an ID3 tag. - * The output strings are to be free()d. - */ -void get_songinfo(const char *filepath, gchar ** artist, gchar ** title, gchar **album) -{ - struct id3_file * file; - struct id3_frame const *frame; - struct id3_tag *tag; - union id3_field const *field; - - file = id3_file_open(filepath, ID3_FILE_MODE_READONLY); - - tag = id3_file_tag(file); - if(!tag) - { - printf("no tags\n"); - id3_file_close(file); - return; - } - - frame = id3_tag_findframe (tag, ID3_FRAME_ARTIST, 0); - if(frame && (field = &frame->fields[1]) && - id3_field_getnstrings(field) > 0) - *artist = (gchar*) id3_ucs4_utf8duplicate(id3_field_getstrings(field,0)); - else - *artist = NULL; - - frame = id3_tag_findframe (tag, ID3_FRAME_TITLE, 0); - if(frame && (field = &frame->fields[1]) && - id3_field_getnstrings(field) > 0) - *title = (gchar*) id3_ucs4_utf8duplicate(id3_field_getstrings(field,0)); - else - *title = NULL; - - frame = id3_tag_findframe (tag, ID3_FRAME_ALBUM, 0); - if(frame && (field = &frame->fields[1]) && - id3_field_getnstrings(field) > 0) - *album = (gchar*) id3_ucs4_utf8duplicate(id3_field_getstrings(field,0)); - else - *album = NULL; - - id3_file_close(file); -} - void block_init(struct blockinfo * b, short int nframes, short int lendata, unsigned int serial_number, unsigned char * cid) { strncpy((char*)&b->type, "SMPA", 4); @@ -626,7 +581,8 @@ void himd_writemp3(struct himd *h, const char *filepath) cid[i] = g_random_int_range(0,0xFF); // Get track ID3 information - get_songinfo(filepath, &artist, &title, &album); + if(himd_get_songinfo(filepath, &artist, &title, &album, &status) < 0) + printf("no tags\n"); // Load mp3 stream mp3file = g_mapped_file_new(filepath, FALSE, NULL); diff --git a/libhimd/himd.h b/libhimd/himd.h index 3f2ade7..6a3aa16 100644 --- a/libhimd/himd.h +++ b/libhimd/himd.h @@ -84,7 +84,8 @@ enum himdstatus { HIMD_OK, HIMD_ERROR_BAD_DATA_FORMAT, HIMD_ERROR_UNSUPPORTED_ENCRYPTION, HIMD_ERROR_ENCRYPTION_FAILURE, - HIMD_ERROR_OUT_OF_MEMORY }; + HIMD_ERROR_OUT_OF_MEMORY, + HIMD_ERROR_NO_ID3_TAGS_FOUND }; enum himd_rw_mode { HIMD_READ_ONLY, HIMD_READ_WRITE }; @@ -260,6 +261,9 @@ struct himd_holelist { int himd_find_holes(struct himd * himd, struct himd_holelist * holes, struct himderrinfo * status); +/* mp3tools.c */ + +int himd_get_songinfo(const char *filepath, char ** artist, char ** title, char **album, struct himderrinfo * status); #ifdef __cplusplus } diff --git a/libhimd/libhimd.pro b/libhimd/libhimd.pro index 58b6f22..6edd8a0 100644 --- a/libhimd/libhimd.pro +++ b/libhimd/libhimd.pro @@ -18,4 +18,4 @@ else: !build_pass: message(You disabled mad: MP3 transfer will be limited) PKGCONFIG += glib-2.0 HEADERS += codecinfo.h himd.h himd_private.h sony_oma.h -SOURCES += codecinfo.c encryption.c himd.c mdstream.c trackindex.c sony_oma.c frag.c +SOURCES += codecinfo.c encryption.c himd.c mdstream.c trackindex.c sony_oma.c frag.c mp3tools.c diff --git a/libhimd/mp3tools.c b/libhimd/mp3tools.c new file mode 100644 index 0000000..7fcb074 --- /dev/null +++ b/libhimd/mp3tools.c @@ -0,0 +1,74 @@ +/* + * mp3tools.c + * + * This file is part of libhimd, a library for accessing Sony HiMD devices. + * + * Copyright (C) 2009-2011 Michael Karcher + * Copyright (C) 2011 MÃ¥rten Cassel + * Copyright (C) 2011 Thomas Arp + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include +#include "himd.h" + +/* + * gets artist, title and album info from an ID3 tag. + * The output strings are to be free()d. + * Returns -1, if id3 informations could be extracted. + */ +int himd_get_songinfo(const char *filepath, char ** artist, char ** title, char **album, struct himderrinfo * status) +{ + struct id3_file * file; + struct id3_frame const *frame; + struct id3_tag *tag; + union id3_field const *field; + + file = id3_file_open(filepath, ID3_FILE_MODE_READONLY); + + tag = id3_file_tag(file); + if(!tag) + { + id3_file_close(file); + set_status_printf(status, HIMD_ERROR_NO_ID3_TAGS_FOUND, "no id3 tags found in file '%s'", filepath); + return -1; + } + + frame = id3_tag_findframe (tag, ID3_FRAME_ARTIST, 0); + if(frame && (field = &frame->fields[1]) && + id3_field_getnstrings(field) > 0) + *artist = id3_ucs4_utf8duplicate(id3_field_getstrings(field,0)); + else + *artist = NULL; + + frame = id3_tag_findframe (tag, ID3_FRAME_TITLE, 0); + if(frame && (field = &frame->fields[1]) && + id3_field_getnstrings(field) > 0) + *title = id3_ucs4_utf8duplicate(id3_field_getstrings(field,0)); + else + *title = NULL; + + frame = id3_tag_findframe (tag, ID3_FRAME_ALBUM, 0); + if(frame && (field = &frame->fields[1]) && + id3_field_getnstrings(field) > 0) + *album = id3_ucs4_utf8duplicate(id3_field_getstrings(field,0)); + else + *album = NULL; + + id3_file_close(file); + return 0; +} -- 1.8.0