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

Fix ogg with extra buffer at end #95

Closed
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/SDL_sound_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ typedef struct __SOUND_SAMPLEINTERNAL__
Uint32 buffer_size;
void *decoder_private;
Sint32 total_time;
Uint32 total_length;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You add a new member to the struct here, but only init it in VORBIS_open. I think you're supposed to init it at least to some default value in each function that initializes this struct.

Uint32 mix_position;
MixFunc mix;
} Sound_SampleInternal;
Expand Down
12 changes: 11 additions & 1 deletion src/SDL_sound_vorbis.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ static int VORBIS_open(Sound_Sample *sample, const char *ext)
const unsigned int rate = stb->sample_rate;
internal->total_time = (num_frames / rate) * 1000;
internal->total_time += (num_frames % rate) * 1000 / rate;
internal->total_length = num_frames;
} /* else */

return 1; /* we'll handle this data. */
Expand All @@ -152,14 +153,23 @@ static Uint32 VORBIS_read(Sound_Sample *sample)
Uint32 retval;
int rc;
int err;
int delta;
Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
stb_vorbis *stb = (stb_vorbis *) internal->decoder_private;
const int channels = (int) sample->actual.channels;
const int want_samples = (int) (internal->buffer_size / sizeof (float));
const int offset = stb_vorbis_get_playback_sample_offset(stb);

delta = internal->total_length - offset;

stb_vorbis_get_error(stb); /* clear any error state */
rc = stb_vorbis_get_samples_float_interleaved(stb, channels, (float *) internal->buffer, want_samples);
retval = (Uint32) (rc * channels * sizeof (float)); /* rc == number of sample frames read */
if(delta > 0 && delta < rc) {
retval = (Uint32) (delta * channels * sizeof (float)); /* prevents bug in stb_vorbis */
} else {
retval = (Uint32) (rc * channels * sizeof (float)); /* rc == number of sample frames read */
}

err = stb_vorbis_get_error(stb);

if (retval == 0)
Expand Down