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

Method allocationSize

src/audio_data.ts:134–185  ·  view source on GitHub ↗

* Returns the number of bytes needed to hold a copy of the audio data.

(options: AudioDataCopyToOptions)

Source from the content-addressed store, hash-verified

132 * Returns the number of bytes needed to hold a copy of the audio data.
133 */
134 allocationSize(options: AudioDataCopyToOptions) {
135 this.ensureNotClosed();
136
137 if (!options || typeof options !== 'object') {
138 throw new TypeError('options must be an object.');
139 }
140
141 // Spec: Compute Copy Element Count algorithm starts here
142 const destFormat = options.format ?? this.format!;
143
144 // 1. Check Plane Index (Interleaved vs Planar checks)
145 const isPlanar = formatIsPlanar(destFormat);
146 if (!Number.isInteger(options.planeIndex) || options.planeIndex < 0) {
147 throw new TypeError('planeIndex must be a non-negative integer.');
148 }
149
150 if (isPlanar) {
151 if (options.planeIndex >= this.numberOfChannels) {
152 throw new RangeError('planeIndex out of range');
153 }
154 } else {
155 if (options.planeIndex !== 0) {
156 throw new RangeError('planeIndex out of range');
157 }
158 }
159
160 // 2. Check Frame Offset
161 const frameOffset = options.frameOffset ?? 0;
162 if (!Number.isInteger(frameOffset) || frameOffset < 0) {
163 throw new TypeError('frameOffset must be a non-negative integer.');
164 }
165 if (frameOffset >= this.numberOfFrames) {
166 throw new RangeError('frameOffset out of range');
167 }
168
169 // 3. Check Frame Count
170 const copyFrameCount
171 = options.frameCount !== undefined ? options.frameCount : (this.numberOfFrames - frameOffset);
172
173 if (options.frameCount !== undefined && (!Number.isInteger(options.frameCount) || options.frameCount < 0)) {
174 throw new TypeError('frameCount must be a non-negative integer.');
175 }
176
177 if (copyFrameCount > (this.numberOfFrames - frameOffset)) {
178 throw new RangeError('frameCount out of range');
179 }
180
181 const bytesPerSample = getBytesPerSample(destFormat);
182 const elementCount = isPlanar ? copyFrameCount : copyFrameCount * this.numberOfChannels;
183
184 return elementCount * bytesPerSample;
185 }
186
187 /**
188 * Copies the audio data to a destination buffer.

Callers 4

encodeInternalMethod · 0.45
encodeInternalMethod · 0.45

Calls 3

ensureNotClosedMethod · 0.95
formatIsPlanarFunction · 0.85
getBytesPerSampleFunction · 0.85

Tested by

no test coverage detected