sdl : fix audio callback (#1523)

This commit is contained in:
Georgi Gerganov 2023-11-20 13:16:38 +02:00 committed by GitHub
parent 8159a9ab99
commit a01b2e0971
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 5 deletions

View File

@ -139,10 +139,13 @@ void audio_async::callback(uint8_t * stream, int len) {
return; return;
} }
const size_t n_samples = len / sizeof(float); size_t n_samples = len / sizeof(float);
m_audio_new.resize(n_samples); if (n_samples > m_audio.size()) {
memcpy(m_audio_new.data(), stream, n_samples * sizeof(float)); 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); //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; const size_t n0 = m_audio.size() - m_audio_pos;
memcpy(&m_audio[m_audio_pos], stream, n0 * sizeof(float)); 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_pos = (m_audio_pos + n_samples) % m_audio.size();
m_audio_len = m_audio.size(); m_audio_len = m_audio.size();

View File

@ -41,7 +41,6 @@ private:
std::mutex m_mutex; std::mutex m_mutex;
std::vector<float> m_audio; std::vector<float> m_audio;
std::vector<float> m_audio_new;
size_t m_audio_pos = 0; size_t m_audio_pos = 0;
size_t m_audio_len = 0; size_t m_audio_len = 0;
}; };