File: mp3tools.c

package info (click to toggle)
linux-minidisc 0.9.16-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,640 kB
  • sloc: ansic: 6,389; cpp: 2,731; python: 2,537; perl: 866; sh: 207; makefile: 10
file content (80 lines) | stat: -rw-r--r-- 2,602 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * 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 <id3tag.h>
#include "himd.h"
#include "himd_private.h"

static inline char *dup_id3_first_string(union id3_field const *field)
{
    return (char *)id3_ucs4_utf8duplicate(id3_field_getstrings(field, 0));
}

/*
 * 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 = dup_id3_first_string(field);
    else
        *artist = NULL;

    frame = id3_tag_findframe (tag, ID3_FRAME_TITLE, 0);
    if(frame && (field = &frame->fields[1]) &&
                 id3_field_getnstrings(field) > 0)
        *title = dup_id3_first_string(field);
    else
        *title = NULL;

    frame = id3_tag_findframe (tag, ID3_FRAME_ALBUM, 0);
    if(frame && (field = &frame->fields[1]) &&
                 id3_field_getnstrings(field) > 0)
        *album = dup_id3_first_string(field);
    else
        *album = NULL;

    id3_file_close(file);
    return 0;
}