Commit 96a3fd57 authored by 郭铭瑶's avatar 郭铭瑶 🤘

调整语音输入

parent 4845ee9c
;(function () {
const self = this
self.onmessage = function (e) {
transAudioData.transcode(e.data)
}
const transAudioData = {
transcode(audioData) {
let output = transAudioData.to16kHz(audioData)
output = transAudioData.to16BitPCM(output)
output = Array.from(new Uint8Array(output.buffer))
self.postMessage(output)
// return output
},
to16kHz(audioData) {
const data = new Float32Array(audioData)
const fitCount = Math.round(data.length * (16000 / 44100))
const newData = new Float32Array(fitCount)
const springFactor = (data.length - 1) / (fitCount - 1)
newData[0] = data[0]
for (let i = 1; i < fitCount - 1; i++) {
const tmp = i * springFactor
const before = Math.floor(tmp).toFixed()
const after = Math.ceil(tmp).toFixed()
const atPoint = tmp - before
newData[i] = data[before] + (data[after] - data[before]) * atPoint
}
newData[fitCount - 1] = data[data.length - 1]
return newData
},
to16BitPCM(input) {
const dataLength = input.length * (16 / 8)
const dataBuffer = new ArrayBuffer(dataLength)
const dataView = new DataView(dataBuffer)
let offset = 0
for (let i = 0; i < input.length; i++, offset += 2) {
const s = Math.max(-1, Math.min(1, input[i]))
dataView.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7fff, true)
}
return dataView
},
}
})()
import CryptoJS from 'crypto-js' import CryptoJS from 'crypto-js'
const worker =
process.env.NODE_ENV === 'production'
? './transcode.worker.js'
: '/transcode.worker.js'
const transWorker = new Worker('./transcode.worker.js') const transWorker = new Worker('./transcode.worker.js')
const APPID = '4f3360ec' const APPID = '4f3360ec'
const API_SECRET = 'ODQ2ZDAyYTg3ZjVhMWQyMWJhYjUxNWQ3' const API_SECRET = 'ODQ2ZDAyYTg3ZjVhMWQyMWJhYjUxNWQ3'
...@@ -87,10 +91,10 @@ export default class IatRecorder { ...@@ -87,10 +91,10 @@ export default class IatRecorder {
this.result(e.data) this.result(e.data)
} }
iatWS.onerror = (e) => { iatWS.onerror = (e) => {
this.recorderStop('end') this.recorderStop()
} }
iatWS.onclose = (e) => { iatWS.onclose = (e) => {
this.recorderStop('end') this.recorderStop()
} }
}) })
} }
...@@ -197,7 +201,7 @@ export default class IatRecorder { ...@@ -197,7 +201,7 @@ export default class IatRecorder {
} }
} }
// 暂停录音 // 暂停录音
recorderStop(status) { recorderStop() {
// safari下suspend后再次resume录音内容将是空白,设置safari下不做suspend // safari下suspend后再次resume录音内容将是空白,设置safari下不做suspend
if ( if (
!( !(
...@@ -206,7 +210,7 @@ export default class IatRecorder { ...@@ -206,7 +210,7 @@ export default class IatRecorder {
) { ) {
this.audioContext && this.audioContext.suspend() this.audioContext && this.audioContext.suspend()
} }
this.setStatus(status) this.setStatus('end')
} }
// 处理音频数据 // 处理音频数据
// transAudioData(audioData) { // transAudioData(audioData) {
...@@ -237,7 +241,7 @@ export default class IatRecorder { ...@@ -237,7 +241,7 @@ export default class IatRecorder {
language: this.language, //小语种可在控制台--语音听写(流式)--方言/语种处添加试用 language: this.language, //小语种可在控制台--语音听写(流式)--方言/语种处添加试用
domain: 'iat', domain: 'iat',
accent: this.accent, //中文方言可在控制台--语音听写(流式)--方言/语种处添加试用 accent: this.accent, //中文方言可在控制台--语音听写(流式)--方言/语种处添加试用
vad_eos: 2000, vad_eos: 3000,
dwa: 'wpgs', //为使该功能生效,需到控制台开通动态修正功能(该功能免费) dwa: 'wpgs', //为使该功能生效,需到控制台开通动态修正功能(该功能免费)
ptt: 1, ptt: 1,
}, },
...@@ -330,6 +334,6 @@ export default class IatRecorder { ...@@ -330,6 +334,6 @@ export default class IatRecorder {
this.setResultText({ resultText: '', resultTextTemp: '' }) this.setResultText({ resultText: '', resultTextTemp: '' })
} }
stop() { stop() {
this.recorderStop('stop') this.recorderStop()
} }
} }
...@@ -253,7 +253,7 @@ ...@@ -253,7 +253,7 @@
:class="{ recording: status === 'ing' }" :class="{ recording: status === 'ing' }"
@click="toggleRecorder" @click="toggleRecorder"
> >
{{ status === 'ing' ? '停止输入' : '语音输入' }} {{ status === 'ing' ? '输入中…' : '语音输入' }}
</n-button> </n-button>
</n-form-item-gi> </n-form-item-gi>
<n-form-item-gi <n-form-item-gi
...@@ -343,20 +343,24 @@ import useVoiceRecorder from '@/hooks/useVoiceRecorder' ...@@ -343,20 +343,24 @@ import useVoiceRecorder from '@/hooks/useVoiceRecorder'
const { result, toggleRecorder, status, resetRecorder } = useVoiceRecorder() const { result, toggleRecorder, status, resetRecorder } = useVoiceRecorder()
const activityDescribtion = ref('') const activityDescribtion = ref('')
watch([() => result.value, () => status.value], ([txt, sta]) => { watch([() => result.value, () => status.value], ([txt, sta]) => {
console.log(sta, txt)
if (!txt) return if (!txt) return
if (activityDescribtion.value) {
detailData.value.attachment = activityDescribtion.value + txt
} else {
detailData.value.attachment = txt
}
if (sta === 'end') { if (sta === 'end') {
activityDescribtion.value = detailData.value.attachment activityDescribtion.value = detailData.value.attachment
} else {
if (activityDescribtion.value) {
detailData.value.attachment = activityDescribtion.value + txt
} else {
detailData.value.attachment = txt
}
} }
}) })
function onInput(val) { function onInput(val) {
if (!val) { if (!val) {
activityDescribtion.value = ''
resetRecorder() resetRecorder()
} }
activityDescribtion.value = val
} }
const message = useMessage() const message = useMessage()
const props = defineProps({ const props = defineProps({
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment