1 /* libxmms-flac - XMMS FLAC input plugin
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007,2008,2009 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.
30 #include <sys/types.h>
33 #include <xmms/plugin.h>
34 #include <xmms/util.h>
35 #include <xmms/configfile.h>
36 #include <xmms/titlestring.h>
38 #ifdef HAVE_LANGINFO_CODESET
43 #include "plugin_common/all.h"
44 #include "share/grabbag.h"
45 #include "share/replaygain_synthesis.h"
46 #include "configure.h"
54 #define min(x,y) ((x)<(y)?(x):(y))
56 extern void FLAC_XMMS__file_info_box(char *filename);
59 FLAC__bool abort_flag;
60 FLAC__bool is_playing;
61 FLAC__bool is_http_source;
63 FLAC__bool play_thread_open; /* if true, is_playing must also be true */
64 FLAC__uint64 total_samples;
65 unsigned bits_per_sample;
68 int length_in_msec; /* int (instead of FLAC__uint64) only because that's what XMMS uses; seeking won't work right if this maxes out */
70 AFormat sample_format;
71 unsigned sample_format_bytes_per_sample;
73 FLAC__bool has_replaygain;
75 DitherContext dither_context;
78 static void FLAC_XMMS__init(void);
79 static int FLAC_XMMS__is_our_file(char *filename);
80 static void FLAC_XMMS__play_file(char *filename);
81 static void FLAC_XMMS__stop(void);
82 static void FLAC_XMMS__pause(short p);
83 static void FLAC_XMMS__seek(int time);
84 static int FLAC_XMMS__get_time(void);
85 static void FLAC_XMMS__cleanup(void);
86 static void FLAC_XMMS__get_song_info(char *filename, char **title, int *length);
88 static void *play_loop_(void *arg);
90 static FLAC__bool safe_decoder_init_(const char *filename, FLAC__StreamDecoder *decoder);
91 static void safe_decoder_finish_(FLAC__StreamDecoder *decoder);
92 static void safe_decoder_delete_(FLAC__StreamDecoder *decoder);
94 static FLAC__StreamDecoderReadStatus http_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
95 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
96 static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
97 static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
103 "FLAC Player v" VERSION,
106 FLAC_XMMS__configure,
107 FLAC_XMMS__is_our_file,
109 FLAC_XMMS__play_file,
122 FLAC_XMMS__get_song_info,
123 FLAC_XMMS__file_info_box,
127 #define SAMPLES_PER_WRITE 512
128 #define SAMPLE_BUFFER_SIZE ((FLAC__MAX_BLOCK_SIZE + SAMPLES_PER_WRITE) * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS * (24/8))
129 static FLAC__byte sample_buffer_[SAMPLE_BUFFER_SIZE];
130 static unsigned sample_buffer_first_, sample_buffer_last_;
132 static FLAC__StreamDecoder *decoder_ = 0;
133 static stream_data_struct stream_data_;
134 static pthread_t decode_thread_;
135 static FLAC__bool audio_error_ = false;
136 static FLAC__bool is_big_endian_host_;
138 #define BITRATE_HIST_SEGMENT_MSEC 500
139 /* 500ms * 50 = 25s should be enough */
140 #define BITRATE_HIST_SIZE 50
141 static unsigned bitrate_history_[BITRATE_HIST_SIZE];
144 InputPlugin *get_iplugin_info(void)
146 flac_ip.description = g_strdup_printf("Reference FLAC Player v%s", FLAC__VERSION_STRING);
150 void set_track_info(const char* title, int length_in_msec)
152 if (stream_data_.is_playing) {
153 flac_ip.set_info((char*) title, length_in_msec, stream_data_.sample_rate * stream_data_.channels * stream_data_.bits_per_sample, stream_data_.sample_rate, stream_data_.channels);
157 static gchar* homedir(void)
160 char *env_home = getenv("HOME");
162 result = g_strdup (env_home);
164 uid_t uid = getuid();
165 struct passwd *pwent;
168 } while (pwent && pwent->pw_uid != uid);
169 result = pwent ? g_strdup (pwent->pw_dir) : NULL;
175 static FLAC__bool is_http_source(const char *source)
177 return 0 == strncasecmp(source, "http://", 7);
180 void FLAC_XMMS__init(void)
183 FLAC__uint32 test = 1;
185 is_big_endian_host_ = (*((FLAC__byte*)(&test)))? false : true;
187 flac_cfg.title.tag_override = FALSE;
188 if (flac_cfg.title.tag_format)
189 g_free(flac_cfg.title.tag_format);
190 flac_cfg.title.convert_char_set = FALSE;
192 cfg = xmms_cfg_open_default_file();
196 xmms_cfg_read_boolean(cfg, "flac", "title.tag_override", &flac_cfg.title.tag_override);
198 if(!xmms_cfg_read_string(cfg, "flac", "title.tag_format", &flac_cfg.title.tag_format))
199 flac_cfg.title.tag_format = g_strdup("%p - %t");
201 xmms_cfg_read_boolean(cfg, "flac", "title.convert_char_set", &flac_cfg.title.convert_char_set);
203 if(!xmms_cfg_read_string(cfg, "flac", "title.user_char_set", &flac_cfg.title.user_char_set))
204 flac_cfg.title.user_char_set = FLAC_plugin__charset_get_current();
208 xmms_cfg_read_boolean(cfg, "flac", "output.replaygain.enable", &flac_cfg.output.replaygain.enable);
210 xmms_cfg_read_boolean(cfg, "flac", "output.replaygain.album_mode", &flac_cfg.output.replaygain.album_mode);
212 if(!xmms_cfg_read_int(cfg, "flac", "output.replaygain.preamp", &flac_cfg.output.replaygain.preamp))
213 flac_cfg.output.replaygain.preamp = 0;
215 xmms_cfg_read_boolean(cfg, "flac", "output.replaygain.hard_limit", &flac_cfg.output.replaygain.hard_limit);
217 xmms_cfg_read_boolean(cfg, "flac", "output.resolution.normal.dither_24_to_16", &flac_cfg.output.resolution.normal.dither_24_to_16);
218 xmms_cfg_read_boolean(cfg, "flac", "output.resolution.replaygain.dither", &flac_cfg.output.resolution.replaygain.dither);
220 if(!xmms_cfg_read_int(cfg, "flac", "output.resolution.replaygain.noise_shaping", &flac_cfg.output.resolution.replaygain.noise_shaping))
221 flac_cfg.output.resolution.replaygain.noise_shaping = 1;
223 if(!xmms_cfg_read_int(cfg, "flac", "output.resolution.replaygain.bps_out", &flac_cfg.output.resolution.replaygain.bps_out))
224 flac_cfg.output.resolution.replaygain.bps_out = 16;
228 xmms_cfg_read_int(cfg, "flac", "stream.http_buffer_size", &flac_cfg.stream.http_buffer_size);
229 xmms_cfg_read_int(cfg, "flac", "stream.http_prebuffer", &flac_cfg.stream.http_prebuffer);
230 xmms_cfg_read_boolean(cfg, "flac", "stream.use_proxy", &flac_cfg.stream.use_proxy);
231 if(flac_cfg.stream.proxy_host)
232 g_free(flac_cfg.stream.proxy_host);
233 if(!xmms_cfg_read_string(cfg, "flac", "stream.proxy_host", &flac_cfg.stream.proxy_host))
234 flac_cfg.stream.proxy_host = g_strdup("");
235 xmms_cfg_read_int(cfg, "flac", "stream.proxy_port", &flac_cfg.stream.proxy_port);
236 xmms_cfg_read_boolean(cfg, "flac", "stream.proxy_use_auth", &flac_cfg.stream.proxy_use_auth);
237 if(flac_cfg.stream.proxy_user)
238 g_free(flac_cfg.stream.proxy_user);
239 flac_cfg.stream.proxy_user = NULL;
240 xmms_cfg_read_string(cfg, "flac", "stream.proxy_user", &flac_cfg.stream.proxy_user);
241 if(flac_cfg.stream.proxy_pass)
242 g_free(flac_cfg.stream.proxy_pass);
243 flac_cfg.stream.proxy_pass = NULL;
244 xmms_cfg_read_string(cfg, "flac", "stream.proxy_pass", &flac_cfg.stream.proxy_pass);
245 xmms_cfg_read_boolean(cfg, "flac", "stream.save_http_stream", &flac_cfg.stream.save_http_stream);
246 if (flac_cfg.stream.save_http_path)
247 g_free (flac_cfg.stream.save_http_path);
248 if (!xmms_cfg_read_string(cfg, "flac", "stream.save_http_path", &flac_cfg.stream.save_http_path) || ! *flac_cfg.stream.save_http_path) {
249 if (flac_cfg.stream.save_http_path)
250 g_free (flac_cfg.stream.save_http_path);
251 flac_cfg.stream.save_http_path = homedir();
253 xmms_cfg_read_boolean(cfg, "flac", "stream.cast_title_streaming", &flac_cfg.stream.cast_title_streaming);
254 xmms_cfg_read_boolean(cfg, "flac", "stream.use_udp_channel", &flac_cfg.stream.use_udp_channel);
256 decoder_ = FLAC__stream_decoder_new();
261 int FLAC_XMMS__is_our_file(char *filename)
265 ext = strrchr(filename, '.');
267 if(!strcasecmp(ext, ".flac") || !strcasecmp(ext, ".fla"))
272 void FLAC_XMMS__play_file(char *filename)
276 sample_buffer_first_ = sample_buffer_last_ = 0;
277 audio_error_ = false;
278 stream_data_.abort_flag = false;
279 stream_data_.is_playing = false;
280 stream_data_.is_http_source = is_http_source(filename);
281 stream_data_.eof = false;
282 stream_data_.play_thread_open = false;
283 stream_data_.has_replaygain = false;
285 if(!is_http_source(filename)) {
286 if(0 == (f = fopen(filename, "r")))
294 if(!safe_decoder_init_(filename, decoder_))
297 if(stream_data_.has_replaygain && flac_cfg.output.replaygain.enable) {
298 if(flac_cfg.output.resolution.replaygain.bps_out == 8) {
299 stream_data_.sample_format = FMT_U8;
300 stream_data_.sample_format_bytes_per_sample = 1;
302 else if(flac_cfg.output.resolution.replaygain.bps_out == 16) {
303 stream_data_.sample_format = (is_big_endian_host_) ? FMT_S16_BE : FMT_S16_LE;
304 stream_data_.sample_format_bytes_per_sample = 2;
307 /*@@@ need some error here like wa2: MessageBox(mod_.hMainWindow, "ERROR: plugin can only handle 8/16-bit samples\n", "ERROR: plugin can only handle 8/16-bit samples", 0); */
308 fprintf(stderr, "libxmms-flac: can't handle %d bit output\n", flac_cfg.output.resolution.replaygain.bps_out);
309 safe_decoder_finish_(decoder_);
314 if(stream_data_.bits_per_sample == 8) {
315 stream_data_.sample_format = FMT_U8;
316 stream_data_.sample_format_bytes_per_sample = 1;
318 else if(stream_data_.bits_per_sample == 16 || (stream_data_.bits_per_sample == 24 && flac_cfg.output.resolution.normal.dither_24_to_16)) {
319 stream_data_.sample_format = (is_big_endian_host_) ? FMT_S16_BE : FMT_S16_LE;
320 stream_data_.sample_format_bytes_per_sample = 2;
323 /*@@@ need some error here like wa2: MessageBox(mod_.hMainWindow, "ERROR: plugin can only handle 8/16-bit samples\n", "ERROR: plugin can only handle 8/16-bit samples", 0); */
324 fprintf(stderr, "libxmms-flac: can't handle %d bit output\n", stream_data_.bits_per_sample);
325 safe_decoder_finish_(decoder_);
329 FLAC__replaygain_synthesis__init_dither_context(&stream_data_.dither_context, stream_data_.sample_format_bytes_per_sample * 8, flac_cfg.output.resolution.replaygain.noise_shaping);
330 stream_data_.is_playing = true;
332 if(flac_ip.output->open_audio(stream_data_.sample_format, stream_data_.sample_rate, stream_data_.channels) == 0) {
334 safe_decoder_finish_(decoder_);
338 stream_data_.title = flac_format_song_title(filename);
339 flac_ip.set_info(stream_data_.title, stream_data_.length_in_msec, stream_data_.sample_rate * stream_data_.channels * stream_data_.bits_per_sample, stream_data_.sample_rate, stream_data_.channels);
341 stream_data_.seek_to_in_sec = -1;
342 stream_data_.play_thread_open = true;
343 pthread_create(&decode_thread_, NULL, play_loop_, NULL);
346 void FLAC_XMMS__stop(void)
348 if(stream_data_.is_playing) {
349 stream_data_.is_playing = false;
350 if(stream_data_.play_thread_open) {
351 stream_data_.play_thread_open = false;
352 pthread_join(decode_thread_, NULL);
354 flac_ip.output->close_audio();
355 safe_decoder_finish_(decoder_);
359 void FLAC_XMMS__pause(short p)
361 flac_ip.output->pause(p);
364 void FLAC_XMMS__seek(int time)
366 if(!stream_data_.is_http_source) {
367 stream_data_.seek_to_in_sec = time;
368 stream_data_.eof = false;
370 while(stream_data_.seek_to_in_sec != -1)
375 int FLAC_XMMS__get_time(void)
379 if(!stream_data_.is_playing || (stream_data_.eof && !flac_ip.output->buffer_playing()))
382 return flac_ip.output->output_time();
385 void FLAC_XMMS__cleanup(void)
387 safe_decoder_delete_(decoder_);
391 void FLAC_XMMS__get_song_info(char *filename, char **title, int *length_in_msec)
393 FLAC__StreamMetadata streaminfo;
398 if(!FLAC__metadata_get_streaminfo(filename, &streaminfo)) {
399 /* @@@ how to report the error? */
401 if (!is_http_source(filename)) {
402 static const char *errtitle = "Invalid FLAC File: ";
403 if(strlen(errtitle) + 1 + strlen(filename) + 1 + 1 < strlen(filename)) { /* overflow check */
407 *title = g_malloc(strlen(errtitle) + 1 + strlen(filename) + 1 + 1);
408 sprintf(*title, "%s\"%s\"", errtitle, filename);
415 *length_in_msec = -1;
420 *title = flac_format_song_title(filename);
423 FLAC__uint64 l = (FLAC__uint64)((double)streaminfo.data.stream_info.total_samples / (double)streaminfo.data.stream_info.sample_rate * 1000.0 + 0.5);
426 *length_in_msec = (int)l;
430 /***********************************************************************
432 **********************************************************************/
434 void *play_loop_(void *arg)
436 unsigned written_time_last = 0, bh_index_last_w = 0, bh_index_last_o = BITRATE_HIST_SIZE, blocksize = 1;
437 FLAC__uint64 decode_position_last = 0, decode_position_frame_last = 0, decode_position_frame = 0;
441 while(stream_data_.is_playing) {
442 if(!stream_data_.eof) {
443 while(sample_buffer_last_ - sample_buffer_first_ < SAMPLES_PER_WRITE) {
446 s = sample_buffer_last_ - sample_buffer_first_;
447 if(FLAC__stream_decoder_get_state(decoder_) == FLAC__STREAM_DECODER_END_OF_STREAM) {
448 stream_data_.eof = true;
451 else if(!FLAC__stream_decoder_process_single(decoder_)) {
452 /*@@@ this should probably be a dialog */
453 fprintf(stderr, "libxmms-flac: READ ERROR processing frame\n");
454 stream_data_.eof = true;
457 blocksize = sample_buffer_last_ - sample_buffer_first_ - s;
458 decode_position_frame_last = decode_position_frame;
459 if(stream_data_.is_http_source || !FLAC__stream_decoder_get_decode_position(decoder_, &decode_position_frame))
460 decode_position_frame = 0;
462 if(sample_buffer_last_ - sample_buffer_first_ > 0) {
463 const unsigned n = min(sample_buffer_last_ - sample_buffer_first_, SAMPLES_PER_WRITE);
464 int bytes = n * stream_data_.channels * stream_data_.sample_format_bytes_per_sample;
465 FLAC__byte *sample_buffer_start = sample_buffer_ + sample_buffer_first_ * stream_data_.channels * stream_data_.sample_format_bytes_per_sample;
466 unsigned written_time, bh_index_w;
467 FLAC__uint64 decode_position;
469 sample_buffer_first_ += n;
470 flac_ip.add_vis_pcm(flac_ip.output->written_time(), stream_data_.sample_format, stream_data_.channels, bytes, sample_buffer_start);
471 while(flac_ip.output->buffer_free() < (int)bytes && stream_data_.is_playing && stream_data_.seek_to_in_sec == -1)
473 if(stream_data_.is_playing && stream_data_.seek_to_in_sec == -1)
474 flac_ip.output->write_audio(sample_buffer_start, bytes);
476 /* compute current bitrate */
478 written_time = flac_ip.output->written_time();
479 bh_index_w = written_time / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
480 if(bh_index_w != bh_index_last_w) {
481 bh_index_last_w = bh_index_w;
482 decode_position = decode_position_frame - (double)(sample_buffer_last_ - sample_buffer_first_) * (double)(decode_position_frame - decode_position_frame_last) / (double)blocksize;
483 bitrate_history_[(bh_index_w + BITRATE_HIST_SIZE - 1) % BITRATE_HIST_SIZE] =
484 decode_position > decode_position_last && written_time > written_time_last ?
485 8000 * (decode_position - decode_position_last) / (written_time - written_time_last) :
486 stream_data_.sample_rate * stream_data_.channels * stream_data_.bits_per_sample;
487 decode_position_last = decode_position;
488 written_time_last = written_time;
492 stream_data_.eof = true;
498 if(!stream_data_.is_http_source && stream_data_.seek_to_in_sec != -1) {
499 const double distance = (double)stream_data_.seek_to_in_sec * 1000.0 / (double)stream_data_.length_in_msec;
500 FLAC__uint64 target_sample = (FLAC__uint64)(distance * (double)stream_data_.total_samples);
501 if(stream_data_.total_samples > 0 && target_sample >= stream_data_.total_samples)
502 target_sample = stream_data_.total_samples - 1;
503 if(FLAC__stream_decoder_seek_absolute(decoder_, target_sample)) {
504 flac_ip.output->flush(stream_data_.seek_to_in_sec * 1000);
505 bh_index_last_w = bh_index_last_o = flac_ip.output->output_time() / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
506 if(!FLAC__stream_decoder_get_decode_position(decoder_, &decode_position_frame))
507 decode_position_frame = 0;
508 stream_data_.eof = false;
509 sample_buffer_first_ = sample_buffer_last_ = 0;
511 else if(FLAC__stream_decoder_get_state(decoder_) == FLAC__STREAM_DECODER_SEEK_ERROR) {
512 /*@@@ this should probably be a dialog */
513 fprintf(stderr, "libxmms-flac: SEEK ERROR\n");
514 FLAC__stream_decoder_flush(decoder_);
515 stream_data_.eof = false;
516 sample_buffer_first_ = sample_buffer_last_ = 0;
518 stream_data_.seek_to_in_sec = -1;
521 /* display the right bitrate from history */
522 unsigned bh_index_o = flac_ip.output->output_time() / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
523 if(bh_index_o != bh_index_last_o && bh_index_o != bh_index_last_w && bh_index_o != (bh_index_last_w + 1) % BITRATE_HIST_SIZE) {
524 bh_index_last_o = bh_index_o;
525 flac_ip.set_info(stream_data_.title, stream_data_.length_in_msec, bitrate_history_[bh_index_o], stream_data_.sample_rate, stream_data_.channels);
530 safe_decoder_finish_(decoder_);
532 /* are these two calls necessary? */
533 flac_ip.output->buffer_free();
534 flac_ip.output->buffer_free();
536 g_free(stream_data_.title);
539 return 0; /* to silence the compiler warning about not returning a value */
542 FLAC__bool safe_decoder_init_(const char *filename, FLAC__StreamDecoder *decoder)
547 safe_decoder_finish_(decoder);
549 FLAC__stream_decoder_set_md5_checking(decoder, false);
550 FLAC__stream_decoder_set_metadata_ignore_all(decoder);
551 FLAC__stream_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_STREAMINFO);
552 FLAC__stream_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT);
553 if(stream_data_.is_http_source) {
554 flac_http_open(filename, 0);
555 if(FLAC__stream_decoder_init_stream(decoder, http_read_callback_, /*seek_callback=*/0, /*tell_callback=*/0, /*length_callback=*/0, /*eof_callback=*/0, write_callback_, metadata_callback_, error_callback_, /*client_data=*/&stream_data_) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
559 if(FLAC__stream_decoder_init_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, /*client_data=*/&stream_data_) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
563 if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder))
569 void safe_decoder_finish_(FLAC__StreamDecoder *decoder)
571 if(decoder && FLAC__stream_decoder_get_state(decoder) != FLAC__STREAM_DECODER_UNINITIALIZED)
572 (void)FLAC__stream_decoder_finish(decoder);
573 if(stream_data_.is_http_source)
577 void safe_decoder_delete_(FLAC__StreamDecoder *decoder)
580 safe_decoder_finish_(decoder);
581 FLAC__stream_decoder_delete(decoder);
585 FLAC__StreamDecoderReadStatus http_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
589 *bytes = flac_http_read(buffer, *bytes);
590 return *bytes ? FLAC__STREAM_DECODER_READ_STATUS_CONTINUE : FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
593 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
595 stream_data_struct *stream_data = (stream_data_struct *)client_data;
596 const unsigned channels = stream_data->channels, wide_samples = frame->header.blocksize;
597 const unsigned bits_per_sample = stream_data->bits_per_sample;
598 FLAC__byte *sample_buffer_start;
602 if(stream_data->abort_flag)
603 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
605 if((sample_buffer_last_ + wide_samples) > (SAMPLE_BUFFER_SIZE / (channels * stream_data->sample_format_bytes_per_sample))) {
606 memmove(sample_buffer_, sample_buffer_ + sample_buffer_first_ * channels * stream_data->sample_format_bytes_per_sample, (sample_buffer_last_ - sample_buffer_first_) * channels * stream_data->sample_format_bytes_per_sample);
607 sample_buffer_last_ -= sample_buffer_first_;
608 sample_buffer_first_ = 0;
610 sample_buffer_start = sample_buffer_ + sample_buffer_last_ * channels * stream_data->sample_format_bytes_per_sample;
611 if(stream_data->has_replaygain && flac_cfg.output.replaygain.enable) {
612 FLAC__replaygain_synthesis__apply_gain(
614 !is_big_endian_host_,
615 stream_data->sample_format_bytes_per_sample == 1, /* unsigned_data_out */
620 stream_data->sample_format_bytes_per_sample * 8,
621 stream_data->replay_scale,
622 flac_cfg.output.replaygain.hard_limit,
623 flac_cfg.output.resolution.replaygain.dither,
624 &stream_data->dither_context
627 else if(is_big_endian_host_) {
628 FLAC__plugin_common__pack_pcm_signed_big_endian(
634 stream_data->sample_format_bytes_per_sample * 8
638 FLAC__plugin_common__pack_pcm_signed_little_endian(
644 stream_data->sample_format_bytes_per_sample * 8
648 sample_buffer_last_ += wide_samples;
650 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
653 void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
655 stream_data_struct *stream_data = (stream_data_struct *)client_data;
657 if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
658 stream_data->total_samples = metadata->data.stream_info.total_samples;
659 stream_data->bits_per_sample = metadata->data.stream_info.bits_per_sample;
660 stream_data->channels = metadata->data.stream_info.channels;
661 stream_data->sample_rate = metadata->data.stream_info.sample_rate;
663 FLAC__uint64 l = (FLAC__uint64)((double)stream_data->total_samples / (double)stream_data->sample_rate * 1000.0 + 0.5);
666 stream_data->length_in_msec = (int)l;
669 else if(metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
670 double reference, gain, peak;
671 if(grabbag__replaygain_load_from_vorbiscomment(metadata, flac_cfg.output.replaygain.album_mode, /*strict=*/false, &reference, &gain, &peak)) {
672 stream_data->has_replaygain = true;
673 stream_data->replay_scale = grabbag__replaygain_compute_scale_factor(peak, gain, (double)flac_cfg.output.replaygain.preamp, /*prevent_clipping=*/!flac_cfg.output.replaygain.hard_limit);
678 void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
680 stream_data_struct *stream_data = (stream_data_struct *)client_data;
682 if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
683 stream_data->abort_flag = true;