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

bug or feature by design when handle sdl callback? #1518

Closed
yexiangyu opened this issue Nov 18, 2023 · 3 comments · Fixed by #1523
Closed

bug or feature by design when handle sdl callback? #1518

yexiangyu opened this issue Nov 18, 2023 · 3 comments · Fixed by #1523
Labels
bug Something isn't working

Comments

@yexiangyu
Copy link

file: examples/common-sdl.cpp, line: 156

memcpy(&m_audio[0], &stream[n0], (n_samples - n0) * sizeof(float));

should change to

memcpy(&m_audio[0], &stream[n0 * sizeof(float)], (n_samples - n0) * sizeof(float));

https://github.com/ggerganov/whisper.cpp/blob/e2f0eba2d4dbe4ded5625cbd8a9c17e13f2482f6/examples/common-sdl.cpp#L156C1-L156C80

@bobqianic
Copy link
Collaborator

Indeed, you're correct. This appears to be a bug.
#1164

@bobqianic bobqianic added the bug Something isn't working label Nov 18, 2023
@bobqianic
Copy link
Collaborator

memcpy(&m_audio[0], &stream[n0 * sizeof(float)], (n_samples - n0) * sizeof(float));

The logic in this case isn't entirely flawless. Consider a scenario where your n_samples unexpectedly grows to be much larger than m_audio.size() – for instance, ten times larger. This could result in undefined behavior, as it involves attempting to write data beyond the bounds of the array.

if (m_audio_pos + n_samples > m_audio.size()) {
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));
m_audio_pos = (m_audio_pos + n_samples) % m_audio.size();
m_audio_len = m_audio.size();
} else {
memcpy(&m_audio[m_audio_pos], stream, n_samples * sizeof(float));
m_audio_pos = (m_audio_pos + n_samples) % m_audio.size();
m_audio_len = std::min(m_audio_len + n_samples, m_audio.size());
}

@ggerganov
Copy link
Owner

Thank you for spotting this!

Could you check #1523 - I think it should fix the issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants