File: mpc.cpp

package info (click to toggle)
libtunepimp 0.5.3-7
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,668 kB
  • ctags: 3,470
  • sloc: ansic: 15,652; cpp: 12,752; sh: 11,929; perl: 1,179; python: 720; makefile: 310; pascal: 33
file content (318 lines) | stat: -rw-r--r-- 9,560 bytes parent folder | download | duplicates (2)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*----------------------------------------------------------------------------

   libtunepimp -- The MusicBrainz tagging library.  
                  Let a thousand taggers bloom!
   
   Copyright (C) 2005 Lukas Lalinsky
   
   This file is part of libtunepimp.

   libtunepimp 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.

   libtunepimp 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 libtunepimp; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

   $Id: mpc.cpp 8497 2006-09-29 21:26:44Z luks $

----------------------------------------------------------------------------*/

#include <string.h>
#include <apetag.h>
#include <mpcfile.h>
#include "metadata.h"
#include "plugin.h"
#ifndef WIN32
#include "../../lib/utf8/utf8util.h"
#endif

using namespace std;

#ifndef WIN32
#define initPlugin mpcInitPlugin
#endif

#define PLUGIN_VERSION "1.1.0"
#ifdef HAVE_MPCDEC
#define PLUGIN_NAME    "Musepack decoder & metadata reader/writer"
#else
#define PLUGIN_NAME    "Musepack metadata reader/writer"
#endif

static char *formats[][2] = {
  { ".mpc", "Musepack audio format" },
};

#define NUM_FORMATS 1

char *mpcErrorString = "";

static void
mpcShutdown()
{
/*  if (strlen(mpcErrorString))
    free(mpcErrorString);*/
}

static const char *
mpcGetVersion()
{
  return PLUGIN_VERSION;
}

static const char *
mpcGetName()
{
  return PLUGIN_NAME;
}

static int
mpcGetNumFormats(void)
{
  return NUM_FORMATS;
}

static int
mpcGetFormat(int i, char ext[TP_EXTENSION_LEN],
		char desc[TP_PLUGIN_DESC_LEN],int *functions)
{
  if (i < 0 || i >= NUM_FORMATS)
    return 0;
  
  strcpy(ext, formats[i][0]);
  strcpy(desc, formats[i][0]);
#ifdef HAVE_MPCDEC
  *functions = TP_PLUGIN_FUNCTION_DECODE | TP_PLUGIN_FUNCTION_METADATA;
#else
  *functions = TP_PLUGIN_FUNCTION_METADATA;
#endif

  return 1;
}

static const char *
mpcGetError()
{
  return mpcErrorString;
}

