Fix argument order, sizing, and interleaving bugs in microphone handling

Adapted from commit dd43d476b023acfcaab4bdee942c98fd30d83f50 in
https://github.com/jimrandomh/g2flash/pull/4.

Co-Authored-By: Kalani Helekunihi <i@am.guru>
This commit is contained in:
James BabcockandKalani Helekunihi committed 2026-09-26 11:57:40 -07:00
1 parent 51edaee883
commit e8d2f5ade9
2 files changed
+15 -14

No files matched your search

+13 -12
View File
@@ -12,8 +12,8 @@
* *
* Per-temple: each arm is its own endpoint and its own 2-mic array (front * Per-temple: each arm is its own endpoint and its own 2-mic array (front
* mic near the hinge = chanMask bit0, rear mic near the touchpad = bit1); * mic near the hinge = chanMask bit0, rear mic near the touchpad = bit1);
* the same config goes to both temples. Stereo payloads arrive as two * the same config goes to both temples. Stereo payloads are INTERLEAVED PCM16
* CONCATENATED channel blocks (front then rear), not interleaved. Sessions * (ch0, ch1, ch0, ch1, ...), the capture dispatch buffer verbatim. Sessions
* hold a 90 s lease; renew with op RENEW while streaming. * hold a 90 s lease; renew with op RENEW while streaming.
* *
* Pure module: byte codecs only, no platform imports (the BLE plumbing lives * Pure module: byte codecs only, no platform imports (the BLE plumbing lives
@@ -54,7 +54,7 @@ export type MicConfig = {
/** /**
* Host-side per-microphone selection. Each temple streams its full * Host-side per-microphone selection. Each temple streams its full
* front+rear stereo pair over BLE (wire channelMask stays 0b11); the phone * front+rear stereo pair over BLE (wire channelMask stays 0b11); the phone
* splits the concatenated channels and this mask decides which of the four * deinterleaves the channels and this mask decides which of the four
* mics feed levels-for-use, beamforming, captions, and recording. A temple * mics feed levels-for-use, beamforming, captions, and recording. A temple
* with both mics off is sent STOP so its stream doesn't waste bandwidth. * with both mics off is sent STOP so its stream doesn't waste bandwidth.
*/ */
@@ -260,25 +260,26 @@ export function decodeMicStreamFrame(data: Uint8Array): MicStreamFrame | null {
/** /**
* Split a raw PCM16 SM payload into per-channel Float32Arrays in [-1, 1]. * Split a raw PCM16 SM payload into per-channel Float32Arrays in [-1, 1].
* Channels arrive as CONCATENATED blocks (front then rear), matching the * Channels arrive INTERLEAVED: the firmware forwards the stock capture
* firmware's stereo callback, which dispatches two back-to-back extraction * dispatch buffer, which the stock algo reads as ch0 = pcm[2i], ch1 =
* results rather than interleaving. * pcm[2i+1] (in samples). Channel 0 is assumed to be the front mic; that
* mapping is not yet confirmed on hardware.
*/ */
export function splitConcatenatedPcm16(frame: MicStreamFrame): Float32Array[] | null { export function splitInterleavedPcm16(frame: MicStreamFrame): Float32Array[] | null {
if (frame.codec !== "raw" || frame.format !== "pcm16" || frame.channelCount < 1) { if (frame.codec !== "raw" || frame.format !== "pcm16" || frame.channelCount < 1) {
return null; return null;
} }
const channels = frame.channelCount; const channels = frame.channelCount;
const bytesPerChannel = Math.floor(frame.payload.length / channels) & ~1; const frameBytes = channels * 2;
const samplesPerChannel = bytesPerChannel / 2; const samplesPerChannel = Math.floor(frame.payload.length / frameBytes);
if (samplesPerChannel <= 0) return null; if (samplesPerChannel <= 0) return null;
const out: Float32Array[] = []; const out: Float32Array[] = [];
for (let channel = 0; channel < channels; channel++) { for (let channel = 0; channel < channels; channel++) {
const base = channel * bytesPerChannel;
const samples = new Float32Array(samplesPerChannel); const samples = new Float32Array(samplesPerChannel);
for (let index = 0; index < samplesPerChannel; index++) { for (let index = 0; index < samplesPerChannel; index++) {
const lo = frame.payload[base + index * 2]!; const offset = index * frameBytes + channel * 2;
const hi = frame.payload[base + index * 2 + 1]!; const lo = frame.payload[offset]!;
const hi = frame.payload[offset + 1]!;
let value = lo | (hi << 8); let value = lo | (hi << 8);
if (value >= 0x8000) value -= 0x10000; if (value >= 0x8000) value -= 0x10000;
samples[index] = value / 32768; samples[index] = value / 32768;
+2 -2
View File
@@ -24,7 +24,7 @@ import {
isStockAudioPacket, isStockAudioPacket,
micChannelKey, micChannelKey,
micChannelLabel, micChannelLabel,
splitConcatenatedPcm16, splitInterleavedPcm16,
templeActive, templeActive,
type MicChannelKey, type MicChannelKey,
type MicSide, type MicSide,
@@ -586,7 +586,7 @@ class MicSession {
return; return;
} }
const side: MicSide = arm === "L" ? "left" : "right"; const side: MicSide = arm === "L" ? "left" : "right";
const channels = splitConcatenatedPcm16(frame); const channels = splitInterleavedPcm16(frame);
if (!channels || channels.length === 0) return; if (!channels || channels.length === 0) return;
// Meter every arriving channel — a host-disabled mic still shows its // Meter every arriving channel — a host-disabled mic still shows its