00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00031 #include "stdinc.h"
00032
00033 #include <mad.h>
00034 #include <id3tag.h>
00035
00036 #include "audio_file.h"
00037 #include "audio_format.h"
00038 #include "audio_output.h"
00039
00043 struct mp3_drv_data {
00047 struct mad_stream mstream;
00051 struct mad_frame mframe;
00055 struct mad_header mheader;
00059 struct mad_synth msynth;
00063 mad_timer_t mtimer;
00067 int cursample;
00071 off_t flen;
00072
00076 unsigned char buf_input[65536];
00077 };
00078
00079
00080
00081
00082
00086 static int
00087 mp3_match(FILE *fp, const char *ext)
00088 {
00089 unsigned char buf[3];
00090 int ret = 0;
00091
00092
00093 if (ext != NULL && strcmp(ext, "mp3") == 0)
00094 goto done;
00095
00096 if (fread(buf, sizeof buf, 1, fp) != 1) {
00097 ret = -1;
00098 goto done;
00099 }
00100
00101
00102 if (buf[0] == 0xff && (buf[1] & 0xf0) == 0xf0)
00103 goto done;
00104
00105
00106 if (buf[0] == 'I' && buf[1] == 'D' && buf[2] == '3')
00107 goto done;
00108
00109 ret = -1;
00110
00111 done: rewind(fp);
00112 return (ret);
00113 }
00114
00118 static void
00119 mp3_readtags(struct audio_file *fd)
00120 {
00121 int tmpfd;
00122 unsigned int i;
00123 off_t orig_off;
00124 struct id3_file *id3f;
00125 struct id3_tag *tag;
00126 id3_ucs4_t const *str;
00127 char **dst = NULL;
00128
00129
00130 tmpfd = dup(fileno(fd->fp));
00131 if (tmpfd < 0)
00132 return;
00133
00134
00135 orig_off = lseek(tmpfd, 0, SEEK_CUR);
00136
00137 id3f = id3_file_fdopen(tmpfd, ID3_FILE_MODE_READONLY);
00138 if (id3f == NULL)
00139 goto bad;
00140
00141
00142 tag = id3_file_tag(id3f);
00143 if (tag == NULL)
00144 goto done;
00145
00146 for (i = 0; i < tag->nframes; i++) {
00147 if (strncmp("TPE1", tag->frames[i]->id, 4) == 0) {
00148 dst = &fd->artist;
00149 } else if (strncmp("TIT2", tag->frames[i]->id, 4) == 0) {
00150 dst = &fd->title;
00151 #ifdef BUILD_SCROBBLER
00152 } else if (strncmp("TALB", tag->frames[i]->id, 4) == 0) {
00153 dst = &fd->album;
00154 #endif
00155 } else {
00156 continue;
00157 }
00158 if (id3_field_getnstrings(&tag->frames[i]->fields[1]) != 0) {
00159 str = id3_field_getstrings(&tag->frames[i]->fields[1], 0);
00160 if (str != NULL)
00161 *dst = (char*)id3_ucs4_utf8duplicate(str);
00162 }
00163 }
00164
00165 done:
00166 id3_file_close(id3f);
00167 bad:
00168 lseek(tmpfd, orig_off, SEEK_SET);
00169 close(tmpfd);
00170 }
00171
00172
00173
00174
00175
00180 static size_t
00181 mp3_fill_buffer(struct audio_file *fd)
00182 {
00183 struct mp3_drv_data *data = fd->drv_data;
00184 size_t offset, filledlen, readlen;
00185
00186 if (data->mstream.next_frame == NULL) {
00187
00188 offset = 0;
00189 } else {
00190
00191
00192
00193
00194 offset = data->mstream.bufend - data->mstream.next_frame;
00195 memmove(data->buf_input, data->mstream.next_frame, offset);
00196 }
00197
00198 readlen = sizeof data->buf_input - offset;
00199
00200 filledlen = fread(data->buf_input + offset, 1, readlen, fd->fp);
00201
00202 if (filledlen <= 0)
00203 return (0);
00204
00205 if (filledlen < readlen) {
00206
00207
00208
00209
00210 memset(data->buf_input + offset + filledlen, 0x00000000,
00211 MAD_BUFFER_GUARD);
00212 filledlen += MAD_BUFFER_GUARD;
00213 }
00214
00215
00216 return (offset + filledlen);
00217 }
00218
00222 static int
00223 mp3_read_frame(struct audio_file *fd)
00224 {
00225 struct mp3_drv_data *data = fd->drv_data;
00226 size_t buflen;
00227
00228
00229 for (;;) {
00230
00231 if ((data->mstream.buffer == NULL) ||
00232 (data->mstream.error == MAD_ERROR_BUFLEN)) {
00233 if ((buflen = mp3_fill_buffer(fd)) == 0)
00234
00235 return (1);
00236
00237
00238 mad_stream_buffer(&data->mstream, data->buf_input,
00239 buflen);
00240 data->mstream.error = MAD_ERROR_NONE;
00241 }
00242
00243 if (mad_header_decode(&data->mheader, &data->mstream) == 0) {
00244
00245 mad_timer_add(&data->mtimer,
00246 data->mframe.header.duration);
00247 fd->time_cur = data->mtimer.seconds;
00248 data->mframe.header = data->mheader;
00249 return (0);
00250 }
00251
00252 if (!MAD_RECOVERABLE(data->mstream.error) &&
00253 (data->mstream.error != MAD_ERROR_BUFLEN)) {
00254
00255 return (1);
00256 }
00257 }
00258
00259 }
00260
00264 static inline int16_t
00265 mp3_fixed_to_short(mad_fixed_t fixed)
00266 {
00267 if (fixed >= MAD_F_ONE)
00268 return (SHRT_MAX);
00269 else if (fixed <= -MAD_F_ONE)
00270 return (-SHRT_MAX);
00271
00272 return (fixed >> (MAD_F_FRACBITS - 15));
00273 }
00274
00278 static void
00279 mp3_rewind(struct audio_file *fd)
00280 {
00281 struct mp3_drv_data *data = fd->drv_data;
00282
00283 rewind(fd->fp);
00284
00285 data->cursample = 0;
00286 fd->time_cur = 0;
00287
00288 mad_stream_init(&data->mstream);
00289 mad_frame_init(&data->mframe);
00290 mad_header_init(&data->mheader);
00291 mad_synth_init(&data->msynth);
00292 mad_timer_reset(&data->mtimer);
00293 }
00294
00298 static void
00299 mp3_calc_length(struct audio_file *fd)
00300 {
00301 struct mp3_drv_data *data = fd->drv_data;
00302 off_t curpos;
00303
00304
00305 do {
00306 if (mp3_read_frame(fd) != 0) {
00307
00308 fd->time_len = data->mtimer.seconds;
00309 data->flen = ftello(fd->fp);
00310 goto done;
00311 }
00312 } while (data->mtimer.seconds < 100);
00313
00314
00315 curpos = ftello(fd->fp);
00316 fseek(fd->fp, 0, SEEK_END);
00317 data->flen = ftello(fd->fp);
00318
00319 fd->time_len = ((double)data->flen / curpos) *
00320 data->mtimer.seconds;
00321 done:
00322
00323 mp3_rewind(fd);
00324 }
00325
00326
00327
00328
00329
00330 int
00331 mp3_open(struct audio_file *fd, const char *ext)
00332 {
00333 struct mp3_drv_data *data;
00334
00335
00336 if (!fd->stream) {
00337 if (mp3_match(fd->fp, ext) != 0)
00338 return (-1);
00339 mp3_readtags(fd);
00340 }
00341
00342 data = g_slice_new(struct mp3_drv_data);
00343 fd->drv_data = (void *)data;
00344
00345 mp3_rewind(fd);
00346 if (!fd->stream)
00347 mp3_calc_length(fd);
00348
00349 return (0);
00350 }
00351
00352 void
00353 mp3_close(struct audio_file *fd)
00354 {
00355 struct mp3_drv_data *data = fd->drv_data;
00356
00357 mad_frame_finish(&data->mframe);
00358 mad_stream_finish(&data->mstream);
00359 mad_synth_finish(&data->msynth);
00360
00361 g_slice_free(struct mp3_drv_data, data);
00362 }
00363
00364 size_t
00365 mp3_read(struct audio_file *fd, int16_t *buf, size_t len)
00366 {
00367 struct mp3_drv_data *data = fd->drv_data;
00368 size_t written = 0;
00369 int i;
00370
00371 do {
00372
00373 if (data->cursample == 0) {
00374 if (mp3_read_frame(fd) != 0)
00375 goto done;
00376 if (mad_frame_decode(&data->mframe, &data->mstream) == -1)
00377 continue;
00378
00379
00380 fd->srate = data->mframe.header.samplerate;
00381 fd->channels = MAD_NCHANNELS(&data->mframe.header);
00382
00383 mad_synth_frame(&data->msynth, &data->mframe);
00384 }
00385
00386 while ((data->cursample < data->msynth.pcm.length) &&
00387 (written < len)) {
00388
00389 for (i = 0; i < MAD_NCHANNELS(&data->mframe.header); i++) {
00390 buf[written++] = mp3_fixed_to_short(
00391 data->msynth.pcm.samples[i][data->cursample]);
00392 }
00393
00394 data->cursample++;
00395 }
00396
00397
00398 if (data->cursample == data->msynth.pcm.length)
00399 data->cursample = 0;
00400
00401 } while (written < len);
00402
00403 done:
00404 return (written);
00405 }
00406
00407 void
00408 mp3_seek(struct audio_file *fd, int len, int rel)
00409 {
00410 struct mp3_drv_data *data = fd->drv_data;
00411 off_t newpos;
00412
00413 if (rel) {
00414
00415 len += fd->time_cur;
00416 }
00417
00418
00419 len = CLAMP(len, 0, (int)fd->time_len);
00420 newpos = ((double)len / fd->time_len) * data->flen;
00421
00422
00423 mp3_rewind(fd);
00424 fseek(fd->fp, newpos, SEEK_SET);
00425 data->mtimer.seconds = len;
00426 data->mtimer.fraction = 0;
00427 fd->time_cur = data->mtimer.seconds;
00428 }