Skip to content
This repository has been archived by the owner on Jun 9, 2019. It is now read-only.

Commit

Permalink
decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
mohayonao committed Dec 11, 2015
1 parent 02edad6 commit 658c661
Show file tree
Hide file tree
Showing 51 changed files with 644 additions and 939 deletions.
77 changes: 22 additions & 55 deletions src/AnalyserNode.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import utils from "./utils";
import Configuration from "./utils/Configuration";
import AudioNode from "./AudioNode";
import * as props from "./decorators/props";
import * as methods from "./decorators/methods";
import * as validators from "./validators";

let configuration = Configuration.getInstance();

Expand All @@ -16,79 +17,45 @@ export default class AnalyserNode extends AudioNode {
channelCountMode: "explicit",
channelInterpretation: "speakers",
});

this._.minDecibels = -100;
this._.maxDecibels = 30;
this._.smoothingTimeConstant = 0.8;
this._.fftSize = 2048;
this._.JSONKeys = AnalyserNode.$JSONKeys.slice();
}

@props.enum([ 32, 64, 128, 256, 512, 1024, 2048 ], 2048)
@props.enum([ 32, 64, 128, 256, 512, 1024, 2048 ])
fftSize() {}

@props.readonly()
frequencyBinCount() {
return this.fftSize >> 1;
}

@props.typed(-100, utils.isNumber, "number")
@props.typed(validators.isNumber, -100)
minDecibels() {}

@props.typed(30, utils.isNumber, "number")
@props.typed(validators.isNumber, 30)
maxDecibels() {}

@props.typed(0.8, utils.isNumber, "number")
@props.typed(validators.isNumber, 0.8)
smoothingTimeConstant() {}

getFloatFrequencyData(array) {
this._.inspector.describe("getFloatFrequencyData", ($assert) => {
$assert(utils.isInstanceOf(array, Float32Array), (fmt) => {
throw new TypeError(fmt.plain `
${fmt.form};
${fmt.butGot(array, "array", "Float32Array")}
`);
});
});
}
@methods.param("array", validators.isInstanceOf(Float32Array))
getFloatFrequencyData() {}

getByteFrequencyData(array) {
this._.inspector.describe("getByteFrequencyData", ($assert) => {
$assert(utils.isInstanceOf(array, Uint8Array), (fmt) => {
throw new TypeError(fmt.plain `
${fmt.form};
${fmt.butGot(array, "array", "Uint8Array")}
`);
});
});
}
@methods.param("array", validators.isInstanceOf(Uint8Array))
getByteFrequencyData() {}

getFloatTimeDomainData(array) {
this._.inspector.describe("getFloatTimeDomainData", ($assert) => {
$assert(configuration.getState("AnalyserNode#getFloatTimeDomainData") === "enabled", (fmt) => {
throw new TypeError(fmt.plain `
${fmt.form};
not enabled
`);
});
$assert(utils.isInstanceOf(array, Float32Array), (fmt) => {
throw new TypeError(fmt.plain `
${fmt.form};
${fmt.butGot(array, "array", "Float32Array")}
`);
});
});
}
@methods.param("array", validators.isInstanceOf(Float32Array))
@methods.contract({
precondition() {
if (configuration.getState("AnalyserNode#getFloatTimeDomainData") !== "enabled") {
throw new TypeError("not enabled");
}
}
})
getFloatTimeDomainData() {}

getByteTimeDomainData(array) {
this._.inspector.describe("getByteTimeDomainData", ($assert) => {
$assert(utils.isInstanceOf(array, Uint8Array), (fmt) => {
throw new TypeError(fmt.plain `
${fmt.form};
${fmt.butGot(array, "array", "Uint8Array")}
`);
});
});
}
@methods.param("array", validators.isInstanceOf(Uint8Array))
getByteTimeDomainData() {}
}

