Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
C
CharIP-Electron
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ali
CharIP-Electron
Commits
c1557511
Commit
c1557511
authored
Dec 05, 2023
by
ali
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 临时文件
parent
73a7a2a5
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
103 additions
and
0 deletions
+103
-0
voice_client_with_script_processor.js
...enderer/public/vosk/voice_client_with_script_processor.js
+103
-0
No files found.
src/renderer/public/vosk/voice_client_with_script_processor.js
0 → 100644
View file @
c1557511
var
context
;
var
source
;
var
processor
;
var
streamLocal
;
var
webSocket
;
var
inputArea
;
var
bufferSize
=
8192
;
var
sampleRate
=
8000
;
var
wsURL
=
'ws://10.90.120.27:2700'
;
var
initComplete
=
false
;
;(
function
()
{
document
.
addEventListener
(
'DOMContentLoaded'
,
(
event
)
=>
{
inputArea
=
document
.
getElementById
(
'q'
);
const
listenButton
=
document
.
getElementById
(
'listenWithScript'
);
const
stopListeningButton
=
document
.
getElementById
(
'stopListeningWithScript'
);
listenButton
.
addEventListener
(
'mousedown'
,
function
()
{
listenButton
.
disabled
=
true
;
initWS
();
navigator
.
mediaDevices
.
getUserMedia
({
audio
:
{
echoCancellation
:
true
,
noiseSuppression
:
true
,
channelCount
:
1
,
sampleRate
},
video
:
false
}).
then
(
handleSuccess
);
listenButton
.
style
.
color
=
'green'
;
initComplete
=
true
;
});
stopListeningButton
.
addEventListener
(
'mouseup'
,
function
()
{
if
(
initComplete
===
true
)
{
webSocket
.
send
(
'{"eof" : 1}'
);
webSocket
.
close
();
source
.
disconnect
(
processor
);
processor
.
disconnect
(
context
.
destination
);
if
(
streamLocal
.
active
)
{
streamLocal
.
getTracks
()[
0
].
stop
();
}
listenButton
.
style
.
color
=
'black'
;
listenButton
.
disabled
=
false
;
initComplete
=
false
;
inputArea
.
innerText
=
""
}
});
});
}())
var
handleSuccess
=
function
(
stream
)
{
streamLocal
=
stream
;
context
=
new
AudioContext
({
sampleRate
:
sampleRate
});
source
=
context
.
createMediaStreamSource
(
stream
);
processor
=
context
.
createScriptProcessor
();
source
.
connect
(
processor
);
processor
.
connect
(
context
.
destination
);
processor
.
onaudioprocess
=
function
(
audioDataChunk
)
{
console
.
log
(
audioDataChunk
.
inputBuffer
);
sendAudio
(
audioDataChunk
);
};
};
function
sendAudio
(
audioDataChunk
)
{
if
(
webSocket
.
readyState
===
WebSocket
.
OPEN
)
{
// convert to 16-bit payload
const
inputData
=
audioDataChunk
.
inputBuffer
.
getChannelData
(
0
)
||
new
Float32Array
(
bufferSize
);
const
targetBuffer
=
new
Int16Array
(
inputData
.
length
);
for
(
let
index
=
inputData
.
length
;
index
>
0
;
index
--
)
{
targetBuffer
[
index
]
=
32767
*
Math
.
min
(
1
,
inputData
[
index
]);
}
webSocket
.
send
(
targetBuffer
.
buffer
);
}
}
function
initWS
()
{
webSocket
=
new
WebSocket
(
wsURL
);
webSocket
.
binaryType
=
"arraybuffer"
;
webSocket
.
onopen
=
function
(
event
)
{
console
.
log
(
'New connection established'
);
};
webSocket
.
onerror
=
function
(
event
)
{
console
.
error
(
event
.
data
);
};
webSocket
.
onmessage
=
function
(
event
)
{
if
(
event
.
data
)
{
let
parsed
=
JSON
.
parse
(
event
.
data
);
if
(
parsed
.
partial
&&
parsed
.
partial
!==
'the'
)
inputArea
.
innerText
=
parsed
.
partial
+
'|'
;
if
(
parsed
.
result
)
console
.
log
(
parsed
.
result
);
if
(
parsed
.
text
)
inputArea
.
innerText
=
parsed
.
text
;
}
};
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment