Skip to content

Commit

Permalink
add speech recognition (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
dragazo authored Jul 22, 2024
1 parent 19be344 commit 18babde
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
4 changes: 4 additions & 0 deletions extensions/Speech/extension.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"description": "Blocks for speech recognition and synthesis",
"useDev": false
}
74 changes: 74 additions & 0 deletions extensions/Speech/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
(function () {
const I32_MAX = 2147483647;

class Speech extends Extension {
constructor(ide) {
super('Speech');
this.ide = ide;

this.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
this.requestQueue = [];

if (this.SpeechRecognition) {
this.recognizer = new this.SpeechRecognition();

this.recognizer.continuous = false;
this.recognizer.lang = 'en-US';
this.recognizer.interimResults = false;
this.recognizer.maxAlternatives = 1;

this.recognizer.onstart = (e) => {
this.recognized = '';
};
this.recognizer.onresult = (e) => {
this.recognized = ((e.results || [])[0] || [])[0]?.transcript || '';
};
this.recognizer.onend = (e) => {
const requests = this.requestQueue;
this.requestQueue = [];
for (const req of requests) {
req(this.recognized);
}
};
}
}

getMenu() {
return {};
}

getCategories() {
return [];
}

getPalette() {
const blocks = [
new Extension.Palette.Block('speechRecognize'),
];
return [
new Extension.PaletteCategory('sensing', blocks, SpriteMorph),
new Extension.PaletteCategory('sensing', blocks, StageMorph),
];
}

getBlocks() {
const thisExtension = this;
return [
new Extension.Block('speechRecognize', 'reporter', 'sensing', 'listen for speech', [],
function () {
return this.runAsyncFn(async () => {
return await new Promise((resolve, reject) => {
thisExtension.requestQueue.push(resolve);
try {
thisExtension.recognizer.start();
} catch (e) {}
});
}, { timeout: I32_MAX })
},
).for(SpriteMorph, StageMorph),
];
}
}

NetsBloxExtensions.register(Speech);
})();

0 comments on commit 18babde

Please sign in to comment.