-
-
Save rwaldron/9c4955e5a3662e4cd5e1 to your computer and use it in GitHub Desktop.
This is basically a weapon. I'm not kidding. Use with caution. http://www.globalsecurity.org/military/systems/munitions/accoustic.htm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<style> | |
input,button { | |
font-size: 40px; | |
} | |
</style> | |
<script src="audio-test.js"></script> | |
</head> | |
<body> | |
<button>sin</button> | |
<button>tan</button> | |
<label for="freq"><input type="number" size="4" id="freq" value="440">Hz</label> | |
<button>Play</button> | |
<button>Pause</button> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://wiki.mozilla.org/Audio_Data_API | |
// http://chromium.googlecode.com/svn/trunk/samples/audio/specification/specification.html | |
document.addEventListener("DOMContentLoaded", function() { | |
var // setup ui/controls module | |
controller = (function() { | |
var controls = { | |
math: "", | |
hz: 0, | |
freq: function() { | |
controller.hz = document.getElementById("freq").value; | |
}, | |
play: function() { | |
controller.freq(); | |
node.connect( context.destination ); | |
}, | |
pause: function() { | |
node.disconnect(); | |
} | |
}; | |
["sin", "tan"].forEach(function( math ) { | |
controls[ math ] = function() { | |
controller.math = math; | |
}; | |
}); | |
return controls; | |
})(); | |
// Event Setup | |
// button events for controlling tone play | |
[].forEach.call( document.querySelectorAll("button"), function( button ) { | |
// Button labels are same as `controller` property methods for demo | |
button.addEventListener( "click", function( event ) { | |
controller[ event.target.innerHTML.toLowerCase() ]( event ); | |
}, false ); | |
}); | |
// frequency events | |
document.getElementById("freq").addEventListener("input", controller.freq , false); | |
// Audio Processing | |
var // create a new audio api context | |
context = new webkitAudioContext(), | |
// create a buffer with 1 input and 1 output | |
// 256, 512, 1024, 2048, 4096, 8192, 16384 | |
// This value controls how frequently the onaudioprocess event | |
// handler is called and how many sample-frames need to be processed each call | |
node = context.createJavaScriptNode( 2048, 1, 1 ); | |
// global sine-tone value | |
sine = 0; | |
// NOTE: node does not implement addEventListener? | |
// TODO: wrap in deferred? methodize? | |
node.onaudioprocess = function( event ) { | |
var audioBuffer = event.outputBuffer, | |
channels = { | |
left: audioBuffer.getChannelData( 0 ), | |
right: audioBuffer.getChannelData( 1 ) | |
}, | |
// channelData length will be same as node buffer | |
len = channels.left.length, | |
idx = 0, | |
sampleFreq; | |
// debug vals | |
// console.log( channels, channels.left.length, p ); | |
sampleFreq = 2 * Math.PI * controller.hz / audioBuffer.sampleRate; | |
for ( ; idx < len; idx++ ) { | |
// Float32Array values with sine-tone value | |
channels.left[ idx ] = Math[ controller.math || "sin" ]( sampleFreq * sine++ ); | |
channels.right[ idx ] = Math[ controller.math || "sin" ]( sampleFreq * sine++ ); | |
} | |
}; | |
}, false); |
I guess I should include instructions...
NOTE: OSX, Chrome dev channel (12.0.733.0 dev is the version I wrote this in) w/ Web Audio API enabled is the only way this is going to work for you. Everyone else will have to wait until the API is implemented in your OS of choice.
(reposted from: http://news.ycombinator.com/item?id=2541317 )
What is this suppose to do? Is it like that Borland C program which can resonate chicken's skulls?
http://everything2.com/title/7+hertz+-+the+resonant+frequency+of+a+chicken%2527s+skull
@Marak shit... I hope so.
That snippet in the Borlands Turbo C++ seriously influenced my career as a programmer. I remember reading that in like 9th grade and being like, fuck man, programming is cool.
TODO:
- Implement websocket remote feature
- Add it to chrome store within a background page
- Install on coworker's computer
- ???
- Profit
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uncaught ReferenceError: webkitAudioContext is not defined on Chrome 11.0.696.68