Skip to content

Instantly share code, notes, and snippets.

@Mr-Kumar-Abhishek
Created January 22, 2023 05:29
Show Gist options
  • Save Mr-Kumar-Abhishek/5fea0ca3ae5eb9794b7ac0e34716dd22 to your computer and use it in GitHub Desktop.
Save Mr-Kumar-Abhishek/5fea0ca3ae5eb9794b7ac0e34716dd22 to your computer and use it in GitHub Desktop.

Revisions

  1. Mr-Kumar-Abhishek created this gist Jan 22, 2023.
    36 changes: 36 additions & 0 deletions isochronic-tone.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    // Create an audio context
    const audioCtx = new (window.AudioContext || window.webkitAudioContext)();

    // Create an oscillator node
    const oscillator = audioCtx.createOscillator();

    // Set the frequency of the oscillator
    oscillator.frequency.value = 1000;

    // Create a gain node
    const gainNode = audioCtx.createGain();

    // Connect the oscillator to the gain node
    oscillator.connect(gainNode);

    // Connect the gain node to the audio context's destination
    gainNode.connect(audioCtx.destination);

    // Start the oscillator
    oscillator.start();

    // Set the gain to 0
    gainNode.gain.value = 0;
    let flip = true

    function toggleTone() {
    if (flip) {
    flip = false
    gainNode.gain.value = 1
    } else {
    flip = true
    gainNode.gain.value = 0.1
    }
    }
    // Set the tone to turn on and off at specific intervals
    setInterval(toggleTone, 500); // toggle tone every 500ms