AnalyserNode.$JSONKeys = [
Expand Down
195 changes: 52 additions & 143 deletions src/AudioBuffer.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,29 @@
import utils from "./utils";
import Configuration from "./utils/Configuration";
import Immigration from "./utils/Immigration";
import Inspector from "./utils/Inspector";
import * as props from "./decorators/props";
import * as methods from "./decorators/methods";
import * as validators from "./validators";

let configuration = Configuration.getInstance();
let immigration = Immigration.getInstance();

const CONSTRUCTOR = Symbol("constructor");

export default class AudioBuffer {
constructor(admission, context, numberOfChannels, length, sampleRate) {
immigration.check(admission, () => {
throw new TypeError("Illegal constructor");
});

Object.defineProperty(this, "_", {
value: {
inspector: new Inspector(this),
},
});

this._.inspector.describe("constructor", ($assert) => {
$assert(utils.isPositiveInteger(numberOfChannels), (fmt) => {
throw new TypeError(fmt.plain `
${fmt.form};
${fmt.butGot(numberOfChannels, "numberOfChannels", "positive integer")}
`);
});

$assert(utils.isPositiveInteger(length), (fmt) => {
throw new TypeError(fmt.plain `
${fmt.form};
${fmt.butGot(length, "length", "positive integer")}
`);
});

$assert(utils.isPositiveInteger(sampleRate), (fmt) => {
throw new TypeError(fmt.plain `
${fmt.form};
${fmt.butGot(sampleRate, "sampleRate", "positive integer")}
`);
});
});
Object.defineProperty(this, "_", { value: {} });

this._.context = context;
this.__createAudioBuffer(numberOfChannels, length, sampleRate);
}

@methods.param("numberOfChannels", validators.isPositiveInteger)
@methods.param("length", validators.isPositiveInteger)
@methods.param("sampleRate", validators.isPositiveInteger)
__createAudioBuffer(numberOfChannels, length, sampleRate) {
this._.numberOfChannels = numberOfChannels;
this._.length = length;
this._.sampleRate = sampleRate;
Expand Down Expand Up @@ -81,129 +62,57 @@ export default class AudioBuffer {
return this._.context;
}

@methods.param("channel", validators.isPositiveInteger)
@methods.contract({
precondition(channel) {
if (this._.data.length <= channel) {
throw new TypeError(`channel index (${channel}) exceeds number of channels (${this._.data.length})`);
}
},
})
getChannelData(channel) {
this._.inspector.describe("getChannelData", ($assert) => {
$assert(utils.isPositiveInteger(channel), (fmt) => {
throw new TypeError(fmt.plain `
${fmt.form};
${fmt.butGot(channel, "channel", "positive integer")}
`);
});

$assert(channel < this._.data.length, (fmt) => {
throw new TypeError(fmt.plain `
${fmt.form};
channel index (${channel}) exceeds number of channels (${this._.data.length})
`);
});
});

return this._.data[channel];
}

copyFromChannel(destination, channelNumber, startInChannel) {
if (arguments.length < 3) {
startInChannel = 0;
@methods.param("destination", validators.isInstanceOf(Float32Array))
@methods.param("channelNumber", validators.isPositiveInteger)
@methods.param("[ startInChannel ]", validators.isPositiveInteger)
@methods.contract({
precondition(destination, channelNumber, startInChannel = 0) {
if (channelNumber < 0 || this._.data.length <= channelNumber) {
throw new TypeError(`The channelNumber provided (${channelNumber}) is outside the range [0, ${this._.data.length})`);
}
if (startInChannel < 0 || this._.length <= startInChannel) {
throw new TypeError(`The startInChannel provided (${startInChannel}) is outside the range [0, ${this._.length}).`);
}
if (configuration.getState("AudioBuffer#copyFromChannel") !== "enabled") {
throw new TypeError("not enabled");
}
}

this._.inspector.describe("copyFromChannel", ($assert) => {
$assert(configuration.getState("AudioBuffer#copyFromChannel") === "enabled", (fmt) => {
throw new TypeError(fmt.plain `
${fmt.form};
not enabled
`);
});

$assert(utils.isInstanceOf(destination, Float32Array), (fmt) => {
throw new TypeError(fmt.plain `
${fmt.form};
${fmt.butGot(destination, "destination", "Float32Array")}
`);
});

$assert(utils.isPositiveInteger(channelNumber), (fmt) => {
throw new TypeError(fmt.plain `
${fmt.form};
${fmt.butGot(channelNumber, "channelNumber", "positive integer")}
`);
});

$assert(utils.isPositiveInteger(startInChannel), (fmt) => {
throw new TypeError(fmt.plain `
${fmt.form};
${fmt.butGot(channelNumber, "startInChannel", "positive integer")}
`);
});

$assert(0 <= channelNumber && channelNumber < this._.data.length, (fmt) => {
throw new TypeError(fmt.plain `
${fmt.form};
The channelNumber provided (${channelNumber}) is outside the range [0, ${this._.data.length})
`);
});

$assert(0 <= startInChannel && startInChannel < this._.length, (fmt) => {
throw new TypeError(fmt.plain `
${fmt.form};
The startInChannel provided (${startInChannel}) is outside the range [0, ${this._.length}).
`);
});
});

})
copyFromChannel(destination, channelNumber, startInChannel = 0) {
let source = this._.data[channelNumber].subarray(startInChannel);

destination.set(source.subarray(0, Math.min(source.length, destination.length)));
}

copyToChannel(source, channelNumber, startInChannel) {
if (arguments.length < 3) {
startInChannel = 0;
@methods.param("source", validators.isInstanceOf(Float32Array))
@methods.param("channelNumber", validators.isPositiveInteger)
@methods.param("[ startInChannel ]", validators.isPositiveInteger)
@methods.contract({
precondition(source, channelNumber, startInChannel = 0) {
if (channelNumber < 0 || this._.data.length <= channelNumber) {
throw new TypeError(`The channelNumber provided (${channelNumber}) is outside the range [0, ${this._.data.length})`);
}
if (startInChannel < 0 || this._.length <= startInChannel) {
throw new TypeError(`The startInChannel provided (${startInChannel}) is outside the range [0, ${this._.length}).`);
}
if (configuration.getState("AudioBuffer#copyToChannel") !== "enabled") {
throw new TypeError("not enabled");
}
}

this._.inspector.describe("copyToChannel", ($assert) => {
$assert(configuration.getState("AudioBuffer#copyToChannel") === "enabled", (fmt) => {
throw new TypeError(fmt.plain `
${fmt.form};
not enabled
`);
});

$assert(utils.isInstanceOf(source, Float32Array), (fmt) => {
throw new TypeError(fmt.plain `
${fmt.form};
${fmt.butGot(source, "destination", "Float32Array")}
`);
});

$assert(utils.isPositiveInteger(channelNumber), (fmt) => {
throw new TypeError(fmt.plain `
${fmt.form};
${fmt.butGot(channelNumber, "channelNumber", "positive integer")}
`);
});

$assert(utils.isPositiveInteger(startInChannel), (fmt) => {
throw new TypeError(fmt.plain `
${fmt.form};
${fmt.butGot(channelNumber, "startInChannel", "positive integer")}
`);
});

$assert(0 <= channelNumber && channelNumber < this._.data.length, (fmt) => {
throw new TypeError(fmt.plain `
${fmt.form};
The channelNumber provided (${channelNumber}) is outside the range [0, ${this._.data.length})
`);
});

$assert(0 <= startInChannel && startInChannel < this._.length, (fmt) => {
throw new TypeError(fmt.plain `
${fmt.form};
The startInChannel provided (${startInChannel}) is outside the range [0, ${this._.length}).
`);
});
});

})
copyToChannel(source, channelNumber, startInChannel = 0) {
let clipped = source.subarray(0, Math.min(source.length, this._.length - startInChannel));

this._.data[channelNumber].set(clipped, startInChannel);
Expand Down
Loading

0 comments on commit 658c661

Please sign in to comment.