diff --git a/web-speech-api/README.md b/web-speech-api/README.md index b72a15dd..5df30aa5 100644 --- a/web-speech-api/README.md +++ b/web-speech-api/README.md @@ -8,16 +8,18 @@ Code for demos illustrating features of the Web Speech API. See [Web_Speech_API] [Run recognition demo live](https://mdn.github.io/dom-examples/web-speech-api/speech-color-changer/) -Tap the screen then say a colour — the grammar string contains a large number of HTML keywords to choose from, although we've removed most of the multiple word colors to remove ambiguity. We did keep goldenrod, cos, well. +Tap the screen then say a color — the grammar string contains a large number of HTML named color keywords to choose from. -This current works only on Chrome/Chrome Mobile. To get this code running successfully, you'll need to run the code through a web server (localhost will work.) +## On-device speech color changer demo + +[Run on-device recognition demo live](https://mdn.github.io/dom-examples/web-speech-api/on-device-speech-color-changer/) + +This version of the color changer demo uses the new on-device color changer functionality, which at the time of writing works in Chrome 139+ desktop only. ## Phrase matcher demo Speak the phrase and then see if the recognition engine successfully recognises it — this is another demo that relies on speech recognition, written for a research team at the University of Nebraska at Kearney. -This current works only on Chrome/Chrome Mobile. To get this code running successfully, you'll need to run the code through a web server (localhost will work.) - [Run phrase matcher demo live](https://mdn.github.io/dom-examples/web-speech-api/phrase-matcher/) ## Speak easy synthesis demo @@ -25,5 +27,3 @@ This current works only on Chrome/Chrome Mobile. To get this code running succes [Run synthesis demo live](https://mdn.github.io/dom-examples/web-speech-api/speak-easy-synthesis/) Type words in the input then submit the form to hear it spoken. You can also select the different voices available on the system, and alter the rate and pitch. - -This currently works in Chrome, Firefox and Safari. diff --git a/web-speech-api/index.html b/web-speech-api/index.html index b96dbd59..d3fc9063 100644 --- a/web-speech-api/index.html +++ b/web-speech-api/index.html @@ -20,6 +20,11 @@
This demo works in Chrome 142+ on desktop and equivalent Chromiums.
+ + + + + +...diagnostic messages
+ + diff --git a/web-speech-api/on-device-speech-color-changer/script.js b/web-speech-api/on-device-speech-color-changer/script.js new file mode 100644 index 00000000..b1bbccce --- /dev/null +++ b/web-speech-api/on-device-speech-color-changer/script.js @@ -0,0 +1,131 @@ +const colors = [ + "aqua", + "azure", + "bisque", + "blue", + "brown", + "chocolate", + "coral", + "cornflowerblue", + "crimson", + "cyan", + "deepskyblue", + "fuchsia", + "ghostwhite", + "gold", + "goldenrod", + "gray", + "green", + "greenyellow", + "hotpink", + "indigo", + "ivory", + "khaki", + "lavender", + "lightseagreen", + "lime", + "linen", + "magenta", + "maroon", + "moccasin", + "navy", + "olive", + "orange", + "orchid", + "peru", + "pink", + "plum", + "purple", + "rebeccapurple", + "red", + "salmon", + "sienna", + "silver", + "snow", + "steelblue", + "tan", + "teal", + "thistle", + "tomato", + "turquoise", + "violet", + "yellow", +]; + +const phraseData = [ + { phrase: "azure", boost: 10.0 }, + { phrase: "khaki", boost: 3.0 }, + { phrase: "tan", boost: 2.0 }, +]; + +const phraseObjects = phraseData.map( + (p) => new SpeechRecognitionPhrase(p.phrase, p.boost) +); + +const recognition = new SpeechRecognition(); +recognition.continuous = false; +recognition.lang = "en-US"; +recognition.interimResults = false; +recognition.processLocally = true; +recognition.phrases = phraseObjects; + +recognition.phrases.push(new SpeechRecognitionPhrase("thistle", 5.0)); + +const diagnostic = document.querySelector(".output"); +const bg = document.querySelector("html"); +const hints = document.querySelector(".hints"); +const startBtn = document.querySelector("button"); + +let colorHTML = ""; +colors.forEach(function (v, i, a) { + console.log(v, i); + colorHTML += `${v} `; +}); +hints.innerHTML = `Press the button then say a color to change the background color of the app. For example, you could try ${colorHTML}`; + +startBtn.addEventListener("click", () => { + // check availability of target language + SpeechRecognition.available({ langs: ["en-US"], processLocally: true }).then( + (result) => { + if (result === "unavailable") { + diagnostic.textContent = `en-US not available to download at this time. Sorry!`; + } else if (result === "available") { + recognition.start(); + console.log("Ready to receive a color command."); + } else { + diagnostic.textContent = `en-US language pack downloading`; + SpeechRecognition.install({ + langs: ["en-US"], + processLocally: true, + }).then((result) => { + if (result) { + diagnostic.textContent = `en-US language pack downloaded. Try again.`; + } else { + diagnostic.textContent = `en-US language pack failed to download. Try again later.`; + } + }); + } + } + ); +}); + +recognition.addEventListener("result", (event) => { + let color = event.results[0][0].transcript; + // Remove whitespace from recognized speech + color = color.replace(/\s+/g, ""); + diagnostic.textContent = `Result received: ${color}.`; + bg.style.backgroundColor = color; + console.log(`Confidence: ${event.results[0][0].confidence}`); +}); + +recognition.addEventListener("speechend", () => { + recognition.stop(); +}); + +recognition.addEventListener("nomatch", (event) => { + diagnostic.textContent = "I didn't recognise that color."; +}); + +recognition.addEventListener("error", (event) => { + diagnostic.textContent = `Error occurred in recognition: ${event.error}`; +}); diff --git a/web-speech-api/on-device-speech-color-changer/style.css b/web-speech-api/on-device-speech-color-changer/style.css new file mode 100644 index 00000000..bdac962a --- /dev/null +++ b/web-speech-api/on-device-speech-color-changer/style.css @@ -0,0 +1,39 @@ +body, +html { + margin: 0; +} + +html { + height: 100%; +} + +body { + height: inherit; + overflow: hidden; + max-width: 800px; + margin: 0 auto; + font-family: system-ui; + text-align: center; +} + +.output { + height: 100px; + margin: 0; + overflow: auto; + position: absolute; + bottom: 0; + right: 0; + left: 0; + background-color: rgba(255 255 255 / 0.2); + display: flex; + justify-content: center; + align-items: center; +} + +ul { + margin: 0; +} + +.hints span { + text-shadow: 0px 0px 6px rgba(255 255 255 / 0.7); +} diff --git a/web-speech-api/speech-color-changer/index.html b/web-speech-api/speech-color-changer/index.html index 781ef505..fd55c3fd 100644 --- a/web-speech-api/speech-color-changer/index.html +++ b/web-speech-api/speech-color-changer/index.html @@ -14,9 +14,10 @@...diagnostic messages
-...diagnostic messages