Permalink
Browse files

Convert a load more AudioletNodes to single-sample ticking.

  • Loading branch information...
1 parent f716b8a commit 2fa6882719d5b5c5ae2a84621552721a736947bf @oampo committed Feb 12, 2012
View
@@ -58,9 +58,6 @@ BiquadFilter.prototype.calculateCoefficients = function(frequency) {
/**
* Process a block of samples
- *
- * @param {AudioletBuffer[]} inputBuffers Samples received from the inputs.
- * @param {AudioletBuffer[]} outputBuffers Samples to be sent to the outputs.
*/
BiquadFilter.prototype.generate = function() {
var input = this.inputs[0];
View
@@ -32,11 +32,8 @@ extend(BitCrusher, AudioletNode);
/**
* Process a block of samples
- *
- * @param {AudioletBuffer[]} inputBuffers Samples received from the inputs.
- * @param {AudioletBuffer[]} outputBuffers Samples to be sent to the outputs.
*/
-BitCrusher.prototype.generate = function(inputBuffers, outputBuffers) {
+BitCrusher.prototype.generate = function() {
var input = this.inputs[0];
var maxValue = Math.pow(2, this.bits.getValue()) - 1;
View
@@ -33,101 +33,42 @@ var CombFilter = function(audiolet, maximumDelayTime, delayTime, decayTime) {
this.maximumDelayTime = maximumDelayTime;
this.delayTime = new AudioletParameter(this, 1, delayTime || 1);
this.decayTime = new AudioletParameter(this, 2, decayTime);
- var bufferSize = maximumDelayTime * this.audiolet.device.sampleRate;
this.buffers = [];
this.readWriteIndex = 0;
};
extend(CombFilter, AudioletNode);
/**
* Process a block of samples
- *
- * @param {AudioletBuffer[]} inputBuffers Samples received from the inputs.
- * @param {AudioletBuffer[]} outputBuffers Samples to be sent to the outputs.
*/
-CombFilter.prototype.generate = function(inputBuffers, outputBuffers) {
- var inputBuffer = inputBuffers[0];
- var outputBuffer = outputBuffers[0];
-
- if (inputBuffer.isEmpty) {
- outputBuffer.isEmpty = true;
- return;
- }
+CombFilter.prototype.generate = function() {
+ var input = this.inputs[0];
+ var output = this.outputs[0];
- // Local processing variables
- var maximumDelayTime = this.maximumDelayTime;
var sampleRate = this.audiolet.device.sampleRate;
- var delayTimeParameter = this.delayTime;
- var delayTime, delayTimeChannel;
- if (delayTimeParameter.isStatic()) {
- delayTime = Math.floor(delayTimeParameter.getValue() * sampleRate);
- }
- else {
- delayTimeChannel = delayTimeParameter.getChannel();
- }
-
- var decayTimeParameter = this.decayTime;
- var decayTime, decayTimeChannel;
- if (decayTimeParameter.isStatic()) {
- decayTime = Math.floor(decayTimeParameter.getValue() * sampleRate);
- }
- else {
- decayTimeChannel = decayTimeParameter.getChannel();
- }
-
-
- var feedback;
- if (delayTimeParameter.isStatic() && decayTimeParameter.isStatic()) {
- feedback = Math.exp(-3 * delayTime / decayTime);
- }
-
+ var delayTime = this.delayTime.getValue() * sampleRate;
+ var decayTime = this.decayTime.getValue() * sampleRate;
+ var feedback = Math.exp(-3 * delayTime / decayTime);
-
- var buffers = this.buffers;
- var readWriteIndex = this.readWriteIndex;
-
- var inputChannels = inputBuffer.channels;
- var outputChannels = outputBuffer.channels;
- var numberOfChannels = inputBuffer.numberOfChannels;
- var numberOfBuffers = buffers.length;
- for (var i = numberOfBuffers; i < numberOfChannels; i++) {
- // Create buffer for channel if it doesn't already exist
- var bufferSize = maximumDelayTime * sampleRate;
- buffers.push(new Float32Array(bufferSize));
- }
-
-
- var bufferLength = inputBuffer.length;
- for (var i = 0; i < bufferLength; i++) {
- if (delayTimeChannel) {
- delayTime = Math.floor(delayTimeChannel[i] * sampleRate);
- }
-
- if (decayTimeChannel) {
- decayTime = Math.floor(decayTimeChannel[i] * sampleRate);
- }
-
- if (delayTimeChannel || decayTimeChannel) {
- feedback = Math.exp(-3 * delayTime / decayTime);
+ var numberOfChannels = input.samples.length;
+ for (var i = 0; i < numberOfChannels; i++) {
+ if (i >= this.buffers.length) {
+ // Create buffer for channel if it doesn't already exist
+ var bufferSize = this.maximumDelayTime * sampleRate;
+ this.buffers.push(new Float32Array(bufferSize));
}
- for (var j = 0; j < numberOfChannels; j++) {
- var inputChannel = inputChannels[j];
- var outputChannel = outputChannels[j];
- var buffer = buffers[j];
- var output = buffer[readWriteIndex];
- outputChannel[i] = output;
- buffer[readWriteIndex] = inputChannel[i] +
- feedback * output;
- }
+ var buffer = this.buffers[i];
+ var outputValue = buffer[this.readWriteIndex];
+ output.samples[i] = outputValue;
+ buffer[this.readWriteIndex] = input.samples[i] + feedback * outputValue;
+ }
- readWriteIndex += 1;
- if (readWriteIndex >= delayTime) {
- readWriteIndex = 0;
- }
+ this.readWriteIndex += 1;
+ if (this.readWriteIndex >= delayTime) {
+ this.readWriteIndex = 0;
}
- this.readWriteIndex = readWriteIndex;
};
/**
View
@@ -35,69 +35,24 @@ extend(CrossFade, AudioletNode);
/**
* Process a block of samples
- *
- * @param {AudioletBuffer[]} inputBuffers Samples received from the inputs.
- * @param {AudioletBuffer[]} outputBuffers Samples to be sent to the outputs.
*/
-CrossFade.prototype.generate = function(inputBuffers, outputBuffers) {
- var inputBufferA = inputBuffers[0];
- var inputBufferB = inputBuffers[1];
- var outputBuffer = outputBuffers[0];
-
- var inputChannelsA = inputBufferA.channels;
- var inputChannelsB = inputBufferB.channels;
- var outputChannels = outputBuffer.channels;
-
- if (inputBufferA.isEmpty && inputBufferB.isEmpty) {
- outputBuffer.isEmpty = true;
- return;
- }
+CrossFade.prototype.generate = function() {
+ var inputA = this.inputs[0];
+ var inputB = this.inputs[1];
+ var output = this.outputs[0];
// Local processing variables
- var positionParameter = this.position;
- var position, positionChannel;
- if (positionParameter.isStatic()) {
- position = positionParameter.getValue();
- }
- else {
- positionChannel = positionParameter.getChannel();
- }
-
- var bufferLength = outputBuffer.length;
- for (var i = 0; i < bufferLength; i++) {
- if (positionChannel) {
- position = positionChannel[i];
- }
-
- var tableLength = Sine.TABLE.length / 4;
- var scaledPosition = Math.floor(position * tableLength);
- // TODO: Use sine/cos tables?
- var gainA = Sine.TABLE[scaledPosition + tableLength];
- var gainB = Sine.TABLE[scaledPosition];
-
- var numberOfChannels = inputBufferA.numberOfChannels;
- for (var j = 0; j < numberOfChannels; j++) {
- var inputChannelA = inputChannelsA[j];
- var inputChannelB = inputChannelsB[j];
- var outputChannel = outputChannels[j];
+ var position = this.position.getValue();
- var valueA, valueB;
- if (!inputBufferA.isEmpty) {
- valueA = inputChannelA[i];
- }
- else {
- valueA = 0;
- }
+ var scaledPosition = position * Math.PI / 2;
+ var gainA = Math.cos(scaledPosition);
+ var gainB = Math.sin(scaledPosition);
- if (!inputBufferB.isEmpty) {
- valueB = inputChannelB[i];
- }
- else {
- valueB = 0;
- }
- outputChannel[i] = valueA * gainA +
- valueB * gainB;
- }
+ var numberOfChannels = output.samples.length;
+ for (var i = 0; i < numberOfChannels; i++) {
+ var valueA = inputA.samples[i] || 0;
+ var valueB = inputB.samples[i] || 0;
+ output.samples[i] = valueA * gainA + valueB * gainB;
}
};
View
@@ -40,11 +40,8 @@ extend(DCFilter, AudioletNode);
/**
* Process a block of samples
- *
- * @param {AudioletBuffer[]} inputBuffers Samples received from the inputs.
- * @param {AudioletBuffer[]} outputBuffers Samples to be sent to the outputs.
*/
-DCFilter.prototype.generate = function(inputBuffers, outputBuffers) {
+DCFilter.prototype.generate = function() {
var coefficient = this.coefficient.getValue();
var input = this.inputs[0];
var numberOfChannels = input.samples.length;
View
@@ -41,116 +41,52 @@ var DampedCombFilter = function(audiolet, maximumDelayTime, delayTime,
var bufferSize = maximumDelayTime * this.audiolet.device.sampleRate;
this.buffers = [];
this.readWriteIndex = 0;
- this.filterStore = 0;
+ this.filterStores = [];
};
extend(DampedCombFilter, AudioletNode);
/**
* Process a block of samples
- *
- * @param {AudioletBuffer[]} inputBuffers Samples received from the inputs.
- * @param {AudioletBuffer[]} outputBuffers Samples to be sent to the outputs.
*/
-DampedCombFilter.prototype.generate = function(inputBuffers, outputBuffers) {
- var inputBuffer = inputBuffers[0];
- var outputBuffer = outputBuffers[0];
-
- if (inputBuffer.isEmpty) {
- outputBuffer.isEmpty = true;
- return;
- }
+DampedCombFilter.prototype.generate = function() {
+ var input = this.inputs[0];
+ var output = this.outputs[0];
- // Local processing variables
- var maximumDelayTime = this.maximumDelayTime;
var sampleRate = this.audiolet.device.sampleRate;
- var delayTimeParameter = this.delayTime;
- var delayTime, delayTimeChannel;
- if (delayTimeParameter.isStatic()) {
- delayTime = Math.floor(delayTimeParameter.getValue() * sampleRate);
- }
- else {
- delayTimeChannel = delayTimeParameter.getChannel();
- }
+ var delayTime = this.delayTime.getValue() * sampleRate;
+ var decayTime = this.decayTime.getValue() * sampleRate;
+ var damping = this.damping.getValue();
+ var feedback = Math.exp(-3 * delayTime / decayTime);
- var decayTimeParameter = this.decayTime;
- var decayTime, decayTimeChannel;
- if (decayTimeParameter.isStatic()) {
- decayTime = Math.floor(decayTimeParameter.getValue() * sampleRate);
- }
- else {
- decayTimeChannel = decayTimeParameter.getChannel();
- }
-
- var dampingParameter = this.damping;
- var damping, dampingChannel;
- if (dampingParameter.isStatic()) {
- damping = dampingParameter.getValue();
- }
- else {
- dampingChannel = dampingParameter.getChannel();
- }
-
-
- var feedback;
- if (delayTimeParameter.isStatic() && decayTimeParameter.isStatic()) {
- feedback = Math.exp(-3 * delayTime / decayTime);
- }
-
-
-
- var buffers = this.buffers;
- var readWriteIndex = this.readWriteIndex;
- var filterStore = this.filterStore;
-
- var inputChannels = inputBuffer.channels;
- var outputChannels = outputBuffer.channels;
- var numberOfChannels = inputBuffer.numberOfChannels;
- var numberOfBuffers = buffers.length;
- for (var i = numberOfBuffers; i < numberOfChannels; i++) {
- // Create buffer for channel if it doesn't already exist
- var bufferSize = maximumDelayTime * sampleRate;
- buffers.push(new Float32Array(bufferSize));
- }
-
-
- var bufferLength = inputBuffer.length;
- for (var i = 0; i < bufferLength; i++) {
- if (delayTimeChannel) {
- delayTime = Math.floor(delayTimeChannel[i] * sampleRate);
+ var numberOfChannels = input.samples.length;
+ for (var i = 0; i < numberOfChannels; i++) {
+ if (i >= this.buffers.length) {
+ var bufferSize = this.maximumDelayTime * sampleRate;
+ this.buffers.push(new Float32Array(bufferSize));
}
- if (decayTimeChannel) {
- decayTime = Math.floor(decayTimeChannel[i] * sampleRate);
+ if (i >= this.filterStores.length) {
+ this.filterStores.push(0);
}
- if (dampingChannel) {
- damping = dampingChannel[i];
- }
+ var buffer = this.buffers[i];
+ var filterStore = this.filterStores[i];
- if (delayTimeChannel || decayTimeChannel) {
- feedback = Math.exp(-3 * delayTime / decayTime);
- }
+ var outputValue = buffer[this.readWriteIndex];
+ filterStore = (outputValue * (1 - damping)) +
+ (filterStore * damping);
+ output.samples[i] = outputValue;
+ buffer[this.readWriteIndex] = input.samples[i] +
+ feedback * filterStore;
- for (var j = 0; j < numberOfChannels; j++) {
- var inputChannel = inputChannels[j];
- var outputChannel = outputChannels[j];
- var buffer = buffers[j];
- var output = buffer[readWriteIndex];
- filterStore = (output * (1 - damping)) +
- (filterStore * damping);
- outputChannel[i] = output;
- buffer[readWriteIndex] = inputChannel[i] +
- feedback * filterStore;
- }
+ this.filterStores[i] = filterStore;
+ }
- readWriteIndex += 1;
- if (readWriteIndex >= delayTime) {
- readWriteIndex = 0;
- }
+ this.readWriteIndex += 1;
+ if (this.readWriteIndex >= delayTime) {
+ this.readWriteIndex = 0;
}
- this.readWriteIndex = readWriteIndex;
- this.filterStore = filterStore;
};
/**
Oops, something went wrong.

0 comments on commit 2fa6882

Please sign in to comment.