| 37 | * Corresponds to the WebCodecs AudioData API. |
| 38 | */ |
| 39 | export class AudioDataPolyfill { |
| 40 | /** @internal */ |
| 41 | _data: Uint8Array<ArrayBuffer> | Frame | null = null; |
| 42 | private closed: boolean = false; |
| 43 | |
| 44 | /** |
| 45 | * The sample format, or null if closed. |
| 46 | */ |
| 47 | readonly format: AudioSampleFormat | null; |
| 48 | /** |
| 49 | * The number of samples per second. |
| 50 | */ |
| 51 | readonly sampleRate: number; |
| 52 | /** |
| 53 | * The number of audio frames (samples per channel). |
| 54 | */ |
| 55 | readonly numberOfFrames: number; |
| 56 | /** |
| 57 | * The number of audio channels. |
| 58 | */ |
| 59 | readonly numberOfChannels: number; |
| 60 | /** |
| 61 | * The timestamp in integer microseconds. |
| 62 | */ |
| 63 | readonly timestamp: number; |
| 64 | /** |
| 65 | * The duration in integer microseconds. |
| 66 | */ |
| 67 | readonly duration: number; |
| 68 | |
| 69 | constructor(init: Frame); |
| 70 | constructor(init: AudioDataInit); |
| 71 | constructor(init: AudioDataInit | Frame) { |
| 72 | if (init instanceof Frame) { |
| 73 | const format = toAudioSampleFormat(init.format as AVSampleFormat); |
| 74 | if (!format) { |
| 75 | throw new TypeError('Unsupported audio sample format: ' + init.format); |
| 76 | } |
| 77 | |
| 78 | this.format = format; |
| 79 | this.sampleRate = init.sampleRate; |
| 80 | this.numberOfFrames = init.nbSamples; |
| 81 | this.numberOfChannels = init.channels; |
| 82 | this.timestamp = Number(init.pts); |
| 83 | this.duration = Number(init.duration); |
| 84 | |
| 85 | this._data = init; |
| 86 | } else { |
| 87 | if (!Number.isFinite(init.sampleRate) || init.sampleRate <= 0) { |
| 88 | throw new TypeError('Invalid AudioDataInit: sampleRate must be > 0.'); |
| 89 | } |
| 90 | if (!Number.isInteger(init.numberOfChannels) || init.numberOfChannels <= 0) { |
| 91 | throw new TypeError('Invalid AudioDataInit: numberOfChannels must be an integer > 0.'); |
| 92 | } |
| 93 | // Spec: numberOfFrames is required and must be > 0 |
| 94 | if (!Number.isInteger(init.numberOfFrames) || init.numberOfFrames <= 0) { |
| 95 | throw new TypeError('Invalid AudioDataInit: numberOfFrames must be an integer > 0.'); |
| 96 | } |
nothing calls this directly
no outgoing calls
no test coverage detected