static int
mpcReadMetadata(metadata_t *mdata, const char *fileName, int flags, const char *encoding)
{
  memset(mdata, 0, sizeof(metadata_t));

#ifndef WIN32
  TagLib::MPC::File f(utf8ToEncoding(fileName, encoding).c_str());
#else
  TagLib::MPC::File f(fileName);
#endif  

  if (f.isOpen() && f.isValid()) {
      
    TagLib::Tag *t = f.tag();
    if (!t)
      return 1;
    
    strcpy(mdata->artist, t->artist().to8Bit(true).c_str());
    strcpy(mdata->track, t->title().to8Bit(true).c_str());
    strcpy(mdata->album, t->album().to8Bit(true).c_str());
    mdata->trackNum = t->track();
    mdata->releaseYear = t->year();

    TagLib::AudioProperties *properties = f.audioProperties();
    if (properties)
      mdata->duration = properties->length() * 1000;
 
    strcpy(mdata->fileFormat, "mpc");

    TagLib::APE::Tag *tag = f.APETag();
    if (!tag)
      return 1;
    
    const TagLib::APE::ItemListMap &items = tag->itemListMap();

    if (!items["TRACK"].isEmpty()) {
        mdata->totalInSet = 0;
        sscanf(items["TRACK"].toString().toCString(true), "%d/%d", &mdata->trackNum, &mdata->totalInSet);
    }
    
    if (!items["MUSICBRAINZ_TRACKID"].isEmpty()) 
      strcpy(mdata->trackId, items["MUSICBRAINZ_TRACKID"].toString().toCString(true));   

    if (!items["MUSICIP_PUID"].isEmpty()) 
      strcpy(mdata->filePUID, items["MUSICIP_PUID"].toString().toCString(true));   

    if (!items["MUSICBRAINZ_ARTISTID"].isEmpty()) 
      strcpy(mdata->artistId, items["MUSICBRAINZ_ARTISTID"].toString().toCString(true));   

    if (!items["MUSICBRAINZ_ALBUMID"].isEmpty()) 
      strcpy(mdata->albumId, items["MUSICBRAINZ_ALBUMID"].toString().toCString(true));   

    if (!items["MUSICBRAINZ_ALBUMARTISTID"].isEmpty()) 
      strcpy(mdata->albumArtistId, items["MUSICBRAINZ_ALBUMARTISTID"].toString().toCString(true));   

    if (!items["MUSICBRAINZ_ALBUMARTIST"].isEmpty()) 
      strcpy(mdata->albumArtist, items["MUSICBRAINZ_ALBUMARTIST"].toString().toCString(true));   

    if (!items["MUSICBRAINZ_ALBUMARTISTSORTNAME"].isEmpty()) 
      strcpy(mdata->albumArtistSortName, items["MUSICBRAINZ_ALBUMARTISTSORTNAME"].toString().toCString(true));   

    if (!items["MUSICBRAINZ_SORTNAME"].isEmpty()) 
      strcpy(mdata->sortName, items["MUSICBRAINZ_SORTNAME"].toString().toCString(true));   

    if (!items["MUSICBRAINZ_ALBUMTYPE"].isEmpty()) 
      mdata->albumType = convertToAlbumType(items["MUSICBRAINZ_ALBUMTYPE"].toString().toCString(true));
    
    if (!items["MUSICBRAINZ_ALBUMSTATUS"].isEmpty()) 
      mdata->albumStatus = convertToAlbumStatus(items["MUSICBRAINZ_ALBUMSTATUS"].toString().toCString(true));
    
    if (!items["YEAR"].isEmpty()) { 
      int year = 0, month = 0, day = 0;
      if (sscanf(items["YEAR"].toString().toCString(true), "%04d-%02d-%02d", &year, &month, &day) > 0) {
        mdata->releaseYear  = year;
        mdata->releaseMonth = month;
        mdata->releaseDay   = day;
      }
    }
    
    if (!items["MUSICBRAINZ_ALBUMRELEASECOUNTRY"].isEmpty()) 
      strcpy(mdata->releaseCountry, items["MUSICBRAINZ_ALBUMRELEASECOUNTRY"].toString().toCString(true));   
  
    if (!items["MUSICBRAINZ_NONALBUM"].isEmpty()) 
      mdata->nonAlbum = items["MUSICBRAINZ_NONALBUM"].toString().toInt();   

    if (!items["MUSICBRAINZ_VARIOUSARTISTS"].isEmpty()) 
      mdata->variousArtist = items["MUSICBRAINZ_VARIOUSARTISTS"].toString().toInt();   

    return 1;
  }
  
  return 0;
}

