1 /* libxmms-flac - XMMS FLAC input plugin
2 * Copyright (C) 2000,2001,2002 Josh Coalson
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include <xmms/plugin.h>
26 #include <xmms/util.h>
27 #include <xmms/configfile.h>
28 #include <xmms/titlestring.h>
30 #ifdef HAVE_LANGINFO_CODESET
35 #include "configure.h"
42 #define min(x,y) ((x)<(y)?(x):(y))
45 FLAC__bool abort_flag;
46 FLAC__bool is_playing;
48 FLAC__bool play_thread_open; /* if true, is_playing must also be true */
49 unsigned total_samples;
50 unsigned bits_per_sample;
53 unsigned length_in_msec;
54 AFormat sample_format;
58 static void FLAC_XMMS__init();
59 static int FLAC_XMMS__is_our_file(char *filename);
60 static void FLAC_XMMS__play_file(char *filename);
61 static void FLAC_XMMS__stop();
62 static void FLAC_XMMS__pause(short p);
63 static void FLAC_XMMS__seek(int time);
64 static int FLAC_XMMS__get_time();
65 static void FLAC_XMMS__cleanup();
66 static void FLAC_XMMS__get_song_info(char *filename, char **title, int *length);
68 static void *play_loop_(void *arg);
69 static FLAC__bool safe_decoder_init_(const char *filename, FLAC__FileDecoder *decoder);
70 static void safe_decoder_finish_(FLAC__FileDecoder *decoder);
71 static void safe_decoder_delete_(FLAC__FileDecoder *decoder);
72 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
73 static void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
74 static void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
80 "FLAC Player v" FLAC__VERSION_STRING,
84 FLAC_XMMS__is_our_file,
99 FLAC_XMMS__get_song_info,
100 NULL, /* file_info_box */
104 #define SAMPLES_PER_WRITE 512
105 static FLAC__byte reservoir_[FLAC__MAX_BLOCK_SIZE * 2 * 2 * 2]; /* *2 for max bytes-per-sample, *2 for max channels, another *2 for overflow */
106 static FLAC__byte output_[FLAC__MAX_BLOCK_SIZE * 2 * 2]; /* *2 for max bytes-per-sample, *2 for max channels */
107 static unsigned reservoir_samples_ = 0;
108 static FLAC__FileDecoder *decoder_ = 0;
109 static file_info_struct file_info_;
110 static pthread_t decode_thread_;
111 static FLAC__bool audio_error_ = false;
113 InputPlugin *get_iplugin_info()
115 flac_ip.description = g_strdup_printf("FLAC Player v%s", FLAC__VERSION_STRING);
119 void FLAC_XMMS__init()
123 flac_cfg.tag_override = FALSE;
124 g_free(flac_cfg.tag_format);
125 flac_cfg.convert_char_set = FALSE;
127 cfg = xmms_cfg_open_default_file();
129 xmms_cfg_read_boolean(cfg, "flac", "tag_override", &flac_cfg.tag_override);
131 if(!xmms_cfg_read_string(cfg, "flac", "tag_format", &flac_cfg.tag_format))
132 flac_cfg.tag_format = g_strdup("%p - %t");
134 xmms_cfg_read_boolean(cfg, "flac", "convert_char_set", &flac_cfg.convert_char_set);
136 if(!xmms_cfg_read_string(cfg, "flac", "file_char_set", &flac_cfg.file_char_set))
137 flac_cfg.file_char_set = get_current_charset();
139 if(!xmms_cfg_read_string(cfg, "flac", "user_char_set", &flac_cfg.user_char_set))
140 flac_cfg.user_char_set = get_current_charset();
142 decoder_ = FLAC__file_decoder_new();
145 int FLAC_XMMS__is_our_file(char *filename)
149 ext = strrchr(filename, '.');
151 if(!strcasecmp(ext, ".flac") || !strcasecmp(ext, ".fla"))
156 void FLAC_XMMS__play_file(char *filename)
161 reservoir_samples_ = 0;
162 audio_error_ = false;
163 file_info_.abort_flag = false;
164 file_info_.is_playing = false;
165 file_info_.eof = false;
166 file_info_.play_thread_open = false;
168 if(0 == (f = fopen(filename, "r")))
175 if(!safe_decoder_init_(filename, decoder_))
178 file_info_.is_playing = true;
180 if(flac_ip.output->open_audio(file_info_.sample_format, file_info_.sample_rate, file_info_.channels) == 0) {
182 safe_decoder_finish_(decoder_);
186 ret = flac_format_song_title(filename);
187 flac_ip.set_info(ret, file_info_.length_in_msec, file_info_.sample_rate * file_info_.channels * file_info_.bits_per_sample, file_info_.sample_rate, file_info_.channels);
191 file_info_.seek_to_in_sec = -1;
192 file_info_.play_thread_open = true;
193 pthread_create(&decode_thread_, NULL, play_loop_, NULL);
196 void FLAC_XMMS__stop()
198 if(file_info_.is_playing) {
199 file_info_.is_playing = false;
200 if(file_info_.play_thread_open) {
201 file_info_.play_thread_open = false;
202 pthread_join(decode_thread_, NULL);
204 flac_ip.output->close_audio();
205 safe_decoder_finish_(decoder_);
209 void FLAC_XMMS__pause(short p)
211 flac_ip.output->pause(p);
214 void FLAC_XMMS__seek(int time)
216 file_info_.seek_to_in_sec = time;
217 file_info_.eof = false;
219 while(file_info_.seek_to_in_sec != -1)
223 int FLAC_XMMS__get_time()
227 if(!file_info_.is_playing || (file_info_.eof && !flac_ip.output->buffer_playing()))
230 return flac_ip.output->output_time();
233 void FLAC_XMMS__cleanup()
235 safe_decoder_delete_(decoder_);
239 void FLAC_XMMS__get_song_info(char *filename, char **title, int *length_in_msec)
241 FLAC__StreamMetadata streaminfo;
246 if(!FLAC__metadata_get_streaminfo(filename, &streaminfo)) {
247 /* @@@ how to report the error? */
249 static const char *errtitle = "Invalid FLAC File: ";
250 *title = g_malloc(strlen(errtitle) + 1 + strlen(filename) + 1 + 1);
251 sprintf(*title, "%s\"%s\"", errtitle, filename);
254 *length_in_msec = -1;
259 *title = flac_format_song_title(filename);
262 *length_in_msec = streaminfo.data.stream_info.total_samples * 10 / (streaminfo.data.stream_info.sample_rate / 100);
265 /***********************************************************************
267 **********************************************************************/
269 void *play_loop_(void *arg)
273 while(file_info_.is_playing) {
274 if(!file_info_.eof) {
275 while(reservoir_samples_ < SAMPLES_PER_WRITE) {
276 if(FLAC__file_decoder_get_state(decoder_) == FLAC__FILE_DECODER_END_OF_FILE) {
277 file_info_.eof = true;
280 else if(!FLAC__file_decoder_process_single(decoder_)) {
281 /*@@@ this should probably be a dialog */
282 fprintf(stderr, "libxmms-flac: READ ERROR processing frame\n");
283 file_info_.eof = true;
287 if(reservoir_samples_ > 0) {
288 const unsigned channels = file_info_.channels;
289 const unsigned bytes_per_sample = (file_info_.bits_per_sample+7)/8;
290 unsigned i, n = min(reservoir_samples_, SAMPLES_PER_WRITE), delta;
291 unsigned bytes = n * bytes_per_sample * channels;
293 for(i = 0; i < bytes; i++)
294 output_[i] = reservoir_[i];
296 for( ; i < reservoir_samples_*bytes_per_sample*channels; i++)
297 reservoir_[i-delta] = reservoir_[i];
298 reservoir_samples_ -= n;
300 flac_ip.add_vis_pcm(flac_ip.output->written_time(), file_info_.sample_format, channels, bytes, output_);
301 while(flac_ip.output->buffer_free() < (int)bytes && file_info_.is_playing && file_info_.seek_to_in_sec == -1)
303 if(file_info_.is_playing && file_info_.seek_to_in_sec == -1)
304 flac_ip.output->write_audio(output_, bytes);
307 file_info_.eof = true;
313 if(file_info_.seek_to_in_sec != -1) {
314 const double distance = (double)file_info_.seek_to_in_sec * 1000.0 / (double)file_info_.length_in_msec;
315 unsigned target_sample = (unsigned)(distance * (double)file_info_.total_samples);
316 if(FLAC__file_decoder_seek_absolute(decoder_, (FLAC__uint64)target_sample)) {
317 flac_ip.output->flush(file_info_.seek_to_in_sec * 1000);
318 file_info_.seek_to_in_sec = -1;
319 file_info_.eof = false;
320 reservoir_samples_ = 0;
325 safe_decoder_finish_(decoder_);
327 /* are these two calls necessary? */
328 flac_ip.output->buffer_free();
329 flac_ip.output->buffer_free();
332 return 0; /* to silence the compiler warning about not returning a value */
335 FLAC__bool safe_decoder_init_(const char *filename, FLAC__FileDecoder *decoder)
340 safe_decoder_finish_(decoder);
342 FLAC__file_decoder_set_md5_checking(decoder, false);
343 FLAC__file_decoder_set_filename(decoder, filename);
344 FLAC__file_decoder_set_write_callback(decoder, write_callback_);
345 FLAC__file_decoder_set_metadata_callback(decoder, metadata_callback_);
346 FLAC__file_decoder_set_error_callback(decoder, error_callback_);
347 FLAC__file_decoder_set_client_data(decoder, &file_info_);
348 if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK)
351 if(!FLAC__file_decoder_process_until_end_of_metadata(decoder))
357 void safe_decoder_finish_(FLAC__FileDecoder *decoder)
359 if(decoder && FLAC__file_decoder_get_state(decoder) != FLAC__FILE_DECODER_UNINITIALIZED)
360 FLAC__file_decoder_finish(decoder);
363 void safe_decoder_delete_(FLAC__FileDecoder *decoder)
366 safe_decoder_finish_(decoder);
367 FLAC__file_decoder_delete(decoder);
371 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
373 file_info_struct *file_info = (file_info_struct *)client_data;
374 const unsigned bps = file_info->bits_per_sample, channels = file_info->channels, wide_samples = frame->header.blocksize;
375 unsigned wide_sample, sample, channel;
376 FLAC__int8 *scbuffer = (FLAC__int8*)reservoir_;
377 FLAC__int16 *ssbuffer = (FLAC__int16*)reservoir_;
381 if(file_info->abort_flag)
382 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
385 for(sample = reservoir_samples_*channels, wide_sample = 0; wide_sample < wide_samples; wide_sample++)
386 for(channel = 0; channel < channels; channel++, sample++)
387 scbuffer[sample] = (FLAC__int8)buffer[channel][wide_sample];
390 for(sample = reservoir_samples_*channels, wide_sample = 0; wide_sample < wide_samples; wide_sample++)
391 for(channel = 0; channel < channels; channel++, sample++)
392 ssbuffer[sample] = (FLAC__int16)buffer[channel][wide_sample];
395 file_info->abort_flag = true;
396 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
399 reservoir_samples_ += wide_samples;
401 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
404 void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
406 file_info_struct *file_info = (file_info_struct *)client_data;
408 if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
409 FLAC__ASSERT(metadata->data.stream_info.total_samples < 0x100000000); /* this plugin can only handle < 4 gigasamples */
410 file_info->total_samples = (unsigned)(metadata->data.stream_info.total_samples&0xffffffff);
411 file_info->bits_per_sample = metadata->data.stream_info.bits_per_sample;
412 file_info->channels = metadata->data.stream_info.channels;
413 file_info->sample_rate = metadata->data.stream_info.sample_rate;
415 if(file_info->bits_per_sample == 8) {
416 file_info->sample_format = FMT_S8;
418 else if(file_info->bits_per_sample == 16) {
419 file_info->sample_format = FMT_S16_NE;
422 file_info->abort_flag = true;
425 file_info->length_in_msec = file_info->total_samples * 10 / (file_info->sample_rate / 100);
429 void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
431 file_info_struct *file_info = (file_info_struct *)client_data;
433 if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
434 file_info->abort_flag = true;