Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

语音不中断支持 #365

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion src/inputHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ const vscode = require("vscode");
const share = require("./share.js");

var inputHistory = "";
var playHistory = [];
// 存放音频触发的历史记录,以便于判断是否因为tab补全而二次触发或被立刻打断,这些音频会取消或延时播放。
// Save the history of triggered voices, so that some
// voices that are triggered right after others can be cancelled or delayed.
// The situation may happen especially when you use "tab" to auto-complete.
// example: playHistory = [
// [ ['if'], 1650457549077 ] ,
// [ ['=>'], 1650457589074 ]
// ]

function keywordsCheck() {
var candidate = [];
Expand All @@ -11,6 +20,7 @@ function keywordsCheck() {
if (!voicePackage.enabled) {
return;
}
var playHistoryIsAdded = false;
voicePackage.contributes.forEach(contribute => {
var triggered = false;
var keywords = [].concat(contribute.keywords || []);
Expand All @@ -28,14 +38,51 @@ function keywordsCheck() {
if (triggered) {
var voices = [].concat(contribute.voices);
candidate.push(voicePackage.name + "/" + voices[Math.floor(voices.length * Math.random())])
if (!playHistoryIsAdded) {
// playHistory will add a new element first, then it will be deleted if duplicate is found.
playHistory.push([contribute.keywords, Date.now()]);
// One keyword may exits in many contributes.keywords. Only add once.
playHistoryIsAdded = true;
}
}
});
});

if (candidate.length) {
inputHistory = "";
share.play(candidate[Math.floor(Math.random() * candidate.length)]);
if(duplicateCheck()){
share.play(candidate[Math.floor(Math.random() * candidate.length)]);
}

}
}


function duplicateCheck(){
if (playHistory.length > 10) {
playHistory.shift();
}

if (playHistory.length == 1) {
return true;
}
else if (playHistory[playHistory.length - 1][1] - playHistory[playHistory.length - 2][1] <= 1000) {
console.log("Two voices triggered in %i milliseconds, so the latter one is cancelled.",
playHistory[playHistory.length - 1][1] - playHistory[playHistory.length - 2][1]);
playHistory.pop();
return false;
}
else if (playHistory[playHistory.length - 2][0] == playHistory[playHistory.length - 1][0]
&& playHistory[playHistory.length - 1][1] - playHistory[playHistory.length - 2][1] <= 2000) {
console.log("SAME voices triggered in %i milliseconds, so the latter one is cancelled.",
playHistory[playHistory.length - 1][1] - playHistory[playHistory.length - 2][1]);
playHistory.pop();
return false;
}
else {
return true;
}

}

module.exports = function () {
Expand Down