diff --git a/main/xiaozhi-server/core/providers/tts/base.py b/main/xiaozhi-server/core/providers/tts/base.py index 443c861e9..6ed829675 100644 --- a/main/xiaozhi-server/core/providers/tts/base.py +++ b/main/xiaozhi-server/core/providers/tts/base.py @@ -377,10 +377,10 @@ class TTSProviderBase(ABC): enqueue_audio = [] enqueue_text = text - # 收集上报音频数据 + # 收集上报音频数据(使用副本,不修改原始 audio_datas) if isinstance(audio_datas, bytes) and enqueue_audio is not None: - audio_datas = pack_opus_with_header(audio_datas, message_tag) - enqueue_audio.append(audio_datas) + audio_with_header = pack_opus_with_header(audio_datas, message_tag) + enqueue_audio.append(audio_with_header) # 等待上一个发送完成(保持顺序),但使用短超时避免长时间阻塞 if last_send_future is not None: diff --git a/main/xiaozhi-server/core/providers/tts/fish_dual_stream.py b/main/xiaozhi-server/core/providers/tts/fish_dual_stream.py index aae190155..56727f29f 100644 --- a/main/xiaozhi-server/core/providers/tts/fish_dual_stream.py +++ b/main/xiaozhi-server/core/providers/tts/fish_dual_stream.py @@ -309,10 +309,10 @@ class TTSProvider(TTSProviderBase): enqueue_text = text enqueue_report_time = report_time # save timestamp from FIRST for later reporting - # collect TTS audio data for reporting + # collect TTS audio data for reporting (use copy, don't modify original audio_datas) if isinstance(audio_datas, bytes) and enqueue_audio is not None: - audio_datas = pack_opus_with_header(audio_datas, message_tag) - enqueue_audio.append(audio_datas) + audio_with_header = pack_opus_with_header(audio_datas, message_tag) + enqueue_audio.append(audio_with_header) # wait for the previous send to complete (maintain order) but use short timeout to avoid long blocking if last_send_future is not None: diff --git a/main/xiaozhi-server/test/test_page.html b/main/xiaozhi-server/test/test_page.html index 7543dcc4f..6d2e48da9 100644 --- a/main/xiaozhi-server/test/test_page.html +++ b/main/xiaozhi-server/test/test_page.html @@ -1808,7 +1808,8 @@ // Header structure: // byte 0: type (1 = audio) // byte 1: message_tag (0=normal, 1=opening, 2=notification) - // bytes 2-15: reserved + // bytes 2-5: opus data length (big-endian uint32) + // bytes 6-15: reserved const HEADER_SIZE = 16; if (arrayBuffer.byteLength <= HEADER_SIZE) { @@ -1822,11 +1823,26 @@ const headerView = new DataView(arrayBuffer); const type = headerView.getUint8(0); const messageTag = headerView.getUint8(1); + // Read opus data length from bytes 2-5 (big-endian) + const opusLength = headerView.getUint32(2, false); // false = big-endian - // Extract opus data (skip 16-byte header) - const opusData = new Uint8Array(arrayBuffer, HEADER_SIZE); + // Debug: print header bytes + const headerBytes = new Uint8Array(arrayBuffer, 0, Math.min(16, arrayBuffer.byteLength)); + log(`Header bytes: [${Array.from(headerBytes).map(b => b.toString(16).padStart(2, '0')).join(' ')}]`, 'debug'); + log(`Parsed: type=${type}, tag=${messageTag}, opusLength=${opusLength}, totalSize=${arrayBuffer.byteLength}`, 'debug'); - log(`收到音频数据: type=${type}, tag=${messageTag}, payload=${opusData.length}字节`, 'debug'); + // Validate length + if (opusLength <= 0 || opusLength > arrayBuffer.byteLength - HEADER_SIZE) { + log(`无效的opus数据长度: ${opusLength}, 包大小: ${arrayBuffer.byteLength}`, 'warning'); + return; + } + + // Extract opus data based on length from header + const opusData = new Uint8Array(arrayBuffer, HEADER_SIZE, opusLength); + + // Debug: print first few bytes of opus data (TOC byte is important) + const opusPreview = Array.from(opusData.slice(0, 5)).map(b => b.toString(16).padStart(2, '0')).join(' '); + log(`Opus data preview: [${opusPreview}], length=${opusData.length}字节`, 'debug'); // Handle opening message - skip if this is a reconnect if (messageTag === MESSAGE_TAG.OPENING && skipCurrentOpening) {