Skip to content

Codigotchi 5 #9

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

Open
wants to merge 1 commit into
base: 03-codigotchi-04
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions 03-codigotchi/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ <h3>Supported Commands</h3>

</section>

<!-- Transcript -->
<p id="transcript"></p>

</main>

</div>
Expand Down
34 changes: 32 additions & 2 deletions 03-codigotchi/script.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
// Query the page for elements
const micBtn = document.getElementById('microphone')
const panelsData = document.getElementById('panels-data');
const panelsData = document.getElementById('panels-data')
const transcript = document.getElementById('transcript')

// Set a default SpeechRecognition browser library to use
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition

// Build the speech recognition library
const recognition = new SpeechRecognition()

// When button clicked, show the available commands
function onStartListening() {
// Start listening to the microphone
recognition.start()

// Show the available commands
panelsData.classList.add('listening')
}

micBtn.addEventListener('click', onStartListening)
// When finished speaking, run this function
function onResult(event) {
// Hide the available commands
panelsData.classList.remove('listening')

// Get the words we spoke
const text = event.results[0][0].transcript

// Add what we spoke to the transcript HTML element for visibility
transcript.textContent = `You said: ${text}`
}

// If anything goes wrong (it shouldn't), let us know in the console
function onError(event) {
console.error(event.error)
}

micBtn.addEventListener('click', onStartListening)
recognition.addEventListener('result', onResult)
recognition.addEventListener('error', onError)