MCPcopy Create free account
hub / github.com/Moddable-OpenSource/moddable / constructor

Method constructor

modules/data/wavreader/wavreader.js:22–58  ·  view source on GitHub ↗
(buffer)

Source from the content-addressed store, hash-verified

20
21class 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;

Callers

nothing calls this directly

Calls 5

readFourCCMethod · 0.95
seekByMethod · 0.95
readUint32Method · 0.95
readUint16Method · 0.95
seekToMethod · 0.95

Tested by

no test coverage detected