fix: match listen logic exactly from aa85dd9
- Add CHANNELS constant (1 for mono)
- Use audioBuffer.length for loop instead of float32Array.length
- Use getChannelData(0) assignment pattern from aa85dd9
- Proper buffer frame calculation with CHANNELS divisor
This commit is contained in:
@@ -15,6 +15,7 @@ const state = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const SAMPLE_RATE = 24000;
|
const SAMPLE_RATE = 24000;
|
||||||
|
const CHANNELS = 1;
|
||||||
const NOISE_GATE_THRESHOLD = 0.01;
|
const NOISE_GATE_THRESHOLD = 0.01;
|
||||||
|
|
||||||
const el = {
|
const el = {
|
||||||
@@ -419,8 +420,6 @@ function toggleListen() {
|
|||||||
|
|
||||||
function playPcm(arrayBuffer) {
|
function playPcm(arrayBuffer) {
|
||||||
if (!state.isListening || !state.audioContextListen) return;
|
if (!state.isListening || !state.audioContextListen) return;
|
||||||
const bytes = new Uint8Array(arrayBuffer);
|
|
||||||
if (bytes.byteLength <= 4) return;
|
|
||||||
|
|
||||||
const headerView = new DataView(arrayBuffer, 0, 4);
|
const headerView = new DataView(arrayBuffer, 0, 4);
|
||||||
const userIdHash = headerView.getInt32(0, true);
|
const userIdHash = headerView.getInt32(0, true);
|
||||||
@@ -430,9 +429,9 @@ function playPcm(arrayBuffer) {
|
|||||||
const float32Array = new Float32Array(int16Array.length);
|
const float32Array = new Float32Array(int16Array.length);
|
||||||
for (let i = 0; i < int16Array.length; i++) float32Array[i] = int16Array[i] / 32768;
|
for (let i = 0; i < int16Array.length; i++) float32Array[i] = int16Array[i] / 32768;
|
||||||
|
|
||||||
const audioBuffer = state.audioContextListen.createBuffer(1, float32Array.length, SAMPLE_RATE);
|
const audioBuffer = state.audioContextListen.createBuffer(CHANNELS, float32Array.length / CHANNELS, SAMPLE_RATE);
|
||||||
const channelData = audioBuffer.getChannelData(0);
|
const nowBuffering = audioBuffer.getChannelData(0);
|
||||||
for (let i = 0; i < float32Array.length; i++) channelData[i] = float32Array[i];
|
for (let i = 0; i < audioBuffer.length; i++) nowBuffering[i] = float32Array[i];
|
||||||
|
|
||||||
const source = state.audioContextListen.createBufferSource();
|
const source = state.audioContextListen.createBufferSource();
|
||||||
source.buffer = audioBuffer;
|
source.buffer = audioBuffer;
|
||||||
|
|||||||
Reference in New Issue
Block a user