node.js - Translate NodeJS to C# to load WAV-format audio and remove some unessecary headers -


i'm trying convert .sd9 files .wav files in c#, these files .wav files unessecary headers (used in games).

i found nodejs code online so, code works fine:

   var snd_buf = fs.readfilesync(item);      if (snd_buf.slice(0x00, 0x03).tostring() != "sd9") {         console.log("[-] '" + path.basename(item) + "' not sd9 file.");         continue;     }      var wav_buf = snd_buf.slice(0x20, snd_buf.length);      if (sd9_file_list.length > 1) {         if (!fs.existssync(output_path)) {             fs.mkdirsync(output_path);         }          output_path += "/" + path.basename(item, ".sd9") + ".wav";     }      output_path = path.normalize(output_path);      fs.writefilesync(output_path, wav_buf); 

item: full path file

now i'm trying 'translate' c# without luck:

string[] files = directory.getfiles(fbd.selectedpath, "*.sd9", searchoption.alldirectories);  foreach (string file in files) { utf8encoding nobom = new utf8encoding(false);  string raw_buffer = file.readalltext(file, nobom);  string wave_buffer = "";  if (raw_buffer.substring(0x00, 0x03) == "sd9") {     wave_buffer = raw_buffer.substring(0x20);     messagebox.show(wave_buffer); }  if (file.exists(fbdd.selectedpath + @"\" + path.getfilenamewithoutextension(file) + ".wav")) { file.delete(fbdd.selectedpath + @"\" + path.getfilenamewithoutextension(file) + ".wav");  }  binarywriter sw = new binarywriter(new filestream(fbdd.selectedpath + @"\" + path.getfilenamewithoutextension(file) + ".wav", filemode.createnew)); sw.write(wave_buffer); sw.flush(); sw.close(); } 

either way of writing or reading seems add unnecessary headers / malformed bytes or whatever make file unreadable, example, correctly converted file nodejs script 91kb while script outputs corrupted file of 140kb.

your problem reading in sd9 file string. you're using string functions slice file apart. instead, want read file byte[].

  1. use file.readallbytes() read file byte[].
  2. use trick "splice" byte array: getting sub-array existing array
  3. use system.text.encoding.ascii.getstring() convert byte[] string "sd9" header comparison.
  4. use file.writeallbytes() write new file.

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -