diff --git a/README.md b/README.md index f211f6f26..05bcf89ac 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ # Lab 5 - Starter + +Name: Edwin Pham + +Lab Partner(s): Shreya Gupta and Nathaniel Greenberg \ No newline at end of file diff --git a/assets/scripts/explore.js b/assets/scripts/explore.js index 777f5ee3a..ee06f9d44 100644 --- a/assets/scripts/explore.js +++ b/assets/scripts/explore.js @@ -3,5 +3,75 @@ window.addEventListener('DOMContentLoaded', init); function init() { - // TODO + const speechSynth = window.speechSynthesis; + const voiceDropdown = document.querySelector("#voice-select"); + const playButton = document.querySelector("button"); + let utterSpeech; + + voiceDropdown.addEventListener('change', handleVoiceChange); + playButton.addEventListener('click', playSound); + + + function populateVoiceList() { + if (typeof speechSynth === "undefined") { + return; + } + + const voices = speechSynth.getVoices(); + + for (let i = 0; i < voices.length; i++) { + const option = document.createElement("option"); + option.textContent = `${voices[i].name} (${voices[i].lang})`; + + if (voices[i].default) { + option.textContent += " — DEFAULT"; + } + + option.setAttribute("data-lang", voices[i].lang); + option.setAttribute("data-name", voices[i].name); + document.getElementById("voice-select").appendChild(option); + } + + } + + populateVoiceList(); + + if ( + typeof speechSynthesis !== "undefined" && + speechSynthesis.onvoiceschanged !== undefined + ) { + speechSynthesis.onvoiceschanged = populateVoiceList; + } + + function handleVoiceChange(event){ + const selectedVoice = event.target.value; + const voices = speechSynth.getVoices(); + console.log(voices); + console.log(selectedVoice); + const utterThis = new SpeechSynthesisUtterance(document.getElementById("text-to-speak").value); + for (let i = 0; i < voices.length; i++) { + console.log(`${voices[i].name} (${voices[i].lang})`); + + if (`${voices[i].name} (${voices[i].lang})` === selectedVoice) { + utterThis.voice = voices[i]; + } + } + + utterSpeech = utterThis; + } + + function playSound(){ + utterSpeech.addEventListener("start", (event) => { + document.querySelector("#explore img").src = 'assets/images/smiling-open.png'; + }); + + utterSpeech.addEventListener("end", (event) => { + document.querySelector("#explore img").src = 'assets/images/smiling.png'; + }); + + speechSynth.speak(utterSpeech); + + + } + } \ No newline at end of file diff --git a/assets/scripts/expose.js b/assets/scripts/expose.js index 962d7a33c..289c9f9df 100644 --- a/assets/scripts/expose.js +++ b/assets/scripts/expose.js @@ -3,5 +3,63 @@ window.addEventListener('DOMContentLoaded', init); function init() { - // TODO + const hornDropdown = document.querySelector("#horn-select"); + const volumeSlider = document.querySelector("#volume"); + const playButton = document.querySelector("button"); + const imageElement = document.querySelector("img"); + const audioElement = document.querySelector("audio"); + const jsConfetti = new JSConfetti(); + + hornDropdown.addEventListener('change', handleHornChange); + volumeSlider.addEventListener('input', handleVolumeChange); + playButton.addEventListener('click', playSound); + + function handleHornChange(event) { + const selectedHorn = event.target.value; + // Sets the correct image + if (selectedHorn === 'air-horn') { + imageElement.src = 'assets/images/air-horn.svg'; + } else if (selectedHorn === 'car-horn') { + imageElement.src = 'assets/images/car-horn.svg'; + } else if (selectedHorn === 'party-horn') { + imageElement.src = 'assets/images/party-horn.svg'; + } else { + imageElement.src = 'assets/images/no-image.png'; + } + + // Sets the correct audio sound file + audioElement.src = `assets/audio/${selectedHorn}.mp3`; + } + + function handleVolumeChange(event) { + const volume = event.target.value; + // Sets the volume icon img + if (volume >= 67) { + document.querySelector("#volume-controls img").src = 'assets/icons/volume-level-3.svg'; + } else if (volume >= 33) { + document.querySelector("#volume-controls img").src = 'assets/icons/volume-level-2.svg'; + } else if (volume >= 1) { + document.querySelector("#volume-controls img").src = 'assets/icons/volume-level-1.svg'; + } else { + document.querySelector("#volume-controls img").src = 'assets/icons/volume-level-0.svg'; + } + + // Sets the corresponding volume for the audio element + audioElement.volume = volume / 100; + } + + function playSound() { + const selectedHorn = hornDropdown.value; + + // Plays the corresponding sound for the selected horn at the specified volume + //audioElement.play(); + + if (selectedHorn === 'party-horn') { + jsConfetti.addConfetti(); + audioElement.play(); + } + else{ + audioElement.play(); + } + } } \ No newline at end of file