Advertisement
Guest User

Untitled

a guest
Aug 24th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. // ****************************************************
  4. //
  5. // Parser for RIFF WAVE files.
  6. //
  7. // ****************************************************
  8.  
  9. //
  10. // **** WaveParser object
  11. //
  12.  
  13. function WaveParser(filename){
  14.  
  15.   if(typeof filename == 'string'){
  16.     this.loadFile(filename);
  17.   };
  18.  
  19. };
  20.  
  21. //
  22. // **** Load a file into the WaveParser instance
  23. //
  24.  
  25. WaveParser.prototype.loadFile = function(filename){
  26.  
  27.   try{
  28.     this.file = fso.openTextFile(filename);
  29.   }
  30.  
  31.   catch(e){
  32.     log("WaveParser couldn't open " + filename);
  33.     log("Error read:");
  34.     log(e.description);
  35.   }
  36.  
  37. };
  38.  
  39. //
  40. // **** Parse the RIFF header and cue the file pointer to the start of sample data
  41. //
  42.  
  43. WaveParser.prototype.parse = function(){
  44.  
  45.   var metadata = {};
  46.       layout = WaveParser.waveFormatEx;
  47.  
  48.   for(var i = 0; i < layout.length; i++){
  49.  
  50.     var id = layout[i].id,
  51.         len = layout[i].len,
  52.         val = this.file.read(len).toString();
  53.  
  54.     switch(layout[i].type){
  55.  
  56.       case RIFFMEMBER_TYPE_ASCII:
  57.         // Nothing doing
  58.       break;
  59.  
  60.       default:
  61.         val = val.reverse().toInt();
  62.  
  63.     };
  64.  
  65.     metadata[id] = val;
  66.  
  67.   }
  68.  
  69.   if(metadata.mainChunkId != 'RIFF'){
  70.     throw WaveParser.Exception("File is not a RIFF.");
  71.   }
  72.  
  73.   if(metadata.riffFormat != 'WAVE'){
  74.     throw WaveParser.Exception("File is not a WAVE.");
  75.   }
  76.  
  77.   if(metadata.audioType != 1){
  78.     throw WaveParser.Exception("Timecode reader can only parse uncompressed PCM data.");
  79.   }
  80.  
  81.   this.meta = metadata;
  82.  
  83. };
  84.  
  85. //
  86. // **** Wave header layout; lengths in big-endian bytes
  87. //
  88.  
  89. WaveParser.waveFormatEx = [
  90.  
  91.   {
  92.     id : 'mainChunkId',
  93.     len : 4,
  94.     type : RIFFMEMBER_TYPE_ASCII
  95.   },
  96.   {
  97.     id : 'mainChunkSz',
  98.     len : 4
  99.   },
  100.   {
  101.     id : 'riffFormat',
  102.     len : 4,
  103.     type : RIFFMEMBER_TYPE_ASCII
  104.   },
  105.   {
  106.     id : 'fmtChunkId',
  107.     len : 4,
  108.     type : RIFFMEMBER_TYPE_ASCII
  109.   },
  110.   {
  111.     id : 'fmtChunkSz',
  112.     len : 4
  113.   },
  114.   {
  115.     id : 'audioType',
  116.     len : 2
  117.   },
  118.   {
  119.     id : 'channels',
  120.     len : 2
  121.   },
  122.   {
  123.     id : 'sampleRate',
  124.     len : 4
  125.   },
  126.   {
  127.     id : 'byteRate',
  128.     len : 4
  129.   },
  130.   {
  131.     id : 'blockAlign',
  132.     len : 2
  133.   },
  134.   {
  135.     id : 'bits',
  136.     len : 2
  137.   }
  138.  
  139. ];
  140.  
  141. //
  142. // **** Exception generator
  143. //
  144.  
  145. WaveParser.Exception = function(msg){
  146.   return new Error(-1,'Wave parser - ' + msg);
  147. };
  148.  
  149. //
  150. // **** End of RIFF WAVE parser
  151. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement