Square Osc
How do I make an square osc?
Http://mdn.github.io/violent-theremin/scripts/app.js https://developer.mozilla.org/en-US/docs/Web/API/OscillatorNode. What is a square osc? How do you make a square osc? This script and codes were developed by Martin Baillie on 25 January 2023, Wednesday.
Square Osc - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Square Osc</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <ul id="notes"> <li data-note="0"></li> <li data-note="1"></li> <li data-note="2"></li> <li data-note="3"></li> <li data-note="4"></li> <li data-note="5"></li> <li data-note="6"></li> <li data-note="7"></li> <li data-note="8"></li> <li data-note="9"></li> <li data-note="10"></li> <li data-note="11"></li> <li data-note="12"></li>
</ul>
<ul id="waveforms"> <li data-waveform="sine">Sine</li> <li data-waveform="triangle">Triangle</li> <li data-waveform="sawtooth">Saw</li> <li data-waveform="square">Square</li>
</ul>
<ul id="volume"> <li data-volume="-0.001">-</li> <li data-volume="0.001">+</li> <li data-volume="0">Mute</li>
</ul> <script src="js/index.js"></script>
</body>
</html>
Square Osc - Script Codes CSS Codes
li { list-style:none; cursor: pointer; color: #fff; background: #333; border: 1px solid #fff;
}
#notes li { width: 20px; height: 100px; float: left;
}
#waveforms { float: left; margin: 10px;
}
#waveforms li { width: 100px; height: 20px; clear: both; text-indent: 10px;
}
#volume { float: left;
}
#volume li { float:left; height: 20px; width: 20px; text-indent: 6px;
}
#volume li:last-child { clear: both; height: 20px; width: 42px; text-indent: 4px;
}
Square Osc - Script Codes JS Codes
// create web audio api context
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
// create Oscillator and gain node
var oscillator = audioCtx.createOscillator();
var gainNode = audioCtx.createGain();
// connect oscillator to gain node to speakers
oscillator.connect(gainNode);
gainNode.connect(audioCtx.destination);
// create an array of notes from middle C to C5
var octave = (function (){ var arr = []; var i; var middleC = 261.626; var frequencyRatio = 1.059463094; var currentHertz = middleC; arr[0] = currentHertz; for (i = 1; i < 13; i ++) { currentHertz = currentHertz * frequencyRatio; arr[i] = currentHertz; } return arr; })();
// initialise the oscillator parameters
oscillator.type = 'square';
oscillator.frequency.value = octave[0];
oscillator.start();
// initialise the volume and set max and min volumes
gainNode.gain.value = 0.05;
var maxVolume = 0.09;
var minVolume = 0.0;
// initialise events for the 'keyboard'
document.getElementById("notes").addEventListener('click', function(e) { console.log(1) var noteNumber; if (e.target && e.target.nodeName === "LI") { noteNumber = e.target.getAttribute('data-note'); oscillator.frequency.value = octave[noteNumber]; }
});
// initialise events for the waveform selector
document.getElementById("waveforms").addEventListener('click', function(e) { var waveform; if (e.target && e.target.nodeName === "LI") { waveform = e.target.getAttribute('data-waveform'); oscillator.type = waveform; }
});
// initialise events for the volume controls
document.getElementById("volume").addEventListener('click', function(e) { var volumeAdj; var volume; if (e.target && e.target.nodeName === "LI") { volumeAdj = e.target.getAttribute('data-volume'); if (volumeAdj === '0') { gainNode.gain.value = volumeAdj; } else { volumeAdj = parseFloat(volumeAdj); volume = gainNode.gain.value + volumeAdj; if (volume > minVolume && volume < maxVolume) { gainNode.gain.value = volume; } } }
});
Developer | Martin Baillie |
Username | martin42 |
Uploaded | January 25, 2023 |
Rating | 3 |
Size | 2,616 Kb |
Views | 2,024 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
Q try catch timeout | 1,758 Kb |
Draughts | 6,273 Kb |
M-test | 1,250 Kb |
Shortest Path | 2,661 Kb |
JS Interview Questions | 7,121 Kb |
Sticky Header | 1,911 Kb |
Drag n Drop | 2,594 Kb |
More interview Questions | 5,531 Kb |
More Array Methods | 2,327 Kb |
Shuffle List Animation | 2,362 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
React Template | Isac | 1,241 Kb |
Web Spiral - p5.js | TWAIN | 2,183 Kb |
Sort the Knowlege | Eprouver | 3,915 Kb |
3d css cube | Semenchenko | 4,578 Kb |
Base64 SVG Me | MrBambule | 44,786 Kb |
Practice using Wixel | Emnk | 3,057 Kb |
Contact | GanNichiHa | 2,514 Kb |
SVG hamburger menu button | Elifitch | 2,602 Kb |
CSS 3D Radio buttons | Andreasnylin | 1,650 Kb |
Elon Musk - Tribute Page - FreeCodeCamp | Yunnimun | 8,615 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!