mirror of
https://github.com/Tonejs/Tone.js
synced 2024-12-26 11:33:09 +00:00
started renaming process
This commit is contained in:
parent
e739f8d811
commit
26a85abc9b
5 changed files with 241 additions and 97 deletions
|
@ -5,6 +5,7 @@
|
|||
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
|
||||
<script type="text/javascript" src="../src/core/WebAudio.js"></script>
|
||||
<script type="text/javascript" src="../src/core/Unit.js"></script>
|
||||
<script type="text/javascript" src="../src/core/AudioUnit.js"></script>
|
||||
<script type="text/javascript" src="../src/components/Player.js"></script>
|
||||
<script type="text/javascript" src="../src/components/Meter.js"></script>
|
||||
<script type="text/javascript" src="../src/components/Envelope.js"></script>
|
||||
|
@ -27,15 +28,16 @@
|
|||
<div id='Instructions'>Press 'e'</div>
|
||||
<div id='meter'></div>
|
||||
<script type="text/javascript">
|
||||
var player = new WebAudio.Player("../audio/A3.mp3");
|
||||
var player = new AudioUnit.Player("../audio/A3.mp3");
|
||||
var meter = new WebAudio.Meter(2);
|
||||
var env = new WebAudio.Envelope(.01, .05, 0, 0);
|
||||
var lfo = new WebAudio.LFO(undefined, 1, .5, 1);
|
||||
var noise = new WebAudio.Noise();
|
||||
var feedbackDelay = new WebAudio.PingPongDelay(.25);
|
||||
var meterGui = new WebAudio.GUI.Meter($("#meter")[0], meter);
|
||||
// var meterGui = new WebAudio.GUI.Meter($("#meter")[0], meter);
|
||||
|
||||
noise.connect(env);
|
||||
//player.connect(env);
|
||||
player.toSpeakers();
|
||||
env.connect(feedbackDelay);
|
||||
feedbackDelay.setFeedback(.5);
|
||||
// lfo.connect(feedbackDelay);
|
||||
|
|
5
src/GUI/GUI.Bars.js
Normal file
5
src/GUI/GUI.Bars.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
AudioUnit.GUI.Bars = function(){
|
||||
AudioUnit.GUI.call(this);
|
||||
}
|
||||
|
||||
AudioUnit.extend(AudioUnit.GUI, AudioUnit.GUI.Bars);
|
|
@ -1,85 +1,19 @@
|
|||
WebAudio.GUI = WebAudio.GUI || {};
|
||||
|
||||
WebAudio.GUI.Meter = function(container, meter, height){
|
||||
this.meter = meter;
|
||||
setInterval(this.update.bind(this), 200);
|
||||
this.element = document.createElement("div");
|
||||
container.appendChild(this.element);
|
||||
//some light styling
|
||||
this.element.style.fontFamily = "monospace";
|
||||
this.element.style.whiteSpace = "pre";
|
||||
AudioUnit.GUI = function(){
|
||||
this._allGUIs.push(this);
|
||||
this.hello = "hiHI";
|
||||
}
|
||||
|
||||
WebAudio.GUI.Meter.prototype.update = function(){
|
||||
var text = this.drawBars(this.meter.volume, 40);
|
||||
// text += gainToDb(leftVol).toFixed(1);
|
||||
// text += gainToDb(rightVol).toFixed(1);
|
||||
this.element.textContent = text;
|
||||
if (this.meter.isClipped()){
|
||||
this.element.style.color = "red";
|
||||
} else {
|
||||
this.element.style.color = "inherit";
|
||||
AudioUnit.GUI.prototype._allGUIs = [];
|
||||
|
||||
AudioUnit.GUI.prototype._doUpdate = function(){
|
||||
requestAnimationFrame(this.doUpdate.bind(this));
|
||||
for (var i = 0; i < this._allGUIs.length; i++){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
WebAudio.GUI.Meter.prototype.slowUpdate = function(){
|
||||
this.meterNumbers = "";
|
||||
}
|
||||
AudioUnit.GUI.prototype.dispose = function(){
|
||||
//remove the element
|
||||
|
||||
|
||||
WebAudio.GUI.Meter.prototype.drawBars = function(volumes, segments){
|
||||
var text = [];
|
||||
for (var channel = 0; channel < volumes.length; channel++){
|
||||
text.push(" _____ ");
|
||||
}
|
||||
text.push("\n");
|
||||
for (var i = 0; i < segments; i++){
|
||||
text.push("|");
|
||||
for (var channel = 0; channel < volumes.length; channel++){
|
||||
var volume = Math.pow(1 - volumes[channel], 6);
|
||||
var volumeSegments = Math.round(volume * segments);
|
||||
if (i < volumeSegments){
|
||||
text.push(" ");
|
||||
} else {
|
||||
if (volumeSegments === segments){
|
||||
text.push("_____");
|
||||
} else {
|
||||
text.push("-----");
|
||||
}
|
||||
}
|
||||
if (channel == volumes.length - 1){
|
||||
text.push("|\n");
|
||||
} else {
|
||||
text.push(" ");
|
||||
}
|
||||
}
|
||||
}
|
||||
for (var channel = 0; channel < volumes.length; channel++){
|
||||
var db = this.meter.getDb(channel);
|
||||
if (isFinite(db)){
|
||||
if (db > -10){
|
||||
db = " "+db.toFixed(1)+" ";
|
||||
} else {
|
||||
db = " "+db.toFixed(1);
|
||||
}
|
||||
} else {
|
||||
db = " -inf ";
|
||||
}
|
||||
text.push(db + " ");
|
||||
}
|
||||
text.push("\n");
|
||||
// console.log(text);
|
||||
return text.join("");
|
||||
}
|
||||
|
||||
//@param {number} db
|
||||
//@returns {number} gain
|
||||
var dbToGain = function(db) {
|
||||
return Math.pow(2, db / 6);
|
||||
}
|
||||
|
||||
//@param {number} gain
|
||||
//@returns {number} db
|
||||
var gainToDb = function(gain) {
|
||||
return 20 * (Math.log(gain) / Math.LN10);
|
||||
}
|
||||
//remove it from GUIs
|
||||
}
|
|
@ -4,9 +4,9 @@
|
|||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
WebAudio.Player = function(url){
|
||||
AudioUnit.Player = function(url){
|
||||
//extend Unit
|
||||
WebAudio.Unit.call(this);
|
||||
AudioUnit.call(this);
|
||||
|
||||
//player vars
|
||||
this.url = url;
|
||||
|
@ -14,19 +14,19 @@ WebAudio.Player = function(url){
|
|||
this.buffer = null;
|
||||
}
|
||||
|
||||
WebAudio.extend(WebAudio.Player, WebAudio.Unit);
|
||||
AudioUnit.extend(AudioUnit.Player, AudioUnit);
|
||||
|
||||
//makes an xhr for the buffer at the url
|
||||
//invokes the callback at the end
|
||||
//@param {function(WebAudio.Player)=} callback
|
||||
WebAudio.Player.prototype.load = function(callback){
|
||||
//@param {function(AudioUnit.Player)=} callback
|
||||
AudioUnit.Player.prototype.load = function(callback){
|
||||
var request = new XMLHttpRequest();
|
||||
request.open('GET', this.url, true);
|
||||
request.responseType = 'arraybuffer';
|
||||
// decode asynchronously
|
||||
var self = this;
|
||||
request.onload = function() {
|
||||
WebAudio.decodeAudioData(request.response, function(b) {
|
||||
self.context.decodeAudioData(request.response, function(b) {
|
||||
self.buffer = b;
|
||||
if (callback){
|
||||
callback(self);
|
||||
|
@ -39,14 +39,14 @@ WebAudio.Player.prototype.load = function(callback){
|
|||
}
|
||||
|
||||
//play the buffer from start to finish at a time
|
||||
WebAudio.Player.prototype.start = function(startTime, offset, duration){
|
||||
AudioUnit.Player.prototype.start = function(startTime, offset, duration){
|
||||
if (this.buffer){
|
||||
//default args
|
||||
startTime = this.defaultArgument(startTime, WebAudio.now);
|
||||
startTime = this.defaultArgument(startTime, AudioUnit.now());
|
||||
offset = this.defaultArgument(offset, 0);
|
||||
duration = this.defaultArgument(duration, this.buffer.duration - offset);
|
||||
//make the source
|
||||
this.source = WebAudio.createBufferSource();
|
||||
this.source = this.context.createBufferSource();
|
||||
this.source.buffer = this.buffer;
|
||||
this.source.loop = false;
|
||||
this.source.connect(this.output);
|
||||
|
@ -55,16 +55,16 @@ WebAudio.Player.prototype.start = function(startTime, offset, duration){
|
|||
}
|
||||
|
||||
//play the buffer from start to finish at a time
|
||||
WebAudio.Player.prototype.loop = function(startTime, loopStart, loopEnd, offset, duration){
|
||||
AudioUnit.Player.prototype.loop = function(startTime, loopStart, loopEnd, offset, duration){
|
||||
if (this.buffer){
|
||||
//default args
|
||||
startTime = this.defaultArgument(startTime, WebAudio.now);
|
||||
startTime = this.defaultArgument(startTime, this.now());
|
||||
loopStart = this.defaultArgument(loopStart, 0);
|
||||
loopEnd = this.defaultArgument(loopEnd, this.buffer.duration);
|
||||
offset = this.defaultArgument(offset, loopStart);
|
||||
duration = this.defaultArgument(duration, this.buffer.duration - offset);
|
||||
//make/play the source
|
||||
this.source = WebAudio.createBufferSource();
|
||||
this.source = this.context.createBufferSource();
|
||||
this.source.buffer = this.buffer;
|
||||
this.source.loop = true;
|
||||
this.source.loopStart = loopStart;
|
||||
|
@ -75,15 +75,15 @@ WebAudio.Player.prototype.loop = function(startTime, loopStart, loopEnd, offset,
|
|||
}
|
||||
|
||||
//stop playback
|
||||
WebAudio.Player.prototype.stop = function(stopTime){
|
||||
AudioUnit.Player.prototype.stop = function(stopTime){
|
||||
if (this.buffer){
|
||||
stopTime = this.defaultArgument(stopTime, WebAudio.now);
|
||||
stopTime = this.defaultArgument(stopTime, this.now());
|
||||
this.source.stop(stopTime);
|
||||
}
|
||||
}
|
||||
|
||||
//@returns {number} the buffer duration
|
||||
WebAudio.Player.prototype.getDuration = function(){
|
||||
AudioUnit.Player.prototype.getDuration = function(){
|
||||
if (this.buffer){
|
||||
this.buffer.duration;
|
||||
} else {
|
||||
|
|
203
src/core/AudioUnit.js
Normal file
203
src/core/AudioUnit.js
Normal file
|
@ -0,0 +1,203 @@
|
|||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// AUDIO UNIT
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
(function(global){
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// WEB AUDIO CONTEXT
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//ALIAS
|
||||
if (!global.AudioContext){
|
||||
global.AudioContext = global.webkitAudioContext;
|
||||
}
|
||||
|
||||
var audioContext;
|
||||
if (global.AudioContext){
|
||||
audioContext = new global.AudioContext();
|
||||
}
|
||||
|
||||
//SHIMS////////////////////////////////////////////////////////////////////
|
||||
|
||||
if (typeof audioContext.createGain !== "function"){
|
||||
audioContext.createGain = audioContext.createGainNode;
|
||||
}
|
||||
if (typeof audioContext.createDelay !== "function"){
|
||||
audioContext.createDelay = audioContext.createDelayNode;
|
||||
}
|
||||
if (typeof AudioBufferSourceNode.prototype.start !== "function"){
|
||||
AudioBufferSourceNode.prototype.start = AudioBufferSourceNode.prototype.noteGrainOn;
|
||||
}
|
||||
if (typeof AudioBufferSourceNode.prototype.stop !== "function"){
|
||||
AudioBufferSourceNode.prototype.stop = AudioBufferSourceNode.prototype.noteOff;
|
||||
}
|
||||
if (typeof OscillatorNode.prototype.start !== "function"){
|
||||
OscillatorNode.prototype.start = OscillatorNode.prototype.noteOn;
|
||||
}
|
||||
if (typeof OscillatorNode.prototype.stop !== "function"){
|
||||
OscillatorNode.prototype.stop = OscillatorNode.prototype.noteOff;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// AUDIO UNIT
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
var AudioUnit = function(){
|
||||
this.context = audioContext;
|
||||
this.input = audioContext.createGain();
|
||||
this.output = audioContext.createGain();
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// STATIC VARS
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
AudioUnit.prototype.fadeTime = .001; //1ms
|
||||
AudioUnit.prototype.bufferSize = 1024; //default buffer size
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// CLASS METHODS
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//@returns {number} the currentTime from the AudioContext
|
||||
AudioUnit.prototype.now = function(){
|
||||
return audioContext.currentTime;
|
||||
}
|
||||
|
||||
//@param {AudioParam | AudioUnit} unit
|
||||
AudioUnit.prototype.connect = function(unit){
|
||||
if (unit.input && unit.input instanceof GainNode){
|
||||
this.output.connect(unit.input);
|
||||
} else {
|
||||
this.output.connect(unit);
|
||||
}
|
||||
}
|
||||
|
||||
//connect together an array of units in series
|
||||
//@param {...AudioParam | AudioUnit} units
|
||||
AudioUnit.prototype.chain = function(){
|
||||
if (arguments.length > 1){
|
||||
var currentUnit = arguments[0];
|
||||
for (var i = 1; i < arguments.length; i++){
|
||||
var toUnit = arguments[i];
|
||||
if (toUnit.input && toUnit.input instanceof GainNode){
|
||||
currentUnit.connect(toUnit.input);
|
||||
} else {
|
||||
currentUnit.connect(toUnit);
|
||||
}
|
||||
currentUnit = toUnit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//set the output volume
|
||||
//@param {number} vol
|
||||
AudioUnit.prototype.setVolume = function(vol){
|
||||
this.output.gain.value = vol;
|
||||
}
|
||||
|
||||
//fade the output volume
|
||||
//@param {number} value
|
||||
//@param {number=} duration (in seconds)
|
||||
AudioUnit.prototype.fadeTo = function(value, duration){
|
||||
this.defaultArgument(duration, this.fadeTime);
|
||||
this.rampToValue(this.output.gain, value, duration);
|
||||
}
|
||||
|
||||
//tear down a node
|
||||
AudioUnit.prototype.tearDown = function(){
|
||||
//go through all of the attributes, if any of them has a disconnect function, call it
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// UTILITIES / HELPERS
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//ramps to value linearly starting now
|
||||
//@param {AudioParam} audioParam
|
||||
//@param {number} value
|
||||
//@param {number=} duration (in seconds)
|
||||
AudioUnit.prototype.rampToValue = function(audioParam, value, duration){
|
||||
var currentValue = audioParam.value;
|
||||
var now = this.now();
|
||||
duration = this.defaultArgument(duration, this.fadeTime);
|
||||
audioParam.setValueAtTime(currentValue, now);
|
||||
audioParam.linearRampToValueAtTime(value, now + duration);
|
||||
}
|
||||
|
||||
//ramps to value exponentially starting now
|
||||
//@param {AudioParam} audioParam
|
||||
//@param {number} value
|
||||
//@param {number=} duration (in seconds)
|
||||
AudioUnit.prototype.exponentialRampToValue = function(audioParam, value, duration){
|
||||
var currentValue = audioParam.value;
|
||||
var now = this.now();
|
||||
audioParam.setValueAtTime(currentValue, now);
|
||||
audioParam.exponentialRampToValueAtTime(value, now + duration);
|
||||
}
|
||||
|
||||
//if the given argument is undefined, go with the default
|
||||
//@param {*} given
|
||||
//@param {*} fallback
|
||||
//@returns {*}
|
||||
AudioUnit.prototype.defaultArgument = function(given, fallback){
|
||||
return typeof(given) !== 'undefined' ? given : fallback;
|
||||
}
|
||||
|
||||
//@param {number} percent (0-1)
|
||||
//@returns {number} the equal power gain
|
||||
//good for cross fades
|
||||
AudioUnit.prototype.equalPowerGain = function(percent){
|
||||
return Math.sin((percent) * 0.5*Math.PI);
|
||||
}
|
||||
|
||||
//@param {number} db
|
||||
//@returns {number} gain
|
||||
AudioUnit.prototype.dbToGain = function(db) {
|
||||
return Math.pow(2, db / 6);
|
||||
}
|
||||
|
||||
//@param {number} gain
|
||||
//@returns {number} db
|
||||
AudioUnit.prototype.gainToDb = function(gain) {
|
||||
return 20 * (Math.log(gain) / Math.LN10);
|
||||
}
|
||||
|
||||
//@param {AudioParam|AudioUnit=} unit
|
||||
AudioUnit.prototype.toSpeakers = function(unit){
|
||||
unit = this.defaultArgument(unit, this.output);
|
||||
unit.connect(audioContext.destination);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// STATIC METHODS
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//A extends B
|
||||
AudioUnit.extend = function(A, B){
|
||||
A.prototype = new B();
|
||||
A.prototype.constructor = A;
|
||||
}
|
||||
|
||||
AudioUnit.muteAll = function(){
|
||||
|
||||
}
|
||||
|
||||
AudioUnit.unmuteAll = function(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
AudioUnit.setGlobalVolume = function(){
|
||||
|
||||
}
|
||||
|
||||
//make it global
|
||||
global.AudioUnit = AudioUnit;
|
||||
|
||||
})(window);
|
Loading…
Reference in a new issue