Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Created May 11, 2011 21:39
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save rwaldron/9c4955e5a3662e4cd5e1 to your computer and use it in GitHub Desktop.
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
<!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>
// 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);
@deathbob
Copy link

Uncaught ReferenceError: webkitAudioContext is not defined on Chrome 11.0.696.68

@rwaldron
Copy link
Author

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 )

@Marak
Copy link

Marak commented May 12, 2011

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

@rwaldron
Copy link
Author

@Marak shit... I hope so.

@Marak
Copy link

Marak commented May 12, 2011

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.

@peritus
Copy link

peritus commented May 13, 2011

TODO:

  1. Implement websocket remote feature
  2. Add it to chrome store within a background page
  3. Install on coworker's computer
  4. ???

@Marak
Copy link

Marak commented May 13, 2011

  1. Profit

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