Skip to content

Commit

Permalink
sdl : fix audio callback
Browse files Browse the repository at this point in the history
  • Loading branch information
ggerganov committed Nov 19, 2023
1 parent 46cc26d commit aabe6d9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
11 changes: 7 additions & 4 deletions examples/common-sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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();
Expand Down
1 change: 0 additions & 1 deletion examples/common-sdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class audio_async {
std::mutex m_mutex;

std::vector<float> m_audio;
std::vector<float> m_audio_new;
size_t m_audio_pos = 0;
size_t m_audio_len = 0;
};
Expand Down

0 comments on commit aabe6d9

Please sign in to comment.