-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#19: Added support for RIFF/WAVE metadata
- Loading branch information
Showing
8 changed files
with
290 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import * as Token from "token-types"; | ||
|
||
export interface IChunkHeader { | ||
|
||
/** | ||
* A chunk ID (ie, 4 ASCII bytes) | ||
*/ | ||
chunkID: string, | ||
/** | ||
* Number of data bytes following this data header | ||
*/ | ||
size: number | ||
} | ||
|
||
/** | ||
* Common RIFF chunk header | ||
*/ | ||
export const Header: Token.IGetToken<IChunkHeader> = { | ||
len: 8, | ||
|
||
get: (buf, off): IChunkHeader => { | ||
return { | ||
// Group-ID | ||
chunkID: new Token.StringType(4, 'ascii').get(buf, off), | ||
// Size | ||
size: buf.readUInt32BE(off + 4) | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import {ITokenParser} from "../ParserFactory"; | ||
import {EndOfFile, ITokenizer} from "strtok3"; | ||
import * as strtok3 from "strtok3"; | ||
import {IOptions, INativeAudioMetadata} from "../"; | ||
import * as Token from "token-types"; | ||
import * as RiffChunk from "./RiffChunk"; | ||
import * as WaveChunk from "./../wav/WaveChunk"; | ||
import {Readable} from "stream"; | ||
import {ID3v2Parser} from "../id3v2/ID3v2Parser"; | ||
|
||
/** | ||
* Resource Interchange File Format (RIFF) Parser | ||
* | ||
* WAVE PCM soundfile format | ||
* | ||
* Ref: | ||
* http://www.johnloomis.org/cpe102/asgn/asgn1/riff.html | ||
* http://soundfile.sapp.org/doc/WaveFormat | ||
*/ | ||
export class WavePcmParser implements ITokenParser { | ||
|
||
private tokenizer: ITokenizer; | ||
private options: IOptions; | ||
|
||
private metadata: INativeAudioMetadata = { | ||
format: { | ||
dataformat: "WAVE PCM" | ||
}, | ||
native: {} | ||
}; | ||
private warnings: string[] = []; | ||
|
||
private blockAlign: number; | ||
|
||
private native: INativeAudioMetadata; | ||
|
||
public parse(tokenizer: ITokenizer, options: IOptions): Promise<INativeAudioMetadata> { | ||
|
||
this.tokenizer = tokenizer; | ||
this.options = options; | ||
|
||
return this.tokenizer.readToken<RiffChunk.IChunkHeader>(RiffChunk.Header) | ||
.then((header) => { | ||
if (header.chunkID !== 'RIFF') | ||
return null; // Not AIFF format | ||
|
||
return this.tokenizer.readToken<string>(new Token.StringType(4, 'ascii')).then((type) => { | ||
this.metadata.format.dataformat = type; | ||
}).then(() => { | ||
return this.readChunk().then(() => { | ||
return null; | ||
}); | ||
}); | ||
}) | ||
.catch((err) => { | ||
if (err === EndOfFile) { | ||
return this.metadata; | ||
} else { | ||
throw err; | ||
} | ||
}); | ||
} | ||
|
||
public readChunk(): Promise<void> { | ||
return this.tokenizer.readToken<RiffChunk.IChunkHeader>(WaveChunk.Header) | ||
.then((header) => { | ||
switch (header.chunkID) { | ||
|
||
case "fmt ": // The Common Chunk | ||
return this.tokenizer.readToken<WaveChunk.IFormat>(new WaveChunk.Format(header)) | ||
.then((common) => { | ||
this.metadata.format.bitsPerSample = common.bitsPerSample; | ||
this.metadata.format.sampleRate = common.sampleRate; | ||
this.metadata.format.numberOfChannels = common.numChannels; | ||
this.metadata.format.bitrate = common.blockAlign * common.sampleRate * 8; | ||
this.blockAlign = common.blockAlign; | ||
}); | ||
|
||
case "id3 ": // The way Picard currently stores, ID3 meta-data | ||
case "ID3 ": // The way Mp3Tags stores ID3 meta-data | ||
return this.tokenizer.readToken<Buffer>(new Token.BufferType(header.size)) | ||
.then((id3_data) => { | ||
const id3stream = new ID3Stream(id3_data); | ||
return strtok3.fromStream(id3stream).then((rst) => { | ||
return ID3v2Parser.getInstance().parse(rst, this.options).then((id3) => { | ||
this.metadata.format.headerType = id3.format.headerType; | ||
this.metadata.native = id3.native; | ||
}); | ||
}); | ||
}); | ||
|
||
case 'data': // PCM-data | ||
this.metadata.format.numberOfSamples = header.size / this.blockAlign; | ||
this.metadata.format.duration = this.metadata.format.numberOfSamples / this.metadata.format.sampleRate; | ||
return this.tokenizer.ignore(header.size); | ||
|
||
case "LIST": // LIST ToDo? | ||
default: | ||
this.warnings.push("Ignore chunk: " + header.chunkID); | ||
return this.tokenizer.ignore(header.size); | ||
} | ||
}).then(() => { | ||
return this.readChunk(); | ||
}); | ||
} | ||
|
||
} | ||
|
||
class ID3Stream extends Readable { | ||
|
||
constructor(private buf: Buffer) { | ||
super(); | ||
} | ||
|
||
public _read() { | ||
this.push(this.buf); | ||
this.push(null); // push the EOF-signaling `null` chunk | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import * as Token from "token-types"; | ||
import * as assert from "assert"; | ||
import {IChunkHeader} from "../riff/RiffChunk"; | ||
|
||
/** | ||
* WAVE header chunk | ||
*/ | ||
export const Header: Token.IGetToken<IChunkHeader> = { | ||
len: 8, | ||
|
||
get: (buf, off): IChunkHeader => { | ||
return { | ||
// Group-ID | ||
chunkID: new Token.StringType(4, 'ascii').get(buf, off), | ||
// Size | ||
size: buf.readUInt32LE(off + 4) | ||
}; | ||
} | ||
}; | ||
|
||
/** | ||
* "fmt" sub-chunk describes the sound data's format | ||
* Ref: http://soundfile.sapp.org/doc/WaveFormat | ||
*/ | ||
export interface IFormat { | ||
/** | ||
* PCM = 1 (i.e. Linear quantization). Values other than 1 indicate some form of compression. | ||
*/ | ||
audioFormat: number, | ||
/** | ||
* Mono = 1, Stereo = 2, etc. | ||
*/ | ||
numChannels: number, | ||
/** | ||
* 8000, 44100, etc. | ||
*/ | ||
sampleRate: number, | ||
byteRate: number, | ||
blockAlign: number, | ||
bitsPerSample: number | ||
} | ||
|
||
/** | ||
* "fmt " chunk | ||
* http://soundfile.sapp.org/doc/WaveFormat/ | ||
*/ | ||
export class Format implements Token.IGetToken<IFormat> { | ||
|
||
public len: number; | ||
|
||
public constructor(header: IChunkHeader) { | ||
assert.ok(header.size >= 16, "16 for PCM."); | ||
this.len = header.size; | ||
} | ||
|
||
public get(buf: Buffer, off: number): IFormat { | ||
return { | ||
audioFormat: buf.readUInt16LE(off), | ||
numChannels: buf.readUInt16LE(off + 2), | ||
sampleRate: buf.readUInt32LE(off + 4), | ||
byteRate: buf.readUInt32LE(off + 8), | ||
blockAlign: buf.readUInt16LE(off + 12), | ||
bitsPerSample: buf.readUInt16LE(off + 14) | ||
}; | ||
} | ||
|
||
} |
Binary file modified
BIN
-620 Bytes
(100%)
test/samples/MusicBrainz - Beth Hart - Sinner's Prayer [id3v2.3].V2.mp3
Binary file not shown.
Binary file modified
BIN
-719 Bytes
(100%)
test/samples/MusicBrainz - Beth Hart - Sinner's Prayer [id3v2.4].V2.mp3
Binary file not shown.
Oops, something went wrong.