Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ****************************************************
- //
- // Parser for RIFF WAVE files.
- //
- // ****************************************************
- //
- // **** WaveParser object
- //
- function WaveParser(filename){
- if(typeof filename == 'string'){
- this.loadFile(filename);
- };
- };
- //
- // **** Load a file into the WaveParser instance
- //
- WaveParser.prototype.loadFile = function(filename){
- try{
- this.file = fso.openTextFile(filename);
- }
- catch(e){
- log("WaveParser couldn't open " + filename);
- log("Error read:");
- log(e.description);
- }
- };
- //
- // **** Parse the RIFF header and cue the file pointer to the start of sample data
- //
- WaveParser.prototype.parse = function(){
- var metadata = {};
- layout = WaveParser.waveFormatEx;
- for(var i = 0; i < layout.length; i++){
- var id = layout[i].id,
- len = layout[i].len,
- val = this.file.read(len).toString();
- switch(layout[i].type){
- case RIFFMEMBER_TYPE_ASCII:
- // Nothing doing
- break;
- default:
- val = val.reverse().toInt();
- };
- metadata[id] = val;
- }
- if(metadata.mainChunkId != 'RIFF'){
- throw WaveParser.Exception("File is not a RIFF.");
- }
- if(metadata.riffFormat != 'WAVE'){
- throw WaveParser.Exception("File is not a WAVE.");
- }
- if(metadata.audioType != 1){
- throw WaveParser.Exception("Timecode reader can only parse uncompressed PCM data.");
- }
- this.meta = metadata;
- };
- //
- // **** Wave header layout; lengths in big-endian bytes
- //
- WaveParser.waveFormatEx = [
- {
- id : 'mainChunkId',
- len : 4,
- type : RIFFMEMBER_TYPE_ASCII
- },
- {
- id : 'mainChunkSz',
- len : 4
- },
- {
- id : 'riffFormat',
- len : 4,
- type : RIFFMEMBER_TYPE_ASCII
- },
- {
- id : 'fmtChunkId',
- len : 4,
- type : RIFFMEMBER_TYPE_ASCII
- },
- {
- id : 'fmtChunkSz',
- len : 4
- },
- {
- id : 'audioType',
- len : 2
- },
- {
- id : 'channels',
- len : 2
- },
- {
- id : 'sampleRate',
- len : 4
- },
- {
- id : 'byteRate',
- len : 4
- },
- {
- id : 'blockAlign',
- len : 2
- },
- {
- id : 'bits',
- len : 2
- }
- ];
- //
- // **** Exception generator
- //
- WaveParser.Exception = function(msg){
- return new Error(-1,'Wave parser - ' + msg);
- };
- //
- // **** End of RIFF WAVE parser
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement