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

Commit 658c661

Browse files
committed
decorator
1 parent 02edad6 commit 658c661

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+644
-939
lines changed

src/AnalyserNode.js

Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import utils from "./utils";
21
import Configuration from "./utils/Configuration";
32
import AudioNode from "./AudioNode";
43
import * as props from "./decorators/props";
4+
import * as methods from "./decorators/methods";
5+
import * as validators from "./validators";
56

67
let configuration = Configuration.getInstance();
78

@@ -16,79 +17,45 @@ export default class AnalyserNode extends AudioNode {
1617
channelCountMode: "explicit",
1718
channelInterpretation: "speakers",
1819
});
19-
20-
this._.minDecibels = -100;
21-
this._.maxDecibels = 30;
22-
this._.smoothingTimeConstant = 0.8;
20+
this._.fftSize = 2048;
2321
this._.JSONKeys = AnalyserNode.$JSONKeys.slice();
2422
}
2523

26-
@props.enum([ 32, 64, 128, 256, 512, 1024, 2048 ], 2048)
24+
@props.enum([ 32, 64, 128, 256, 512, 1024, 2048 ])
2725
fftSize() {}
2826

2927
@props.readonly()
3028
frequencyBinCount() {
3129
return this.fftSize >> 1;
3230
}
3331

34-
@props.typed(-100, utils.isNumber, "number")
32+
@props.typed(validators.isNumber, -100)
3533
minDecibels() {}
3634

37-
@props.typed(30, utils.isNumber, "number")
35+
@props.typed(validators.isNumber, 30)
3836
maxDecibels() {}
3937

40-
@props.typed(0.8, utils.isNumber, "number")
38+
@props.typed(validators.isNumber, 0.8)
4139
smoothingTimeConstant() {}
4240

43-
getFloatFrequencyData(array) {
44-
this._.inspector.describe("getFloatFrequencyData", ($assert) => {
45-
$assert(utils.isInstanceOf(array, Float32Array), (fmt) => {
46-
throw new TypeError(fmt.plain `
47-
${fmt.form};
48-
${fmt.butGot(array, "array", "Float32Array")}
49-
`);
50-
});
51-
});
52-
}
41+
@methods.param("array", validators.isInstanceOf(Float32Array))
42+
getFloatFrequencyData() {}
5343

54-
getByteFrequencyData(array) {
55-
this._.inspector.describe("getByteFrequencyData", ($assert) => {
56-
$assert(utils.isInstanceOf(array, Uint8Array), (fmt) => {
57-
throw new TypeError(fmt.plain `
58-
${fmt.form};
59-
${fmt.butGot(array, "array", "Uint8Array")}
60-
`);
61-
});
62-
});
63-
}
44+
@methods.param("array", validators.isInstanceOf(Uint8Array))
45+
getByteFrequencyData() {}
6446

65-
getFloatTimeDomainData(array) {
66-
this._.inspector.describe("getFloatTimeDomainData", ($assert) => {
67-
$assert(configuration.getState("AnalyserNode#getFloatTimeDomainData") === "enabled", (fmt) => {
68-
throw new TypeError(fmt.plain `
69-
${fmt.form};
70-
not enabled
71-
`);
72-
});
73-
$assert(utils.isInstanceOf(array, Float32Array), (fmt) => {
74-
throw new TypeError(fmt.plain `
75-
${fmt.form};
76-
${fmt.butGot(array, "array", "Float32Array")}
77-
`);
78-
});
79-
});
80-
}
47+
@methods.param("array", validators.isInstanceOf(Float32Array))
48+
@methods.contract({
49+
precondition() {
50+
if (configuration.getState("AnalyserNode#getFloatTimeDomainData") !== "enabled") {
51+
throw new TypeError("not enabled");
52+
}
53+
}
54+
})
55+
getFloatTimeDomainData() {}
8156

82-
getByteTimeDomainData(array) {
83-
this._.inspector.describe("getByteTimeDomainData", ($assert) => {
84-
$assert(utils.isInstanceOf(array, Uint8Array), (fmt) => {
85-
throw new TypeError(fmt.plain `
86-
${fmt.form};
87-
${fmt.butGot(array, "array", "Uint8Array")}
88-
`);
89-
});
90-
});
91-
}
57+
@methods.param("array", validators.isInstanceOf(Uint8Array))
58+
getByteTimeDomainData() {}
9259
}
9360

