|
1 | | -import utils from "./utils"; |
2 | 1 | import Configuration from "./utils/Configuration"; |
3 | 2 | import Immigration from "./utils/Immigration"; |
4 | | -import Inspector from "./utils/Inspector"; |
5 | 3 | import * as props from "./decorators/props"; |
| 4 | +import * as methods from "./decorators/methods"; |
| 5 | +import * as validators from "./validators"; |
6 | 6 |
|
7 | 7 | let configuration = Configuration.getInstance(); |
8 | 8 | let immigration = Immigration.getInstance(); |
9 | 9 |
|
| 10 | +const CONSTRUCTOR = Symbol("constructor"); |
| 11 | + |
10 | 12 | export default class AudioBuffer { |
11 | 13 | constructor(admission, context, numberOfChannels, length, sampleRate) { |
12 | 14 | immigration.check(admission, () => { |
13 | 15 | throw new TypeError("Illegal constructor"); |
14 | 16 | }); |
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: {} }); |
44 | 18 |
|
45 | 19 | 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) { |
46 | 27 | this._.numberOfChannels = numberOfChannels; |
47 | 28 | this._.length = length; |
48 | 29 | this._.sampleRate = sampleRate; |
@@ -81,129 +62,57 @@ export default class AudioBuffer { |
81 | 62 | return this._.context; |
82 | 63 | } |
83 | 64 |
|
| 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 | + }) |
84 | 73 | 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 | | - |
101 | 74 | return this._.data[channel]; |
102 | 75 | } |
103 | 76 |
|
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 | + } |
107 | 91 | } |
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) { |
153 | 94 | let source = this._.data[channelNumber].subarray(startInChannel); |
154 | 95 |
|
155 | 96 | destination.set(source.subarray(0, Math.min(source.length, destination.length))); |
156 | 97 | } |
157 | 98 |
|
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 | + } |
161 | 113 | } |
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) { |
207 | 116 | let clipped = source.subarray(0, Math.min(source.length, this._.length - startInChannel)); |
208 | 117 |
|
209 | 118 | this._.data[channelNumber].set(clipped, startInChannel); |
|
0 commit comments