Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stb_vorbis: report the sample-accurate offset #1295

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion stb_vorbis.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
//
// Feature contributors:
// Dougall Johnson (sample-exact seeking)
// Vitaly Novichkov (sample-accurate tell)
//
// Bugfix/warning contributors:
// Terje Mathisen Niklas Frykholm Andy Hill
Expand Down Expand Up @@ -165,6 +166,12 @@ extern void stb_vorbis_close(stb_vorbis *f);
// NOT WORKING YET after a seek with PULLDATA API
extern int stb_vorbis_get_sample_offset(stb_vorbis *f);

// this function returns the count of returned samples from the beginning of the
// file. Functions "stb_vorbis_get_samples_*", "stb_vorbis_seek_*()" will
// affect the returned value. Use this call to get the accurate sample position
// during playback.
extern int stb_vorbis_get_playback_sample_offset(stb_vorbis *f);

// returns the current seek point within the file, or offset from the beginning
// of the memory buffer. In pushdata mode it returns 0.
extern unsigned int stb_vorbis_get_file_offset(stb_vorbis *f);
Expand Down Expand Up @@ -863,6 +870,9 @@ struct stb_vorbis
uint32 current_loc; // sample location of next frame to decode
int current_loc_valid;

int32 current_playback_loc; // sample location of played samples
int current_playback_loc_valid;

// per-blocksize precomputed data

// twiddle factors
Expand Down Expand Up @@ -3512,6 +3522,8 @@ static int vorbis_pump_first_frame(stb_vorbis *f)
res = vorbis_decode_packet(f, &len, &left, &right);
if (res)
vorbis_finish_frame(f, len, left, right);
f->current_playback_loc = 0;
f->current_playback_loc_valid = TRUE;
return res;
}

Expand Down Expand Up @@ -4302,6 +4314,14 @@ int stb_vorbis_get_sample_offset(stb_vorbis *f)
return -1;
}

int stb_vorbis_get_playback_sample_offset(stb_vorbis *f)
{
if (f->current_playback_loc_valid)
return f->current_playback_loc;
else
return -1;
}

stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f)
{
stb_vorbis_info d;
Expand Down Expand Up @@ -4913,13 +4933,16 @@ int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number)
}
// the next frame should start with the sample
if (f->current_loc != sample_number) return error(f, VORBIS_seek_failed);
f->current_playback_loc = sample_number;
return 1;
}

int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number)
{
if (!stb_vorbis_seek_frame(f, sample_number))
if (!stb_vorbis_seek_frame(f, sample_number)) {
f->current_playback_loc_valid = FALSE;
return 0;
}

if (sample_number != f->current_loc) {
int n;
Expand All @@ -4930,6 +4953,9 @@ int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number)
f->channel_buffer_start += (sample_number - frame_start);
}

f->current_playback_loc_valid = TRUE;
f->current_playback_loc = sample_number;

return 1;
}

Expand Down Expand Up @@ -5322,6 +5348,7 @@ int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short
if (n == len) break;
if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break;
}
f->current_playback_loc += n;
return n;
}

Expand All @@ -5339,6 +5366,7 @@ int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, in
if (n == len) break;
if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break;
}
f->current_playback_loc += n;
return n;
}

Expand Down Expand Up @@ -5447,6 +5475,7 @@ int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float
if (!stb_vorbis_get_frame_float(f, NULL, &outputs))
break;
}
f->current_playback_loc += n;
return n;
}

Expand All @@ -5473,6 +5502,7 @@ int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, in
if (!stb_vorbis_get_frame_float(f, NULL, &outputs))
break;
}
f->current_playback_loc += n;
return n;
}
#endif // STB_VORBIS_NO_PULLDATA_API
Expand Down