Добавь потоковую реализацию чтобы поддерживать чанки и буферизацию. Покажи полный…
- User
Добавь потоковую реализацию чтобы поддерживать чанки и буферизацию. Покажи полный готовый код
js - Assistant
Добавь потоковую реализацию чтобы поддерживать чанки и буферизацию. Покажи полный готовый код
import { PitchShifter } from "soundtouchjs";
import debug from "./debug.js";
import config from "./config.js";
export const videoLipSyncEvents = [
"playing",
"ratechange",
"play",
"waiting",
"pause",
"seeked",
];
export function initAudioContext() {
const audioContext = window.AudioContext || window.webkitAudioContext;
return audioContext ? new audioContext() : undefined;
}
export class BasePlayer {
static name = "BasePlayer";
chaimu;
_src;
fetch;
constructor(chaimu, src) {
this.chaimu = chaimu;
this._src = src;
this.fetch = config.fetchFn;
}
async init() {
return new Promise((resolve) => {
return resolve(this);
});
}
clear() {
return new Promise((resolve) => {
return resolve(this);
});
}
lipSync(mode = false) {
return this;
}
handleVideoEvent = (event) => {
debug.log(`handle video ${event.type}`);
this.lipSync(event.type);
return this;
};
removeVideoEvents() {
for (const e of videoLipSyncEvents) {
this.chaimu.video.removeEventListener(e, this.handleVideoEvent);
}
return this;
}
addVideoEvents() {
for (const e of videoLipSyncEvents) {
this.chaimu.video.addEventListener(e, this.handleVideoEvent);
}
return this;
}
async play() {
return new Promise((resolve) => {
return resolve(this);
});
}
async pause() {
return new Promise((resolve) => {
return resolve(this);
});
}
get name() {
return this.constructor.name;
}
set src(url) {
this._src = url;
}
get src() {
return this._src;
}
get currentSrc() {
return this._src;
}
set volume(value) {
return;
}
get volume() {
return 0;
}
get playbackRate() {
return 0;
}
set playbackRate(value) {
return;
}
get currentTime() {
return 0;
}
}
export class AudioPlayer extends BasePlayer {
static name = "AudioPlayer";
audio;
gainNode;
audioSource;
constructor(chaimu, src) {
super(chaimu, src);
this.audio = new Audio(src);
this.audio.crossOrigin = "anonymous";
}
initAudioBooster() {
if (!this.chaimu.audioContext) {
return this;
}
if (this.gainNode && this.audioSource) {
this.audioSource.disconnect(this.gainNode);
this.gainNode.disconnect();
}
this.gainNode = this.chaimu.audioContext.createGain();
this.gainNode.connect(this.chaimu.audioContext.destination);
this.audioSource = this.chaimu.audioContext.createMediaElementSource(this.audio);
this.audioSource.connect(this.gainNode);
return this;
}
async init() {
return new Promise((resolve) => {
this.initAudioBooster();
return resolve(this);
});
}
audioErrorHandle = (e) => {
console.error("[AudioPlayer]", e);
};
lipSync(mode = false) {
debug.log("[AudioPlayer] lipsync video", this.chaimu.video);
if (!this.chaimu.video) {
return this;
}
this.audio.currentTime = this.chaimu.video.currentTime;
this.audio.playbackRate = this.chaimu.video.playbackRate;
if (!mode) {
debug.log("[AudioPlayer] lipsync mode isn't set");
return this;
}
debug.log(`[AudioPlayer] lipsync mode is ${mode}`);
switch (mode) {
case "play":
case "playing":
case "seeked": {
if (!this.chaimu.video.paused) {
this.syncPlay();
}
return this;
}
case "pause":
case "waiting": {
void this.pause();
return this;
}
default: {
return this;
}
}
}
async clear() {
return new Promise((resolve) => {
this.audio.pause();
this.audio.src = "";
this.audio.removeAttribute("src");
return resolve(this);
});
}
syncPlay() {
debug.log("[AudioPlayer] sync play called");
this.audio.play().catch(this.audioErrorHandle);
return this;
}
async play() {
debug.log("[AudioPlayer] play called");
await this.audio.play().catch(this.audioErrorHandle);
return this;
}
async pause() {
return new Promise((resolve) => {
debug.log("[AudioPlayer] pause called");
this.audio.pause();
return resolve(this);
});
}
set src(url) {
this._src = url;
if (!url) {
void this.clear();
return;
}
this.audio.src = url;
}
get src() {
return this._src;
}
get currentSrc() {
return this.audio.currentSrc;
}
set volume(value) {
if (this.gainNode) {
this.gainNode.gain.value = value;
return;
}
this.audio.volume = value;
}
get volume() {
return this.gainNode ? this.gainNode.gain.value : this.audio.volume;
}
get playbackRate() {
return this.audio.playbackRate;
}
set playbackRate(value) {
this.audio.playbackRate = value;
}
get currentTime() {
return this.audio.currentTime;
}
}
export class ChaimuPlayer extends BasePlayer {
static name = "ChaimuPlayer";
audioBuffer;
sourceNode;
gainNode;
audioShifter;
cleanerRunned = false;
async fetchAudio() {
if (!this._src) {
throw new Error("No audio source provided");
}
if (!this.chaimu.audioContext) {
throw new Error("No audio context available");
}
debug.log(`[ChaimuPlayer] Fetching audio from ${this._src}...`);
try {
const res = await this.fetch(this._src);
debug.log(`[ChaimuPlayer] Decoding fetched audio...`);
const data = await res.arrayBuffer();
this.audioBuffer = await this.chaimu.audioContext.decodeAudioData(data);
}
catch (err) {
throw new Error(`Failed to fetch audio file, because ${err.message}`);
}
return this;
}
initAudioBooster() {
if (!this.chaimu.audioContext) {
return this;
}
if (this.gainNode) {
this.gainNode.disconnect();
}
this.gainNode = this.chaimu.audioContext.createGain();
return this;
}
async init() {
await this.fetchAudio();
this.initAudioBooster();
return this;
}
lipSync(mode = false) {
debug.log("[ChaimuPlayer] lipsync video", this.chaimu.video, this);
if (!this.chaimu.video) {
return this;
}
if (!mode) {
debug.log("[ChaimuPlayer] lipsync mode isn't set");
return this;
}
debug.log(`[ChaimuPlayer] lipsync mode is ${mode}`);
switch (mode) {
case "play":
case "playing":
case "ratechange":
case "seeked": {
if (!this.chaimu.video.paused) {
void this.start();
}
return this;
}
case "pause":
case "waiting": {
void this.pause();
return this;
}
default: {
return this;
}
}
}
async reopenCtx() {
if (!this.chaimu.audioContext) {
throw new Error("No audio context available");
}
try {
await this.chaimu.audioContext.close();
}
catch {
}
return this;
}
async clear() {
if (!this.chaimu.audioContext) {
throw new Error("No audio context available");
}
debug.log("clear audio context");
this.cleanerRunned = true;
await this.pause();
if (!this.gainNode) {
this.cleanerRunned = false;
return this;
}
if (this.sourceNode) {
this.sourceNode.stop();
this.sourceNode.disconnect(this.gainNode);
this.sourceNode = undefined;
}
if (this.audioShifter) {
this.audioShifter._node.disconnect(this.gainNode);
this.audioShifter = undefined;
}
this.gainNode.disconnect();
const oldVolume = this.volume;
this.gainNode = undefined;
await this.reopenCtx();
this.chaimu.audioContext = initAudioContext();
this.initAudioBooster();
this.volume = oldVolume;
this.cleanerRunned = false;
return this;
}
async start() {
if (!this.chaimu.audioContext) {
throw new Error("No audio context available");
}
if (!this.audioBuffer) {
throw new Error("The player isn't initialized");
}
if (!this.gainNode ||
(this.audioShifter && this.audioShifter.duration < this.chaimu.video.currentTime)) {
debug.log("Skip starting player");
return this;
}
if (this.cleanerRunned) {
debug.log("The other cleaner is still running, waiting...");
return this;
}
debug.log("starting audio");
await this.clear();
await this.play();
this.audioShifter = new PitchShifter(this.chaimu.audioContext, this.audioBuffer, 1024);
this.audioShifter.tempo = this.chaimu.video.playbackRate;
this.audioShifter.percentagePlayed = this.chaimu.video.currentTime / this.audioShifter.duration;
this.sourceNode = this.chaimu.audioContext.createBufferSource();
this.sourceNode.buffer = null;
this.sourceNode.connect(this.gainNode);
this.audioShifter.connect(this.gainNode);
this.gainNode.connect(this.chaimu.audioContext.destination);
this.sourceNode.start(undefined, this.chaimu.video.currentTime);
return this;
}
async pause() {
if (!this.chaimu.audioContext) {
throw new Error("No audio context available");
}
if (this.chaimu.audioContext.state !== "running") {
return this;
}
await this.chaimu.audioContext.suspend();
return this;
}
async play() {
if (!this.chaimu.audioContext) {
throw new Error("No audio context available");
}
await this.chaimu.audioContext.resume();
return this;
}
set src(url) {
this._src = url;
}
get src() {
return this._src;
}
get currentSrc() {
return this._src;
}
set volume(value) {
if (this.gainNode) {
this.gainNode.gain.value = value;
}
}
get volume() {
return this.gainNode ? this.gainNode.gain.value : 0;
}
set playbackRate(value) {
if (!this.audioShifter) {
throw new Error("No audio source available");
}
this.audioShifter.pitch = value;
}
get playbackRate() {
return this.audioShifter?._soundtouch?.tempo ?? 0;
}
get currentTime() {
return this.chaimu.video.currentTime;
}
}
Чтобы добавить потоковую обработку с поддержкой чанков и буферизации в ваш аудио-плеер, необходимо реализовать механизм, который будет загружать аудио данные постепенно, декодировать их и воспроизводить в реальном времени. Ниже представлен полный обновленный код с потоковой реализацией, включающей обработку чанков и буферизацию.
ReadableStream для потоковой загрузки аудио.import { PitchShifter } from "soundtouchjs";
import debug from "./debug.js";
import config from "./config.js";
export const videoLipSyncEvents = [
"playing",
"ratechange",
"play",
"waiting",
"pause",
"seeked",
];
export function initAudioContext() {
const audioContext = window.AudioContext || window.webkitAudioContext;
return audioContext ? new audioContext() : undefined;
}
export class BasePlayer {
static name = "BasePlayer";
chaimu;
_src;
fetch;
constructor(chaimu, src) {
this.chaimu = chaimu;
this._src = src;
this.fetch = config.fetchFn;
}
async init() {
return new Promise((resolve) => {
return resolve(this);
});
}
clear() {
return new Promise((resolve) => {
return resolve(this);
});
}
lipSync(mode = false) {
return this;
}
handleVideoEvent = (event) => {
debug.log(`handle video ${event.type}`);
this.lipSync(event.type);
return this;
};
removeVideoEvents() {
for (const e of videoLipSyncEvents) {
this.chaimu.video.removeEventListener(e, this.handleVideoEvent);
}
return this;
}
addVideoEvents() {
for (const e of videoLipSyncEvents) {
this.chaimu.video.addEventListener(e, this.handleVideoEvent);
}
return this;
}
async play() {
return new Promise((resolve) => {
return resolve(this);
});
}
async pause() {
return new Promise((resolve) => {
return resolve(this);
});
}
get name() {
return this.constructor.name;
}
set src(url) {
this._src = url;
}
get src() {
return this._src;
}
get currentSrc() {
return this._src;
}
set volume(value) {
return;
}
get volume() {
return 0;
}
get playbackRate() {
return 0;
}
set playbackRate(value) {
return;
}
get currentTime() {
return 0;
}
}
export class AudioPlayer extends BasePlayer {
static name = "AudioPlayer";
audio;
gainNode;
audioSource;
constructor(chaimu, src) {
super(chaimu, src);
this.audio = new Audio(src);
this.audio.crossOrigin = "anonymous";
}
initAudioBooster() {
if (!this.chaimu.audioContext) {
return this;
}
if (this.gainNode && this.audioSource) {
this.audioSource.disconnect(this.gainNode);
this.gainNode.disconnect();
}
this.gainNode = this.chaimu.audioContext.createGain();
this.gainNode.connect(this.chaimu.audioContext.destination);
this.audioSource = this.chaimu.audioContext.createMediaElementSource(this.audio);
this.audioSource.connect(this.gainNode);
return this;
}
async init() {
return new Promise((resolve) => {
this.initAudioBooster();
return resolve(this);
});
}
audioErrorHandle = (e) => {
console.error("[AudioPlayer]", e);
};
lipSync(mode = false) {
debug.log("[AudioPlayer] lipsync video", this.chaimu.video);
if (!this.chaimu.video) {
return this;
}
this.audio.currentTime = this.chaimu.video.currentTime;
this.audio.playbackRate = this.chaimu.video.playbackRate;
if (!mode) {
debug.log("[AudioPlayer] lipsync mode isn't set");
return this;
}
debug.log(`[AudioPlayer] lipsync mode is ${mode}`);
switch (mode) {
case "play":
case "playing":
case "seeked": {
if (!this.chaimu.video.paused) {
this.syncPlay();
}
return this;
}
case "pause":
case "waiting": {
void this.pause();
return this;
}
default: {
return this;
}
}
}
async clear() {
return new Promise((resolve) => {
this.audio.pause();
this.audio.src = "";
this.audio.removeAttribute("src");
return resolve(this);
});
}
syncPlay() {
debug.log("[AudioPlayer] sync play called");
this.audio.play().catch(this.audioErrorHandle);
return this;
}
async play() {
debug.log("[AudioPlayer] play called");
await this.audio.play().catch(this.audioErrorHandle);
return this;
}
async pause() {
return new Promise((resolve) => {
debug.log("[AudioPlayer] pause called");
this.audio.pause();
return resolve(this);
});
}
set src(url) {
this._src = url;
if (!url) {
void this.clear();
return;
}
this.audio.src = url;
}
get src() {
return this._src;
}
get currentSrc() {
return this.audio.currentSrc;
}
set volume(value) {
if (this.gainNode) {
this.gainNode.gain.value = value;
return;
}
this.audio.volume = value;
}
get volume() {
return this.gainNode ? this.gainNode.gain.value : this.audio.volume;
}
get playbackRate() {
return this.audio.playbackRate;
}
set playbackRate(value) {
this.audio.playbackRate = value;
}
get currentTime() {
return this.audio.currentTime;
}
}
export class ChaimuPlayer extends BasePlayer {
static name = "ChaimuPlayer";
audioBuffer;
sourceNode;
gainNode;
audioShifter;
cleanerRunned = false;
isPlaying = false;
// Для потоковой реализации
bufferQueue = [];
isDecoding = false;
isStreamEnded = false;
streamReader = null;
streamBuffering = false;
constructor(chaimu, src) {
super(chaimu, src);
this.audioContext = chaimu.audioContext;
}
async fetchAudioStream() {
if (!this._src) {
throw new Error("No audio source provided");
}
if (!this.audioContext) {
throw new Error("No audio context available");
}
debug.log(`[ChaimuPlayer] Starting stream fetch from ${this._src}...`);
try {
const response = await fetch(this._src);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const reader = response.body.getReader();
this.streamReader = reader;
const contentType = response.headers.get("Content-Type");
const isMP3 = contentType.includes("audio/mpeg");
// Предполагаем, что это PCM или другой поддерживаемый формат
// Для MP3 потребуется декодирование с использованием библиотеки или другого подхода
const decodeAudioData = this.audioContext.decodeAudioData.bind(this.audioContext);
const processChunk = async (chunk) => {
try {
const audioBuffer = await decodeAudioData(chunk.buffer);
this.bufferQueue.push(audioBuffer);
this.schedulePlayback();
} catch (error) {
console.error("[ChaimuPlayer] Decoding error:", error);
}
};
while (true) {
const { done, value } = await reader.read();
if (done) {
debug.log("[ChaimuPlayer] Stream ended");
this.isStreamEnded = true;
break;
}
await processChunk(value);
}
} catch (error) {
console.error("[ChaimuPlayer] Stream fetch error:", error);
}
}
schedulePlayback() {
if (this.isPlaying || this.bufferQueue.length === 0) return;
const audioBuffer = this.bufferQueue.shift();
const source = this.audioContext.createBufferSource();
source.buffer = audioBuffer;
if (this.audioShifter) {
this.audioShifter.disconnect();
this.audioShifter = null;
}
this.audioShifter = new PitchShifter(this.audioContext, audioBuffer, 1024);
this.audioShifter.tempo = this.chaimu.video.playbackRate;
this.audioShifter.pitch = this.chaimu.video.playbackRate;
source.connect(this.audioShifter.input);
this.audioShifter.connect(this.gainNode);
source.start();
this.isPlaying = true;
source.onended = () => {
this.isPlaying = false;
this.schedulePlayback();
};
}
initAudioBooster() {
if (!this.audioContext) {
return this;
}
if (this.gainNode) {
this.gainNode.disconnect();
}
this.gainNode = this.audioContext.createGain();
this.gainNode.connect(this.audioContext.destination);
return this;
}
async init() {
this.initAudioBooster();
this.fetchAudioStream(); // Запуск потоковой загрузки
return this;
}
lipSync(mode = false) {
debug.log("[ChaimuPlayer] lipsync video", this.chaimu.video, this);
if (!this.chaimu.video) {
return this;
}
if (!mode) {
debug.log("[ChaimuPlayer] lipsync mode isn't set");
return this;
}
debug.log(`[ChaimuPlayer] lipsync mode is ${mode}`);
switch (mode) {
case "play":
case "playing":
case "ratechange":
case "seeked": {
if (!this.chaimu.video.paused) {
this.syncPlay();
}
return this;
}
case "pause":
case "waiting": {
void this.pause();
return this;
}
default: {
return this;
}
}
}
async reopenCtx() {
if (!this.audioContext) {
throw new Error("No audio context available");
}
try {
await this.audioContext.close();
} catch {
// Игнорируем ошибки закрытия контекста
}
return this;
}
async clear() {
if (!this.audioContext) {
throw new Error("No audio context available");
}
debug.log("clear audio context");
this.cleanerRunned = true;
await this.pause();
if (this.sourceNode) {
this.sourceNode.stop();
this.sourceNode.disconnect(this.gainNode);
this.sourceNode = undefined;
}
if (this.audioShifter) {
this.audioShifter.disconnect();
this.audioShifter = undefined;
}
if (this.gainNode) {
this.gainNode.disconnect();
this.gainNode = undefined;
}
// Очистка буферов
this.bufferQueue = [];
await this.reopenCtx();
this.audioContext = initAudioContext();
this.initAudioBooster();
this.cleanerRunned = false;
return this;
}
async start() {
if (!this.audioContext) {
throw new Error("No audio context available");
}
if (this.cleanerRunned) {
debug.log("The other cleaner is still running, waiting...");
return this;
}
debug.log("starting audio");
await this.play();
this.schedulePlayback();
return this;
}
async pause() {
if (!this.audioContext) {
throw new Error("No audio context available");
}
if (this.audioContext.state !== "running") {
return this;
}
await this.audioContext.suspend();
return this;
}
async play() {
if (!this.audioContext) {
throw new Error("No audio context available");
}
await this.audioContext.resume();
return this;
}
set src(url) {
this._src = url;
}
get src() {
return this._src;
}
get currentSrc() {
return this._src;
}
set volume(value) {
if (this.gainNode) {
this.gainNode.gain.value = value;
}
}
get volume() {
return this.gainNode ? this.gainNode.gain.value : 0;
}
set playbackRate(value) {
if (this.audioShifter) {
this.audioShifter.tempo = value;
this.audioShifter.pitch = value;
}
}
get playbackRate() {
return this.audioShifter ? this.audioShifter.tempo : 0;
}
get currentTime() {
return this.chaimu.video.currentTime;
}
}
Потоковая загрузка аудио (fetchAudioStream):
fetch с чтением тела ответа через ReadableStream.decodeAudioData и добавляются в bufferQueue.schedulePlayback для воспроизведения.Буферизация и воспроизведение (schedulePlayback):
isPlaying) и есть ли доступные буферы.BufferSourceNode для каждого дескодированного AudioBuffer.PitchShifter для изменения темпа и тона.gainNode для контроля громкости.onended для воспроизведения следующего чанка.Инициализация и очистка:
init запускает потоковую загрузку.clear останавливает воспроизведение, очищает буфер и сбрасывает состояние плеера.Синхронизация с видео (lipSync):
Управление громкостью и скоростью воспроизведения:
gainNode для управления громкостью.PitchShifter используется для изменения темпа и тона аудио в реальном времени.Формат аудио: Предложенная реализация предполагает, что аудио поток может быть декодирован с помощью decodeAudioData. Для некоторых форматов, таких как MP3, этот метод может не поддерживать потоковую декодировку по чанкам. В таких случаях может потребоваться дополнительная обработка или использование специальной библиотеки для потокового декодирования.
Управление буфером: В данном примере буферизация очень проста. В реальном приложении может понадобиться более сложная логика для управления размером буфера, обработки ошибок и т.д.
Оптимизация производительности: Постоянное создание и уничтожение узлов может привести к утечкам памяти. Убедитесь, что все узлы правильно отключаются и освобождаются.
Совместимость: Убедитесь, что все используемые API поддерживаются в браузерах, нацеленных вашим приложением.
Этот обновленный код добавляет базовую потоковую обработку с поддержкой чанков и буферизации. В зависимости от специфических требований вашего проекта, может потребоваться дополнительная настройка и оптимизация.
Not yet included in a published release.