flat:语音功能优化

This commit is contained in:
史典卓
2025-07-22 15:20:21 +08:00
parent ea04387b58
commit 58c36c01a0
11 changed files with 229 additions and 479 deletions

15
App.vue
View File

@@ -3,11 +3,11 @@ import { reactive, inject, onMounted } from 'vue';
import { onLaunch, onShow, onHide } from '@dcloudio/uni-app'; import { onLaunch, onShow, onHide } from '@dcloudio/uni-app';
import useUserStore from './stores/useUserStore'; import useUserStore from './stores/useUserStore';
import useDictStore from './stores/useDictStore'; import useDictStore from './stores/useDictStore';
import { setupWechatShare, generateShareLink } from '@/utils/wechatShare.js';
const { $api, navTo, appendScriptTagElement } = inject('globalFunction'); const { $api, navTo, appendScriptTagElement } = inject('globalFunction');
import config from '@/config.js'; import config from '@/config.js';
onLaunch((options) => { onLaunch((options) => {
useUserStore().initSeesionId(); //更新
useDictStore().getDictData(); useDictStore().getDictData();
// uni.hideTabBar(); // uni.hideTabBar();
@@ -30,7 +30,7 @@ onMounted(() => {
// #ifndef MP-WEIXIN // #ifndef MP-WEIXIN
appendScriptTagElement('https://qd.zhaopinzao8dian.com/file/csn/jweixin-1.4.0.js').then(() => { appendScriptTagElement('https://qd.zhaopinzao8dian.com/file/csn/jweixin-1.4.0.js').then(() => {
console.log('✅ 微信 JSSDK 加载完成'); console.log('✅ 微信 JSSDK 加载完成');
signatureFn(); // signatureFn();
}); });
// #endif // #endif
}); });
@@ -42,17 +42,6 @@ onShow(() => {
onHide(() => { onHide(() => {
console.log('App Hide'); console.log('App Hide');
}); });
function signatureFn() {
const link = generateShareLink();
// console.log('首页link', link);
setupWechatShare({
title: config.shareConfig.title,
desc: config.shareConfig.desc,
link: link,
imgUrl: config.shareConfig.imgUrl,
});
}
</script> </script>
<style> <style>

View File

@@ -30,7 +30,6 @@ const props = defineProps({
}); });
const readMsg = useReadMsg(); const readMsg = useReadMsg();
const currentItem = ref(0); const currentItem = ref(0);
console.log(readMsg);
const tabbarList = computed(() => [ const tabbarList = computed(() => [
{ {
id: 0, id: 0,

View File

@@ -1,387 +1,246 @@
import { import {
ref, ref,
onUnmounted onUnmounted
} from 'vue'; } from 'vue'
import {
$api,
function mergeText(prevText, newText) { } from '../common/globalFunction';
if (newText.startsWith(prevText)) {
return newText; // 直接替换,避免重复拼接
}
return prevText + newText; // 兼容意外情况
}
export function useAudioRecorder(wsUrl) { import config from '@/config'
// 状态变量
const isRecording = ref(false);
const isStopping = ref(false);
const isSocketConnected = ref(false);
const recordingDuration = ref(0);
const audioDataForDisplay = ref(new Array(16).fill(0.01));
const volumeLevel = ref(0);
// 音频相关 export function useAudioRecorder() {
const audioContext = ref(null); const isRecording = ref(false)
const mediaStream = ref(null); const isStopping = ref(false)
const workletNode = ref(null); const isSocketConnected = ref(false)
const analyser = ref(null); const recordingDuration = ref(0)
// 网络相关 const audioDataForDisplay = ref(new Array(16).fill(0))
const socket = ref(null); const volumeLevel = ref(0)
// 配置常量 const recognizedText = ref('')
const SAMPLE_RATE = 16000; const lastFinalText = ref('')
const SILENCE_THRESHOLD = 0.05; // 静音阈值 (0-1)
const SILENCE_DURATION = 100; // 静音持续时间(ms)后切片
const MIN_SOUND_DURATION = 200; // 最小有效声音持续时间(ms)
// 音频处理变量 let audioStream = null
const lastSoundTime = ref(0); let audioContext = null
const audioChunks = ref([]); let audioInput = null
const currentChunkStartTime = ref(0); let scriptProcessor = null
const silenceStartTime = ref(0); let websocket = null
let durationTimer = null
// 语音识别结果 const generateUUID = () => {
const recognizedText = ref(''); return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11)
const lastFinalText = ref(''); // 保存最终确认的文本 .replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
// AudioWorklet处理器代码 ).replace(/-/g, '')
const workletProcessorCode = `
class AudioProcessor extends AudioWorkletProcessor {
constructor(options) {
super();
this.silenceThreshold = options.processorOptions.silenceThreshold;
this.sampleRate = options.processorOptions.sampleRate;
this.samplesPerChunk = Math.floor(this.sampleRate * 0.05); // 50ms的块
this.buffer = new Int16Array(this.samplesPerChunk);
this.index = 0;
this.lastUpdate = 0;
} }
calculateVolume(inputs) { const fetchWsUrl = async () => {
const input = inputs[0]; const res = await $api.createRequest('/app/speech/getToken')
if (!input || input.length === 0) return 0; if (res.code !== 200) throw new Error('无法获取语音识别 wsUrl')
const wsUrl = res.msg
let sum = 0; return wsUrl
const inputChannel = input[0];
for (let i = 0; i < inputChannel.length; i++) {
sum += inputChannel[i] * inputChannel[i];
}
return Math.sqrt(sum / inputChannel.length);
} }
process(inputs) { function extractWsParams(wsUrl) {
const now = currentTime; const url = new URL(wsUrl)
const volume = this.calculateVolume(inputs); const appkey = url.searchParams.get('appkey')
const token = url.searchParams.get('token')
// 每50ms发送一次分析数据 return {
if (now - this.lastUpdate > 0.05) { appkey,
this.lastUpdate = now; token
// 简单的频率分析 (模拟16个频段)
const simulatedFreqData = [];
for (let i = 0; i < 16; i++) {
simulatedFreqData.push(
Math.min(1, volume * 10 + (Math.random() * 0.2 - 0.1))
);
}
this.port.postMessage({
type: 'analysis',
volume: volume,
frequencyData: simulatedFreqData,
isSilent: volume < this.silenceThreshold,
timestamp: now
});
}
// 原始音频处理
const input = inputs[0];
if (input && input.length > 0) {
const inputChannel = input[0];
for (let i = 0; i < inputChannel.length; i++) {
this.buffer[this.index++] = Math.max(-32768, Math.min(32767, inputChannel[i] * 32767));
if (this.index >= this.samplesPerChunk) {
this.port.postMessage({
type: 'audio',
audioData: this.buffer.buffer,
timestamp: now
}, [this.buffer.buffer]);
this.buffer = new Int16Array(this.samplesPerChunk);
this.index = 0;
} }
} }
}
return true;
}
}
registerProcessor('audio-processor', AudioProcessor);
`;
// 初始化WebSocket连接
const initSocket = (wsUrl) => {
return new Promise((resolve, reject) => {
socket.value = new WebSocket(wsUrl);
socket.value.onopen = () => { const connectWebSocket = async () => {
console.log('open') const wsUrl = await fetchWsUrl()
isSocketConnected.value = true;
resolve();
};
socket.value.onerror = (error) => {
reject(error);
};
socket.value.onclose = () => {
isSocketConnected.value = false;
};
socket.value.onmessage = handleMessage;
});
};
const handleMessage = (values) => {
try {
const data = JSON.parse(event.data);
if (data.text) {
const { const {
asrEnd, appkey,
text token
} = data } = extractWsParams(wsUrl)
if (asrEnd === 'true') { return new Promise((resolve, reject) => {
recognizedText.value += data.text; websocket = new WebSocket(wsUrl)
} else { websocket.binaryType = 'arraybuffer'
lastFinalText.value = '';
}
}
} catch (error) {
console.error('解析识别结果失败:', error);
}
}
// 处理音频切片 websocket.onopen = () => {
const processAudioChunk = (isSilent) => { isSocketConnected.value = true
const now = Date.now();
if (!isSilent) { // 发送 StartTranscription 消息(参考 demo.html
// 检测到声音 const startTranscriptionMessage = {
lastSoundTime.value = now; header: {
appkey: appkey, // 不影响使用,可留空或由 wsUrl 带入
if (silenceStartTime.value > 0) { namespace: 'SpeechTranscriber',
// 从静音恢复到有声音 name: 'StartTranscription',
silenceStartTime.value = 0; task_id: generateUUID(),
} message_id: generateUUID()
} else {
// 静音状态
if (silenceStartTime.value === 0) {
silenceStartTime.value = now;
}
// 检查是否达到静音切片条件
if (now - silenceStartTime.value >= SILENCE_DURATION &&
now - currentChunkStartTime.value >= MIN_SOUND_DURATION) {
sendCurrentChunk();
}
}
};
// 发送当前音频块
const sendCurrentChunk = () => {
if (audioChunks.value.length === 0 || !socket.value || socket.value.readyState !== WebSocket.OPEN) {
return;
}
try {
// 合并所有块
const totalBytes = audioChunks.value.reduce((total, chunk) => total + chunk.byteLength, 0);
const combined = new Int16Array(totalBytes / 2);
let offset = 0;
audioChunks.value.forEach(chunk => {
const samples = new Int16Array(chunk);
combined.set(samples, offset);
offset += samples.length;
});
// 发送合并后的数据
socket.value.send(combined.buffer);
audioChunks.value = [];
// 记录新块的开始时间
currentChunkStartTime.value = Date.now();
silenceStartTime.value = 0;
} catch (error) {
console.error('发送音频数据时出错:', error);
}
};
// 开始录音
const startRecording = async () => {
if (isRecording.value) return;
try {
// 重置状态
recognizedText.value = '';
lastFinalText.value = '';
// 重置状态
recordingDuration.value = 0;
audioChunks.value = [];
lastSoundTime.value = 0;
currentChunkStartTime.value = Date.now();
silenceStartTime.value = 0;
// 初始化WebSocket
await initSocket(wsUrl);
// 获取音频流
mediaStream.value = await navigator.mediaDevices.getUserMedia({
audio: {
sampleRate: SAMPLE_RATE,
channelCount: 1,
echoCancellation: true,
noiseSuppression: true,
autoGainControl: false
}, },
video: false payload: {
}); format: 'pcm',
sample_rate: 16000,
// 创建音频上下文 enable_intermediate_result: true,
audioContext.value = new(window.AudioContext || window.webkitAudioContext)({ enable_punctuation_prediction: true,
sampleRate: SAMPLE_RATE enable_inverse_text_normalization: true
});
// 注册AudioWorklet
const blob = new Blob([workletProcessorCode], {
type: 'application/javascript'
});
const workletUrl = URL.createObjectURL(blob);
await audioContext.value.audioWorklet.addModule(workletUrl);
URL.revokeObjectURL(workletUrl);
// 创建AudioWorkletNode
workletNode.value = new AudioWorkletNode(audioContext.value, 'audio-processor', {
processorOptions: {
silenceThreshold: SILENCE_THRESHOLD,
sampleRate: SAMPLE_RATE
} }
});
// 处理音频数据
workletNode.value.port.onmessage = (e) => {
if (e.data.type === 'audio') {
audioChunks.value.push(e.data.audioData);
} else if (e.data.type === 'analysis') {
audioDataForDisplay.value = e.data.frequencyData;
volumeLevel.value = e.data.volume;
processAudioChunk(e.data.isSilent);
} }
}; websocket.send(JSON.stringify(startTranscriptionMessage))
resolve()
// 连接音频节点
const source = audioContext.value.createMediaStreamSource(mediaStream.value);
source.connect(workletNode.value);
workletNode.value.connect(audioContext.value.destination);
isRecording.value = true;
} catch (error) {
console.error('启动录音失败:', error);
cleanup();
throw error;
} }
};
// 停止录音 websocket.onerror = (e) => {
const stopRecording = async () => { isSocketConnected.value = false
if (!isRecording.value || isStopping.value) return; reject(e)
}
isStopping.value = true; websocket.onclose = () => {
isSocketConnected.value = false
}
websocket.onmessage = (e) => {
const msg = JSON.parse(e.data)
const name = msg?.header?.name
const payload = msg?.payload
switch (name) {
case 'TranscriptionResultChanged': {
// 中间识别文本(可选:使用 stash_result.unfixedText 更精确)
const text = payload?.unfixed_result || payload?.result || ''
lastFinalText.value = text
break
}
case 'SentenceBegin': {
// 可选:开始新的一句,重置状态
// console.log('开始新的句子识别')
break
}
case 'SentenceEnd': {
const text = payload?.result || ''
const confidence = payload?.confidence || 0
if (text && confidence > 0.5) {
recognizedText.value += text
lastFinalText.value = ''
// console.log('识别完成:', {
// text,
// confidence
// })
}
break
}
case 'TranscriptionStarted': {
// console.log('识别任务已开始')
break
}
case 'TranscriptionCompleted': {
lastFinalText.value = ''
// console.log('识别全部完成')
break
}
case 'TaskFailed': {
console.error('识别失败:', msg?.header?.status_text)
break
}
default:
console.log('未知消息类型:', name, msg)
break
}
}
})
}
const startRecording = async () => {
if (isRecording.value) return
try { try {
// 发送最后一个音频块(无论是否静音) recognizedText.value = ''
sendCurrentChunk(); lastFinalText.value = ''
await connectWebSocket()
// 发送结束标记 audioStream = await navigator.mediaDevices.getUserMedia({
if (socket.value?.readyState === WebSocket.OPEN) { audio: true
socket.value.send(JSON.stringify({ })
action: 'end', audioContext = new(window.AudioContext || window.webkitAudioContext)({
duration: recordingDuration.value sampleRate: 16000
})); })
audioInput = audioContext.createMediaStreamSource(audioStream)
scriptProcessor = audioContext.createScriptProcessor(2048, 1, 1)
await new Promise(resolve => { scriptProcessor.onaudioprocess = (event) => {
if (socket.value.bufferedAmount === 0) { const input = event.inputBuffer.getChannelData(0)
resolve(); const pcm = new Int16Array(input.length)
} else { let sum = 0
const timer = setInterval(() => { for (let i = 0; i < input.length; ++i) {
if (socket.value.bufferedAmount === 0) { const s = Math.max(-1, Math.min(1, input[i]))
clearInterval(timer); pcm[i] = s * 0x7FFF
resolve(); sum += s * s
}
}, 50);
}
});
socket.value.close();
} }
cleanup(); volumeLevel.value = Math.sqrt(sum / input.length)
audioDataForDisplay.value = Array(16).fill(volumeLevel.value)
} catch (error) { if (websocket?.readyState === WebSocket.OPEN) {
console.error('停止录音时出错:', error); websocket.send(pcm.buffer)
throw error;
} finally {
isStopping.value = false;
} }
};
// 清理资源
const cleanup = () => {
if (mediaStream.value) {
mediaStream.value.getTracks().forEach(track => track.stop());
mediaStream.value = null;
} }
if (workletNode.value) { audioInput.connect(scriptProcessor)
workletNode.value.disconnect(); scriptProcessor.connect(audioContext.destination)
workletNode.value = null;
}
if (audioContext.value && audioContext.value.state !== 'closed') { isRecording.value = true
audioContext.value.close(); recordingDuration.value = 0
audioContext.value = null; durationTimer = setInterval(() => recordingDuration.value++, 1000)
} } catch (err) {
console.error('启动失败:', err)
audioChunks.value = [];
isRecording.value = false;
isSocketConnected.value = false;
};
/// 取消录音
const cancelRecording = async () => {
if (!isRecording.value || isStopping.value) return;
isStopping.value = true;
try {
if (socket.value?.readyState === WebSocket.OPEN) {
console.log('发送结束标记...');
socket.value.send(JSON.stringify({
action: 'cancel'
}));
socket.value.close();
}
cleanup() cleanup()
} catch (error) {
console.error('取消录音时出错:', error);
throw error;
} finally {
isStopping.value = false;
} }
}; }
const stopRecording = () => {
if (!isRecording.value || isStopping.value) return
isStopping.value = true
if (websocket?.readyState === WebSocket.OPEN) {
websocket.send(JSON.stringify({
header: {
namespace: 'SpeechTranscriber',
name: 'StopTranscription',
message_id: generateUUID()
}
}))
websocket.close()
}
cleanup()
isStopping.value = false
}
const cancelRecording = () => {
if (!isRecording.value || isStopping.value) return
isStopping.value = true
websocket?.close()
cleanup()
isStopping.value = false
}
const cleanup = () => {
clearInterval(durationTimer)
scriptProcessor?.disconnect()
audioInput?.disconnect()
audioStream?.getTracks().forEach(track => track.stop())
audioContext?.close()
audioStream = null
audioContext = null
audioInput = null
scriptProcessor = null
websocket = null
isRecording.value = false
isSocketConnected.value = false
}
onUnmounted(() => { onUnmounted(() => {
if (isRecording.value) { if (isRecording.value) stopRecording()
stopRecording(); })
}
});
return { return {
isRecording, isRecording,
@@ -390,10 +249,10 @@ export function useAudioRecorder(wsUrl) {
recordingDuration, recordingDuration,
audioDataForDisplay, audioDataForDisplay,
volumeLevel, volumeLevel,
startRecording,
stopRecording,
recognizedText, recognizedText,
lastFinalText, lastFinalText,
startRecording,
stopRecording,
cancelRecording cancelRecording
}; }
} }

View File

@@ -143,7 +143,6 @@ import { reactive, inject, watch, ref, onMounted, computed } from 'vue';
import { onLoad, onShow, onHide } from '@dcloudio/uni-app'; import { onLoad, onShow, onHide } from '@dcloudio/uni-app';
import dictLabel from '@/components/dict-Label/dict-Label.vue'; import dictLabel from '@/components/dict-Label/dict-Label.vue';
import RadarMap from './component/radarMap.vue'; import RadarMap from './component/radarMap.vue';
import { updateWechatShare, generateShareLink } from '@/utils/wechatShare.js';
const { $api, navTo, getLenPx, parseQueryParams, navBack, isEmptyObject } = inject('globalFunction'); const { $api, navTo, getLenPx, parseQueryParams, navBack, isEmptyObject } = inject('globalFunction');
import config from '@/config.js'; import config from '@/config.js';
const matchingDegree = ref(['一般', '良好', '优秀', '极好']); const matchingDegree = ref(['一般', '良好', '优秀', '极好']);
@@ -170,31 +169,11 @@ onShow(() => {
} }
}); });
onHide(() => {
const link = generateShareLink();
// console.log('首页link', link);
updateWechatShare({
title: config.shareConfig.title,
desc: config.shareConfig.desc,
link: link,
imgUrl: config.shareConfig.imgUrl,
});
});
function initLoad(option) { function initLoad(option) {
const jobId = atob(option.jobId); const jobId = atob(option.jobId);
if (jobId !== jobIdRef.value) { if (jobId !== jobIdRef.value) {
jobIdRef.value = jobId; jobIdRef.value = jobId;
getDetail(jobId).then((data) => { getDetail(jobId);
const link = generateShareLink(btoa(data.jobId));
// console.log('详情', link);
updateWechatShare({
title: '职位推荐:' + data.jobTitle,
desc: data.description,
link: link,
imgUrl: 'https://qd.zhaopinzao8dian.com/file/csn/qd_shareLogo.jpg',
});
});
} }
} }

