[go: up one dir, main page]

File: songinfo.cc

package info (click to toggle)
imms 3.1.0~svn301-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,320 kB
  • sloc: cpp: 7,810; ansic: 1,923; xml: 252; sh: 189; makefile: 142
file content (205 lines) | stat: -rw-r--r-- 4,983 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 IMMS: Intelligent Multimedia Management System
 Copyright (C) 2001-2009 Michael Grigoriev

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program 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 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
#include "songinfo.h"
#include "strmanip.h"

#include <stdio.h>

#ifdef WITH_ID3LIB
# include <id3/tag.h>

class Mp3Info : public InfoSlave
{
public:
    Mp3Info(const string &filename)
    {
        id3tag.Clear();
        id3tag.Link(filename.c_str());
    }
    virtual string get_artist()
        { return get_text_frame(ID3FID_LEADARTIST); }
    virtual string get_title()
        { return get_text_frame(ID3FID_TITLE); }
    virtual string get_album()
        { return get_text_frame(ID3FID_ALBUM); }
    virtual time_t get_length() { return 0; }
protected:
    string get_text_frame(ID3_FrameID id);
    ID3_Tag id3tag;
};

string Mp3Info::get_text_frame(ID3_FrameID id)
{
    static char buffer[1024];
    ID3_Frame *myFrame = id3tag.Find(id);
    if (myFrame)
    {
        myFrame->Field(ID3FN_TEXT).Get(buffer, 1024);
        return buffer;
    }
    return "";
}
#endif

#ifdef WITH_VORBIS
# include <vorbis/vorbisfile.h>
# include <vorbis/codec.h>

class OggInfo : public InfoSlave
{
public:
    OggInfo(const string &filename);

    virtual string get_artist()
        { return get_comment("artist"); }
    virtual string get_title()
        { return get_comment("title"); }
    virtual string get_album()
        { return get_comment("album"); }
    virtual time_t get_length() { return 0; }

    ~OggInfo();
private:
    string get_comment(const string &id);

    OggVorbis_File vf;
    vorbis_comment *comment;
};

// OggInfo
OggInfo::OggInfo(const string &filename)
{
    comment = 0;

    FILE *fh = fopen(filename.c_str(), "r");
    if (!fh)
        return;

    if (ov_open(fh, &vf, NULL, 0))
        return;

    comment = ov_comment(&vf, -1);
}

OggInfo::~OggInfo()
{
    vorbis_comment_clear(comment);
    ov_clear(&vf);
}

string OggInfo::get_comment(const string &id)
{
    // Why exactly does vorbis_comment_query take a non-const char* ??
    char *content = 0, *tag = const_cast<char*>(id.c_str());
    return (comment &&
            (content = vorbis_comment_query(comment, tag, 0))) ?  content : "";
}
#endif

#ifdef WITH_TAGLIB

#include <fileref.h>
#include <tag.h>

using namespace TagLib;

class TagInfo : public InfoSlave
{
    public:
        TagInfo(const string &filename)
            : fileref(filename.c_str(), false) {}

        virtual string get_artist()
        {
            return !fileref.isNull() && fileref.tag() ? 
                    fileref.tag()->artist().toCString() : "";
        }
        virtual string get_title()
        {
            return !fileref.isNull() && fileref.tag() ? 
                    fileref.tag()->title().toCString() : "";
        }
        virtual string get_album()
        {
            return !fileref.isNull() && fileref.tag() ? 
                    fileref.tag()->album().toCString() : "";
        }
        virtual time_t get_length()
        {
            return !fileref.isNull() && fileref.audioProperties()
                ? fileref.audioProperties()->length() : 0;
        }
        virtual int get_track_num()
        {
            return !fileref.isNull() && fileref.tag() ? 
                    fileref.tag()->track() : 0;
        }
    private:
        FileRef fileref;
};
#endif

// SongInfo
void SongInfo::link(const string &_filename)
{
    if (filename == _filename)
        return;

    filename = _filename;

    delete myslave;
    myslave = 0;

    if (filename.length() > 3)
    {
        string ext = string_tolower(path_get_extension(filename));

        if (false) {}
#ifdef WITH_TAGLIB
        else if (1)
            myslave = new TagInfo(filename);
#endif
#ifdef WITH_ID3LIB
        else if (ext == "mp3")
            myslave = new Mp3Info(filename);
#endif
#ifdef WITH_VORBIS
        else if (ext == "ogg")
            myslave = new OggInfo(filename);
#endif
    }

    if (!myslave)
        myslave = new InfoSlave();
}

string SongInfo::get_artist()
    { return trim(myslave->get_artist()); }

string SongInfo::get_title()
    { return trim(myslave->get_title()); }

string SongInfo::get_album()
    { return trim(myslave->get_album()); }

time_t SongInfo::get_length()
    { return myslave->get_length(); }

int SongInfo::get_track_num()
    { return myslave->get_track_num(); }