fix: mix L/R channels when converting system audio to mono#215
fix: mix L/R channels when converting system audio to mono#215werpoz wants to merge 1 commit intosohzm:masterfrom
Conversation
📝 WalkthroughWalkthroughModified the Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Tip Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/utils/gemini.js (1)
743-745: Optional: guard against non-multiple-of-4 buffer lengths.
convertStereoToMonois exported and called by external paths. IfstereoBuffer.lengthis not a multiple of 4,samplesis fractional, the loop runs one extra iteration, and bothreadInt16LEandwriteInt16LEwill throw aRangeErrorat the boundary. Current callers always passCHUNK_SIZE = 9600(a multiple of 4), so this is safe today, but a small guard would make the function robust against future callers.🛡️ Proposed defensive guard
function convertStereoToMono(stereoBuffer) { - const samples = stereoBuffer.length / 4; + const samples = Math.floor(stereoBuffer.length / 4); const monoBuffer = Buffer.alloc(samples * 2);🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/utils/gemini.js` around lines 743 - 745, convertStereoToMono currently assumes stereoBuffer.length is a multiple of 4 which can cause RangeError when callers pass other sizes; update the convertStereoToMono function to guard against non-multiple-of-4 lengths by computing samples = Math.floor(stereoBuffer.length / 4) (or truncating/slicing the input to samples * 4) before allocating monoBuffer and entering the readInt16LE/writeInt16LE loop, and ensure any extra trailing bytes are ignored or handled safely so readInt16LE/writeInt16LE never read past the buffer end.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/utils/gemini.js`:
- Around line 743-745: convertStereoToMono currently assumes stereoBuffer.length
is a multiple of 4 which can cause RangeError when callers pass other sizes;
update the convertStereoToMono function to guard against non-multiple-of-4
lengths by computing samples = Math.floor(stereoBuffer.length / 4) (or
truncating/slicing the input to samples * 4) before allocating monoBuffer and
entering the readInt16LE/writeInt16LE loop, and ensure any extra trailing bytes
are ignored or handled safely so readInt16LE/writeInt16LE never read past the
buffer end.
What
This PR fixes mono conversion in the macOS system-audio path so both stereo channels are used.
Why
Root cause:
convertStereoToMono()was reading only the left channel and discarding the right channel.On some setups, useful signal is mostly on the right channel, which caused very low/empty audio input.
Change
Updated
convertStereoToMono()insrc/utils/gemini.jsto downmix stereo frames as:mono = round((left + right) / 2)Scope
src/utils/gemini.jsconvertStereoToMono97f23ebValidation
Risk
Low. Change is localized to stereo->mono conversion and does not alter IPC/session flow.
Summary by CodeRabbit