Skip to content

Instantly share code, notes, and snippets.

@Shchvova Shchvova/build.settings
Last active Mar 11, 2019

Embed
What would you like to do?
WebkitSpeech

Simple demo on how to write JS plugins for Corona

Tiny sample of using JavaScript with Corona HTML5 builds. JavaScript plugin_speech.js exposes Lua module plugin.speech with APIs to access browser's Web Speech capabilities: speech.start(...), speech.stop() and speech.isRunning().

Live Demo:

Guide on using JavaScript in Corona:

Web Speech API info:

settings =
{
orientation =
{
default = "portrait",
supported = { "portrait", },
},
}
application =
{
content =
{
width = 320,
height = 480,
scale = "letterbox",
},
}
-- using protected call because plugin is only in browser, no Lua wrapper
local imported, speech = pcall(require, "plugin.speech")
if not imported then speech = {isRunning=function()end,start=function()end,stop=function()end,} end
-- text objects to hold results or instructions
local phase = display.newText {
text="event phase goes here", fontSize = 12,
x = display.contentCenterX, y = 20, width = display.contentWidth*0.9, height = 20,
}
local text = display.newText{
text="Click to start/stop/restart. Note mic access request!", fontSize = 12,
width = display.contentWidth*0.9, x = display.contentCenterX, y = display.contentCenterY,
}
Runtime:addEventListener("tap", function()
if speech.isRunning() then
speech.stop()
else
if not speech.start{
lang = "en-US",
continuous = true,
interimResults = true,
listener = function(event)
-- print(require('json').encode(event))
phase.text = event.phase
if event.phase == "result" then
text.text = event.final .. event.interim
elseif event.phase == "started" then
text.text = "Start talking!"
end
end
} then
text.text = "Error while starting speech recognition!"
end
end
end)
plugin_speech = {
start: function(params) {
var self = this;
if (!('webkitSpeechRecognition' in window)) {
return null;
}
if(!params || !LuaIsFunction(params.listener)) {
console.error("No listener passed to params table")
return null;
}
self.callback = LuaCreateFunction( params.listener );
self.final_transcript = "";
self.recognition = new webkitSpeechRecognition();
self.recognition.continuous = params.continuous || false;
self.recognition.interimResults = params.interimResults || false;
if(params.lang) {
self.recognition.lang = params.lang;
}
self.recognition.onstart = function() {
self.callback({
name: "speech",
phase: "started",
})
};
self.recognition.onerror = function(event) {
self.callback({
name: "speech",
phase: "error",
error: event.error,
isError: true,
})
self.recognition = null;
};
self.recognition.onend = function() {
self.callback({
name: "speech",
phase: "ended",
final: self.final_transcript,
})
self.recognition = null;
LuaReleaseFunction(self.callback);
};
self.recognition.onresult = function(event) {
var interim_transcript = '';
for (var i = event.resultIndex; i < event.results.length; ++i) {
if (event.results[i].isFinal) {
self.final_transcript += event.results[i][0].transcript;
} else {
interim_transcript += event.results[i][0].transcript;
}
}
self.callback({
name: "speech",
phase: "result",
interim: interim_transcript,
final: self.final_transcript,
})
};
self.recognition.start();
return true;
},
isRunning: function() {
return this.recognition != null;
},
stop: function() {
if(this.recognition) {
this.recognition.stop()
}
this.recognition = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.