(buffer)
| 20 | |
| 21 | class WavReader { |
| 22 | constructor(buffer) { |
| 23 | this.wav = new DataView(buffer); |
| 24 | this.position = 0; |
| 25 | this.waveSize = buffer.byteLength; |
| 26 | |
| 27 | if ("RIFF" !== this.readFourCC()) |
| 28 | throw new Error("expected RIFF"); |
| 29 | this.seekBy(4); // file size |
| 30 | if ("WAVE" !== this.readFourCC()) |
| 31 | throw new Error("expected WAVE"); |
| 32 | |
| 33 | if ("JUNK" === this.readFourCC()) |
| 34 | this.seekBy(this.readUint32()) |
| 35 | else |
| 36 | this.seekBy(-4); |
| 37 | |
| 38 | if ("fmt " !== this.readFourCC()) |
| 39 | throw new Error("expected fmt"); |
| 40 | let next = this.readUint32(); |
| 41 | next += this.position; |
| 42 | |
| 43 | this.audioFormat = this.readUint16(); |
| 44 | this.numChannels = this.readUint16(); |
| 45 | this.sampleRate = this.readUint32(); |
| 46 | this.seekBy(4 + 2); |
| 47 | this.bitsPerSample = this.readUint16(); |
| 48 | |
| 49 | this.seekTo(next); |
| 50 | |
| 51 | while ("data" !== this.readFourCC()) |
| 52 | this.seekBy(this.readUint32()); |
| 53 | |
| 54 | let bytes = this.readUint32(); |
| 55 | if (bytes > (this.waveSize - this.position)) |
| 56 | bytes = this.waveSize - this.position; |
| 57 | this.samples = Math.floor(bytes / ((this.numChannels * this.bitsPerSample) >> 3)); |
| 58 | } |
| 59 | |
| 60 | getSamples(buffer, count) { // always returns signed 16-bit sample values |
| 61 | this.samples -= count; |
nothing calls this directly
no test coverage detected