View File

@@ -285,7 +285,7 @@ const {
volumeLevel, volumeLevel,
recognizedText, recognizedText,
lastFinalText, lastFinalText,
} = useAudioRecorder(config.vioceBaseURl); } = useAudioRecorder();
const { speak, pause, resume, isSpeaking, isPaused, cancelAudio } = useTTSPlayer(config.speechSynthesis); const { speak, pause, resume, isSpeaking, isPaused, cancelAudio } = useTTSPlayer(config.speechSynthesis);

View File

@@ -412,7 +412,8 @@ function getJobList(type = 'add') {
list.value = []; list.value = [];
pageState.page = 1; pageState.page = 1;
pageState.maxPage = 2; pageState.maxPage = 2;
waterfallsFlowRef.value.refresh(); // waterfallsFlowRef.value.refresh();
if (waterfallsFlowRef.value) waterfallsFlowRef.value.refresh();
} }
let params = { let params = {
current: pageState.page, current: pageState.page,

View File

@@ -46,18 +46,22 @@ export const useReadMsg = defineStore('readMsg', () => {
// 设置 TabBar 角标 // 设置 TabBar 角标
function updateTabBarBadge() { function updateTabBarBadge() {
const count = unreadCount.value const count = unreadCount.value
const index = 3
const countVal = count > 99 ? '99+' : String(count)
if (count === 0) { if (count === 0) {
uni.removeTabBarBadge({ uni.removeTabBarBadge({
index: 3 index
}) // 替换为你消息页面的 TabBar index }) // 替换为你消息页面的 TabBar index
} else {
const val = count > 99 ? '99+' : String(count)
badges.value[index] = { badges.value[index] = {
count: val count: 0
}
} else {
badges.value[index] = {
count: countVal
} }
uni.setTabBarBadge({ uni.setTabBarBadge({
index: 3, index,
text: val text: countVal
}) })
} }
} }

BIN
unpackage/.DS_Store vendored

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,81 +0,0 @@
import config from "@/config.js"
import {
$api
} from '../common/globalFunction';
export function setupWechatShare({
title,
desc,
link,
imgUrl
}) {
// 通过后端接口获取签名(必须)
$api.createRequest('/app/job/getWechatUrl', {
imgUrl: location.href.split('#')[0]
}, 'POST').then((resData) => {
const {
appId,
timestamp,
nonceStr,
signature
} = resData.data
wx.config({
debug: false,
appId,
timestamp,
nonceStr,
signature,
jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData']
})
wx.ready(() => {
// 分享给好友
wx.updateAppMessageShareData({
title,
desc,
link,
imgUrl,
success: () => {
$api.msg('分享配置成功')
}
})
// 分享到朋友圈
wx.updateTimelineShareData({
title,
link,
imgUrl,
success: () => {
$api.msg('朋友圈分享配置成功')
}
})
}).catch((err) => {
$api.msg('获取微信签名失败')
console.error('获取微信签名失败:', err);
});
})
}
export function updateWechatShare({
title,
desc,
link,
imgUrl
}) {
if (!window.wx) return;
wx.updateAppMessageShareData({
title,
desc,
link,
imgUrl,
success: () => console.log('分享配置成功'),
fail: (err) => console.warn('分享配置失败', err)
});
}
// tools
export function generateShareLink(jobId) {
const base = location.origin + '/app/static/share.html';
const query = jobId ? `?jobId=${jobId}&_t=${Date.now()}` : `?_t=${Date.now()}`;
return `${base}${query}`;
}