Code
HTTP
/** @jsxImportSource https://esm.sh/react */
import React, { useEffect, useRef, useState } from "https://esm.sh/react";
import { createRoot } from "https://esm.sh/react-dom/client";
function SoundMaker() {
const [isRecording, setIsRecording] = useState(false);
const [recordedNotes, setRecordedNotes] = useState([]);
const audioContext = useRef(null);
const oscillator = useRef(null);
useEffect(() => {
audioContext.current = new (window.AudioContext || window.webkitAudioContext)();
return () => {
if (audioContext.current) {
audioContext.current.close();
}
};
}, []);
const playSound = (frequency) => {
if (audioContext.current) {
oscillator.current = audioContext.current.createOscillator();
oscillator.current.type = "sine";
oscillator.current.frequency.setValueAtTime(frequency, audioContext.current.currentTime);
oscillator.current.connect(audioContext.current.destination);
oscillator.current.start();
setTimeout(() => oscillator.current.stop(), 200);
if (isRecording) {
setRecordedNotes(prev => [...prev, { frequency, time: Date.now() }]);
}
}
};
const toggleRecording = () => {
setIsRecording(!isRecording);
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
all-tomasovalmax.web.val.run
Updated: October 22, 2024