feat: bugfix with audio pack

This commit is contained in:
Harold 2025-12-07 13:07:16 +08:00
parent e393aefd96
commit 50d3db6f09
3 changed files with 26 additions and 10 deletions

View file

@ -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:

View file

@ -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:

View file

@ -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) {