Skip to content

Commit

Permalink
lichen-community-systemsgh-97: Adds support for raw arrays and bufDes…
Browse files Browse the repository at this point in the history
…cs to all buffer ugens.
  • Loading branch information
colinbdclark committed Mar 11, 2015
1 parent 2946607 commit 05bf602
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 122 deletions.
75 changes: 45 additions & 30 deletions dist/flocking-all.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! Flocking 0.1.0 (March 9, 2015), Copyright 2015 Colin Clark | flockingjs.org */
/*! Flocking 0.1.0 (March 11, 2015), Copyright 2015 Colin Clark | flockingjs.org */

/*!
* jQuery JavaScript Library v2.1.3
Expand Down Expand Up @@ -21826,36 +21826,61 @@ var fluid = fluid || require("infusion"),
// and should be clearly named as such.
flock.bufferDesc = function (data) {
var fn = flock.platform.isWebAudio && data instanceof AudioBuffer ?
flock.bufferDesc.fromAudioBuffer : flock.bufferDesc.fromRawData;
flock.bufferDesc.fromAudioBuffer : flock.isIterable(data) ?
flock.bufferDesc.fromChannelArray : flock.bufferDesc.expand;

return fn(data);
};

flock.bufferDesc.fromRawData = function (data) {
data.container = data.container || {};
data.format = data.format || {};
flock.bufferDesc.inferFormat = function (bufDesc) {
var format = bufDesc.format,
data = bufDesc.data;

data.format.numChannels = data.format.numChannels || data.data.channels.length;
format.sampleRate = format.sampleRate || 44100;
format.numSampleFrames = format.numSampleFrames || data.channels[0].length;
format.duration = format.numSampleFrames / format.sampleRate;

if (data.data && data.data.channels) {
return bufDesc;
};

flock.bufferDesc.fromChannelArray = function (arr, sampleRate) {
var bufDesc = {
container: {},

format: {
numChannels: 1,
sampleRate: sampleRate
},

data: {
channels: [arr]
}
};

return flock.bufferDesc.inferFormat(bufDesc);
};

flock.bufferDesc.expand = function (bufDesc) {
bufDesc.container = bufDesc.container || {};
bufDesc.format = bufDesc.format || {};

bufDesc.format.numChannels = bufDesc.format.numChannels || bufDesc.data.channels.length;

if (bufDesc.data && bufDesc.data.channels) {
// Special case for an unwrapped single-channel array.
if (data.format.numChannels === 1 && data.data.channels.length !== 1) {
data.data.channels = [data.data.channels];
if (bufDesc.format.numChannels === 1 && bufDesc.data.channels.length !== 1) {
bufDesc.data.channels = [bufDesc.data.channels];
}

if (data.format.numChannels !== data.data.channels.length) {
if (bufDesc.format.numChannels !== bufDesc.data.channels.length) {
throw new Error("The specified number of channels does not match " +
"the actual channel data. " +
"numChannels was: " + data.format.numChannels +
" but the sample data contains " + data.data.channels.length + " channels.");
"numChannels was: " + bufDesc.format.numChannels +
" but the sample data contains " + bufDesc.data.channels.length + " channels.");
}
}

data.format.sampleRate = data.format.sampleRate || 44100;
data.format.numSampleFrames = data.format.numSampleFrames || data.data.channels[0].length;
data.format.duration = data.format.numSampleFrames / data.format.sampleRate;

return data;
return flock.bufferDesc.inferFormat(bufDesc);
};

flock.bufferDesc.fromAudioBuffer = function (audioBuffer) {
Expand Down Expand Up @@ -22394,19 +22419,9 @@ var fluid = fluid || require("infusion"),
};

flock.parse.expandBufferDef = function (bufDef) {
if (flock.isIterable(bufDef)) {
// If we get a direct array reference, wrap it up in a buffer description.
return flock.bufferDesc({
data: {
channels: bufDef // TODO: What about bare single-channel arrays?
}
});
}

// If we get a bare string, interpret it as an id reference.
return typeof (bufDef) !== "string" ? bufDef : {
id: bufDef
};
return typeof bufDef === "string" ? {id: bufDef} :
(flock.isIterable(bufDef) || bufDef.data || bufDef.format) ?
flock.bufferDesc(bufDef) : bufDef;
};

flock.parse.bufferForDef = function (bufDef, ugen, enviro) {
Expand Down
32 changes: 18 additions & 14 deletions dist/flocking-all.min.js

Large diffs are not rendered by default.

75 changes: 45 additions & 30 deletions dist/flocking-no-jquery.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! Flocking 0.1.0 (March 9, 2015), Copyright 2015 Colin Clark | flockingjs.org */
/*! Flocking 0.1.0 (March 11, 2015), Copyright 2015 Colin Clark | flockingjs.org */