static int
mpcWriteMetadata(const metadata_t *mdata, const char *fileName, int flags,
		   const char *encoding)
{
  string temp;  
    
#ifndef WIN32
  TagLib::MPC::File f(utf8ToEncoding(fileName, encoding).c_str());
#else
  TagLib::MPC::File f(fileName);
#endif  

  if (f.isOpen() && f.isValid()) {
      
    TagLib::APE::Tag *tag = f.APETag(true);
    
    TagLib::Tag *t = f.tag();
    if (!t)
      return 1;
    
    t->setArtist(TagLib::String(mdata->artist, TagLib::String::UTF8));
    t->setAlbum(TagLib::String(mdata->album, TagLib::String::UTF8));
    t->setTitle(TagLib::String(mdata->track, TagLib::String::UTF8));
    t->setTrack(mdata->trackNum);
    t->setYear(mdata->releaseYear);
    
    tag->addValue("MUSICBRAINZ_TRACKID", TagLib::String(mdata->trackId, TagLib::String::UTF8));
    tag->addValue("MUSICIP_PUID", TagLib::String(mdata->filePUID, TagLib::String::UTF8));
    tag->addValue("MUSICBRAINZ_ARTISTID", TagLib::String(mdata->artistId, TagLib::String::UTF8));
    tag->addValue("MUSICBRAINZ_ALBUMID", TagLib::String(mdata->albumId, TagLib::String::UTF8));
    tag->addValue("MUSICBRAINZ_SORTNAME", TagLib::String(mdata->sortName, TagLib::String::UTF8));
    
    tag->addValue("MUSICBRAINZ_ALBUMARTISTID", TagLib::String(mdata->albumArtistId, TagLib::String::UTF8));
    tag->addValue("MUSICBRAINZ_ALBUMARTIST", TagLib::String(mdata->albumArtist, TagLib::String::UTF8));
    tag->addValue("MUSICBRAINZ_ALBUMARTISTSORTNAME", TagLib::String(mdata->albumArtistSortName, TagLib::String::UTF8));

    convertFromAlbumType(mdata->albumType, temp);
    tag->addValue("MUSICBRAINZ_ALBUMTYPE", TagLib::String(temp, TagLib::String::UTF8));
    
    convertFromAlbumStatus(mdata->albumStatus, temp);
    tag->addValue("MUSICBRAINZ_ALBUMSTATUS", TagLib::String(temp, TagLib::String::UTF8));

    if (mdata->releaseYear > 0) {
      char temp[16];
      if (mdata->releaseMonth > 0) {
        if (mdata->releaseDay > 0) {
          sprintf(temp, "%04d-%02d-%02d", mdata->releaseYear, mdata->releaseMonth, mdata->releaseDay);
        }
        else {
          sprintf(temp, "%04d-%02d", mdata->releaseYear, mdata->releaseMonth);
        }
      }
      else {
          sprintf(temp, "%04d", mdata->releaseYear);
      }
      tag->addValue("YEAR", TagLib::String(temp, TagLib::String::UTF8));
    }
    
    if (mdata->totalInSet > 0) {
      char temp[16];
      sprintf(temp, "%d/%d", mdata->trackNum, mdata->totalInSet);
      tag->addValue("TRACK", TagLib::String(temp, TagLib::String::UTF8));
    }
    
    tag->addValue("MUSICBRAINZ_ALBUMRELEASECOUNTRY", TagLib::String(mdata->releaseCountry, TagLib::String::UTF8));

    tag->addValue("MUSICBRAINZ_NONALBUM", TagLib::String::number(mdata->nonAlbum));
    tag->addValue("MUSICBRAINZ_VARIOUSARTISTS", TagLib::String::number(mdata->variousArtist));
    
    return f.save() ? 1 : 0;
  }
  
  return 0;
}

static unsigned long
mpcGetDuration(const char *fileName, int flags, const char *encoding)
{
  return 0;
}

#ifdef HAVE_MPCDEC
extern "C" void *mpcDecodeStart(const char *fileName, int flags, const char *encoding);
extern "C" int mpcDecodeInfo(void *decode, unsigned long *duration, unsigned int *samplesPerSecond, unsigned int *bitsPerSample, unsigned int *channels);
extern "C" int mpcDecodeRead(void *decode, char *data, int maxBytes);
extern "C" void mpcDecodeEnd(void *decode);
#endif

static Plugin methods = {
  mpcShutdown,
  mpcGetVersion,
  mpcGetName,
  mpcGetNumFormats,
  mpcGetFormat,
  mpcGetError,
  mpcReadMetadata,
  mpcWriteMetadata,
  mpcGetDuration,
#ifdef HAVE_MPCDEC
  mpcDecodeStart,
  mpcDecodeInfo,
  mpcDecodeRead,
  mpcDecodeEnd
#else
  NULL,
  NULL,
  NULL,
  NULL
#endif
};

extern "C" Plugin *
initPlugin()
{
  return &methods;
}