Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 3 commits
  • 7 files changed
  • 0 comments
  • 1 contributor
25 src/core/soundsystem.js
@@ -11,15 +11,11 @@ var SoundSystem = (function() {
11 11 initialize.apply(this, arguments);
12 12 }, $this = SoundSystem.prototype;
13 13
14   - var initialize = function(streamsize, channels) {
15   - streamsize = streamsize || timbre.streamsize;
16   - channels = channels || timbre.channels;
17   - channels = (channels === 1) ? 1 : 2;
18   -
19   - this.streamsize = streamsize;
20   - this.channels = channels;
21   - this.L = new Float32Array(streamsize);
22   - this.R = new Float32Array(streamsize);
  14 + var initialize = function() {
  15 + this.streamsize = timbre.streamsize;
  16 + this.channels = timbre.channels;
  17 + this.L = new Float32Array(timbre.streamsize);
  18 + this.R = new Float32Array(timbre.streamsize);
23 19 this.cell = new Float32Array(timbre.cellsize);
24 20 this.seq_id = 0;
25 21
@@ -33,6 +29,17 @@ var SoundSystem = (function() {
33 29 this._.impl = new PlayerKlass(this);
34 30 };
35 31
  32 + $this.setup = function() {
  33 + if (this._.impl) this._.impl.setup();
  34 + this.streamsize = timbre.streamsize;
  35 + this.channels = timbre.channels;
  36 + this.L = new Float32Array(timbre.streamsize);
  37 + this.R = new Float32Array(timbre.streamsize);
  38 + this.cell = new Float32Array(timbre.cellsize);
  39 + this._.cellsize = timbre.cellsize;
  40 + if (timbre.samplerate === 0) timbre.samplerate = 44100;
  41 + };
  42 +
36 43 $this.on = function() {
37 44 if (this._.impl) {
38 45 this._.ison = true;
24 src/timbre.js
@@ -4,6 +4,7 @@
4 4 "use strict";
5 5
6 6 // __BEGIN__
  7 +
7 8 var timbre = function() {
8 9 return timbre.fn.init.apply(timbre, arguments);
9 10 };
@@ -59,8 +60,31 @@ Object.defineProperty(timbre, "isOff", {
59 60 get: function() { return !timbre.sys._.ison; }
60 61 });
61 62
  63 +timbre.setup = function(params) {
  64 + var samplerate, channels, cellsize, streamsize;
  65 +
  66 + if (!Object.isFrozen(timbre)) {
  67 + params = params || {};
  68 + if (typeof params.samplerate === "number") {
  69 + timbre.samplerate = params.samplerate;
  70 + }
  71 + if (typeof params.channels === "number") {
  72 + timbre.channels = params.channels;
  73 + }
  74 + if (typeof params.cellsize === "number") {
  75 + timbre.cellsize = params.cellsize;
  76 + }
  77 + if (typeof params.streamsize === "number") {
  78 + timbre.cellsize = params.streamsize;
  79 + }
  80 + timbre.sys.setup();
  81 + Object.freeze(timbre);
  82 + }
  83 +};
  84 +
62 85 timbre.on = function() {
63 86 if (!timbre.sys._.ison) {
  87 + timbre.setup();
64 88 timbre.sys.on();
65 89 timbre.fn.do_event(this, "on");
66 90 }
3  src/timbre.txt
@@ -28,7 +28,6 @@ var timbre = (function(context, timbre) {
28 28 }(context, timbre));
29 29
30 30 timbre.isEnabled = !!timbre.sys._.impl;
31   - Object.freeze(timbre);
32   -
  31 +
33 32 return timbre;
34 33 }(this));
2  src/window/exports.js
@@ -23,6 +23,6 @@ timbre.context = window;
23 23 console.log(x.join(""));
24 24 }());
25 25
26   -window.T = timbre;
  26 +window.timbre = window.T = timbre;
27 27
28 28 // __END__
213 src/window/player.js
@@ -7,84 +7,177 @@ var timbre = require("../timbre");
7 7 var window = {};
8 8 // __BEGIN__
9 9
  10 +var setupTimbre = function(defaultSamplerate) {
  11 + switch (timbre.samplerate) {
  12 + case 11025: case 12000:
  13 + case 22050: case 24000:
  14 + case 44100: case 48000:
  15 + break;
  16 + default:
  17 + timbre.samplerate = defaultSamplerate;
  18 + }
  19 +
  20 + switch (timbre.channels) {
  21 + default:
  22 + timbre.channels = 2;
  23 + }
  24 +
  25 + switch (timbre.cellsize) {
  26 + case 64: case 128:
  27 + case 256: case 512:
  28 + break;
  29 + default:
  30 + timbre.cellsize = 128;
  31 + }
  32 +
  33 + switch (timbre.streamsize) {
  34 + case 512: case 1024: case 2048:
  35 + case 4096: case 8192:
  36 + break;
  37 + default:
  38 + timbre.streamsize = 1024;
  39 + }
  40 +};
  41 +
  42 +
10 43 var WebKitPlayer = function(sys) {
11   - var ctx, node, onaudioprocess;
  44 + var self = this;
  45 + var ctx, onaudioprocess;
  46 + var samplerate, dx;
12 47
13 48 ctx = new webkitAudioContext();
14   - timbre.samplerate = ctx.sampleRate;
  49 + samplerate = ctx.sampleRate;
15 50
16   - node = ctx.createJavaScriptNode(sys.streamsize, 1, sys.channels);
17   -
18   - onaudioprocess = function(e) {
19   - var inL, inR, outL, outR, i;
20   - sys.process();
  51 + this.setup = function() {
  52 + setupTimbre(samplerate);
  53 + this.streamsize = timbre.streamsize;
21 54
22   - inL = sys.L;
23   - inR = sys.R;
24   - outL = e.outputBuffer.getChannelData(0);
25   - outR = e.outputBuffer.getChannelData(1);
26   - for (i = outL.length; i--; ) {
27   - outL[i] = inL[i];
28   - outR[i] = inR[i];
  55 + if (timbre.samplerate === samplerate) {
  56 + onaudioprocess = function(e) {
  57 + var inL, inR, outL, outR, i;
  58 + sys.process();
  59 +
  60 + inL = sys.L;
  61 + inR = sys.R;
  62 + outL = e.outputBuffer.getChannelData(0);
  63 + outR = e.outputBuffer.getChannelData(1);
  64 + for (i = outL.length; i--; ) {
  65 + outL[i] = inL[i];
  66 + outR[i] = inR[i];
  67 + }
  68 + };
  69 + } else {
  70 + dx = timbre.samplerate / samplerate;
  71 + onaudioprocess = function(e) {
  72 + var inL, inR, outL, outR, outLen;
  73 + var streamsize, x, prevL, prevR;
  74 + var index, delta, x0, x1, xx;
  75 + var i, imax;
  76 +
  77 + inL = sys.L;
  78 + inR = sys.R;
  79 + outL = e.outputBuffer.getChannelData(0);
  80 + outR = e.outputBuffer.getChannelData(1);
  81 + outLen = outL.length;
  82 +
  83 + streamsize = self.streamsize;
  84 + x = self.x;
  85 + for (i = 0, imax = outL.length; i < imax; ++i) {
  86 + if (x >= streamsize) {
  87 + sys.process();
  88 + x -= streamsize;
  89 + }
  90 +
  91 + index = x|0;
  92 + delta = x - index;
  93 +
  94 + x1 = inL[index];
  95 + xx = (1.0 - delta) * prevL + delta * x1;
  96 + prevL = x1;
  97 + outL[i] = xx;
  98 +
  99 + x1 = inR[index];
  100 + xx = (1.0 - delta) * prevR + delta * x1;
  101 + prevR = x1;
  102 + outR[i] = xx;
  103 +
  104 + x += dx;
  105 + }
  106 + self.x = x;
  107 + self.prevL = prevL;
  108 + self.prevR = prevR;
  109 + };
29 110 }
  111 +
  112 + return this;
30 113 };
31 114
32   - return {
33   - on : function() {
34   - this.node = ctx.createJavaScriptNode(sys.streamsize, 1, sys.channels);
35   - this.node.onaudioprocess = onaudioprocess;
36   - this.node.connect(ctx.destination);
37   - },
38   - off: function() {
39   - this.node.disconnect();
40   - this.node = null;
41   - }
  115 + this.on = function() {
  116 + this.x = this.streamsize;
  117 + this.prevL = this.prevR = 0;
  118 + this.node = ctx.createJavaScriptNode(sys.streamsize, 1, sys.channels);
  119 + this.node.onaudioprocess = onaudioprocess;
  120 + this.node.connect(ctx.destination);
42 121 };
  122 + this.off = function() {
  123 + this.node.disconnect();
  124 + this.node = null;
  125 + };
  126 +
  127 + return this.setup();
43 128 };
44 129
45 130 var MozPlayer = function(sys) {
46   - var self = this;
47   - var audio, timer;
48   - var interval, interleaved;
49   - var onaudioprocess;
  131 + var timer = new MutekiTimer();
50 132
51   - audio = new Audio();
52   - audio.mozSetup(sys.channels, timbre.samplerate);
53   - timbre.samplerate = audio.mozSampleRate;
54   - timbre.channels = audio.mozChannels;
55   -
56   - timer = new MutekiTimer();
57   - interval = (sys.streamsize / timbre.samplerate) * 1000;
58   -
59   - interleaved = new Float32Array(sys.streamsize * sys.channels);
60   -
61   - onaudioprocess = function() {
62   - var inL, inR, i, j;
63   - audio.mozWriteAudio(interleaved);
64   - sys.process();
  133 + this.setup = function() {
  134 + var self = this;
65 135
66   - inL = sys.L;
67   - inR = sys.R;
  136 + setupTimbre(44100);
68 137
69   - i = interleaved.length;
70   - j = inL.length;
71   - while (j--) {
72   - interleaved[--i] = inR[j];
73   - interleaved[--i] = inL[j];
74   - }
  138 + this.audio = new Audio();
  139 + this.audio.mozSetup(timbre.channels, timbre.samplerate);
  140 + timbre.samplerate = this.audio.mozSampleRate;
  141 + timbre.channels = this.audio.mozChannels;
  142 +
  143 + this.interval = (timbre.streamsize / timbre.samplerate) * 1000;
  144 + this.interleaved = new Float32Array(timbre.streamsize * timbre.channels);
  145 +
  146 + this.onaudioprocess = function() {
  147 + var interleaved;
  148 + var inL, inR, i, j;
  149 +
  150 + interleaved = self.interleaved;
  151 + self.audio.mozWriteAudio(interleaved);
  152 + sys.process();
  153 +
  154 + inL = sys.L;
  155 + inR = sys.R;
  156 +
  157 + i = interleaved.length;
  158 + j = inL.length;
  159 + while (j--) {
  160 + interleaved[--i] = inR[j];
  161 + interleaved[--i] = inL[j];
  162 + }
  163 + };
  164 +
  165 + return this;
75 166 };
76 167
77   - return {
78   - on : function() {
79   - timer.setInterval(onaudioprocess, interval);
80   - },
81   - off: function() {
82   - timer.clearInterval();
83   - for (var i = interleaved.length; i--; ) {
84   - interleaved[i] = 0.0;
85   - }
86   - }
  168 + this.on = function() {
  169 + timer.setInterval(this.onaudioprocess, this.interval);
87 170 };
  171 +
  172 + this.off = function() {
  173 + var interleaved = this.interleaved;
  174 + for (var i = interleaved.length; i--; ) {
  175 + interleaved[i] = 0.0;
  176 + }
  177 + timer.clearInterval();
  178 + }
  179 +
  180 + return this.setup();
88 181 };
89 182
90 183 if (typeof webkitAudioContext === "function") {
270 timbre.js
... ... @@ -1,6 +1,6 @@
1 1 /**
2 2 * timbre 0.2.1 / JavaScript Library for Objective Sound Programming
3   - * build: Tue, 05 Jun 2012 05:20:20 GMT
  3 + * build: Tue, 05 Jun 2012 05:21:40 GMT
4 4 */
5 5 ;
6 6 var timbre = (function(context, timbre) {
@@ -10,7 +10,7 @@ var timbre = (function(context, timbre) {
10 10 return timbre.fn.init.apply(timbre, arguments);
11 11 };
12 12 timbre.VERSION = "0.2.1";
13   - timbre.BUILD = "Tue, 05 Jun 2012 05:20:20 GMT";
  13 + timbre.BUILD = "Tue, 05 Jun 2012 05:21:40 GMT";
14 14 timbre.env = "";
15 15 timbre.platform = "";
16 16 timbre.samplerate = 44100;
@@ -61,8 +61,31 @@ var timbre = (function(context, timbre) {
61 61 get: function() { return !timbre.sys._.ison; }
62 62 });
63 63
  64 + timbre.setup = function(params) {
  65 + var samplerate, channels, cellsize, streamsize;
  66 +
  67 + if (!Object.isFrozen(timbre)) {
  68 + params = params || {};
  69 + if (typeof params.samplerate === "number") {
  70 + timbre.samplerate = params.samplerate;
  71 + }
  72 + if (typeof params.channels === "number") {
  73 + timbre.channels = params.channels;
  74 + }
  75 + if (typeof params.cellsize === "number") {
  76 + timbre.cellsize = params.cellsize;
  77 + }
  78 + if (typeof params.streamsize === "number") {
  79 + timbre.cellsize = params.streamsize;
  80 + }
  81 + timbre.sys.setup();
  82 + Object.freeze(timbre);
  83 + }
  84 + };
  85 +
64 86 timbre.on = function() {
65 87 if (!timbre.sys._.ison) {
  88 + timbre.setup();
66 89 timbre.sys.on();
67 90 timbre.fn.do_event(this, "on");
68 91 }
@@ -682,15 +705,11 @@ var timbre = (function(context, timbre) {
682 705 initialize.apply(this, arguments);
683 706 }, $this = SoundSystem.prototype;
684 707
685   - var initialize = function(streamsize, channels) {
686   - streamsize = streamsize || timbre.streamsize;
687   - channels = channels || timbre.channels;
688   - channels = (channels === 1) ? 1 : 2;
689   -
690   - this.streamsize = streamsize;
691   - this.channels = channels;
692   - this.L = new Float32Array(streamsize);
693   - this.R = new Float32Array(streamsize);
  708 + var initialize = function() {
  709 + this.streamsize = timbre.streamsize;
  710 + this.channels = timbre.channels;
  711 + this.L = new Float32Array(timbre.streamsize);
  712 + this.R = new Float32Array(timbre.streamsize);
694 713 this.cell = new Float32Array(timbre.cellsize);
695 714 this.seq_id = 0;
696 715
@@ -704,6 +723,17 @@ var timbre = (function(context, timbre) {
704 723 this._.impl = new PlayerKlass(this);
705 724 };
706 725
  726 + $this.setup = function() {
  727 + if (this._.impl) this._.impl.setup();
  728 + this.streamsize = timbre.streamsize;
  729 + this.channels = timbre.channels;
  730 + this.L = new Float32Array(timbre.streamsize);
  731 + this.R = new Float32Array(timbre.streamsize);
  732 + this.cell = new Float32Array(timbre.cellsize);
  733 + this._.cellsize = timbre.cellsize;
  734 + if (timbre.samplerate === 0) timbre.samplerate = 44100;
  735 + };
  736 +
707 737 $this.on = function() {
708 738 if (this._.impl) {
709 739 this._.ison = true;
@@ -6043,84 +6073,177 @@ var timbre = (function(context, timbre) {
6043 6073 }());
6044 6074
6045 6075
  6076 + var setupTimbre = function(defaultSamplerate) {
  6077 + switch (timbre.samplerate) {
  6078 + case 11025: case 12000:
  6079 + case 22050: case 24000:
  6080 + case 44100: case 48000:
  6081 + break;
  6082 + default:
  6083 + timbre.samplerate = defaultSamplerate;
  6084 + }
  6085 +
  6086 + switch (timbre.channels) {
  6087 + default:
  6088 + timbre.channels = 2;
  6089 + }
  6090 +
  6091 + switch (timbre.cellsize) {
  6092 + case 64: case 128:
  6093 + case 256: case 512:
  6094 + break;
  6095 + default:
  6096 + timbre.cellsize = 128;
  6097 + }
  6098 +
  6099 + switch (timbre.streamsize) {
  6100 + case 512: case 1024: case 2048:
  6101 + case 4096: case 8192:
  6102 + break;
  6103 + default:
  6104 + timbre.streamsize = 1024;
  6105 + }
  6106 + };
  6107 +
  6108 +
6046 6109 var WebKitPlayer = function(sys) {
6047   - var ctx, node, onaudioprocess;
  6110 + var self = this;
  6111 + var ctx, onaudioprocess;
  6112 + var samplerate, dx;
6048 6113
6049 6114 ctx = new webkitAudioContext();
6050   - timbre.samplerate = ctx.sampleRate;
6051   -
6052   - node = ctx.createJavaScriptNode(sys.streamsize, 1, sys.channels);
  6115 + samplerate = ctx.sampleRate;
6053 6116
6054   - onaudioprocess = function(e) {
6055   - var inL, inR, outL, outR, i;
6056   - sys.process();
  6117 + this.setup = function() {
  6118 + setupTimbre(samplerate);
  6119 + this.streamsize = timbre.streamsize;
6057 6120
6058   - inL = sys.L;
6059   - inR = sys.R;
6060   - outL = e.outputBuffer.getChannelData(0);
6061   - outR = e.outputBuffer.getChannelData(1);
6062   - for (i = outL.length; i--; ) {
6063   - outL[i] = inL[i];
6064   - outR[i] = inR[i];
  6121 + if (timbre.samplerate === samplerate) {
  6122 + onaudioprocess = function(e) {
  6123 + var inL, inR, outL, outR, i;
  6124 + sys.process();
  6125 +
  6126 + inL = sys.L;
  6127 + inR = sys.R;
  6128 + outL = e.outputBuffer.getChannelData(0);
  6129 + outR = e.outputBuffer.getChannelData(1);
  6130 + for (i = outL.length; i--; ) {
  6131 + outL[i] = inL[i];
  6132 + outR[i] = inR[i];
  6133 + }
  6134 + };
  6135 + } else {
  6136 + dx = timbre.samplerate / samplerate;
  6137 + onaudioprocess = function(e) {
  6138 + var inL, inR, outL, outR, outLen;
  6139 + var streamsize, x, prevL, prevR;
  6140 + var index, delta, x0, x1, xx;
  6141 + var i, imax;
  6142 +
  6143 + inL = sys.L;
  6144 + inR = sys.R;
  6145 + outL = e.outputBuffer.getChannelData(0);
  6146 + outR = e.outputBuffer.getChannelData(1);
  6147 + outLen = outL.length;
  6148 +
  6149 + streamsize = self.streamsize;
  6150 + x = self.x;
  6151 + for (i = 0, imax = outL.length; i < imax; ++i) {
  6152 + if (x >= streamsize) {
  6153 + sys.process();
  6154 + x -= streamsize;
  6155 + }
  6156 +
  6157 + index = x|0;
  6158 + delta = x - index;
  6159 +
  6160 + x1 = inL[index];
  6161 + xx = (1.0 - delta) * prevL + delta * x1;
  6162 + prevL = x1;
  6163 + outL[i] = xx;
  6164 +
  6165 + x1 = inR[index];
  6166 + xx = (1.0 - delta) * prevR + delta * x1;
  6167 + prevR = x1;
  6168 + outR[i] = xx;
  6169 +
  6170 + x += dx;
  6171 + }
  6172 + self.x = x;
  6173 + self.prevL = prevL;
  6174 + self.prevR = prevR;
  6175 + };
6065 6176 }
  6177 +
  6178 + return this;
6066 6179 };
6067 6180
6068   - return {
6069   - on : function() {
6070   - this.node = ctx.createJavaScriptNode(sys.streamsize, 1, sys.channels);
6071   - this.node.onaudioprocess = onaudioprocess;
6072   - this.node.connect(ctx.destination);
6073   - },
6074   - off: function() {
6075   - this.node.disconnect();
6076   - this.node = null;
6077   - }
  6181 + this.on = function() {
  6182 + this.x = this.streamsize;
  6183 + this.prevL = this.prevR = 0;
  6184 + this.node = ctx.createJavaScriptNode(sys.streamsize, 1, sys.channels);
  6185 + this.node.onaudioprocess = onaudioprocess;
  6186 + this.node.connect(ctx.destination);
  6187 + };
  6188 + this.off = function() {
  6189 + this.node.disconnect();
  6190 + this.node = null;
6078 6191 };
  6192 +
  6193 + return this.setup();
6079 6194 };
6080 6195
6081 6196 var MozPlayer = function(sys) {
6082   - var self = this;
6083   - var audio, timer;
6084   - var interval, interleaved;
6085   - var onaudioprocess;
  6197 + var timer = new MutekiTimer();
6086 6198
6087   - audio = new Audio();
6088   - audio.mozSetup(sys.channels, timbre.samplerate);
6089   - timbre.samplerate = audio.mozSampleRate;
6090   - timbre.channels = audio.mozChannels;
6091   -
6092   - timer = new MutekiTimer();
6093   - interval = (sys.streamsize / timbre.samplerate) * 1000;
6094   -
6095   - interleaved = new Float32Array(sys.streamsize * sys.channels);
6096   -
6097   - onaudioprocess = function() {
6098   - var inL, inR, i, j;
6099   - audio.mozWriteAudio(interleaved);
6100   - sys.process();
  6199 + this.setup = function() {
  6200 + var self = this;
6101 6201
6102   - inL = sys.L;
6103   - inR = sys.R;
  6202 + setupTimbre(44100);
6104 6203
6105   - i = interleaved.length;
6106   - j = inL.length;
6107   - while (j--) {
6108   - interleaved[--i] = inR[j];
6109   - interleaved[--i] = inL[j];
6110   - }
  6204 + this.audio = new Audio();
  6205 + this.audio.mozSetup(timbre.channels, timbre.samplerate);
  6206 + timbre.samplerate = this.audio.mozSampleRate;
  6207 + timbre.channels = this.audio.mozChannels;
  6208 +
  6209 + this.interval = (timbre.streamsize / timbre.samplerate) * 1000;
  6210 + this.interleaved = new Float32Array(timbre.streamsize * timbre.channels);
  6211 +
  6212 + this.onaudioprocess = function() {
  6213 + var interleaved;
  6214 + var inL, inR, i, j;
  6215 +
  6216 + interleaved = self.interleaved;
  6217 + self.audio.mozWriteAudio(interleaved);
  6218 + sys.process();
  6219 +
  6220 + inL = sys.L;
  6221 + inR = sys.R;
  6222 +
  6223 + i = interleaved.length;
  6224 + j = inL.length;
  6225 + while (j--) {
  6226 + interleaved[--i] = inR[j];
  6227 + interleaved[--i] = inL[j];
  6228 + }
  6229 + };
  6230 +
  6231 + return this;
6111 6232 };
6112 6233
6113   - return {
6114   - on : function() {
6115   - timer.setInterval(onaudioprocess, interval);
6116   - },
6117   - off: function() {
6118   - timer.clearInterval();
6119   - for (var i = interleaved.length; i--; ) {
6120   - interleaved[i] = 0.0;
6121   - }
6122   - }
  6234 + this.on = function() {
  6235 + timer.setInterval(this.onaudioprocess, this.interval);
6123 6236 };
  6237 +
  6238 + this.off = function() {
  6239 + var interleaved = this.interleaved;
  6240 + for (var i = interleaved.length; i--; ) {
  6241 + interleaved[i] = 0.0;
  6242 + }
  6243 + timer.clearInterval();
  6244 + }
  6245 +
  6246 + return this.setup();
6124 6247 };
6125 6248
6126 6249 if (typeof webkitAudioContext === "function") {
@@ -6172,7 +6295,7 @@ var timbre = (function(context, timbre) {
6172 6295 console.log(x.join(""));
6173 6296 }());
6174 6297
6175   - window.T = timbre;
  6298 + window.timbre = window.T = timbre;
6176 6299 }(context, timbre));
6177 6300
6178 6301 typeof importScripts === "function" && (function(worker, timbre) {
@@ -6221,7 +6344,6 @@ var timbre = (function(context, timbre) {
6221 6344 }(context, timbre));
6222 6345
6223 6346 timbre.isEnabled = !!timbre.sys._.impl;
6224   - Object.freeze(timbre);
6225   -
  6347 +
6226 6348 return timbre;
6227 6349 }(this));
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.