Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 3 commits
  • 7 files changed
  • 0 commit comments
  • 1 contributor
View
25 src/core/soundsystem.js
@@ -11,15 +11,11 @@ var SoundSystem = (function() {
initialize.apply(this, arguments);
}, $this = SoundSystem.prototype;
- var initialize = function(streamsize, channels) {
- streamsize = streamsize || timbre.streamsize;
- channels = channels || timbre.channels;
- channels = (channels === 1) ? 1 : 2;
-
- this.streamsize = streamsize;
- this.channels = channels;
- this.L = new Float32Array(streamsize);
- this.R = new Float32Array(streamsize);
+ var initialize = function() {
+ this.streamsize = timbre.streamsize;
+ this.channels = timbre.channels;
+ this.L = new Float32Array(timbre.streamsize);
+ this.R = new Float32Array(timbre.streamsize);
this.cell = new Float32Array(timbre.cellsize);
this.seq_id = 0;
@@ -33,6 +29,17 @@ var SoundSystem = (function() {
this._.impl = new PlayerKlass(this);
};
+ $this.setup = function() {
+ if (this._.impl) this._.impl.setup();
+ this.streamsize = timbre.streamsize;
+ this.channels = timbre.channels;
+ this.L = new Float32Array(timbre.streamsize);
+ this.R = new Float32Array(timbre.streamsize);
+ this.cell = new Float32Array(timbre.cellsize);
+ this._.cellsize = timbre.cellsize;
+ if (timbre.samplerate === 0) timbre.samplerate = 44100;
+ };
+
$this.on = function() {
if (this._.impl) {
this._.ison = true;
View
24 src/timbre.js
@@ -4,6 +4,7 @@
"use strict";
// __BEGIN__
+
var timbre = function() {
return timbre.fn.init.apply(timbre, arguments);
};
@@ -59,8 +60,31 @@ Object.defineProperty(timbre, "isOff", {
get: function() { return !timbre.sys._.ison; }
});
+timbre.setup = function(params) {
+ var samplerate, channels, cellsize, streamsize;
+
+ if (!Object.isFrozen(timbre)) {
+ params = params || {};
+ if (typeof params.samplerate === "number") {
+ timbre.samplerate = params.samplerate;
+ }
+ if (typeof params.channels === "number") {
+ timbre.channels = params.channels;
+ }
+ if (typeof params.cellsize === "number") {
+ timbre.cellsize = params.cellsize;
+ }
+ if (typeof params.streamsize === "number") {
+ timbre.cellsize = params.streamsize;
+ }
+ timbre.sys.setup();
+ Object.freeze(timbre);
+ }
+};
+
timbre.on = function() {
if (!timbre.sys._.ison) {
+ timbre.setup();
timbre.sys.on();
timbre.fn.do_event(this, "on");
}
View
3  src/timbre.txt
@@ -28,7 +28,6 @@ var timbre = (function(context, timbre) {
}(context, timbre));
timbre.isEnabled = !!timbre.sys._.impl;
- Object.freeze(timbre);
-
+
return timbre;
}(this));
View
2  src/window/exports.js
@@ -23,6 +23,6 @@ timbre.context = window;
console.log(x.join(""));
}());
-window.T = timbre;
+window.timbre = window.T = timbre;
// __END__
View
213 src/window/player.js
@@ -7,84 +7,177 @@ var timbre = require("../timbre");
var window = {};
// __BEGIN__
+var setupTimbre = function(defaultSamplerate) {
+ switch (timbre.samplerate) {
+ case 11025: case 12000:
+ case 22050: case 24000:
+ case 44100: case 48000:
+ break;
+ default:
+ timbre.samplerate = defaultSamplerate;
+ }
+
+ switch (timbre.channels) {
+ default:
+ timbre.channels = 2;
+ }
+
+ switch (timbre.cellsize) {
+ case 64: case 128:
+ case 256: case 512:
+ break;
+ default:
+ timbre.cellsize = 128;
+ }
+
+ switch (timbre.streamsize) {
+ case 512: case 1024: case 2048:
+ case 4096: case 8192:
+ break;
+ default:
+ timbre.streamsize = 1024;
+ }
+};
+
+
var WebKitPlayer = function(sys) {
- var ctx, node, onaudioprocess;
+ var self = this;
+ var ctx, onaudioprocess;
+ var samplerate, dx;
ctx = new webkitAudioContext();
- timbre.samplerate = ctx.sampleRate;
+ samplerate = ctx.sampleRate;
- node = ctx.createJavaScriptNode(sys.streamsize, 1, sys.channels);
-
- onaudioprocess = function(e) {
- var inL, inR, outL, outR, i;
- sys.process();
+ this.setup = function() {
+ setupTimbre(samplerate);
+ this.streamsize = timbre.streamsize;
- inL = sys.L;
- inR = sys.R;
- outL = e.outputBuffer.getChannelData(0);
- outR = e.outputBuffer.getChannelData(1);
- for (i = outL.length; i--; ) {
- outL[i] = inL[i];
- outR[i] = inR[i];
+ if (timbre.samplerate === samplerate) {
+ onaudioprocess = function(e) {
+ var inL, inR, outL, outR, i;
+ sys.process();
+
+ inL = sys.L;
+ inR = sys.R;
+ outL = e.outputBuffer.getChannelData(0);
+ outR = e.outputBuffer.getChannelData(1);
+ for (i = outL.length; i--; ) {
+ outL[i] = inL[i];
+ outR[i] = inR[i];
+ }
+ };
+ } else {
+ dx = timbre.samplerate / samplerate;
+ onaudioprocess = function(e) {
+ var inL, inR, outL, outR, outLen;
+ var streamsize, x, prevL, prevR;
+ var index, delta, x0, x1, xx;
+ var i, imax;
+
+ inL = sys.L;
+ inR = sys.R;
+ outL = e.outputBuffer.getChannelData(0);
+ outR = e.outputBuffer.getChannelData(1);
+ outLen = outL.length;
+
+ streamsize = self.streamsize;
+ x = self.x;
+ for (i = 0, imax = outL.length; i < imax; ++i) {
+ if (x >= streamsize) {
+ sys.process();
+ x -= streamsize;
+ }
+
+ index = x|0;
+ delta = x - index;
+
+ x1 = inL[index];
+ xx = (1.0 - delta) * prevL + delta * x1;
+ prevL = x1;
+ outL[i] = xx;
+
+ x1 = inR[index];
+ xx = (1.0 - delta) * prevR + delta * x1;
+ prevR = x1;
+ outR[i] = xx;
+
+ x += dx;
+ }
+ self.x = x;
+ self.prevL = prevL;
+ self.prevR = prevR;
+ };
}
+
+ return this;
};
- return {
- on : function() {
- this.node = ctx.createJavaScriptNode(sys.streamsize, 1, sys.channels);
- this.node.onaudioprocess = onaudioprocess;
- this.node.connect(ctx.destination);
- },
- off: function() {
- this.node.disconnect();
- this.node = null;
- }
+ this.on = function() {
+ this.x = this.streamsize;
+ this.prevL = this.prevR = 0;
+ this.node = ctx.createJavaScriptNode(sys.streamsize, 1, sys.channels);
+ this.node.onaudioprocess = onaudioprocess;
+ this.node.connect(ctx.destination);
};
+ this.off = function() {
+ this.node.disconnect();
+ this.node = null;
+ };
+
+ return this.setup();
};
var MozPlayer = function(sys) {
- var self = this;
- var audio, timer;
- var interval, interleaved;
- var onaudioprocess;
+ var timer = new MutekiTimer();
- audio = new Audio();
- audio.mozSetup(sys.channels, timbre.samplerate);
- timbre.samplerate = audio.mozSampleRate;
- timbre.channels = audio.mozChannels;
-
- timer = new MutekiTimer();
- interval = (sys.streamsize / timbre.samplerate) * 1000;
-
- interleaved = new Float32Array(sys.streamsize * sys.channels);
-
- onaudioprocess = function() {
- var inL, inR, i, j;
- audio.mozWriteAudio(interleaved);
- sys.process();
+ this.setup = function() {
+ var self = this;
- inL = sys.L;
- inR = sys.R;
+ setupTimbre(44100);
- i = interleaved.length;
- j = inL.length;
- while (j--) {
- interleaved[--i] = inR[j];
- interleaved[--i] = inL[j];
- }
+ this.audio = new Audio();
+ this.audio.mozSetup(timbre.channels, timbre.samplerate);
+ timbre.samplerate = this.audio.mozSampleRate;
+ timbre.channels = this.audio.mozChannels;
+
+ this.interval = (timbre.streamsize / timbre.samplerate) * 1000;
+ this.interleaved = new Float32Array(timbre.streamsize * timbre.channels);
+
+ this.onaudioprocess = function() {
+ var interleaved;
+ var inL, inR, i, j;
+
+ interleaved = self.interleaved;
+ self.audio.mozWriteAudio(interleaved);
+ sys.process();
+
+ inL = sys.L;
+ inR = sys.R;
+
+ i = interleaved.length;
+ j = inL.length;
+ while (j--) {
+ interleaved[--i] = inR[j];
+ interleaved[--i] = inL[j];
+ }
+ };
+
+ return this;
};
- return {
- on : function() {
- timer.setInterval(onaudioprocess, interval);
- },
- off: function() {
- timer.clearInterval();
- for (var i = interleaved.length; i--; ) {
- interleaved[i] = 0.0;
- }
- }
+ this.on = function() {
+ timer.setInterval(this.onaudioprocess, this.interval);
};
+
+ this.off = function() {
+ var interleaved = this.interleaved;
+ for (var i = interleaved.length; i--; ) {
+ interleaved[i] = 0.0;
+ }
+ timer.clearInterval();
+ }
+
+ return this.setup();
};
if (typeof webkitAudioContext === "function") {
View
270 timbre.js
@@ -1,6 +1,6 @@
/**
* timbre 0.2.1 / JavaScript Library for Objective Sound Programming
- * build: Tue, 05 Jun 2012 05:20:20 GMT
+ * build: Tue, 05 Jun 2012 05:21:40 GMT
*/
;
var timbre = (function(context, timbre) {
@@ -10,7 +10,7 @@ var timbre = (function(context, timbre) {
return timbre.fn.init.apply(timbre, arguments);
};
timbre.VERSION = "0.2.1";
- timbre.BUILD = "Tue, 05 Jun 2012 05:20:20 GMT";
+ timbre.BUILD = "Tue, 05 Jun 2012 05:21:40 GMT";
timbre.env = "";
timbre.platform = "";
timbre.samplerate = 44100;
@@ -61,8 +61,31 @@ var timbre = (function(context, timbre) {
get: function() { return !timbre.sys._.ison; }
});
+ timbre.setup = function(params) {
+ var samplerate, channels, cellsize, streamsize;
+
+ if (!Object.isFrozen(timbre)) {
+ params = params || {};
+ if (typeof params.samplerate === "number") {
+ timbre.samplerate = params.samplerate;
+ }
+ if (typeof params.channels === "number") {
+ timbre.channels = params.channels;
+ }
+ if (typeof params.cellsize === "number") {
+ timbre.cellsize = params.cellsize;
+ }
+ if (typeof params.streamsize === "number") {
+ timbre.cellsize = params.streamsize;
+ }
+ timbre.sys.setup();
+ Object.freeze(timbre);
+ }
+ };
+
timbre.on = function() {
if (!timbre.sys._.ison) {
+ timbre.setup();
timbre.sys.on();
timbre.fn.do_event(this, "on");
}
@@ -682,15 +705,11 @@ var timbre = (function(context, timbre) {
initialize.apply(this, arguments);
}, $this = SoundSystem.prototype;
- var initialize = function(streamsize, channels) {
- streamsize = streamsize || timbre.streamsize;
- channels = channels || timbre.channels;
- channels = (channels === 1) ? 1 : 2;
-
- this.streamsize = streamsize;
- this.channels = channels;
- this.L = new Float32Array(streamsize);
- this.R = new Float32Array(streamsize);
+ var initialize = function() {
+ this.streamsize = timbre.streamsize;
+ this.channels = timbre.channels;
+ this.L = new Float32Array(timbre.streamsize);
+ this.R = new Float32Array(timbre.streamsize);
this.cell = new Float32Array(timbre.cellsize);
this.seq_id = 0;
@@ -704,6 +723,17 @@ var timbre = (function(context, timbre) {
this._.impl = new PlayerKlass(this);
};
+ $this.setup = function() {
+ if (this._.impl) this._.impl.setup();
+ this.streamsize = timbre.streamsize;
+ this.channels = timbre.channels;
+ this.L = new Float32Array(timbre.streamsize);
+ this.R = new Float32Array(timbre.streamsize);
+ this.cell = new Float32Array(timbre.cellsize);
+ this._.cellsize = timbre.cellsize;
+ if (timbre.samplerate === 0) timbre.samplerate = 44100;
+ };
+
$this.on = function() {
if (this._.impl) {
this._.ison = true;
@@ -6043,84 +6073,177 @@ var timbre = (function(context, timbre) {
}());
+ var setupTimbre = function(defaultSamplerate) {
+ switch (timbre.samplerate) {
+ case 11025: case 12000:
+ case 22050: case 24000:
+ case 44100: case 48000:
+ break;
+ default:
+ timbre.samplerate = defaultSamplerate;
+ }
+
+ switch (timbre.channels) {
+ default:
+ timbre.channels = 2;
+ }
+
+ switch (timbre.cellsize) {
+ case 64: case 128:
+ case 256: case 512:
+ break;
+ default:
+ timbre.cellsize = 128;
+ }
+
+ switch (timbre.streamsize) {
+ case 512: case 1024: case 2048:
+ case 4096: case 8192:
+ break;
+ default:
+ timbre.streamsize = 1024;
+ }
+ };
+
+
var WebKitPlayer = function(sys) {
- var ctx, node, onaudioprocess;
+ var self = this;
+ var ctx, onaudioprocess;
+ var samplerate, dx;
ctx = new webkitAudioContext();
- timbre.samplerate = ctx.sampleRate;
-
- node = ctx.createJavaScriptNode(sys.streamsize, 1, sys.channels);
+ samplerate = ctx.sampleRate;
- onaudioprocess = function(e) {
- var inL, inR, outL, outR, i;
- sys.process();
+ this.setup = function() {
+ setupTimbre(samplerate);
+ this.streamsize = timbre.streamsize;
- inL = sys.L;
- inR = sys.R;
- outL = e.outputBuffer.getChannelData(0);
- outR = e.outputBuffer.getChannelData(1);
- for (i = outL.length; i--; ) {
- outL[i] = inL[i];
- outR[i] = inR[i];
+ if (timbre.samplerate === samplerate) {
+ onaudioprocess = function(e) {
+ var inL, inR, outL, outR, i;
+ sys.process();
+
+ inL = sys.L;
+ inR = sys.R;
+ outL = e.outputBuffer.getChannelData(0);
+ outR = e.outputBuffer.getChannelData(1);
+ for (i = outL.length; i--; ) {
+ outL[i] = inL[i];
+ outR[i] = inR[i];
+ }
+ };
+ } else {
+ dx = timbre.samplerate / samplerate;
+ onaudioprocess = function(e) {
+ var inL, inR, outL, outR, outLen;
+ var streamsize, x, prevL, prevR;
+ var index, delta, x0, x1, xx;
+ var i, imax;
+
+ inL = sys.L;
+ inR = sys.R;
+ outL = e.outputBuffer.getChannelData(0);
+ outR = e.outputBuffer.getChannelData(1);
+ outLen = outL.length;
+
+ streamsize = self.streamsize;
+ x = self.x;
+ for (i = 0, imax = outL.length; i < imax; ++i) {
+ if (x >= streamsize) {
+ sys.process();
+ x -= streamsize;
+ }
+
+ index = x|0;
+ delta = x - index;
+
+ x1 = inL[index];
+ xx = (1.0 - delta) * prevL + delta * x1;
+ prevL = x1;
+ outL[i] = xx;
+
+ x1 = inR[index];
+ xx = (1.0 - delta) * prevR + delta * x1;
+ prevR = x1;
+ outR[i] = xx;
+
+ x += dx;
+ }
+ self.x = x;
+ self.prevL = prevL;
+ self.prevR = prevR;
+ };
}
+
+ return this;
};
- return {
- on : function() {
- this.node = ctx.createJavaScriptNode(sys.streamsize, 1, sys.channels);
- this.node.onaudioprocess = onaudioprocess;
- this.node.connect(ctx.destination);
- },
- off: function() {
- this.node.disconnect();
- this.node = null;
- }
+ this.on = function() {
+ this.x = this.streamsize;
+ this.prevL = this.prevR = 0;
+ this.node = ctx.createJavaScriptNode(sys.streamsize, 1, sys.channels);
+ this.node.onaudioprocess = onaudioprocess;
+ this.node.connect(ctx.destination);
+ };
+ this.off = function() {
+ this.node.disconnect();
+ this.node = null;
};
+
+ return this.setup();
};
var MozPlayer = function(sys) {
- var self = this;
- var audio, timer;
- var interval, interleaved;
- var onaudioprocess;
+ var timer = new MutekiTimer();
- audio = new Audio();
- audio.mozSetup(sys.channels, timbre.samplerate);
- timbre.samplerate = audio.mozSampleRate;
- timbre.channels = audio.mozChannels;
-
- timer = new MutekiTimer();
- interval = (sys.streamsize / timbre.samplerate) * 1000;
-
- interleaved = new Float32Array(sys.streamsize * sys.channels);
-
- onaudioprocess = function() {
- var inL, inR, i, j;
- audio.mozWriteAudio(interleaved);
- sys.process();
+ this.setup = function() {
+ var self = this;
- inL = sys.L;
- inR = sys.R;
+ setupTimbre(44100);
- i = interleaved.length;
- j = inL.length;
- while (j--) {
- interleaved[--i] = inR[j];
- interleaved[--i] = inL[j];
- }
+ this.audio = new Audio();
+ this.audio.mozSetup(timbre.channels, timbre.samplerate);
+ timbre.samplerate = this.audio.mozSampleRate;
+ timbre.channels = this.audio.mozChannels;
+
+ this.interval = (timbre.streamsize / timbre.samplerate) * 1000;
+ this.interleaved = new Float32Array(timbre.streamsize * timbre.channels);
+
+ this.onaudioprocess = function() {
+ var interleaved;
+ var inL, inR, i, j;
+
+ interleaved = self.interleaved;
+ self.audio.mozWriteAudio(interleaved);
+ sys.process();
+
+ inL = sys.L;
+ inR = sys.R;
+
+ i = interleaved.length;
+ j = inL.length;
+ while (j--) {
+ interleaved[--i] = inR[j];
+ interleaved[--i] = inL[j];
+ }
+ };
+
+ return this;
};
- return {
- on : function() {
- timer.setInterval(onaudioprocess, interval);
- },
- off: function() {
- timer.clearInterval();
- for (var i = interleaved.length; i--; ) {
- interleaved[i] = 0.0;
- }
- }
+ this.on = function() {
+ timer.setInterval(this.onaudioprocess, this.interval);
};
+
+ this.off = function() {
+ var interleaved = this.interleaved;
+ for (var i = interleaved.length; i--; ) {
+ interleaved[i] = 0.0;
+ }
+ timer.clearInterval();
+ }
+
+ return this.setup();
};
if (typeof webkitAudioContext === "function") {
@@ -6172,7 +6295,7 @@ var timbre = (function(context, timbre) {
console.log(x.join(""));
}());
- window.T = timbre;
+ window.timbre = window.T = timbre;
}(context, timbre));
typeof importScripts === "function" && (function(worker, timbre) {
@@ -6221,7 +6344,6 @@ var timbre = (function(context, timbre) {
}(context, timbre));
timbre.isEnabled = !!timbre.sys._.impl;
- Object.freeze(timbre);
-
+
return timbre;
}(this));
View
6 timbre.min.js
3 additions, 3 deletions not shown

No commit comments for this range

Something went wrong with that request. Please try again.