write code
run code
CANVASOR
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Web Audio API Example</title> </head> <body> <h1>Web Audio API Sound Generator</h1> <button id="playButton">Play Sound</button> <!-- Button to trigger the sound --> <script> // Create a new AudioContext (this is needed to interact with Web Audio API) const audioContext = new (window.AudioContext || window.webkitAudioContext)(); // Function to create and play a sound function playSound() { // Create an oscillator (a sound wave generator) const oscillator = audioContext.createOscillator(); // Set the type of wave (sine wave in this case) oscillator.type = 'sine'; // Other types can be 'square', 'triangle', 'sawtooth' // Set the frequency of the sound (in Hz) oscillator.frequency.setValueAtTime(440, audioContext.currentTime); // A4 note (440Hz) // Create a gain node (controls the volume of the sound) const gainNode = audioContext.createGain(); // Set the volume (gain) to 0.1 (low volume) gainNode.gain.setValueAtTime(0.1, audioContext.currentTime); // Connect the oscillator to the gain node (to control volume) oscillator.connect(gainNode); // Connect the gain node to the audio context destination (the speakers) gainNode.connect(audioContext.destination); // Start the oscillator (this will play the sound) oscillator.start(); // Stop the oscillator after 1 second (duration of the sound) oscillator.stop(audioContext.currentTime + 1); } // Add an event listener to the button to trigger the playSound function when clicked document.getElementById('playButton').addEventListener('click', playSound); </script> </body> </html>
About THIS CODE (CANVASOR.NET):
81
Copy All Code
Download Code.html