Skip to content

Commit

Permalink
OGG audio loop offset pop fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
strellydev committed Aug 9, 2023
1 parent 11ea4dc commit 24b36b2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
4 changes: 3 additions & 1 deletion modules/ogg/ogg_packet_sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ bool OggPacketSequencePlayback::next_ogg_packet(ogg_packet **p_packet) const {

*p_packet = packet;

packet_cursor++;
if (!packet->e_o_s) { //Added this so it doesn't try to go to the next packet if it's the last packet of the file.
packet_cursor++;
}

return true;
}
Expand Down
45 changes: 26 additions & 19 deletions modules/vorbis/audio_stream_ogg_vorbis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,24 +145,6 @@ int AudioStreamPlaybackOggVorbis::_mix_internal(AudioFrame *p_buffer, int p_fram

int AudioStreamPlaybackOggVorbis::_mix_frames_vorbis(AudioFrame *p_buffer, int p_frames) {
ERR_FAIL_COND_V(!ready, 0);
if (!have_samples_left) {
ogg_packet *packet = nullptr;
int err;

if (!vorbis_data_playback->next_ogg_packet(&packet)) {
have_packets_left = false;
WARN_PRINT("ran out of packets in stream");
return -1;
}

err = vorbis_synthesis(&block, packet);
ERR_FAIL_COND_V_MSG(err != 0, 0, "Error during vorbis synthesis " + itos(err));

err = vorbis_synthesis_blockin(&dsp_state, &block);
ERR_FAIL_COND_V_MSG(err != 0, 0, "Error during vorbis block processing " + itos(err));

have_packets_left = !packet->e_o_s;
}

float **pcm; // Accessed with pcm[channel_idx][sample_idx].

Expand All @@ -186,6 +168,31 @@ int AudioStreamPlaybackOggVorbis::_mix_frames_vorbis(AudioFrame *p_buffer, int p
}
}
vorbis_synthesis_read(&dsp_state, frames);

if (!have_samples_left) { //Moved down so the check happens after we know if we have packets left or not.
ogg_packet *packet = nullptr;
int err;

if (!vorbis_data_playback->next_ogg_packet(&packet)) {
have_packets_left = false;
WARN_PRINT("ran out of packets in stream");
return -1;
}

err = vorbis_synthesis(&block, packet);
ERR_FAIL_COND_V_MSG(err != 0, 0, "Error during vorbis synthesis " + itos(err));

err = vorbis_synthesis_blockin(&dsp_state, &block);
ERR_FAIL_COND_V_MSG(err != 0, 0, "Error during vorbis block processing " + itos(err));

//We do a check for the last packet of the file because if we didn't, we'd skip it, since both have samples and have packets left would be false by mistake
//after processing the last actual packet that still has samples.
if (packet->e_o_s && (dsp_state.pcm_returned > -1 && dsp_state.pcm_current - dsp_state.pcm_returned > p_frames)) {
have_samples_left = true;
}
have_packets_left = !packet->e_o_s;
}

return frames;
}

Expand Down Expand Up @@ -358,7 +365,7 @@ void AudioStreamPlaybackOggVorbis::seek(double p_time) {

int samples_out = vorbis_synthesis_pcmout(&dsp_state, nullptr);
int read_samples = samples_to_burn > samples_out ? samples_out : samples_to_burn;
err = vorbis_synthesis_read(&dsp_state, samples_out);
err = vorbis_synthesis_read(&dsp_state, read_samples); //Changed to read_samples so we don't burn the whole packet.
ERR_FAIL_COND_MSG(err != 0, "Error during vorbis read updating " + itos(err));
samples_to_burn -= read_samples;

Expand Down

0 comments on commit 24b36b2

Please sign in to comment.