refactor: PC端音频引擎重写(Web Audio API) + TTS重新生成音频

This commit is contained in:
testadmin
2026-05-13 12:53:50 +08:00
parent f1ff404ddf
commit 03f30ae157
154 changed files with 75 additions and 56 deletions
+72 -53
View File
@@ -1,60 +1,79 @@
let audioCtx = null
const bufferCache = new Map()
let chainId = 0
var audio = null, function getAudioContext() {
Mp3List=[], if (!audioCtx) {
num=0; audioCtx = new (window.AudioContext || window.webkitAudioContext)()
const resume = () => {
if (audioCtx.state === 'suspended') audioCtx.resume()
}
document.addEventListener('click', resume, { once: true })
document.addEventListener('touchstart', resume, { once: true })
}
if (audioCtx.state === 'suspended') audioCtx.resume()
return audioCtx
}
function getSoundBase() {
try {
const config = JSON.parse(localStorage.getItem('WebConfig') || '{}')
return config.sound && config.sound == 2 ? '/static/sound2/' : '/static/sound/'
} catch (e) {
return '/static/sound/'
}
}
function audioMp3(mp3List,audios){ async function loadBuffer(key) {
var WebConfig=JSON.parse(localStorage.getItem("WebConfig")); if (bufferCache.has(key)) return bufferCache.get(key)
audio=audios; try {
Mp3List=mp3List; const ctx = getAudioContext()
var mp3=new Object(); const url = getSoundBase() + key
mp3.mp3List=mp3List; const response = await fetch(url)
mp3.url="/static/sound/"; if (!response.ok) return null
if(WebConfig.sound&&WebConfig.sound==2){mp3.url="/static/sound2/";} const arrayBuffer = await response.arrayBuffer()
const audioBuffer = await ctx.decodeAudioData(arrayBuffer)
bufferCache.set(key, audioBuffer)
return audioBuffer
} catch (e) {
return null
}
}
mp3.auto_play=false; function playBuffer(buffer) {
mp3.loop=false; return new Promise((resolve) => {
mp3.Play=function(){ const ctx = getAudioContext()
const source = ctx.createBufferSource()
source.buffer = buffer
source.connect(ctx.destination)
source.onended = resolve
source.start(0)
})
}
audio.src=this.url+this.mp3List[0]; function audioMp3(mp3List, _audioEl) {
audio.load(); const myId = ++chainId
audio.play(); const list = mp3List || []
}
mp3.Muted=function(){
audio.muted ? audio.muted = false : audio.muted = true;
}
mp3.volumeAdd=function(){
if(audio.volume.toFixed(1)>=1){
audio.volume=1
}else{
audio.volume = audio.volume + 0.1;
}
}
mp3.volumeMinus=function(){
if(audio.volume.toFixed(1)<=0){
audio.volume=0
}else{
audio.volume = audio.volume - 0.1;
}
}
audio.addEventListener("ended", nextAudio);
return mp3;
}
function nextAudio(){
var WebConfig=JSON.parse(localStorage.getItem("WebConfig"));
num+=1
if(num<Mp3List.length){
audio.src="/static/sound/"+Mp3List[num];
if(WebConfig.sound&&WebConfig.sound==2){audio.src="/static/sound2/"+Mp3List[num];}
audio.play();
}else{
audio.pause();
audio.currentTime = 0.0;
// console.log("播完")
num=0;
}
}
export {audioMp3}; return {
Play() {
;(async () => {
for (let i = 0; i < list.length; i++) {
if (chainId !== myId) return
const buffer = await loadBuffer(list[i])
if (!buffer) continue
if (chainId !== myId) return
await playBuffer(buffer)
}
})()
},
Pause() {
chainId++
},
Muted() {},
volumeAdd() {},
volumeMinus() {}
}
}
export { audioMp3 }
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

Some files were not shown because too many files have changed in this diff Show More