Skip to content

Instantly share code, notes, and snippets.

@kirbysayshi
Last active September 21, 2015 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirbysayshi/f20e6ed34715e99a6fe4 to your computer and use it in GitHub Desktop.
Save kirbysayshi/f20e6ed34715e99a6fe4 to your computer and use it in GitHub Desktop.
requirebin sketch
var binaryxhr = require('binary-xhr');
var actx = null;
function kick () {
var audioContext = new webkitAudioContext()
var osc = audioContext.createOscillator()
var gain = audioContext.createGain()
gain.gain.value = 0
osc.connect(gain)
gain.connect(audioContext.destination)
osc.start(0)
osc.stop(1)
return (actx = audioContext)
}
function play () {
binaryxhr('https://raw.githubusercontent.com/kirbysayshi/synae-server/master/public/audio/mp3/Section_1_Group_1_flutter.mp3', function(err, data) {
actx.decodeAudioData(data, function(buffer) {
var sample = actx.createBufferSource();
sample.buffer = buffer;
sample.connect(actx.destination);
sample.onended = function() { sample.disconnect(); }
sample.start(undefined);
});
})
}
var btn = document.createElement('button');
btn.onclick = kick;
btn.innerHTML = 'kick';
document.body.appendChild(btn);
var btn = document.createElement('button');
btn.onclick = play;
btn.innerHTML = 'play';
document.body.appendChild(btn);
var style = document.createElement('style');
style.innerHTML = ''
+ 'button { font-size: 120px; }'
document.head.appendChild(style);
require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){module.exports=inherits;function inherits(c,p,proto){proto=proto||{};var e={};[c.prototype,proto].forEach(function(s){Object.getOwnPropertyNames(s).forEach(function(k){e[k]=Object.getOwnPropertyDescriptor(s,k)})});c.prototype=Object.create(p.prototype,e);c.super=p}},{}],"binary-xhr":[function(require,module,exports){var inherits=require("inherits");module.exports=function(url,cb){return new BinaryXHR(url,cb)};function BinaryXHR(url,cb){var self=this;var xhr=new XMLHttpRequest;this.xhr=xhr;xhr.open("GET",url,true);xhr.responseType="arraybuffer";xhr.onreadystatechange=function(){if(self.xhr.readyState===4){if(self.xhr.status!==200){cb(self.xhr.status,self.xhr.response)}else if(self.xhr.response&&self.xhr.response.byteLength>0){cb(false,self.xhr.response)}else{if(self.xhr.response&&self.xhr.response.byteLength===0)return cb("response length 0");cb("no response")}}};xhr.send(null)}},{inherits:1}]},{},[]);var binaryxhr=require("binary-xhr");var actx=null;function kick(){var audioContext=new webkitAudioContext;var osc=audioContext.createOscillator();var gain=audioContext.createGain();gain.gain.value=0;osc.connect(gain);gain.connect(audioContext.destination);osc.start(0);osc.stop(1);return actx=audioContext}function play(){binaryxhr("https://raw.githubusercontent.com/kirbysayshi/synae-server/master/public/audio/mp3/Section_1_Group_1_flutter.mp3",function(err,data){actx.decodeAudioData(data,function(buffer){var sample=actx.createBufferSource();sample.buffer=buffer;sample.connect(actx.destination);sample.onended=function(){sample.disconnect()};sample.start(undefined)})})}var btn=document.createElement("button");btn.onclick=kick;btn.innerHTML="kick";document.body.appendChild(btn);var btn=document.createElement("button");btn.onclick=play;btn.innerHTML="play";document.body.appendChild(btn);var style=document.createElement("style");style.innerHTML=""+"button { font-size: 120px; }";document.head.appendChild(style);
{
"name": "requirebin-sketch",
"version": "1.0.0",
"dependencies": {
"binary-xhr": "0.0.2"
}
}
<!-- contents of this file will be placed inside the <body> -->
<!-- contents of this file will be placed inside the <head> -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment