Skip to content

Commit

Permalink
Create randomsound.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Coding4Hours authored Mar 24, 2024
1 parent d182842 commit 65633d1
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions other/randomsound.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
javascript:(function() {
// Create an AudioContext
const audioContext = new (window.AudioContext || window.webkitAudioContext)();

// Function to generate a random tone
function generateRandomTone() {
const duration = 1; // in seconds
const frequency = Math.random() * 2000 + 200; // random frequency between 200 and 2200 Hz

// Create an OscillatorNode
const oscillator = audioContext.createOscillator();
oscillator.type = 'sine'; // you can experiment with other oscillator types like 'sawtooth', 'square', or 'triangle'
oscillator.frequency.value = frequency;

// Create a GainNode for controlling the volume
const gainNode = audioContext.createGain();
gainNode.gain.value = 0.5; // adjust the volume here

// Connect the nodes
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);

// Start the oscillator
oscillator.start();

// Stop the oscillator after the specified duration
setTimeout(() => {
oscillator.stop();
}, duration * 1000);
}

// Generate a random tone
generateRandomTone();
})();

0 comments on commit 65633d1

Please sign in to comment.