Square Osc

Size
2,616 Kb
Views
2,024

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 Previews

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; } } }
});
Square Osc - Script Codes
Square Osc - Script Codes
Home Page Home
Developer Martin Baillie
Username martin42
Uploaded January 25, 2023
Rating 3
Size 2,616 Kb
Views 2,024
Do you need developer help for Square Osc?

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!

Martin Baillie (martin42) Script Codes
Create amazing marketing copy with AI!

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!