Commit e0d5ee00 authored by Max Kellermann's avatar Max Kellermann

decoder/flac: calculate time stamp from current frame

Don't update a float timestamp, this will make imprecisions add up after a while. We already have the number of the current frame, let's just calculate the float timestamp from that for every decoder_data() command. For this, we need to add the attribute "first_frame", for CUE sheet songs.
parent d35efddd
...@@ -36,9 +36,9 @@ flac_data_init(struct flac_data *data, struct decoder * decoder, ...@@ -36,9 +36,9 @@ flac_data_init(struct flac_data *data, struct decoder * decoder,
pcm_buffer_init(&data->buffer); pcm_buffer_init(&data->buffer);
data->have_stream_info = false; data->have_stream_info = false;
data->first_frame = 0;
data->next_frame = 0; data->next_frame = 0;
data->time = 0;
data->position = 0; data->position = 0;
data->decoder = decoder; data->decoder = decoder;
data->input_stream = input_stream; data->input_stream = input_stream;
...@@ -116,6 +116,7 @@ flac_common_write(struct flac_data *data, const FLAC__Frame * frame, ...@@ -116,6 +116,7 @@ flac_common_write(struct flac_data *data, const FLAC__Frame * frame,
size_t buffer_size = frame->header.blocksize * size_t buffer_size = frame->header.blocksize *
audio_format_frame_size(&data->audio_format); audio_format_frame_size(&data->audio_format);
void *buffer; void *buffer;
float position;
unsigned bit_rate; unsigned bit_rate;
buffer = pcm_buffer_get(&data->buffer, buffer_size); buffer = pcm_buffer_get(&data->buffer, buffer_size);
...@@ -124,6 +125,12 @@ flac_common_write(struct flac_data *data, const FLAC__Frame * frame, ...@@ -124,6 +125,12 @@ flac_common_write(struct flac_data *data, const FLAC__Frame * frame,
data->audio_format.bits, buf, data->audio_format.bits, buf,
0, frame->header.blocksize); 0, frame->header.blocksize);
if (data->next_frame >= data->first_frame)
position = (float)(data->next_frame - data->first_frame) /
data->audio_format.sample_rate;
else
position = 0;
if (nbytes > 0) if (nbytes > 0)
bit_rate = nbytes * 8 * frame->header.sample_rate / bit_rate = nbytes * 8 * frame->header.sample_rate /
(1000 * frame->header.blocksize); (1000 * frame->header.blocksize);
...@@ -132,7 +139,7 @@ flac_common_write(struct flac_data *data, const FLAC__Frame * frame, ...@@ -132,7 +139,7 @@ flac_common_write(struct flac_data *data, const FLAC__Frame * frame,
cmd = decoder_data(data->decoder, data->input_stream, cmd = decoder_data(data->decoder, data->input_stream,
buffer, buffer_size, buffer, buffer_size,
data->time, bit_rate, position, bit_rate,
data->replay_gain_info); data->replay_gain_info);
data->next_frame += frame->header.blocksize; data->next_frame += frame->header.blocksize;
switch (cmd) { switch (cmd) {
......
...@@ -52,11 +52,16 @@ struct flac_data { ...@@ -52,11 +52,16 @@ struct flac_data {
FLAC__StreamMetadata_StreamInfo stream_info; FLAC__StreamMetadata_StreamInfo stream_info;
/** /**
* The number of the first frame in this song. This is only
* non-zero if playing sub songs from a CUE sheet.
*/
FLAC__uint64 first_frame;
/**
* The number of the next frame which is going to be decoded. * The number of the next frame which is going to be decoded.
*/ */
FLAC__uint64 next_frame; FLAC__uint64 next_frame;
float time;
struct audio_format audio_format; struct audio_format audio_format;
FLAC__uint64 position; FLAC__uint64 position;
struct decoder *decoder; struct decoder *decoder;
......
...@@ -191,14 +191,9 @@ static FLAC__StreamDecoderWriteStatus ...@@ -191,14 +191,9 @@ static FLAC__StreamDecoderWriteStatus
flac_write_cb(const FLAC__StreamDecoder *dec, const FLAC__Frame *frame, flac_write_cb(const FLAC__StreamDecoder *dec, const FLAC__Frame *frame,
const FLAC__int32 *const buf[], void *vdata) const FLAC__int32 *const buf[], void *vdata)
{ {
FLAC__uint32 samples = frame->header.blocksize;
struct flac_data *data = (struct flac_data *) vdata; struct flac_data *data = (struct flac_data *) vdata;
float timeChange;
FLAC__uint64 nbytes = 0; FLAC__uint64 nbytes = 0;
timeChange = ((float)samples) / frame->header.sample_rate;
data->time += timeChange;
if (FLAC__stream_decoder_get_decode_position(dec, &nbytes)) { if (FLAC__stream_decoder_get_decode_position(dec, &nbytes)) {
if (data->position > 0 && nbytes > data->position) { if (data->position > 0 && nbytes > data->position) {
nbytes -= data->position; nbytes -= data->position;
...@@ -431,6 +426,8 @@ flac_decoder_loop(struct flac_data *data, FLAC__StreamDecoder *flac_dec, ...@@ -431,6 +426,8 @@ flac_decoder_loop(struct flac_data *data, FLAC__StreamDecoder *flac_dec,
struct decoder *decoder = data->decoder; struct decoder *decoder = data->decoder;
enum decoder_command cmd; enum decoder_command cmd;
data->first_frame = t_start;
while (true) { while (true) {
if (data->tag != NULL && !tag_is_empty(data->tag)) { if (data->tag != NULL && !tag_is_empty(data->tag)) {
cmd = decoder_tag(data->decoder, data->input_stream, cmd = decoder_tag(data->decoder, data->input_stream,
...@@ -448,8 +445,6 @@ flac_decoder_loop(struct flac_data *data, FLAC__StreamDecoder *flac_dec, ...@@ -448,8 +445,6 @@ flac_decoder_loop(struct flac_data *data, FLAC__StreamDecoder *flac_dec,
(t_end == 0 || seek_sample <= t_end) && (t_end == 0 || seek_sample <= t_end) &&
FLAC__stream_decoder_seek_absolute(flac_dec, seek_sample)) { FLAC__stream_decoder_seek_absolute(flac_dec, seek_sample)) {
data->next_frame = seek_sample; data->next_frame = seek_sample;
data->time = (float)(seek_sample - t_start) /
data->audio_format.sample_rate;
data->position = 0; data->position = 0;
decoder_command_finished(decoder); decoder_command_finished(decoder);
} else } else
......
...@@ -155,11 +155,6 @@ oggflac_write_cb(G_GNUC_UNUSED const OggFLAC__SeekableStreamDecoder *decoder, ...@@ -155,11 +155,6 @@ oggflac_write_cb(G_GNUC_UNUSED const OggFLAC__SeekableStreamDecoder *decoder,
void *vdata) void *vdata)
{ {
struct flac_data *data = (struct flac_data *) vdata; struct flac_data *data = (struct flac_data *) vdata;
FLAC__uint32 samples = frame->header.blocksize;
float time_change;
time_change = ((float)samples) / frame->header.sample_rate;
data->time += time_change;
return flac_common_write(data, frame, buf, 0); return flac_common_write(data, frame, buf, 0);
} }
...@@ -338,8 +333,6 @@ oggflac_decode(struct decoder * mpd_decoder, struct input_stream *input_stream) ...@@ -338,8 +333,6 @@ oggflac_decode(struct decoder * mpd_decoder, struct input_stream *input_stream)
if (OggFLAC__seekable_stream_decoder_seek_absolute if (OggFLAC__seekable_stream_decoder_seek_absolute
(decoder, seek_sample)) { (decoder, seek_sample)) {
data.next_frame = seek_sample; data.next_frame = seek_sample;
data.time = ((float)seek_sample) /
data.audio_format.sample_rate;
data.position = 0; data.position = 0;
decoder_command_finished(mpd_decoder); decoder_command_finished(mpd_decoder);
} else } else
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment