Permalink
Browse files

Adds flock.ugen.math() for basic math operations powered by dspapi.js

  • Loading branch information...
1 parent b4036ba commit 0fc01a7066d357c129762b81e56fc250ce457e19 @colinbdclark committed Sep 10, 2012
@@ -107,6 +107,15 @@ var flock = flock || {};
return buf;
};
+ flock.generate.constant = function (bufOrSize, val) {
+ var buf = typeof (bufOrSize) === "number" ? new Float32Array(bufOrSize) : bufOrSize,
+ i;
+ for (i = 0; i < buf.length; i++) {
+ buf[i] = val;
+ }
+ return buf;
+ };
+
flock.generate.silence = function (bufOrSize) {
if (typeof (bufOrSize) === "number") {
return new Float32Array(bufOrSize);
View
@@ -196,6 +196,63 @@ var flock = flock || {};
});
+ flock.ugen.math = function (inputs, output, options) {
+ var that = flock.ugen(inputs, output, options);
+ that.expandedSource = new Float32Array(that.options.audioSettings.rates.control);
+
+ that.krSourceKrInputGen = function () {
+ var op = that.activeInput,
+ input = that.inputs[op],
+ sourceBuf = flock.generate(that.expandedSource, that.inputs.source.output[0]);
+ DSP[op](that.output, sourceBuf, input.output[0]);
+ };
+
+ that.krSourceArInputGen = function () {
+ var op = that.activeInput,
+ input = that.inputs[op],
+ sourceBuf = flock.generate(that.expandedSource, that.inputs.source.output[0]);
+ DSP[op](that.output, sourceBuf, input.output);
+ };
+
+ that.arSourceKrInputGen = function () {
+ var op = that.activeInput,
+ input = that.inputs[op],
+ sourceBuf = that.inputs.source.output;
+ DSP[op](that.output, sourceBuf, input.output[0]);
+ };
+
+ that.arSourceArInputGen = function () {
+ var op = that.activeInput,
+ input = that.inputs[op];
+ DSP[op](that.output, that.inputs.source.output, input.output);
+ };
+
+ that.onInputChanged = function () {
+ // Find the first input and use it. Multiple inputters, beware.
+ // TODO: Support multiple operations.
+ var inputs = Object.keys(that.inputs),
+ i,
+ input,
+ isInputAudioRate;
+
+ for (i = 0; i < inputs.length; i++) {
+ input = inputs[i];
+ if (input !== "source") {
+ that.activeInput = input;
+ isInputAudioRate = that.inputs[input].rate === "audio";
+ that.gen = that.inputs.source.rate === "audio" ?
+ (isInputAudioRate ? that.arSourceArInputGen : that.arSourceKrInputGen) :
+ (isInputAudioRate ? that.krSourceArInputGen : that.krSourceKrInputGen);
+ break;
+ }
+ }
+ };
+
+ that.onInputChanged();
+ return that;
+ };
+
+
flock.ugen.sum = function (inputs, output, options) {
var that = flock.ugen(inputs, output, options);
@@ -8,10 +8,12 @@
<script src="../../third-party/qunit/js/qunit.js"></script>
<script src="../../../third-party/jquery/js/jquery.min.js"></script>
+ <script src="../../../third-party/dspapi/js/dspapi.js"></script>
+
<script src="../../../flocking/flocking-core.js"></script>
<script src="../../../flocking/flocking-ugens.js"></script>
<script src="../../../flocking/flocking-parser.js"></script>
-
+
<script src="../js/flocking-test-utils.js"></script>
<script src="../js/ugen-test.js"></script>
</head>
@@ -42,14 +42,7 @@ var flock = flock || {};
return new Float32Array(buf);
};
- flock.test.constantBuffer = function (size, val) {
- var buf = new Float32Array(size),
- i;
- for (i = 0; i < size; i++) {
- buf[i] = val;
- }
- return buf;
- };
+ flock.test.constantBuffer = flock.generate.constant;
flock.test.assertArrayEquals = function (actual, expected, msg) {
var i;
@@ -1019,4 +1019,92 @@ flock.test = flock.test || {};
deepEqual(normalizer.output, expected,
"When the 'max' input is changed to 0.5, the signal should be normalized to 0.5");
});
+
+ module("flock.ugen.math() tests");
+
+ var testMath = function (synthDef, expected, msg) {
+ synthDef.id = "math";
+ var synth = flock.synth(synthDef),
+ math = synth.ugens.named.math;
+ math.gen();
+ deepEqual(math.output, expected, msg);
+ };
+
+ test("flock.ugen.math()", function () {
+ testMath({
+ ugen: "flock.ugen.math",
+ inputs: {
+ source: 2,
+ add: 5
+ }
+ }, flock.test.constantBuffer(64, 7), "Value add");
+
+ testMath({
+ ugen: "flock.ugen.math",
+ inputs: {
+ source: 3,
+ sub: 2
+ }
+ }, flock.test.constantBuffer(64, 1), "Value subtract");
+
+ testMath({
+ ugen: "flock.ugen.math",
+ inputs: {
+ source: 3,
+ mul: 2
+ }
+ }, flock.test.constantBuffer(64, 6), "Value multiply");
+
+ testMath({
+ ugen: "flock.ugen.math",
+ inputs: {
+ source: 3,
+ div: 2
+ }
+ }, flock.test.constantBuffer(64, 1.5), "Value divide");
+
+ var incBuffer = flock.generate(64, function (i) {
+ return i + 1;
+ });
+
+ var expected = flock.generate(64, function (i) {
+ return i + 4;
+ });
+
+ var krArUGenDef = {
+ ugen: "flock.ugen.math",
+ inputs: {
+ source: {
+ ugen: "flock.test.mockUGen",
+ rate: "audio",
+ options: {
+ buffer: incBuffer
+ }
+ },
+ add: 3
+ }
+ };
+
+ testMath(krArUGenDef, expected, "Audio rate source, value add");
+
+ krArUGenDef.inputs.source.rate = "control";
+ testMath(krArUGenDef, flock.test.constantBuffer(64, 4), "Control rate source, value add");
+
+ krArUGenDef.inputs.add = {
+ ugen: "flock.test.mockUGen",
+ rate: "control",
+ options: {
+ buffer: incBuffer
+ }
+ };
+ testMath(krArUGenDef, flock.test.constantBuffer(64, 2), "Control rate source, control rate add.");
+
+ krArUGenDef.inputs.source.rate = "audio";
+ krArUGenDef.inputs.add.rate = "audio";
+ testMath(krArUGenDef, flock.generate(64, function (i) {
+ var j = i + 1;
+ return j + j;
+ }), "Audio rate source, audio rate add.");
+ });
+
}());
Oops, something went wrong.

0 comments on commit 0fc01a7

Please sign in to comment.