The first step
The realization of single voice to write text, when there is only one person in the room, this time is equivalent to the room is a recorder, automatically monitor the sound of the external environment, the voice will be written down
Real-time voice transfer text needs to use Websocket to maintain a persistent connection with the background
Determine whether it is HTTP or HTTPS and determine whether it is WSS or WS
var ws_host
if(window.location.protocol === 'http:'){
ws_host = 'ws://' + window.location.hostname + : '8080'
}else{
ws_host='wss://' + window.location.hostname + '8089'} Bind the start buttonvar ws=null;
var recorder=null;
function startRecord(){
ws=new webSocket(
ws_host + 'Interface name parameter'
)
ws.onopen = function () {
log_println('ASR channel opened')
navigator.mediaDevices
.getUserMedia({ audio: true.video: false })
.then((stream) = > {
recorder = RecordRTC(stream, {
type: 'audio'.mimeType: 'audio/wav'.recorderType: StereoAudioRecorder,
desiredSampRate: 16000.numberOfAudioChannels: 1.timeSlice: 1000.bufferSize: 16384.ondataavailable: sendData,
})
recorder.startRecording()
})
}
// Data is returned
ws.onmessage = function (event) {
let res = JSON.parse(event.data)
if (res.code == 200 && res.final == true) {
console.log(res.text)
}
}
}
// Send data
function sendData(blob) {
blob.arrayBuffer().then((buffer) = > {
let raw = buffer.slice(44)
ws.send(raw)
})
}
Copy the code
The second step
Voice transfer to text for two or more people
For two or more people, distinguish between the local and remote ends
This end onlocalstream:function(stream){
var ws = new WebSocket(
ws_host+ 'Interface names and parameters') connection successful ws.onopen =function () {
let recorder = new RecordRTCPromisesHandler(stream, {
type: "audio".mimeType: "audio/wav".recorderType: StereoAudioRecorder,
desiredSampRate: 16000.numberOfAudioChannels: 1.timeSlice: 1000.bufferSize: 16384.ondataavailable: sendDataaudio,
});
recorder.startRecording();
};
// Audio data is sent
let sendDataaudio = function (blob) {
blob.arrayBuffer().then((buffer) = > {
let raw = buffer.slice(44);
ws.send(raw);
});
};
// Accept audio text in real time
ws.onmessage = function (event) {
let res = JSON.parse(event.data);
if (res.code == 200 && res.final == true) {
console.log(res.text); }}; } remote onRemoteStream:function(stream){
var ws = new WebSocket(
ws_host+ 'Interface names and parameters'
)
ws.onopen = function () {
let recorder = new RecordRTCPromisesHandler(stream, {
type: "audio".mimeType: "audio/wav".recorderType: StereoAudioRecorder,
desiredSampRate: 16000.numberOfAudioChannels: 1.timeSlice: 1000.bufferSize: 16384.ondataavailable: sendData,
});
recorder.startRecording();
};
// Audio data is sent
let sendDataaudio = function (blob) {
blob.arrayBuffer().then((buffer) = > {
let raw = buffer.slice(44);
ws.send(raw);
});
};
// Accept audio text in real time
ws.onmessage = function (event) {
let res = JSON.parse(event.data);
if (res.code == 200 && res.final == true) {
console.log(res.text); }}; }` ``
Copy the code