- fix few audio processing bugs

This commit is contained in:
2026-07-06 12:33:16 +03:00
parent 0e48bed9c1
commit 3d455732ea
3 changed files with 18 additions and 4 deletions
+6 -3
View File
@@ -66,10 +66,13 @@ size_t SpeexResampler::processBuffer(const void* src, size_t sourceLength, size_
if (mDestRate == mSourceRate) if (mDestRate == mSourceRate)
{ {
// Pass-through, but never write past the caller's buffer: clamp to its
// capacity instead of trusting sourceLength (which is caller/file driven).
assert(destCapacity >= sourceLength); assert(destCapacity >= sourceLength);
memcpy(dest, src, sourceLength); size_t copied = std::min(sourceLength, destCapacity);
sourceProcessed = sourceLength; memcpy(dest, src, copied);
return sourceLength; sourceProcessed = copied;
return copied;
} }
if (!mContext) if (!mContext)
+8
View File
@@ -150,6 +150,14 @@ bool WavFileReader::open(const std::filesystem::path& p)
if (mBits != 16) if (mBits != 16)
THROW_READERROR; THROW_READERROR;
// The read path is mono-only: it divides byte counts by AUDIO_CHANNELS
// and starts the resampler with AUDIO_CHANNELS. Reject anything else -
// a multi-channel file would make read() copy more source bytes than the
// caller's (mono-sized) output buffer can hold. Also reject a zero rate,
// which would divide by zero in readRaw() / poison the resampler ratio.
if (mChannels != AUDIO_CHANNELS || mSamplerate == 0)
THROW_READERROR;
// Look for the chunk 'data' // Look for the chunk 'data'
mInput->seekg(fmtStart + std::streampos(fmtSize)); mInput->seekg(fmtStart + std::streampos(fmtSize));
+4 -1
View File
@@ -37,11 +37,14 @@ void SingleAudioStream::copyPcmTo(Audio::DataWindow& output, int needed)
// Number of bytes to fill on this step // Number of bytes to fill on this step
auto requested = needed - output.filled(); auto requested = needed - output.filled();
// requested is in bytes: 16-bit samples at AUDIO_SAMPLERATE/AUDIO_CHANNELS
constexpr int bytesPerMs = AUDIO_SAMPLERATE / 1000 * sizeof(int16_t) * AUDIO_CHANNELS;
auto options = AudioReceiver::DecodeOptions{ auto options = AudioReceiver::DecodeOptions{
.mRealtimeProcessing = true, .mRealtimeProcessing = true,
.mResampleToMainRate = true, .mResampleToMainRate = true,
.mSkipDecode = false, .mSkipDecode = false,
.mElapsed = std::chrono::milliseconds(requested / (AUDIO_SAMPLERATE / 1000)) .mElapsed = std::chrono::milliseconds(requested / bytesPerMs)
}; };
// Try to get the data from receiver / decoder // Try to get the data from receiver / decoder