9461
AnalyserNode.$JSONKeys = [

src/AudioBuffer.js

Lines changed: 52 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,29 @@
1-
import utils from "./utils";
21
import Configuration from "./utils/Configuration";
32
import Immigration from "./utils/Immigration";
4-
import Inspector from "./utils/Inspector";
53
import * as props from "./decorators/props";
4+
import * as methods from "./decorators/methods";
5+
import * as validators from "./validators";
66

77
let configuration = Configuration.getInstance();
88
let immigration = Immigration.getInstance();
99

10+
const CONSTRUCTOR = Symbol("constructor");
11+
1012
export default class AudioBuffer {
1113
constructor(admission, context, numberOfChannels, length, sampleRate) {
1214
immigration.check(admission, () => {
1315
throw new TypeError("Illegal constructor");
1416
});
15-
16-
Object.defineProperty(this, "_", {
17-
value: {
18-
inspector: new Inspector(this),
19-
},
20-
});
21-
22-
this._.inspector.describe("constructor", ($assert) => {
23-
$assert(utils.isPositiveInteger(numberOfChannels), (fmt) => {
24-
throw new TypeError(fmt.plain `
25-
${fmt.form};
26-
${fmt.butGot(numberOfChannels, "numberOfChannels", "positive integer")}
27-
`);
28-
});
29-
30-
$assert(utils.isPositiveInteger(length), (fmt) => {
31-
throw new TypeError(fmt.plain `
32-
${fmt.form};
33-
${fmt.butGot(length, "length", "positive integer")}
34-
`);
35-
});
36-
37-
$assert(utils.isPositiveInteger(sampleRate), (fmt) => {
38-
throw new TypeError(fmt.plain `
39-
${fmt.form};
40-
${fmt.butGot(sampleRate, "sampleRate", "positive integer")}
41-
`);
42-
});
43-
});
17+
Object.defineProperty(this, "_", { value: {} });
4418

4519
this._.context = context;
20+
this.__createAudioBuffer(numberOfChannels, length, sampleRate);
21+
}
22+
23+
@methods.param("numberOfChannels", validators.isPositiveInteger)
24+
@methods.param("length", validators.isPositiveInteger)
25+
@methods.param("sampleRate", validators.isPositiveInteger)
26+
__createAudioBuffer(numberOfChannels, length, sampleRate) {
4627
this._.numberOfChannels = numberOfChannels;
4728
this._.length = length;
4829
this._.sampleRate = sampleRate;
@@ -81,129 +62,57 @@ export default class AudioBuffer {
8162
return this._.context;
8263
}
8364

65+
@methods.param("channel", validators.isPositiveInteger)
66+
@methods.contract({
67+
precondition(channel) {
68+
if (this._.data.length <= channel) {
69+
throw new TypeError(`channel index (${channel}) exceeds number of channels (${this._.data.length})`);
70+
}
71+
},
72+
})
8473
getChannelData(channel) {
85-
this._.inspector.describe("getChannelData", ($assert) => {
86-
$assert(utils.isPositiveInteger(channel), (fmt) => {
87-
throw new TypeError(fmt.plain `
88-
${fmt.form};
89-
${fmt.butGot(channel, "channel", "positive integer")}
90-
`);
91-
});
92-
93-
$assert(channel < this._.data.length, (fmt) => {
94-
throw new TypeError(fmt.plain `
95-
${fmt.form};
96-
channel index (${channel}) exceeds number of channels (${this._.data.length})
97-
`);
98-
});
99-
});
100-
10174
return this._.data[channel];
10275
}
10376

104-
copyFromChannel(destination, channelNumber, startInChannel) {
105-
if (arguments.length < 3) {
106-
startInChannel = 0;
77+
@methods.param("destination", validators.isInstanceOf(Float32Array))
78+
@methods.param("channelNumber", validators.isPositiveInteger)
79+
@methods.param("[ startInChannel ]", validators.isPositiveInteger)
80+
@methods.contract({
81+
precondition(destination, channelNumber, startInChannel = 0) {
82+
if (channelNumber < 0 || this._.data.length <= channelNumber) {
83+
throw new TypeError(`The channelNumber provided (${channelNumber}) is outside the range [0, ${this._.data.length})`);
84+
}
85+
if (startInChannel < 0 || this._.length <= startInChannel) {
86+
throw new TypeError(`The startInChannel provided (${startInChannel}) is outside the range [0, ${this._.length}).`);
87+
}
88+
if (configuration.getState("AudioBuffer#copyFromChannel") !== "enabled") {
89+
throw new TypeError("not enabled");
90+
}
10791
}
108-
109-
this._.inspector.describe("copyFromChannel", ($assert) => {
110-
$assert(configuration.getState("AudioBuffer#copyFromChannel") === "enabled", (fmt) => {
111-
throw new TypeError(fmt.plain `
112-
${fmt.form};
113-
not enabled
114-
`);
115-
});
116-
117-
$assert(utils.isInstanceOf(destination, Float32Array), (fmt) => {
118-
throw new TypeError(fmt.plain `
119-
${fmt.form};
120-
${fmt.butGot(destination, "destination", "Float32Array")}
121-
`);
122-
});
123-
124-
$assert(utils.isPositiveInteger(channelNumber), (fmt) => {
125-
throw new TypeError(fmt.plain `
126-
${fmt.form};
127-
${fmt.butGot(channelNumber, "channelNumber", "positive integer")}
128-
`);
129-
});
130-
131-
$assert(utils.isPositiveInteger(startInChannel), (fmt) => {
132-
throw new TypeError(fmt.plain `
133-
${fmt.form};
134-
${fmt.butGot(channelNumber, "startInChannel", "positive integer")}
135-
`);
136-
});
137-
138-
$assert(0 <= channelNumber && channelNumber < this._.data.length, (fmt) => {
139-
throw new TypeError(fmt.plain `
140-
${fmt.form};
141-
The channelNumber provided (${channelNumber}) is outside the range [0, ${this._.data.length})
142-
`);
143-
});
144-
145-
$assert(0 <= startInChannel && startInChannel < this._.length, (fmt) => {
146-
throw new TypeError(fmt.plain `
147-
${fmt.form};
148-
The startInChannel provided (${startInChannel}) is outside the range [0, ${this._.length}).
149-
`);
150-
});
151-
});
152-
92+
})
93+
copyFromChannel(destination, channelNumber, startInChannel = 0) {
15394
let source = this._.data[channelNumber].subarray(startInChannel);
15495

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

158-
copyToChannel(source, channelNumber, startInChannel) {
159-
if (arguments.length < 3) {
160-
startInChannel = 0;
99+
@methods.param("source", validators.isInstanceOf(Float32Array))
100+
@methods.param("channelNumber", validators.isPositiveInteger)
101+
@methods.param("[ startInChannel ]", validators.isPositiveInteger)
102+
@methods.contract({
103+
precondition(source, channelNumber, startInChannel = 0) {
104+
if (channelNumber < 0 || this._.data.length <= channelNumber) {
105+
throw new TypeError(`The channelNumber provided (${channelNumber}) is outside the range [0, ${this._.data.length})`);
106+
}
107+
if (startInChannel < 0 || this._.length <= startInChannel) {
108+
throw new TypeError(`The startInChannel provided (${startInChannel}) is outside the range [0, ${this._.length}).`);
109+
}
110+
if (configuration.getState("AudioBuffer#copyToChannel") !== "enabled") {
111+
throw new TypeError("not enabled");
112+
}
161113
}
162-
163-
this._.inspector.describe("copyToChannel", ($assert) => {
164-
$assert(configuration.getState("AudioBuffer#copyToChannel") === "enabled", (fmt) => {
165-
throw new TypeError(fmt.plain `
166-
${fmt.form};
167-
not enabled
168-
`);
169-
});
170-
171-
$assert(utils.isInstanceOf(source, Float32Array), (fmt) => {
172-
throw new TypeError(fmt.plain `
173-
${fmt.form};
174-
${fmt.butGot(source, "destination", "Float32Array")}
175-
`);
176-
});
177-
178-
$assert(utils.isPositiveInteger(channelNumber), (fmt) => {
179-
throw new TypeError(fmt.plain `
180-
${fmt.form};
181-
${fmt.butGot(channelNumber, "channelNumber", "positive integer")}
182-
`);
183-
});
184-
185-
$assert(utils.isPositiveInteger(startInChannel), (fmt) => {
186-
throw new TypeError(fmt.plain `
187-
${fmt.form};
188-
${fmt.butGot(channelNumber, "startInChannel", "positive integer")}
189-
`);
190-
});
191-
192-
$assert(0 <= channelNumber && channelNumber < this._.data.length, (fmt) => {
193-
throw new TypeError(fmt.plain `
194-
${fmt.form};
195-
The channelNumber provided (${channelNumber}) is outside the range [0, ${this._.data.length})
196-
`);
197-
});
198-
199-
$assert(0 <= startInChannel && startInChannel < this._.length, (fmt) => {
200-
throw new TypeError(fmt.plain `
201-
${fmt.form};
202-
The startInChannel provided (${startInChannel}) is outside the range [0, ${this._.length}).
203-
`);
204-
});
205-
});
206-
114+
})
115+
copyToChannel(source, channelNumber, startInChannel = 0) {
207116
let clipped = source.subarray(0, Math.min(source.length, this._.length - startInChannel));
208117

209118
this._.data[channelNumber].set(clipped, startInChannel);

0 commit comments

Comments
 (0)