Skip to content

Instantly share code, notes, and snippets.

@mirceapricop
Forked from 140bytes/LICENSE.txt
Created October 11, 2011 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mirceapricop/1279775 to your computer and use it in GitHub Desktop.
Save mirceapricop/1279775 to your computer and use it in GitHub Desktop.
140byt.es -- Click ↑↑ fork ↑↑ to play!

Square Wave Synthesizer 140byt.es

Generates and plays a square wave with custom duration and pitch.

Usage

synth(duration, inv_pitch, "");

  • Duration is approximately in microseconds (though not exact)
  • The higher the inv_pitch argument, the lower the sound sounds
  • Duration MUST be of the form 4x+2 (e.g. 4250+2 = 1002)
  • inv_pitch SHOULD be divisible by 2

WAV header was prepared with riffwave

See the demo here

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

// This works by generating the dataURI for an Audio tag.
function(i, // How many characters we will put after the header
m, // How often we switch the signal value (basically, inverse frequency)
h) { // Used to build the sound data
while(i--)
h+= i%m > m/2 ? 0 : 9; // We alternate between two values (square wave) every m/2 step
// Example for m=4 => 00990099..
// Generate the Audio and play it
// Works in FF and Chrome
new Audio("data:audio/wav;base64,UklGRjQnAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQ"+h).play()
}
function(i,m,h){while(i--)h+=i%m>m/2?0:9;new Audio("data:audio/wav;base64,UklGRjQnAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQ"+h).play()}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "squareSynth",
"description": "Generates and plays a square wave with custom duration and pitch.",
"keywords": [
"generate",
"sound",
"wave"
]
}
<!DOCTYPE html>
<html>
<head>
<title>Square Wave Piano</title>
<script>
var synth = function(i,m,h){while(i--)h+=i%m>m/2?0:9;new Audio("data:audio/wav;base64,UklGRjQnAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQ"+h).play()}
function playnote(i) {
synth(3002,i*10,"");
if(down_keys[i] == true) {
setTimeout(function(){
playnote(i)
}, 200);
} else {
return;
}
}
var down_keys = new Array(10);
document.onkeydown = function(e) {
var key = e.keyCode - 48;
if(key < 0 || key > 9) return;
key = 10-key; // Reverse them for regular piano intuition.
if(down_keys[key] == null || down_keys[key] == false) {
down_keys[key] = true;
playnote(key);
document.body.style.background = "#"+Math.round(0xffffff * Math.random()).toString(16);
}
}
document.onkeyup = function(e) {
var key = 10 - (e.keyCode - 48);
down_keys[key] = false;
}
</script>
</head>
<body>
<h2>Use the number keys to play.</h2>
<h3>It sounds awesome when you press more at the same time.</h3>
<h4>It doesn't sound so awesome when you keep them pressed for
long.</h4>
</body>
</html>
@subzey
Copy link

subzey commented Oct 12, 2011

Yay, that's awesome!
Now it's time to get a disco ball, lights and lead singer :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment