diff --git a/examples/common-sdl.cpp b/examples/common-sdl.cpp index c5986332..5fc28e53 100644 --- a/examples/common-sdl.cpp +++ b/examples/common-sdl.cpp @@ -139,10 +139,13 @@ void audio_async::callback(uint8_t * stream, int len) { return; } - const size_t n_samples = len / sizeof(float); + size_t n_samples = len / sizeof(float); - m_audio_new.resize(n_samples); - memcpy(m_audio_new.data(), stream, n_samples * sizeof(float)); + if (n_samples > m_audio.size()) { + n_samples = m_audio.size(); + + stream += (len - (n_samples * sizeof(float))); + } //fprintf(stderr, "%s: %zu samples, pos %zu, len %zu\n", __func__, n_samples, m_audio_pos, m_audio_len); @@ -153,7 +156,7 @@ void audio_async::callback(uint8_t * stream, int len) { const size_t n0 = m_audio.size() - m_audio_pos; memcpy(&m_audio[m_audio_pos], stream, n0 * sizeof(float)); - memcpy(&m_audio[0], &stream[n0], (n_samples - n0) * sizeof(float)); + memcpy(&m_audio[0], stream + n0 * sizeof(float), (n_samples - n0) * sizeof(float)); m_audio_pos = (m_audio_pos + n_samples) % m_audio.size(); m_audio_len = m_audio.size(); diff --git a/examples/common-sdl.h b/examples/common-sdl.h index a5659db7..9ee8a320 100644 --- a/examples/common-sdl.h +++ b/examples/common-sdl.h @@ -41,7 +41,6 @@ private: std::mutex m_mutex; std::vector m_audio; - std::vector m_audio_new; size_t m_audio_pos = 0; size_t m_audio_len = 0; };