AI模块联调

This commit is contained in:
francis_fh
2026-01-22 18:58:19 +08:00
parent 4dce6d3e39
commit b92e3b8adb
23 changed files with 424 additions and 453 deletions

View File

@@ -1,297 +1,195 @@
import {
ref,
onUnmounted,
onBeforeUnmount,
onMounted
} from 'vue'
import {
onHide,
onUnload
} from '@dcloudio/uni-app'
import WavDecoder from '@/lib/wav-decoder@1.3.0.js'
export function useTTSPlayer(wsUrl) {
const isSpeaking = ref(false)
const isPaused = ref(false)
const isComplete = ref(false)
// #ifdef H5
const audioContext = typeof window !== 'undefined' && (window.AudioContext || window.webkitAudioContext)
? new(window.AudioContext || window.webkitAudioContext)()
: null
// #endif
// #ifdef MP-WEIXIN
const audioContext = null // 微信小程序不支持 AudioContext
// #endif
let playTime = audioContext ? audioContext.currentTime : 0
let sourceNodes = []
let socket = null
let sampleRate = 16000
let numChannels = 1
let isHeaderDecoded = false
let pendingText = null
let currentPlayId = 0
let activePlayId = 0
const speak = (text) => {
if (!audioContext) {
console.warn('⚠️ TTS not supported in current environment');
return;
}
console.log('🎤 TTS speak function called');
console.log('📝 Text to synthesize:', text ? text.substring(0, 100) + '...' : 'No text');
console.log('🔗 WebSocket URL:', wsUrl);
currentPlayId++
const myPlayId = currentPlayId
console.log('🆔 Play ID:', myPlayId);
reset()
pendingText = text
activePlayId = myPlayId
console.log('✅ Speak function setup complete');
}
const pause = () => {
if (!audioContext) {
console.warn('⚠️ TTS not supported in current environment');
return;
}
console.log('⏸️ TTS pause called');
console.log('🔊 AudioContext state:', audioContext.state);
console.log('🔊 Is speaking before pause:', isSpeaking.value);
console.log('⏸️ Is paused before pause:', isPaused.value);
if (audioContext.state === 'running') {
audioContext.suspend()
isPaused.value = true
// 不要设置 isSpeaking.value = false保持当前状态
console.log('✅ Audio paused successfully');
} else {
console.log('⚠️ AudioContext is not running, cannot pause');
}
console.log('🔊 Is speaking after pause:', isSpeaking.value);
console.log('⏸️ Is paused after pause:', isPaused.value);
}
const resume = () => {
if (!audioContext) {
console.warn('⚠️ TTS not supported in current environment');
return;
}
console.log('▶️ TTS resume called');
console.log('🔊 AudioContext state:', audioContext.state);
console.log('🔊 Is speaking before resume:', isSpeaking.value);
console.log('⏸️ Is paused before resume:', isPaused.value);
if (audioContext.state === 'suspended') {
audioContext.resume()
isPaused.value = false
isSpeaking.value = true
console.log('✅ Audio resumed successfully');
} else {
console.log('⚠️ AudioContext is not suspended, cannot resume');
}
console.log('🔊 Is speaking after resume:', isSpeaking.value);
console.log('⏸️ Is paused after resume:', isPaused.value);
}
const cancelAudio = () => {
stop()
}
const stop = () => {
isSpeaking.value = false
isPaused.value = false
isComplete.value = false
playTime = audioContext ? audioContext.currentTime : 0
sourceNodes.forEach(node => {
try {
node.stop()
node.disconnect()
} catch (e) {}
})
sourceNodes = []
if (socket) {
socket.close()
socket = null
}
isHeaderDecoded = false
pendingText = null
}
const reset = () => {
stop()
isSpeaking.value = false
isPaused.value = false
isComplete.value = false
playTime = audioContext ? audioContext.currentTime : 0
initWebSocket()
}
const initWebSocket = () => {
if (!audioContext) {
console.warn('⚠️ WebSocket TTS not supported in current environment');
return;
}
const thisPlayId = currentPlayId
console.log('🔌 Initializing WebSocket connection');
console.log('🔗 WebSocket URL:', wsUrl);
console.log('🆔 This play ID:', thisPlayId);
socket = new WebSocket(wsUrl)
socket.binaryType = 'arraybuffer'
// 设置心跳检测,避免超时
const heartbeatInterval = setInterval(() => {
if (socket && socket.readyState === WebSocket.OPEN) {
socket.send(JSON.stringify({ type: 'ping' }));
}
}, 30000); // 每30秒发送一次心跳
socket.onopen = () => {
console.log('✅ WebSocket connection opened');
if (pendingText && thisPlayId === activePlayId) {
const seepdText = extractSpeechText(pendingText)
console.log('📤 Sending text to TTS server:', seepdText.substring(0, 100) + '...');
socket.send(seepdText)
pendingText = null
} else {
console.log('❌ No pending text or play ID mismatch');
console.log('📝 Pending text exists:', !!pendingText);
console.log('🆔 Play ID match:', thisPlayId === activePlayId);
}
}
socket.onerror = (error) => {
console.error('❌ WebSocket error:', error);
}
socket.onclose = (event) => {
console.log('🔌 WebSocket connection closed:', event.code, event.reason);
clearInterval(heartbeatInterval);
}
socket.onmessage = async (e) => {
if (thisPlayId !== activePlayId) return // 忽略旧播放的消息
if (typeof e.data === 'string') {
try {
const msg = JSON.parse(e.data)
console.log('📨 TTS server message:', msg);
if (msg.status === 'complete') {
console.log('✅ TTS synthesis completed');
isComplete.value = true
// 计算剩余播放时间,确保播放完整
const remainingTime = audioContext ? Math.max(0, (playTime - audioContext.currentTime) * 1000) : 0;
console.log('⏱️ Remaining play time:', remainingTime + 'ms');
setTimeout(() => {
if (thisPlayId === activePlayId) {
console.log('🔇 TTS playback finished, setting isSpeaking to false');
isSpeaking.value = false
}
}, remainingTime + 500) // 额外500ms缓冲时间
}
} catch (e) {
console.log('[TTSPlayer] 文本消息:', e.data)
}
} else if (e.data instanceof ArrayBuffer) {
if (!isHeaderDecoded) {
try {
const decoded = await WavDecoder.decode(e.data)
sampleRate = decoded.sampleRate
numChannels = decoded.channelData.length
decoded.channelData.forEach((channel, i) => {
const audioBuffer = audioContext.createBuffer(1, channel.length,
sampleRate)
audioBuffer.copyToChannel(channel, 0)
playBuffer(audioBuffer)
})
isHeaderDecoded = true
} catch (err) {
console.error('WAV 解码失败:', err)
}
} else {
const pcm = new Int16Array(e.data)
const audioBuffer = pcmToAudioBuffer(pcm, sampleRate, numChannels)
playBuffer(audioBuffer)
}
}
}
}
const pcmToAudioBuffer = (pcm, sampleRate, numChannels) => {
if (!audioContext) return null;
const length = pcm.length / numChannels
const audioBuffer = audioContext.createBuffer(numChannels, length, sampleRate)
for (let ch = 0; ch < numChannels; ch++) {
const channelData = audioBuffer.getChannelData(ch)
for (let i = 0; i < length; i++) {
const sample = pcm[i * numChannels + ch]
channelData[i] = sample / 32768
}
}
return audioBuffer
}
const playBuffer = (audioBuffer) => {
if (!audioContext || !audioBuffer) return;
console.log('🎵 playBuffer called, duration:', audioBuffer.duration + 's');
if (!isSpeaking.value) {
playTime = audioContext.currentTime
console.log('🎵 Starting new audio playback at time:', playTime);
}
const source = audioContext.createBufferSource()
source.buffer = audioBuffer
source.connect(audioContext.destination)
source.start(playTime)
sourceNodes.push(source)
playTime += audioBuffer.duration
isSpeaking.value = true
console.log('🎵 Audio scheduled, new playTime:', playTime);
// 添加音频播放结束监听
source.onended = () => {
console.log('🎵 Audio buffer finished playing');
}
}
onUnmounted(() => {
stop()
})
// 页面刷新/关闭时
onMounted(() => {
if (typeof window !== 'undefined') {
window.addEventListener('beforeunload', cancelAudio)
}
})
onBeforeUnmount(() => {
cancelAudio()
if (typeof window !== 'undefined') {
window.removeEventListener('beforeunload', cancelAudio)
}
})
onHide(cancelAudio)
onUnload(cancelAudio)
import {
ref,
onUnmounted,
onBeforeUnmount,
onMounted
} from 'vue'
import {
onHide,
onUnload
} from '@dcloudio/uni-app'
import WavDecoder from '@/lib/wav-decoder@1.3.0.js'
export function useTTSPlayer(httpUrl) {
const isSpeaking = ref(false)
const isPaused = ref(false)
const isComplete = ref(false)
// #ifdef H5
const audioContext = typeof window !== 'undefined' && (window.AudioContext || window.webkitAudioContext)
? new(window.AudioContext || window.webkitAudioContext)()
: null
// #endif
// #ifdef MP-WEIXIN
const audioContext = null // 微信小程序不支持 AudioContext
// #endif
let currentAudioBuffer = null
let currentSource = null
let playTimeOffset = 0
const speak = async (text) => {
if (!audioContext) {
console.warn('⚠️ TTS not supported in current environment');
return;
}
console.log('🎤 TTS speak function called');
console.log('📝 Text to synthesize:', text ? text.substring(0, 100) + '...' : 'No text');
console.log('🔗 HTTP URL:', httpUrl);
// 停止当前播放
stop()
try {
// 提取要合成的文本
const speechText = extractSpeechText(text)
console.log('📤 Sending text to TTS server via GET:', speechText.substring(0, 100) + '...');
// 构建GET请求URL
const url = `${httpUrl}?text=${encodeURIComponent(speechText)}`
console.log('🔗 Final GET URL:', url);
// 发送GET请求获取语音数据
const response = await fetch(url)
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
// 获取二进制数据
const arrayBuffer = await response.arrayBuffer()
console.log('✅ Received audio data, size:', arrayBuffer.byteLength + ' bytes');
// 解码音频数据
const decoded = await WavDecoder.decode(arrayBuffer)
console.log('✅ Audio decoded, sampleRate:', decoded.sampleRate, 'channels:', decoded.channelData.length);
// 播放音频
playDecodedAudio(decoded)
} catch (error) {
console.error('❌ TTS synthesis failed:', error);
isSpeaking.value = false
isComplete.value = false
}
}
const playDecodedAudio = (decoded) => {
if (!audioContext) return;
// 使用第一个声道的数据
const audioBuffer = audioContext.createBuffer(1, decoded.channelData[0].length, decoded.sampleRate)
audioBuffer.copyToChannel(decoded.channelData[0], 0)
currentAudioBuffer = audioBuffer
// 创建音频源
currentSource = audioContext.createBufferSource()
currentSource.buffer = audioBuffer
currentSource.connect(audioContext.destination)
// 监听播放结束
currentSource.onended = () => {
console.log('🎵 Audio playback completed');
isSpeaking.value = false
isComplete.value = true
}
// 开始播放
currentSource.start()
isSpeaking.value = true
isPaused.value = false
isComplete.value = false
console.log('<27> Audio playback started');
}
const pause = () => {
if (!audioContext || !isSpeaking.value || isPaused.value) {
console.warn('⚠️ Cannot pause TTS playback');
return;
}
console.log('⏸️ TTS pause called');
if (audioContext.state === 'running') {
audioContext.suspend()
isPaused.value = true
// 保存当前播放位置
playTimeOffset = audioContext.currentTime
console.log('✅ Audio paused successfully');
}
}
const resume = () => {
if (!audioContext || !isSpeaking.value || !isPaused.value) {
console.warn('⚠️ Cannot resume TTS playback');
return;
}
console.log('▶️ TTS resume called');
if (audioContext.state === 'suspended') {
audioContext.resume()
isPaused.value = false
console.log('✅ Audio resumed successfully');
}
}
const cancelAudio = () => {
stop()
}
const stop = () => {
console.log('⏹️ TTS stop called');
if (currentSource) {
try {
currentSource.stop()
currentSource.disconnect()
} catch (e) {
console.error('❌ Error stopping audio source:', e);
}
currentSource = null
}
if (audioContext && audioContext.state === 'running') {
try {
audioContext.suspend()
} catch (e) {
console.error('❌ Error suspending audio context:', e);
}
}
isSpeaking.value = false
isPaused.value = false
isComplete.value = false
currentAudioBuffer = null
playTimeOffset = 0
console.log('✅ TTS playback stopped');
}
onUnmounted(() => {
stop()
})
// 页面刷新/关闭时
onMounted(() => {
if (typeof window !== 'undefined') {
window.addEventListener('beforeunload', cancelAudio)
}
})
onBeforeUnmount(() => {
cancelAudio()
if (typeof window !== 'undefined') {
window.removeEventListener('beforeunload', cancelAudio)
}
})
onHide(cancelAudio)
onUnload(cancelAudio)
return {
speak,
pause,
@@ -300,68 +198,68 @@ export function useTTSPlayer(wsUrl) {
isSpeaking,
isPaused,
isComplete
}
}
function extractSpeechText(markdown) {
console.log('🔍 extractSpeechText called');
console.log('📝 Input markdown length:', markdown ? markdown.length : 0);
console.log('📝 Input markdown preview:', markdown ? markdown.substring(0, 200) + '...' : 'No markdown');
const jobRegex = /``` job-json\s*({[\s\S]*?})\s*```/g;
const jobs = [];
let match;
let lastJobEndIndex = 0;
let firstJobStartIndex = -1;
// 提取岗位 json 数据及前后位置
while ((match = jobRegex.exec(markdown)) !== null) {
const jobStr = match[1];
try {
const job = JSON.parse(jobStr);
jobs.push(job);
if (firstJobStartIndex === -1) {
firstJobStartIndex = match.index;
}
lastJobEndIndex = jobRegex.lastIndex;
console.log('✅ Found job:', job.jobTitle);
} catch (e) {
console.warn('JSON 解析失败', e);
}
}
console.log('📊 Jobs found:', jobs.length);
console.log('📍 First job start index:', firstJobStartIndex);
console.log('📍 Last job end index:', lastJobEndIndex);
// 提取引导语(第一个 job-json 之前的文字)
const guideText = firstJobStartIndex > 0 ?
markdown.slice(0, firstJobStartIndex).trim() :
'';
// 提取结束语(最后一个 job-json 之后的文字)
const endingText = lastJobEndIndex < markdown.length ?
markdown.slice(lastJobEndIndex).trim() :
'';
console.log('📝 Guide text:', guideText);
console.log('📝 Ending text:', endingText);
// 岗位信息格式化为语音文本
const jobTexts = jobs.map((job, index) => {
return `${index + 1} 个岗位,岗位名称是:${job.jobTitle},公司是:${job.companyName},薪资:${job.salary},地点:${job.location},学历要求:${job.education},经验要求:${job.experience}`;
});
// 拼接总语音内容
const finalTextParts = [];
if (guideText) finalTextParts.push(guideText);
finalTextParts.push(...jobTexts);
if (endingText) finalTextParts.push(endingText);
const finalText = finalTextParts.join('\n');
console.log('🎤 Final TTS text length:', finalText.length);
console.log('🎤 Final TTS text preview:', finalText.substring(0, 200) + '...');
console.log('🎤 Final TTS text parts count:', finalTextParts.length);
return finalText;
}
}
function extractSpeechText(markdown) {
console.log('🔍 extractSpeechText called');
console.log('📝 Input markdown length:', markdown ? markdown.length : 0);
console.log('📝 Input markdown preview:', markdown ? markdown.substring(0, 200) + '...' : 'No markdown');
const jobRegex = /``` job-json\s*({[\s\S]*?})\s*```/g;
const jobs = [];
let match;
let lastJobEndIndex = 0;
let firstJobStartIndex = -1;
// 提取岗位 json 数据及前后位置
while ((match = jobRegex.exec(markdown)) !== null) {
const jobStr = match[1];
try {
const job = JSON.parse(jobStr);
jobs.push(job);
if (firstJobStartIndex === -1) {
firstJobStartIndex = match.index;
}
lastJobEndIndex = jobRegex.lastIndex;
console.log('✅ Found job:', job.jobTitle);
} catch (e) {
console.warn('JSON 解析失败', e);
}
}
console.log('📊 Jobs found:', jobs.length);
console.log('📍 First job start index:', firstJobStartIndex);
console.log('📍 Last job end index:', lastJobEndIndex);
// 提取引导语(第一个 job-json 之前的文字)
const guideText = firstJobStartIndex > 0 ?
markdown.slice(0, firstJobStartIndex).trim() :
'';
// 提取结束语(最后一个 job-json 之后的文字)
const endingText = lastJobEndIndex < markdown.length ?
markdown.slice(lastJobEndIndex).trim() :
'';
console.log('📝 Guide text:', guideText);
console.log('📝 Ending text:', endingText);
// 岗位信息格式化为语音文本
const jobTexts = jobs.map((job, index) => {
return `${index + 1} 个岗位,岗位名称是:${job.jobTitle},公司是:${job.companyName},薪资:${job.salary},地点:${job.location},学历要求:${job.education},经验要求:${job.experience}`;
});
// 拼接总语音内容
const finalTextParts = [];
if (guideText) finalTextParts.push(guideText);
finalTextParts.push(...jobTexts);
if (endingText) finalTextParts.push(endingText);
const finalText = finalTextParts.join('\n');
console.log('🎤 Final TTS text length:', finalText.length);
console.log('🎤 Final TTS text preview:', finalText.substring(0, 200) + '...');
console.log('🎤 Final TTS text parts count:', finalTextParts.length);
return finalText;
}