(function (root, factory) {
if (typeof exports === "object") {
Expand Down Expand Up @@ -12640,36 +12640,61 @@ var fluid = fluid || require("infusion"),
// and should be clearly named as such.
flock.bufferDesc = function (data) {
var fn = flock.platform.isWebAudio && data instanceof AudioBuffer ?
flock.bufferDesc.fromAudioBuffer : flock.bufferDesc.fromRawData;
flock.bufferDesc.fromAudioBuffer : flock.isIterable(data) ?
flock.bufferDesc.fromChannelArray : flock.bufferDesc.expand;

return fn(data);
};

flock.bufferDesc.fromRawData = function (data) {
data.container = data.container || {};
data.format = data.format || {};
flock.bufferDesc.inferFormat = function (bufDesc) {
var format = bufDesc.format,
data = bufDesc.data;

data.format.numChannels = data.format.numChannels || data.data.channels.length;
format.sampleRate = format.sampleRate || 44100;
format.numSampleFrames = format.numSampleFrames || data.channels[0].length;
format.duration = format.numSampleFrames / format.sampleRate;

if (data.data && data.data.channels) {
return bufDesc;
};

flock.bufferDesc.fromChannelArray = function (arr, sampleRate) {
var bufDesc = {
container: {},

format: {
numChannels: 1,
sampleRate: sampleRate
},

data: {
channels: [arr]
}
};

return flock.bufferDesc.inferFormat(bufDesc);
};

flock.bufferDesc.expand = function (bufDesc) {
bufDesc.container = bufDesc.container || {};
bufDesc.format = bufDesc.format || {};

bufDesc.format.numChannels = bufDesc.format.numChannels || bufDesc.data.channels.length;

if (bufDesc.data && bufDesc.data.channels) {
// Special case for an unwrapped single-channel array.
if (data.format.numChannels === 1 && data.data.channels.length !== 1) {
data.data.channels = [data.data.channels];
if (bufDesc.format.numChannels === 1 && bufDesc.data.channels.length !== 1) {
bufDesc.data.channels = [bufDesc.data.channels];
}

if (data.format.numChannels !== data.data.channels.length) {
if (bufDesc.format.numChannels !== bufDesc.data.channels.length) {
throw new Error("The specified number of channels does not match " +
"the actual channel data. " +
"numChannels was: " + data.format.numChannels +
" but the sample data contains " + data.data.channels.length + " channels.");
"numChannels was: " + bufDesc.format.numChannels +
" but the sample data contains " + bufDesc.data.channels.length + " channels.");
}
}

data.format.sampleRate = data.format.sampleRate || 44100;
data.format.numSampleFrames = data.format.numSampleFrames || data.data.channels[0].length;
data.format.duration = data.format.numSampleFrames / data.format.sampleRate;

return data;
return flock.bufferDesc.inferFormat(bufDesc);
};

flock.bufferDesc.fromAudioBuffer = function (audioBuffer) {
Expand Down Expand Up @@ -13208,19 +13233,9 @@ var fluid = fluid || require("infusion"),
};

flock.parse.expandBufferDef = function (bufDef) {
if (flock.isIterable(bufDef)) {
// If we get a direct array reference, wrap it up in a buffer description.
return flock.bufferDesc({
data: {
channels: bufDef // TODO: What about bare single-channel arrays?
}
});
}

// If we get a bare string, interpret it as an id reference.
return typeof (bufDef) !== "string" ? bufDef : {
id: bufDef
};
return typeof bufDef === "string" ? {id: bufDef} :
(flock.isIterable(bufDef) || bufDef.data || bufDef.format) ?
flock.bufferDesc(bufDef) : bufDef;
};

flock.parse.bufferForDef = function (bufDef, ugen, enviro) {
Expand Down
19 changes: 10 additions & 9 deletions dist/flocking-no-jquery.min.js

Large diffs are not rendered by default.

57 changes: 41 additions & 16 deletions flocking/flocking-buffers.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,36 +121,61 @@ var fluid = fluid || require("infusion"),
// and should be clearly named as such.
flock.bufferDesc = function (data) {
var fn = flock.platform.isWebAudio && data instanceof AudioBuffer ?
flock.bufferDesc.fromAudioBuffer : flock.bufferDesc.fromRawData;
flock.bufferDesc.fromAudioBuffer : flock.isIterable(data) ?
flock.bufferDesc.fromChannelArray : flock.bufferDesc.expand;

return fn(data);
};

flock.bufferDesc.fromRawData = function (data) {
data.container = data.container || {};
data.format = data.format || {};
flock.bufferDesc.inferFormat = function (bufDesc) {
var format = bufDesc.format,
data = bufDesc.data;

data.format.numChannels = data.format.numChannels || data.data.channels.length;
format.sampleRate = format.sampleRate || 44100;
format.numSampleFrames = format.numSampleFrames || data.channels[0].length;
format.duration = format.numSampleFrames / format.sampleRate;

if (data.data && data.data.channels) {
return bufDesc;
};

flock.bufferDesc.fromChannelArray = function (arr, sampleRate) {
var bufDesc = {
container: {},

format: {
numChannels: 1,
sampleRate: sampleRate
},

data: {
channels: [arr]
}
};

return flock.bufferDesc.inferFormat(bufDesc);
};

flock.bufferDesc.expand = function (bufDesc) {
bufDesc.container = bufDesc.container || {};
bufDesc.format = bufDesc.format || {};

bufDesc.format.numChannels = bufDesc.format.numChannels || bufDesc.data.channels.length;

if (bufDesc.data && bufDesc.data.channels) {
// Special case for an unwrapped single-channel array.
if (data.format.numChannels === 1 && data.data.channels.length !== 1) {
data.data.channels = [data.data.channels];
if (bufDesc.format.numChannels === 1 && bufDesc.data.channels.length !== 1) {
bufDesc.data.channels = [bufDesc.data.channels];
}

if (data.format.numChannels !== data.data.channels.length) {
if (bufDesc.format.numChannels !== bufDesc.data.channels.length) {
throw new Error("The specified number of channels does not match " +
"the actual channel data. " +
"numChannels was: " + data.format.numChannels +
" but the sample data contains " + data.data.channels.length + " channels.");
"numChannels was: " + bufDesc.format.numChannels +
" but the sample data contains " + bufDesc.data.channels.length + " channels.");
}
}

data.format.sampleRate = data.format.sampleRate || 44100;
data.format.numSampleFrames = data.format.numSampleFrames || data.data.channels[0].length;
data.format.duration = data.format.numSampleFrames / data.format.sampleRate;

return data;
return flock.bufferDesc.inferFormat(bufDesc);
};

flock.bufferDesc.fromAudioBuffer = function (audioBuffer) {
Expand Down
16 changes: 3 additions & 13 deletions flocking/flocking-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,19 +287,9 @@ var fluid = fluid || require("infusion"),
};

flock.parse.expandBufferDef = function (bufDef) {
if (flock.isIterable(bufDef)) {
// If we get a direct array reference, wrap it up in a buffer description.
return flock.bufferDesc({
data: {
channels: bufDef // TODO: What about bare single-channel arrays?
}
});
}

// If we get a bare string, interpret it as an id reference.
return typeof (bufDef) !== "string" ? bufDef : {
id: bufDef
};
return typeof bufDef === "string" ? {id: bufDef} :
(flock.isIterable(bufDef) || bufDef.data || bufDef.format) ?
flock.bufferDesc(bufDef) : bufDef;
};

flock.parse.bufferForDef = function (bufDef, ugen, enviro) {
Expand Down
23 changes: 22 additions & 1 deletion tests/flocking/js/buffer-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,28 @@ var fluid = fluid || require("infusion"),
}
};

test("BufferDesc expansion: single channel raw sample array", function () {
test("BufferDesc expansion: raw sample array", function () {
var actual = flock.bufferDesc(unwrappedSampleData);
var expected = {
container: {},
format: {
numChannels: 1,
numSampleFrames: 5,
sampleRate: 44100,
duration: 5 / 44100
},
data: {
channels: [
unwrappedSampleData
]
}
};

deepEqual(actual, expected,
"A raw buffer of samples should be wrapped buffer desc.");
});

test("BufferDesc expansion: single channel sample array with numChannels specified", function () {
var bufferDesc = fluid.copy(testDesc);
var actual = flock.bufferDesc(bufferDesc);
deepEqual(actual.data.channels, [unwrappedSampleData],
Expand Down
Loading

0 comments on commit 05bf602

Please sign in to comment.