From aca6e291d64fcbd253ea9c34756db6f8c6915c72 Mon Sep 17 00:00:00 2001 From: Saracen Date: Fri, 27 Jul 2018 03:47:22 +0100 Subject: Fixed audio clipping on WASAPI by fixing argument order on AudioClient Initialize method ensuring a larger capture buffer and adding bounds to the capture and stream. --- servers/audio/audio_stream.cpp | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) (limited to 'servers/audio') diff --git a/servers/audio/audio_stream.cpp b/servers/audio/audio_stream.cpp index 0c8a1810d2..f7622b8a4e 100644 --- a/servers/audio/audio_stream.cpp +++ b/servers/audio/audio_stream.cpp @@ -135,19 +135,31 @@ void AudioStreamPlaybackMicrophone::_mix_internal(AudioFrame *p_buffer, int p_fr AudioDriver::get_singleton()->lock(); Vector buf = AudioDriver::get_singleton()->get_audio_input_buffer(); + unsigned int audio_input_size = AudioDriver::get_singleton()->get_audio_input_size(); - for (int i = 0; i < p_frames; i++) { - - float l = (buf[input_ofs++] >> 16) / 32768.f; - if (input_ofs >= buf.size()) { - input_ofs = 0; - } - float r = (buf[input_ofs++] >> 16) / 32768.f; - if (input_ofs >= buf.size()) { - input_ofs = 0; + // p_frames is multipled by two since an AudioFrame is stereo + if ((p_frames * 2) > audio_input_size) { + for (int i = 0; i < p_frames; i++) { + p_buffer[i] = AudioFrame(0.0f, 0.0f); } + input_ofs = 0; + } else { + for (int i = 0; i < p_frames; i++) { + if (audio_input_size >= input_ofs) { + float l = (buf[input_ofs++] >> 16) / 32768.f; + if (input_ofs >= buf.size()) { + input_ofs = 0; + } + float r = (buf[input_ofs++] >> 16) / 32768.f; + if (input_ofs >= buf.size()) { + input_ofs = 0; + } - p_buffer[i] = AudioFrame(l, r); + p_buffer[i] = AudioFrame(l, r); + } else { + p_buffer[i] = AudioFrame(0.0f, 0.0f); + } + } } AudioDriver::get_singleton()->unlock(); -- cgit v1.2.3