Skip to content

Commit

Permalink
Fixed #63 change the type of processing result (Int16Array->Float32Ar…
Browse files Browse the repository at this point in the history
…ray)
  • Loading branch information
mohayonao committed Dec 24, 2013
1 parent b379067 commit 50eb5cd
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 66 deletions.
16 changes: 7 additions & 9 deletions src/cc/client/client.js
Expand Up @@ -104,8 +104,8 @@ define(function(require, exports, module) {
if (this.api.strmLength) {
this.strmLength = this.api.strmLength;
}
this.strm = new Int16Array(this.strmLength * this.channels);
this.clear = new Int16Array(this.strmLength * this.channels);
this.strm = new Float32Array(this.strmLength * this.channels);
this.clear = new Float32Array(this.strmLength * this.channels);
this.strmList = new Array(C.STRM_LIST_LENGTH);
this.strmListReadIndex = 0;
this.strmListWriteIndex = 0;
Expand Down Expand Up @@ -232,17 +232,15 @@ define(function(require, exports, module) {
return this.compiler.compile(code.trim());
};
SynthClientImpl.prototype.getStream = function() {
var f32 = new Float32Array(this.strm);
for (var i = f32.length; i--; ) {
f32[i] *= 0.000030517578125;
}
var strmLength = this.strmLength;
var f32 = this.strm;
var strmLength = this.strmLength;
var strmLength4 = strmLength * 4;
return {
getChannelData: function(channel) {
if (channel === 0) {
return new Float32Array(f32.buffer, 0, strmLength);
} else if (channel === 1) {
return new Float32Array(f32.buffer, strmLength * 4);
return new Float32Array(f32.buffer, strmLength4);
}
throw new Error("bad channel");
}
Expand Down Expand Up @@ -321,7 +319,7 @@ define(function(require, exports, module) {
}
};
SynthClientImpl.prototype.recvFromLang = function(msg) {
if (msg instanceof Int16Array) {
if (msg instanceof Float32Array) {
this.strmList[this.strmListWriteIndex & C.STRM_LIST_MASK] = msg;
this.strmListWriteIndex += 1;
} else {
Expand Down
22 changes: 11 additions & 11 deletions src/cc/client/client_test.js
Expand Up @@ -304,9 +304,9 @@ define(function(require, exports, module) {
var strm = instance.strm;
for (var i = 0; i < strm.length; i++) {
if (i < strm.length * 0.5) {
strm[i] = 32767;
strm[i] = 1;
} else {
strm[i] = -32768;
strm[i] = -1;
}
}
actual = instance.getStream();
Expand Down Expand Up @@ -412,17 +412,17 @@ define(function(require, exports, module) {
]);
});
it("#process", function() {
var i16;
var f32;
for (var i = 0; i < C.STRM_LIST_LENGTH; i++) {
i16 = new Int16Array(instance.strmLength * 2);
instance.recvFromLang(i16);
f32 = new Float32Array(instance.strmLength * 2);
instance.recvFromLang(f32);
instance.process();
assert.deepEqual(instance.strm, i16);
assert.deepEqual(instance.strm, f32);
}
assert.equal(instance.syncCount, C.STRM_LIST_LENGTH);

instance.process();
assert.deepEqual(instance.strm, i16);
assert.deepEqual(instance.strm, f32);
});
it("#sendToLang", function() {
instance.sendToLang(["/sendToLang(1)", 1, 2, 3]);
Expand All @@ -436,11 +436,11 @@ define(function(require, exports, module) {
instance.sendToLang();
});
it("#recvFromLang", function() {
var i16;
var f32;
for (var i = 0; i <= C.STRM_LIST_LENGTH; i++) {
i16 = new Int16Array(instance.strmLength * 2);
instance.recvFromLang(i16);
assert.equal(instance.strmList[i & C.STRM_LIST_MASK], i16);
f32 = new Float32Array(instance.strmLength * 2);
instance.recvFromLang(f32);
assert.equal(instance.strmList[i & C.STRM_LIST_MASK], f32);
assert.equal(instance.strmListWriteIndex, i+1);
}
});
Expand Down
8 changes: 4 additions & 4 deletions src/cc/common/audioapi-audiodata.js
Expand Up @@ -36,8 +36,8 @@ define(function(require, exports, module) {
var msec = (sys.strmLength / sys.sampleRate) * 1000;
var written = 0;
var start = Date.now();
var inL = new Int16Array(sys.strm.buffer, 0, sys.strmLength);
var inR = new Int16Array(sys.strm.buffer, sys.strmLength * 2);
var inL = new Float32Array(sys.strm.buffer, 0, sys.strmLength);
var inR = new Float32Array(sys.strm.buffer, sys.strmLength * 2);

var onaudioprocess = function() {
if (written - C.PROCESS_MARGIN > Date.now() - start) {
Expand All @@ -47,8 +47,8 @@ define(function(require, exports, module) {
var j = inL.length;
sys.process();
while (j--) {
interleaved[--i] = inR[j] * 0.000030517578125;
interleaved[--i] = inL[j] * 0.000030517578125;
interleaved[--i] = inR[j];
interleaved[--i] = inL[j];
}
audio.mozWriteAudio(interleaved);
written += msec;
Expand Down
6 changes: 4 additions & 2 deletions src/cc/common/audioapi-flashfallback.js
Expand Up @@ -60,9 +60,11 @@ define(function(require, exports, module) {
return;
}
sys.process();
var _in = sys.strm;
var x, _in = sys.strm;
for (var i = 0; i < len; ++i) {
out[i] = String.fromCharCode( ((_in[i] + 32768)>>1) + 16384 );
x = (_in[i] * 16384 + 32768)|0;
x = Math.max(16384, Math.min(x, 49152));
out[i] = String.fromCharCode(x);
}
swf.writeAudio(out.join(""));
written += msec;
Expand Down
7 changes: 4 additions & 3 deletions src/cc/common/audioapi-nodeaudio.js
Expand Up @@ -31,13 +31,14 @@ define(function(require, exports, module) {
var buf = new Buffer(n);
var x, i, j, k = 0;
n = (n >> 2) / sys.strmLength;
x = strm;
while (n--) {
sys._process();
for (i = 0, j = strmLength; i < strmLength; ++i, ++j) {
buf.writeInt16LE(strm[i], k);
x = Math.max(-32768, Math.min((strm[i] * 32768)|0, 32767));
buf.writeInt16LE(x, k);
k += 2;
buf.writeInt16LE(strm[j], k);
x = Math.max(-32768, Math.min((strm[j] * 32768)|0, 32767));
buf.writeInt16LE(x, k);
k += 2;
}
}
Expand Down
14 changes: 6 additions & 8 deletions src/cc/common/audioapi-webaudio.js
Expand Up @@ -21,19 +21,17 @@ define(function(require, exports, module) {
var sys = this.sys;
var onaudioprocess;
var strm = sys.strm;
var strmLength = sys.strmLength;
var strmLength = sys.strmLength;
var strmLength4 = strmLength * 4;
var strmL = new Float32Array(strm.buffer, 0, strmLength);
var strmR = new Float32Array(strm.buffer, strmLength4);
if (this.sys.speaker) {
if (this.sys.sampleRate === this.sampleRate) {
onaudioprocess = function(e) {
var outs = e.outputBuffer;
var outL = outs.getChannelData(0);
var outR = outs.getChannelData(1);
var i = strmLength, j = strmLength << 1;
sys.process();
while (j--, i--) {
outL[i] = strm[i] * 0.000030517578125;
outR[i] = strm[j] * 0.000030517578125;
}
outs.getChannelData(0).set(strmL);
outs.getChannelData(1).set(strmR);
};
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/cc/lang/lang-socket.js
Expand Up @@ -47,7 +47,7 @@ define(function(require, exports, module) {
// receive a message from the socket-server
var msg = e.data;
if (typeof msg !== "string") {
that.sendToClient(new Int16Array(msg));
that.sendToClient(new Float32Array(msg));
return;
}
that.recvFromServer(JSON.parse(msg));
Expand Down
2 changes: 1 addition & 1 deletion src/cc/lang/lang.js
Expand Up @@ -40,7 +40,7 @@ define(function(require, exports, module) {
throw "SynthLang#sendToServer: should be overridden[" + cc.opmode + "]";
};
SynthLang.prototype.recvFromServer = function(msg) {
if (msg instanceof Int16Array) {
if (msg instanceof Float32Array) {
this.sendToClient(msg);
} else {
var func = commands[msg[0]];
Expand Down
19 changes: 9 additions & 10 deletions src/cc/server/server-nodejs.js
Expand Up @@ -32,13 +32,13 @@ define(function(require, exports, module) {
userId = userId|0;
this.instance.play(userId);
if (this.api) {
this._strm = new Int16Array(this.strmLength * this.channels);
this._strm = new Float32Array(this.strmLength * this.channels);
this.strmList = new Array(C.STRM_LIST_LENGTH);
this.strmListReadIndex = 0;
this.strmListWriteIndex = 0;
var strmList = this.strmList;
for (var i = strmList.length; i--; ) {
strmList[i] = new Int16Array(this._strm);
strmList[i] = new Float32Array(this._strm);
}
if (!this.api.isPlaying) {
this.api.play();
Expand Down Expand Up @@ -79,22 +79,21 @@ define(function(require, exports, module) {
var busOutL = this.busOutL;
var busOutR = this.busOutR;
var lang = cc.lang;
var offset = 0;
var offsetL = 0;
var offsetR = strmLength;
for (var i = 0, imax = strmLength / bufLength; i < imax; ++i) {
lang.process();
instance.process(bufLength);
busOut.set(instance.bus);
var j = bufLength, k = strmLength + bufLength;
while (k--, j--) {
strm[j + offset] = Math.max(-32768, Math.min(busOutL[j] * 32768, 32767));
strm[k + offset] = Math.max(-32768, Math.min(busOutR[j] * 32768, 32767));
}
offset += bufLength;
strm.set(busOutL, offsetL);
strm.set(busOutR, offsetR);
offsetL += bufLength;
offsetR += bufLength;
}
this.sendToLang(strm);
this.syncCount += 1;
if (this.api) {
this.strmList[this.strmListWriteIndex & C.STRM_LIST_MASK] = new Int16Array(strm);
this.strmList[this.strmListWriteIndex & C.STRM_LIST_MASK] = new Float32Array(strm);
this.strmListWriteIndex += 1;
}
};
Expand Down
17 changes: 8 additions & 9 deletions src/cc/server/server-socket.js
Expand Up @@ -166,7 +166,7 @@ define(function(require, exports, module) {
SocketSynthServer.prototype.connect = function() {
};
SocketSynthServer.prototype.sendToLang = function(msg, userId) {
if (msg instanceof Int16Array) {
if (msg instanceof Float32Array) {
this.list.forEach(function(ws) {
if (ws.readyState === 1) {
ws.send(msg.buffer, {binary:true, mask:false});
Expand Down Expand Up @@ -198,22 +198,21 @@ define(function(require, exports, module) {
var bufLength = this.bufLength;
var busOutL = this.busOutL;
var busOutR = this.busOutR;
var offset = 0;
var offsetL = 0;
var offsetR = strmLength;
for (var i = 0, imax = strmLength / bufLength; i < imax; ++i) {
instance.process(bufLength);
var j = bufLength, k = strmLength + bufLength;
while (k--, j--) {
strm[j + offset] = Math.max(-32768, Math.min(busOutL[j] * 32768, 32767));
strm[k + offset] = Math.max(-32768, Math.min(busOutR[j] * 32768, 32767));
}
offset += bufLength;
strm.set(busOutL, offsetL);
strm.set(busOutR, offsetR);
offsetL += bufLength;
offsetR += bufLength;
}
this.sendToLang(strm);
this.sendToLang(["/process"]);
this.syncCount += 1;

if (this.api) {
this.strmList[this.strmListWriteIndex] = new Int16Array(strm);
this.strmList[this.strmListWriteIndex] = new Float32Array(strm);
this.strmListWriteIndex = (this.strmListWriteIndex + 1) & C.STRM_LIST_MASK;
}
};
Expand Down
13 changes: 6 additions & 7 deletions src/cc/server/server-worker.js
Expand Up @@ -35,17 +35,16 @@ define(function(require, exports, module) {
var busOutL = this.busOutL;
var busOutR = this.busOutR;
var lang = cc.lang;
var offset = 0;
var offsetL = 0;
var offsetR = strmLength;
for (var i = 0, imax = strmLength / bufLength; i < imax; ++i) {
lang.process();
instance.process(bufLength);
busOut.set(instance.bus);
var j = bufLength, k = strmLength + bufLength;
while (k--, j--) {
strm[j + offset] = Math.max(-32768, Math.min(busOutL[j] * 32768, 32767));
strm[k + offset] = Math.max(-32768, Math.min(busOutR[j] * 32768, 32767));
}
offset += bufLength;
strm.set(busOutL, offsetL);
strm.set(busOutR, offsetR);
offsetL += bufLength;
offsetR += bufLength;
}
this.sendToLang(strm);
this.syncCount += 1;
Expand Down
2 changes: 1 addition & 1 deletion src/cc/server/server.js
Expand Up @@ -46,7 +46,7 @@ define(function(require, exports, module) {
this.channels = msg[2]|0;
this.strmLength = msg[3]|0;
}
this.strm = new Int16Array(this.strmLength * this.channels);
this.strm = new Float32Array(this.strmLength * this.channels);

var busLength = this.bufLength * C.AUDIO_BUS_LEN + C.CONTROL_BUS_LEN;
var bufLength = this.bufLength;
Expand Down

0 comments on commit 50eb5cd

Please sign in to comment.