inspection-host-computer/src/views/camera/VoiceConversation.vue

773 lines
23 KiB
Vue
Raw Normal View History

2026-07-22 13:33:59 +08:00
<template>
<section class="voice-conversation">
<main class="voice-content">
<header class="hero-title">
<p>低延迟实时语音对讲</p>
</header>
<div class="voice-visual" :class="{ active: connectionStatus === 'connected' }">
<div class="sound-wave sound-wave--left" aria-hidden="true">
<i v-for="index in 7" :key="`left-${index}`"></i>
</div>
<div class="microphone-orbit">
<div class="orbit-glow"></div>
<div class="microphone-core">
<svg viewBox="0 0 48 48" aria-label="麦克风" role="img">
<path d="M24 30a8 8 0 0 0 8-8V12a8 8 0 0 0-16 0v10a8 8 0 0 0 8 8Z" />
<path d="M11 21v2a13 13 0 0 0 26 0v-2M24 36v8M17 44h14" />
</svg>
</div>
</div>
<div class="sound-wave sound-wave--right" aria-hidden="true">
<i v-for="index in 7" :key="`right-${index}`"></i>
</div>
</div>
<div class="floating-status">
<span :class="['status-light', connectionStatus]"></span>
{{ statusMeta.label }}
</div>
<div class="call-controls">
<el-button
v-if="!isCalling"
class="call-button"
:loading="isStarting"
@click="startCall"
>
开始通话
</el-button>
<el-button v-else class="call-button call-button--danger" @click="endCall">
结束通话
</el-button>
<el-button
v-if="isCalling"
class="mute-button"
round
@click="toggleMute"
>
{{ isMuted ? '取消静音' : '静音' }}
</el-button>
</div>
<section class="status-card">
<div class="status-copy">
<p>当前状态<strong>{{ statusMeta.label }}</strong></p>
<span>{{ statusMeta.description }}</span>
</div>
<div class="feature-tags">
<span>高清语音</span>
<span>低延迟</span>
<span>加密传输</span>
</div>
</section>
</main>
<!-- PCM 播放由 AudioWorklet 连续输出不需要额外 audio 元素 -->
</section>
</template>
<script setup>
import { computed, onBeforeUnmount, ref } from 'vue'
import { ElMessage, ElNotification } from 'element-plus'
import { useRobotStore } from '@/stores/robot'
// 机器人实际音频流为 48kHz因此浏览器上传也统一为 48kHz。
const TARGET_SAMPLE_RATE = 48000
const TARGET_CHANNELS = 2
const JOIN_TIMEOUT = 10000
const robotStore = useRobotStore()
// 页面交互状态。
const isCalling = ref(false)
const isStarting = ref(false)
const isMuted = ref(false)
const connectionStatus = ref('idle')
const errorMessage = ref('')
// 本次通话占用的浏览器资源。
const localStream = ref(null)
const webSocket = ref(null)
let audioContext
let mediaStreamSource
let captureNode
let silentGain
let nextPlaybackTime = 0
let resampleFraction = 0
let remoteSampleRate = TARGET_SAMPLE_RATE
let remoteChannels = 1
let callSessionId = 0
const statusMeta = computed(() => {
const statusMap = {
idle: { label: '未连接', description: '点击按钮建立语音通道' },
connecting: { label: '连接中', description: '正在建立音频通道,请稍候' },
connected: { label: '通话中', description: 'PCM 音频通道已建立,可以开始对讲' },
error: { label: '未连接', description: '连接出现异常,请检查后重试' }
}
return statusMap[connectionStatus.value] || statusMap.idle
})
/**
* 统一显示通话异常
* @param {string} message 用户可读的错误说明
* @param {Error} [error] 原始异常
*/
function showError(message, error) {
console.error(message, error)
errorMessage.value = message
connectionStatus.value = 'error'
ElNotification.error({ title: '语音通话异常', message })
}
/**
* 发送 WebSocket 控制消息
* 此方法只允许发送 JSONPCM 音频由 sendPcmFrame 单独以二进制发送
* @param {Object} message 控制消息
*/
function sendControlMessage(message) {
if (webSocket.value?.readyState === WebSocket.OPEN) {
webSocket.value.send(JSON.stringify(message))
}
}
/**
* Float32 PCM 转换成明确的小端 Int16 PCM
* 使用 DataView littleEndian=true避免依赖运行平台的原生字节序
* @param {Float32Array} float32 取值范围为 -1 1 PCM
* @returns {ArrayBuffer} PCM_S16LE 数据
*/
function floatToInt16Pcm(float32, channels = 1) {
const buffer = new ArrayBuffer(float32.length * channels * 2)
const view = new DataView(buffer)
for (let index = 0; index < float32.length; index += 1) {
const sample = Math.max(-1, Math.min(1, float32[index]))
const int16 = sample < 0 ? sample * 0x8000 : sample * 0x7fff
// 单声道麦克风复制到左右声道,生成交错 S16LEL0,R0,L1,R1...
for (let channel = 0; channel < channels; channel += 1) {
view.setInt16((index * channels + channel) * 2, int16, true)
}
}
return buffer
}
/**
* 将后端返回的小端 Int16 PCM 转成 AudioWorklet 使用的 Float32 PCM
* @param {ArrayBuffer} buffer 一帧 960 字节 PCM_S16LE
* @returns {Float32Array} 480 个浮点采样
*/
function int16ToFloat32(buffer) {
const view = new DataView(buffer)
const result = new Float32Array(Math.floor(buffer.byteLength / 2))
for (let index = 0; index < result.length; index += 1) {
const value = view.getInt16(index * 2, true)
result[index] = value < 0 ? value / 0x8000 : value / 0x7fff
}
return result
}
/** 当浏览器实际采样率不是 48kHz 时,通过线性插值转换为机器人采样率。 */
function resample(input, fromRate, toRate) {
if (fromRate === toRate) return new Float32Array(input)
// 保留每个回调产生的小数样本,避免逐块四舍五入造成长期采样率漂移。
const exactLength = input.length * toRate / fromRate + resampleFraction
const outputLength = Math.max(1, Math.floor(exactLength))
resampleFraction = exactLength - outputLength
const output = new Float32Array(outputLength)
const ratio = fromRate / toRate
for (let index = 0; index < outputLength; index += 1) {
const position = index * ratio
const left = Math.floor(position)
const right = Math.min(left + 1, input.length - 1)
const fraction = position - left
output[index] = input[left] * (1 - fraction) + input[right] * fraction
}
return output
}
/**
* 发送一帧浏览器麦克风 PCM
* 每次只发送 480 samples 10ms960 bytes
* @param {Float32Array} samples AudioWorklet 采集的一帧数据
*/
function sendPcmFrame(samples) {
const socket = webSocket.value
if (isMuted.value || socket?.readyState !== WebSocket.OPEN) return
socket.send(floatToInt16Pcm(
resample(samples, audioContext.sampleRate, TARGET_SAMPLE_RATE),
TARGET_CHANNELS
))
}
/**
* 将机器人返回的二进制数据按 960 字节切帧后加入播放队列
* 支持后端将半帧或多帧放在一个 WebSocket 消息中的情况
* @param {ArrayBuffer} buffer WebSocket 二进制数据
*/
function enqueueIncomingPcm(buffer) {
if (!audioContext || audioContext.state === 'closed' || buffer.byteLength < 2) return
const interleavedSamples = int16ToFloat32(buffer)
const frameCount = Math.floor(interleavedSamples.length / remoteChannels)
if (!frameCount) return
// 机器人返回交错 PCM按声道拆分后再交给 AudioBuffer 播放。
const audioBuffer = audioContext.createBuffer(remoteChannels, frameCount, remoteSampleRate)
for (let channel = 0; channel < remoteChannels; channel += 1) {
const channelData = audioBuffer.getChannelData(channel)
for (let frame = 0; frame < frameCount; frame += 1) {
channelData[frame] = interleavedSamples[frame * remoteChannels + channel]
}
}
const source = audioContext.createBufferSource()
source.buffer = audioBuffer
source.connect(audioContext.destination)
const now = audioContext.currentTime
if (nextPlaybackTime < now || nextPlaybackTime > now + 1) nextPlaybackTime = now + 0.03
source.start(nextPlaybackTime)
nextPlaybackTime += audioBuffer.duration
}
/**
* 处理后端文本控制消息
* @param {Object} message 已解析的 JSON 消息
* @param {number} sessionToken 前端本地会话编号
*/
function handleControlMessage(message, sessionToken) {
if (sessionToken !== callSessionId) return
if (message.type === 'audio-format') {
const sampleRate = Number(message.sampleRate)
const channels = Number(message.channels)
if (sampleRate >= 8000 && sampleRate <= 192000) remoteSampleRate = sampleRate
if (Number.isInteger(channels) && channels >= 1 && channels <= 8) remoteChannels = channels
} else if (message.type === 'error' || message.type === 'remote-ended') {
showError(message.message || '机器人已结束音频流')
cleanupResources(false)
}
}
/**
* 连接音频 WebSocket发送 join 并等待后端返回 joined
* 文本帧作为控制 JSON 处理二进制帧作为机器人 PCM 处理
* @param {number} sessionToken 前端本地会话编号
* @returns {Promise<Object>} joined 消息
*/
function connectAudioServer(sessionToken) {
return new Promise((resolve, reject) => {
const protocol = location.protocol === 'https:' ? 'wss:' : 'ws:'
const devHost = `${location.hostname}:${import.meta.env.VITE_SERVICE_PORT || 3000}`
const url = import.meta.env.VITE_AUDIO_SERVER_URL
|| `${protocol}//${import.meta.env.DEV ? devHost : location.host}/audio`
const socket = new WebSocket(url)
socket.binaryType = 'arraybuffer'
webSocket.value = socket
const timeout = window.setTimeout(() => reject(new Error('连接音频服务超时')), JOIN_TIMEOUT)
socket.onopen = () => sendControlMessage({
type: 'init',
microphoneId: robotStore.micDeviceId,
speakerId: robotStore.spkDeviceId,
robotAddress: robotStore.ip
})
socket.onmessage = (event) => {
if (typeof event.data !== 'string') {
enqueueIncomingPcm(event.data)
return
}
try {
const message = JSON.parse(event.data)
if (message.type === 'ready') {
window.clearTimeout(timeout)
resolve(message)
} else if (message.type === 'connection-failed') {
window.clearTimeout(timeout)
reject(new Error(message.message || '机器人音频服务连接失败'))
} else {
handleControlMessage(message, sessionToken)
}
} catch (error) {
reject(new Error('音频服务返回了无效消息', { cause: error }))
}
}
socket.onerror = () => reject(new Error('无法连接音频 WebSocket 服务'))
socket.onclose = () => {
window.clearTimeout(timeout)
if (sessionToken === callSessionId && isCalling.value) {
showError('音频连接已断开')
cleanupResources(false)
}
}
})
}
/**
* 创建浏览器 AudioContext 和麦克风采集节点
* 麦克风只进入静音采集支路不会在本地直接回放
* @param {MediaStream} stream 麦克风媒体流
*/
async function setupAudioPipeline(stream) {
await audioContext.resume()
mediaStreamSource = audioContext.createMediaStreamSource(stream)
// ScriptProcessor 兼容旧浏览器;回调中根据 audioContext.sampleRate 手动重采样。
captureNode = audioContext.createScriptProcessor(2048, 1, 1)
silentGain = audioContext.createGain()
silentGain.gain.value = 0
captureNode.onaudioprocess = (event) => sendPcmFrame(event.inputBuffer.getChannelData(0))
mediaStreamSource.connect(captureNode)
captureNode.connect(silentGain)
silentGain.connect(audioContext.destination)
}
/**
* 开始实时语音通话
* 流程连接 WS join/joined 获取麦克风 启动 AudioWorklet 二进制 PCM 双向传输
*/
async function startCall() {
if (isStarting.value || isCalling.value) return
isStarting.value = true
connectionStatus.value = 'connecting'
errorMessage.value = ''
const sessionToken = ++callSessionId
try {
if (!robotStore.ip || !robotStore.micDeviceId || !robotStore.spkDeviceId) {
throw new Error('请先配置机器人地址、麦克风 ID 和扬声器 ID')
}
// 必须在按钮点击的用户手势中创建,以符合浏览器自动播放策略。
audioContext = new AudioContext({ latencyHint: 'interactive' })
await connectAudioServer(sessionToken)
if (sessionToken !== callSessionId) return
const stream = await navigator.mediaDevices.getUserMedia({
audio: {
channelCount: 1,
echoCancellation: true,
noiseSuppression: true,
autoGainControl: true
}
})
localStream.value = stream
await setupAudioPipeline(stream)
isCalling.value = true
connectionStatus.value = 'connected'
ElMessage.success('语音通话已建立')
} catch (error) {
cleanupResources(false)
showError(error.message || '无法开始语音通话', error)
} finally {
isStarting.value = false
}
}
/**
* 切换麦克风静音状态
* 禁用媒体轨道后 Worklet 收到静音采样不会发送真实麦克风声音
*/
function toggleMute() {
const audioTracks = localStream.value?.getAudioTracks() || []
if (!audioTracks.length) return
isMuted.value = !isMuted.value
audioTracks.forEach((track) => {
track.enabled = !isMuted.value
})
ElMessage.info(isMuted.value ? '麦克风已静音' : '麦克风已取消静音')
}
/**
* 释放 WebSocket麦克风和 Web Audio 资源
* @param {boolean} notifyBackend 是否在关闭前发送 stop 控制消息
*/
function cleanupResources(notifyBackend = true) {
callSessionId += 1
if (notifyBackend) sendControlMessage({ type: 'stop' })
const socket = webSocket.value
webSocket.value = null
if (socket && socket.readyState < WebSocket.CLOSING) socket.close(1000, 'call ended')
localStream.value?.getTracks().forEach((track) => track.stop())
localStream.value = null
captureNode?.disconnect()
mediaStreamSource?.disconnect()
silentGain?.disconnect()
captureNode = undefined
mediaStreamSource = undefined
silentGain = undefined
if (audioContext && audioContext.state !== 'closed') audioContext.close()
audioContext = undefined
nextPlaybackTime = 0
resampleFraction = 0
remoteSampleRate = TARGET_SAMPLE_RATE
remoteChannels = 1
isCalling.value = false
isMuted.value = false
}
/**
* 主动结束当前通话
*/
function endCall() {
cleanupResources()
connectionStatus.value = 'idle'
errorMessage.value = ''
ElMessage.success('通话已结束')
}
// 离开页面时释放麦克风、AudioWorklet 和 WebSocket。
onBeforeUnmount(cleanupResources)
</script>
<style lang="scss" scoped>
$cyan: #46c8ff;
$blue: #2478ff;
$purple: #936cff;
$panel-border: rgb(112 192 255 / 24%);
.voice-conversation {
position: relative;
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 400px;
max-height: 400px;
min-height: 0;
padding: 30px 28px 16px;
overflow: hidden;
box-sizing: border-box;
color: #fff;
background:
radial-gradient(circle at 50% 38%, rgb(44 90 198 / 34%) 0%, transparent 33%),
radial-gradient(circle at 75% 20%, rgb(81 49 172 / 18%) 0%, transparent 30%),
linear-gradient(145deg, #071a3e 0%, #08152f 45%, #050c21 100%);
&::before,
&::after {
position: absolute;
content: '';
pointer-events: none;
}
&::before {
inset: 0;
background: linear-gradient(90deg, rgb(32 143 255 / 7%) 1px, transparent 1px),
linear-gradient(rgb(32 143 255 / 7%) 1px, transparent 1px);
background-size: 42px 42px;
mask-image: radial-gradient(circle at center, #000 12%, transparent 82%);
}
&::after {
width: 360px;
height: 360px;
border: 1px solid rgb(70 200 255 / 7%);
border-radius: 50%;
box-shadow: 0 0 100px rgb(36 120 255 / 8%);
}
.tech-grid {
position: absolute;
inset: 0;
background: repeating-linear-gradient(135deg, transparent 0 118px, rgb(89 138 255 / 3%) 119px 120px);
pointer-events: none;
}
.corner-title {
position: absolute;
top: 16px;
left: 22px;
z-index: 2;
font-size: 20px;
font-weight: 700;
letter-spacing: 1px;
text-shadow: 0 0 18px rgb(74 168 255 / 30%);
}
.voice-content {
position: relative;
z-index: 2;
display: flex;
flex-direction: column;
align-items: center;
width: min(100%, 660px);
}
.hero-title {
text-align: center;
h1 {
margin: 0;
font-size: clamp(23px, 3vw, 30px);
line-height: 1.2;
letter-spacing: 5px;
text-shadow: 0 0 25px rgb(79 159 255 / 25%);
}
p {
margin: 4px 0 0;
color: #86c6f4;
font-size: 12px;
letter-spacing: 3px;
}
}
.voice-visual {
display: flex;
align-items: center;
justify-content: center;
gap: clamp(18px, 4vw, 42px);
width: 100%;
margin-top: 13px;
&.active {
.microphone-orbit { animation: orbit-breathe 2.4s ease-in-out infinite; }
.sound-wave i { animation-play-state: running; }
}
}
.microphone-orbit {
position: relative;
display: grid;
place-items: center;
flex: 0 0 auto;
width: 100px;
height: 100px;
border: 1px solid rgb(117 210 255 / 45%);
border-radius: 50%;
background: linear-gradient(145deg, rgb(41 146 255 / 20%), rgb(120 73 255 / 16%));
box-shadow: 0 0 26px rgb(35 158 255 / 35%), inset 0 0 28px rgb(117 83 255 / 24%);
&::before {
position: absolute;
inset: 7px;
border: 1px solid rgb(160 124 255 / 55%);
border-radius: inherit;
box-shadow: inset 0 0 16px rgb(55 186 255 / 22%);
content: '';
}
.orbit-glow {
position: absolute;
inset: -13px;
border-radius: inherit;
background: conic-gradient(from 90deg, transparent, rgb(58 190 255 / 35%), transparent, rgb(146 94 255 / 42%), transparent);
filter: blur(12px);
}
}
.microphone-core {
position: relative;
display: grid;
place-items: center;
width: 62px;
height: 62px;
border-radius: 50%;
background: linear-gradient(145deg, #237bfa, #794be1);
box-shadow: 0 8px 30px rgb(34 111 255 / 48%), inset 0 1px 1px rgb(255 255 255 / 35%);
svg {
width: 31px;
height: 31px;
overflow: visible;
fill: #fff;
stroke: #fff;
stroke-width: 3;
stroke-linecap: round;
stroke-linejoin: round;
path:first-child { stroke: none; }
path:last-child { fill: none; }
}
}
.sound-wave {
display: flex;
align-items: center;
gap: 6px;
width: 86px;
height: 60px;
i {
width: 4px;
height: var(--wave-height, 30px);
border-radius: 10px;
background: linear-gradient(to bottom, $cyan, $purple);
box-shadow: 0 0 10px rgb(72 190 255 / 46%);
opacity: 0.74;
animation: wave-pulse 1.15s ease-in-out infinite alternate;
animation-delay: calc(var(--index) * -0.09s);
animation-play-state: paused;
&:nth-child(1), &:nth-child(7) { --wave-height: 12px; --index: 1; opacity: 0.32; }
&:nth-child(2), &:nth-child(6) { --wave-height: 22px; --index: 2; opacity: 0.48; }
&:nth-child(3), &:nth-child(5) { --wave-height: 35px; --index: 3; opacity: 0.62; }
&:nth-child(4) { --wave-height: 48px; --index: 4; }
}
&--left { justify-content: flex-end; }
&--right { justify-content: flex-start; }
}
.floating-status {
display: flex;
align-items: center;
gap: 8px;
margin-top: 7px;
padding: 4px 13px;
border: 1px solid rgb(255 255 255 / 9%);
border-radius: 20px;
color: #d5d9e4;
background: rgb(31 38 58 / 86%);
box-shadow: 0 7px 18px rgb(0 0 0 / 24%);
font-size: 11px;
.status-light {
width: 7px;
height: 7px;
border-radius: 50%;
background: #8791a7;
&.connecting { background: #ffd166; box-shadow: 0 0 8px #ffd166; }
&.connected { background: #4ef5ae; box-shadow: 0 0 8px #4ef5ae; }
&.error { background: #ff6482; box-shadow: 0 0 8px #ff6482; }
}
}
.call-controls {
display: flex;
align-items: center;
justify-content: center;
gap: 12px;
min-height: 34px;
margin-top: 9px;
:deep(.el-button + .el-button) { margin-left: 0; }
}
:deep(.call-button) {
width: 178px;
height: 34px;
border: 1px solid rgb(125 207 255 / 52%);
border-radius: 10px;
color: #fff;
background: linear-gradient(100deg, #176ee9, #448eff 56%, #7657e9);
box-shadow: 0 10px 28px rgb(24 105 235 / 35%), inset 0 1px rgb(255 255 255 / 18%);
font-size: 13px;
font-weight: 700;
letter-spacing: 2px;
&:hover,
&:focus {
color: #fff;
border-color: #86d5ff;
background: linear-gradient(100deg, #2280ff, #54a0ff 56%, #896afa);
transform: translateY(-1px);
}
&.call-button--danger {
border-color: rgb(255 126 157 / 55%);
background: linear-gradient(100deg, #cf3e67, #e4597e, #8554d9);
}
}
:deep(.mute-button) {
height: 30px;
border-color: $panel-border;
color: #badeff;
background: rgb(16 45 85 / 72%);
&:hover { color: #fff; border-color: $cyan; background: rgb(23 76 128 / 82%); }
}
.status-card {
width: min(100%, 550px);
margin-top: 20px;
padding: 8px 18px 8px;
border: 1px solid $panel-border;
border-radius: 16px;
box-sizing: border-box;
background: linear-gradient(135deg, rgb(14 49 91 / 67%), rgb(20 30 74 / 55%));
box-shadow: 0 16px 40px rgb(0 4 24 / 28%), inset 0 1px rgb(255 255 255 / 5%);
backdrop-filter: blur(16px);
.status-copy {
text-align: center;
p {
margin: 0;
color: #edf7ff;
font-size: 12px;
strong { color: #fff; font-weight: 600; }
}
span {
display: block;
margin-top: 2px;
color: #8eafd0;
font-size: 10px;
}
}
.error-alert { margin-top: 6px; }
}
.feature-tags {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 9px;
margin-top: 6px;
padding-top: 6px;
border-top: 1px solid rgb(116 186 255 / 13%);
span {
padding: 2px 8px;
border: 1px solid rgb(87 183 255 / 35%);
border-radius: 20px;
color: #a9d6f8;
background: rgb(31 104 166 / 13%);
text-align: center;
font-size: 10px;
}
}
audio { display: none; }
}
@keyframes orbit-breathe {
0%, 100% { transform: scale(1); filter: brightness(1); }
50% { transform: scale(1.035); filter: brightness(1.16); }
}
@keyframes wave-pulse {
from { transform: scaleY(0.58); opacity: 0.42; }
to { transform: scaleY(1.08); opacity: 0.9; }
}
@media (max-width: 680px) {
.voice-conversation {
height: 400px;
max-height: 400px;
min-height: 0;
padding: 38px 14px 12px;
.corner-title { top: 12px; left: 16px; font-size: 17px; }
.voice-visual { gap: 10px; }
.microphone-orbit { width: 90px; height: 90px; }
.microphone-core { width: 56px; height: 56px; }
.microphone-core svg { width: 28px; height: 28px; }
.sound-wave { width: 58px; gap: 4px; }
.sound-wave i { width: 3px; }
.call-controls { flex-wrap: wrap; }
}
}
@media (max-width: 420px) {
.voice-conversation {
.hero-title p { letter-spacing: 1px; }
.voice-visual { gap: 7px; }
.sound-wave { width: 48px; gap: 3px; }
.sound-wave i:nth-child(1),
.sound-wave i:nth-child(7) { display: none; }
.feature-tags { gap: 7px; }
.status-card { padding-inline: 15px; }
}
}
</style>