public byte[] Encode(PCM16Audio lwav, string original_filename_no_ext) { if (lwav.OriginalPath != null) { switch (Path.GetExtension(lwav.OriginalPath).ToLowerInvariant()) { case ".brstm": return(File.ReadAllBytes(lwav.OriginalPath)); case ".bcstm": return(CSTMConverter.ToRSTM(File.ReadAllBytes(lwav.OriginalPath))); case ".bfstm": return(FSTMConverter.ToRSTM(File.ReadAllBytes(lwav.OriginalPath))); } } using (var pw = new ProgressWindow(null, "BrawlLib Audio Encoder", $"Encoding {original_filename_no_ext}", true)) using (FileMap rstm = RSTMConverter.Encode(new PCM16AudioStream(lwav), pw, WaveEncoding.ADPCM)) { if (pw.Cancelled) { throw new AudioExporterException("RSTM export cancelled"); } byte[] data = new byte[rstm.Length]; Marshal.Copy(rstm.Address, data, 0, rstm.Length); return(data); } }
public override unsafe void Export(string outPath) { if (outPath.EndsWith(".wav")) { WAV.ToFile(CreateStreams()[0], outPath); } else { if (_audioSource != DataSource.Empty) { int size = WorkingUncompressed.Length + _audioSource.Length; using (FileStream stream = new FileStream(outPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)) { stream.SetLength(size); using (FileMap map = FileMap.FromStreamInternal(stream, FileMapProtect.ReadWrite, 0, size)) { VoidPtr addr = map.Address; //Write header Memory.Move(addr, WorkingUncompressed.Address, (uint)WorkingUncompressed.Length); //Set the offset to the audio samples (_dataLocation) RSTMHeader *hdr = (RSTMHeader *)addr; hdr->_header._length = WorkingUncompressed.Length + _audioSource.Length; hdr->DATAData->Set(_audioSource.Length + 0x20); addr += WorkingUncompressed.Length; //Append audio samples to the end Memory.Move(addr, _audioSource.Address, (uint)_audioSource.Length); if (outPath.EndsWith(".bcstm")) { ShowADPCMConversionWarning(); byte[] bcstm_temp = CSTMConverter.FromRSTM(hdr); fixed(byte *ptr = bcstm_temp) { Memory.Move(map.Address, ptr, (uint)bcstm_temp.Length); } } else if (outPath.EndsWith(".bfstm")) { ShowADPCMConversionWarning(); byte[] bfstm_temp = FSTMConverter.FromRSTM(hdr); fixed(byte *ptr = bfstm_temp) { Memory.Move(map.Address, ptr, (uint)bfstm_temp.Length); } } } } } else { base.Export(outPath); } } }
public override bool OnInitialize() { if (_name == null && _origPath != null) { _name = Path.GetFileNameWithoutExtension(_origPath); } base.OnInitialize(); if (Header->_header._tag == CSTMHeader.Tag) { ShowADPCMConversionWarning(); byte[] brstm_temp = CSTMConverter.ToRSTM((CSTMHeader *)Header); fixed(byte *ptr = brstm_temp) { ReplaceRaw(ptr, brstm_temp.Length); return(false); } } if (Header->_header._tag == FSTMHeader.Tag) { ShowADPCMConversionWarning(); byte[] brstm_temp = FSTMConverter.ToRSTM((FSTMHeader *)Header); fixed(byte *ptr = brstm_temp) { ReplaceRaw(ptr, brstm_temp.Length); return(false); } } StrmDataInfo *part1 = Header->HEADData->Part1; _encoding = part1->_format._encoding; _channels = part1->_format._channels; _looped = part1->_format._looped != 0; _sampleRate = part1->_sampleRate; _loopStart = part1->_loopStartSample; _numSamples = part1->_numSamples; _dataOffset = part1->_dataOffset; _numBlocks = part1->_numBlocks; _blockSize = part1->_blockSize; _bps = part1->_bitsPerSample; int offset = Header->DATAData->Data - Header; if (offset < WorkingUncompressed.Length) { _audioSource = new DataSource(Header->DATAData->Data, WorkingUncompressed.Length - offset); SetSizeInternal(offset); } return(false); }
public void WriteFile(PCM16Audio lwav, string output_dir, string original_filename_no_ext) { byte[] rstmData = new RSTMExporter().Encode(lwav, original_filename_no_ext); byte[] fstmData = FSTMConverter.FromRSTM(rstmData); File.WriteAllBytes(Path.Combine(output_dir, original_filename_no_ext + ".bfstm"), fstmData); }