xiaozhi-esp32-server/main/digital-human/js/core/audio/player.js
lapy a5db8f8b11 merge: integrate downstream westernization onto upstream/main
Merge 154 upstream commits with the 12-commit westernization stack,
preserving upstream features (theme refactor, knowledge base, device
calling, web search) while keeping downstream provider policy, English
defaults, and westernized surfaces.
2026-06-13 11:22:28 +01:00

297 lines
9.8 KiB
JavaScript

// Audio playback module.
import BlockingQueue from '../../utils/blocking-queue.js?v=0205';
import { log } from '../../utils/logger.js?v=0205';
import { createStreamingContext } from './stream-context.js?v=0205';
// Audio player class.
export class AudioPlayer {
constructor() {
// Audio parameters.
this.SAMPLE_RATE = 16000;
this.CHANNELS = 1;
this.FRAME_SIZE = 960;
this.MIN_AUDIO_DURATION = 0.12;
// Runtime state.
this.audioContext = null;
this.opusDecoder = null;
this.streamingContext = null;
this.queue = new BlockingQueue();
this.isPlaying = false;
}
// Get or create the AudioContext.
getAudioContext() {
if (!this.audioContext) {
this.audioContext = new (window.AudioContext || window.webkitAudioContext)({
sampleRate: this.SAMPLE_RATE,
latencyHint: 'interactive'
});
log('Created audio context with sample rate ' + this.SAMPLE_RATE + 'Hz', 'debug');
}
return this.audioContext;
}
// Initialize the Opus decoder.
async initOpusDecoder() {
if (this.opusDecoder) return this.opusDecoder;
try {
if (typeof window.ModuleInstance === 'undefined') {
if (typeof Module !== 'undefined') {
window.ModuleInstance = Module;
log('Using the global Module object as ModuleInstance.', 'info');
} else {
throw new Error('The Opus runtime is not loaded because neither ModuleInstance nor Module exists.');
}
}
const mod = window.ModuleInstance;
this.opusDecoder = {
channels: this.CHANNELS,
rate: this.SAMPLE_RATE,
frameSize: this.FRAME_SIZE,
module: mod,
decoderPtr: null,
init: function () {
if (this.decoderPtr) return true;
const decoderSize = mod._opus_decoder_get_size(this.channels);
log(`Opus decoder size: ${decoderSize} bytes`, 'debug');
this.decoderPtr = mod._malloc(decoderSize);
if (!this.decoderPtr) {
throw new Error('Failed to allocate decoder memory.');
}
const err = mod._opus_decoder_init(
this.decoderPtr,
this.rate,
this.channels
);
if (err < 0) {
this.destroy();
throw new Error(`Opus decoder initialization failed: ${err}`);
}
log('Opus decoder initialized successfully.', 'success');
return true;
},
decode: function (opusData) {
if (!this.decoderPtr) {
if (!this.init()) {
throw new Error('The decoder is not initialized and could not be initialized.');
}
}
try {
const mod = this.module;
const opusPtr = mod._malloc(opusData.length);
mod.HEAPU8.set(opusData, opusPtr);
const pcmPtr = mod._malloc(this.frameSize * 2);
const decodedSamples = mod._opus_decode(
this.decoderPtr,
opusPtr,
opusData.length,
pcmPtr,
this.frameSize,
0
);
if (decodedSamples < 0) {
mod._free(opusPtr);
mod._free(pcmPtr);
throw new Error(`Opus decoding failed: ${decodedSamples}`);
}
const decodedData = new Int16Array(decodedSamples);
for (let i = 0; i < decodedSamples; i++) {
decodedData[i] = mod.HEAP16[(pcmPtr >> 1) + i];
}
mod._free(opusPtr);
mod._free(pcmPtr);
return decodedData;
} catch (error) {
log(`Opus decoding error: ${error.message}`, 'error');
return new Int16Array(0);
}
},
destroy: function () {
if (this.decoderPtr) {
this.module._free(this.decoderPtr);
this.decoderPtr = null;
}
}
};
if (!this.opusDecoder.init()) {
throw new Error('Opus decoder initialization failed.');
}
return this.opusDecoder;
} catch (error) {
log(`Opus decoder initialization failed: ${error.message}`, 'error');
this.opusDecoder = null;
throw error;
}
}
// Start audio buffering.
async startAudioBuffering() {
log('Starting audio buffering...', 'info');
this.initOpusDecoder().catch(error => {
log(`Failed to pre-initialize the Opus decoder: ${error.message}`, 'warning');
});
const timeout = 400;
while (true) {
const packets = await this.queue.dequeue(
6,
timeout,
(count) => {
log(`Buffer timeout reached with ${count} pending packets. Starting playback.`, 'info');
}
);
if (packets.length) {
log(`Buffered ${packets.length} audio packets. Starting playback.`, 'info');
this.streamingContext.pushAudioBuffer(packets);
}
while (true) {
const data = await this.queue.dequeue(99, 30);
if (data.length) {
this.streamingContext.pushAudioBuffer(data);
} else {
break;
}
}
}
}
// Play buffered audio.
async playBufferedAudio() {
try {
this.audioContext = this.getAudioContext();
if (!this.opusDecoder) {
log('Initializing the Opus decoder...', 'info');
try {
this.opusDecoder = await this.initOpusDecoder();
if (!this.opusDecoder) {
throw new Error('Decoder initialization failed.');
}
log('Opus decoder initialized successfully.', 'success');
} catch (error) {
log('Opus decoder initialization failed: ' + error.message, 'error');
this.isPlaying = false;
return;
}
}
if (!this.streamingContext) {
this.streamingContext = createStreamingContext(
this.opusDecoder,
this.audioContext,
this.SAMPLE_RATE,
this.CHANNELS,
this.MIN_AUDIO_DURATION
);
}
this.streamingContext.decodeOpusFrames();
this.streamingContext.startPlaying();
} catch (error) {
log(`Buffered audio playback failed: ${error.message}`, 'error');
this.isPlaying = false;
this.streamingContext = null;
}
}
// Add audio data to the playback queue.
enqueueAudioData(opusData) {
if (opusData.length > 0) {
this.queue.enqueue(opusData);
} else {
log('Received an empty audio frame, which may be the end-of-stream marker.', 'warning');
if (this.isPlaying && this.streamingContext) {
this.streamingContext.endOfStream = true;
}
}
}
// Preload the decoder.
async preload() {
log('Preloading the Opus decoder...', 'info');
try {
await this.initOpusDecoder();
log('Opus decoder preloaded successfully.', 'success');
} catch (error) {
log(`Failed to preload the Opus decoder: ${error.message}. It will retry when needed.`, 'warning');
}
}
// Start the playback system.
async start() {
await this.preload();
this.playBufferedAudio();
this.startAudioBuffering();
}
// Get audio packet statistics.
getAudioStats() {
if (!this.streamingContext) {
return {
pendingDecode: 0,
pendingPlay: 0,
totalPending: 0
};
}
const pendingDecode = this.streamingContext.getPendingDecodeCount();
const pendingPlay = this.streamingContext.getPendingPlayCount();
return {
pendingDecode, // packets waiting to be decoded
pendingPlay, // packets waiting to play
totalPending: pendingDecode + pendingPlay // total pending packets
};
}
// Clear all audio buffers and stop playback.
clearAllAudio() {
log('AudioPlayer: clearing all audio buffers.', 'info');
// Clear the receive queue while preserving references.
this.queue.clear();
// Clear the streaming context buffers.
if (this.streamingContext) {
this.streamingContext.clearAllBuffers();
}
log('AudioPlayer: audio buffers cleared.', 'success');
}
}
// Create the singleton instance.
let audioPlayerInstance = null;
export function getAudioPlayer() {
if (!audioPlayerInstance) {
audioPlayerInstance = new AudioPlayer();
}
return audioPlayerInstance;
}