MCPcopy Create free account
hub / github.com/Vanilagy/webcodecs-polyfill / copyTo

Method copyTo

src/audio_data.ts:190–294  ·  view source on GitHub ↗

* Copies the audio data to a destination buffer.

(destination: AllowSharedBufferSource, options: AudioDataCopyToOptions)

Source from the content-addressed store, hash-verified

188 * Copies the audio data to a destination buffer.
189 */
190 copyTo(destination: AllowSharedBufferSource, options: AudioDataCopyToOptions) {
191 this.ensureNotClosed();
192
193 if (!options || typeof options !== 'object') {
194 throw new TypeError('options must be an object.');
195 }
196
197 // Spec: Compute Copy Element Count algorithm
198 const destFormat = options.format ?? this.format!;
199
200 // 1. Check Plane Index
201 const destIsPlanar = formatIsPlanar(destFormat);
202 if (!Number.isInteger(options.planeIndex) || options.planeIndex < 0) {
203 throw new TypeError('planeIndex must be a non-negative integer.');
204 }
205
206 if (destIsPlanar) {
207 if (options.planeIndex >= this.numberOfChannels) {
208 throw new RangeError('planeIndex out of range');
209 }
210 } else {
211 if (options.planeIndex !== 0) {
212 throw new RangeError('planeIndex out of range');
213 }
214 }
215
216 // 2. Check Frame Offset
217 const frameOffset = options.frameOffset ?? 0;
218 if (!Number.isInteger(frameOffset) || frameOffset < 0) {
219 throw new TypeError('frameOffset must be a non-negative integer.');
220 }
221 if (frameOffset >= this.numberOfFrames) {
222 throw new RangeError('frameOffset out of range');
223 }
224
225 // 3. Check Frame Count
226 const copyFrameCount
227 = options.frameCount !== undefined ? options.frameCount : (this.numberOfFrames - frameOffset);
228
229 if (options.frameCount !== undefined && (!Number.isInteger(options.frameCount) || options.frameCount < 0)) {
230 throw new TypeError('frameCount must be a non-negative integer.');
231 }
232
233 if (copyFrameCount > (this.numberOfFrames - frameOffset)) {
234 throw new RangeError('frameCount out of range');
235 }
236
237 // Spec: Check destination size
238 const destBytesPerSample = getBytesPerSample(destFormat);
239 const destElementCount = destIsPlanar ? copyFrameCount : copyFrameCount * this.numberOfChannels;
240 const requiredSize = destElementCount * destBytesPerSample;
241
242 if (destination.byteLength < requiredSize) {
243 throw new RangeError('Destination buffer is too small');
244 }
245
246 const destView = toDataView(destination);
247 const writeFn = getWriteFunction(destFormat);

Callers 8

decodeInternalMethod · 0.45
encodeInternalMethod · 0.45
decodeInternalMethod · 0.45
encodeInternalMethod · 0.45

Calls 7

ensureNotClosedMethod · 0.95
formatIsPlanarFunction · 0.85
getBytesPerSampleFunction · 0.85
toDataViewFunction · 0.85
getWriteFunctionFunction · 0.85
assertFunction · 0.85
getReadFunctionFunction · 0.85

Tested by

no test coverage detected