ByteBeater: modern JS library for bytebeat tunes


SUBMITTED BY: Guest

DATE: March 27, 2013, 12:35 p.m.

FORMAT: JavaScript

SIZE: 1.4 kB

HITS: 1340

  1. /*ByteBeater tiny library for playing bytebeat tunes in your browser via Web Audio API
  2. * by Multiversum
  3. * Usage: new ByteBeater(pattern_string), then call play() and pause() methods
  4. * e.g.
  5. * var bb = new ByteBeater("((t<<1)^((t<<18)+(t>>7)&t>>12))|t>>(4-(1^7&(t>>19)))|t>>7"); bb.play()
  6. * pattern_string accepts a JS expression from single 't' parameter
  7. * with the following functions specified without Math: sin, cos, tan, floor, ceil
  8. * Supported browsers: latest desktop WebKits and Geckos (Chrome, Firefox)
  9. * This library is public domain. Have fun!
  10. */
  11. ByteBeater = function(pstring) {
  12. this.x = 0;
  13. var bb = this;
  14. var AudioContext = window.AudioContext||window.webkitAudioContext||window.mozAudioContext;
  15. this.pattern = eval("(function(t){return "+pstring.replace(/sin|cos|tan|floor|ceil/g,function(s){return "Math."+s})+"})");
  16. this.context = new AudioContext();
  17. var factor = ~~(this.context.sampleRate/8000);
  18. this.node = this.context.createJavaScriptNode(1024,1,1);
  19. this.node.onaudioprocess = function(e){
  20. var data = e.outputBuffer.getChannelData(0);
  21. for(var i=0;i<data.length;i++)
  22. data[i]=!(i%factor)?((bb.pattern(bb.x++))&255)/255:data[i-1];
  23. }
  24. this.play = function(){this.node.connect(this.context.destination)}
  25. this.pause = function(){this.node.disconnect()}
  26. }

comments powered by Disqus