From 2490b33ff29ea7dbfa84fcb0971d29fc7358d889 Mon Sep 17 00:00:00 2001 From: Eli Manzo <96manzo.eli@gmail.com> Date: Sun, 27 Oct 2024 22:09:56 -0700 Subject: [PATCH 01/12] feat: added playSound function to simons game --- .../simon-game/src/app/util/playSound.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 workspaces/simon-game/src/app/util/playSound.ts diff --git a/workspaces/simon-game/src/app/util/playSound.ts b/workspaces/simon-game/src/app/util/playSound.ts new file mode 100644 index 00000000..1bfaafd6 --- /dev/null +++ b/workspaces/simon-game/src/app/util/playSound.ts @@ -0,0 +1,18 @@ +export const playSound = ( + context: BaseAudioContext, + frequency: number, + duration: number, + volume: number, +) => { + const oscillatorNode = context.createOscillator(); + oscillatorNode.frequency.setValueAtTime(frequency, context.currentTime); + + const gainNode = context.createGain(); + gainNode.gain.setValueAtTime(volume, context.currentTime); + + oscillatorNode.connect(gainNode); + gainNode.connect(context.destination); + + oscillatorNode.start(); + oscillatorNode.stop(context.currentTime + duration / 1000); +}; From d1feaeec52104fc776d332a29fe23ed92409d3c9 Mon Sep 17 00:00:00 2001 From: Eli Manzo <96manzo.eli@gmail.com> Date: Sun, 27 Oct 2024 22:15:29 -0700 Subject: [PATCH 02/12] chore: added some error handling --- .../simon-game/src/app/util/playSound.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/workspaces/simon-game/src/app/util/playSound.ts b/workspaces/simon-game/src/app/util/playSound.ts index 1bfaafd6..30f677a3 100644 --- a/workspaces/simon-game/src/app/util/playSound.ts +++ b/workspaces/simon-game/src/app/util/playSound.ts @@ -1,9 +1,29 @@ +/** + * Plays a sound using the Web Audio API. + * + * @param context - The audio context to use for generating the sound. + * @param frequency - The frequency of the sound in Hertz + * @param duration - The duration of the sound in milliseconds. + * @param volume - The volume of the sound, typically between 0 and 1. + */ export const playSound = ( context: BaseAudioContext, frequency: number, duration: number, volume: number, ) => { + if (frequency <= 0) { + throw new Error("Frequency must be a positive number."); + } + + if (duration <= 0) { + throw new Error("Duration must be a positive number."); + } + + if (volume < 0 || volume > 1) { + throw new Error("Volume must be between 0 and 1."); + } + const oscillatorNode = context.createOscillator(); oscillatorNode.frequency.setValueAtTime(frequency, context.currentTime); From 4af3aef21067dce431560e5977b0191f38bf74ce Mon Sep 17 00:00:00 2001 From: Eli Manzo <96manzo.eli@gmail.com> Date: Sun, 27 Oct 2024 22:38:38 -0700 Subject: [PATCH 03/12] chore: using AudioContext instead of BaseAudioContext as per guideline --- workspaces/simon-game/src/app/util/playSound.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/workspaces/simon-game/src/app/util/playSound.ts b/workspaces/simon-game/src/app/util/playSound.ts index 30f677a3..bbf7d111 100644 --- a/workspaces/simon-game/src/app/util/playSound.ts +++ b/workspaces/simon-game/src/app/util/playSound.ts @@ -7,7 +7,7 @@ * @param volume - The volume of the sound, typically between 0 and 1. */ export const playSound = ( - context: BaseAudioContext, + context: AudioContext, frequency: number, duration: number, volume: number, From 3c7a842d5277647870b11313e8deaeb357589233 Mon Sep 17 00:00:00 2001 From: Eli Manzo <96manzo.eli@gmail.com> Date: Mon, 28 Oct 2024 21:12:11 -0700 Subject: [PATCH 04/12] feat: added some freq buttons to test the playSound function --- .../simon-game/src/app/components/App.tsx | 24 +++++++++++++++++++ workspaces/simon-game/src/app/constants.ts | 5 ++++ .../simon-game/src/app/util/playSound.ts | 6 ++--- 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 workspaces/simon-game/src/app/constants.ts diff --git a/workspaces/simon-game/src/app/components/App.tsx b/workspaces/simon-game/src/app/components/App.tsx index 895d9015..e049c44b 100644 --- a/workspaces/simon-game/src/app/components/App.tsx +++ b/workspaces/simon-game/src/app/components/App.tsx @@ -1,10 +1,34 @@ import React from "react"; +import { buttonSoundConfig } from "../constants.ts"; +import playSound from "../util/playSound.ts"; import { Box } from "./Box.tsx"; export function App() { + let audioContext: AudioContext | null = null; + + const handleFreqClick = (freq: number) => { + if(!audioContext) { + audioContext = new window.AudioContext(); + } + playSound( + audioContext, + freq, + buttonSoundConfig.duration, + buttonSoundConfig.volume, + ) + } + return (