public void Dispose() { if (RiffSource != null) { RiffSource.Dispose(); } RiffSource = null; }
void RunImportJob(IEnumerable <string> files) { foreach (var f in files) { RiffMaster rm = new RiffMaster(); rm.LoadFile(f); var dataChunk = rm.riff.GetSubchunk("data", null) as RiffMaster.RiffSubchunk; var fmt = rm.riff.GetSubchunk("fmt ", null) as RiffMaster.RiffSubchunk_fmt; BinaryReader br = new BinaryReader(new MemoryStream(dataChunk.data)); int nSamples = dataChunk.data.Length / fmt.blockAlign; int nChunks = nSamples / 32; if (fmt.channels == 2) { throw new InvalidOperationException("expected mono file"); } string textoutfile = f + ".txt"; StreamWriter sw = new StreamWriter(textoutfile); br = new BinaryReader(new MemoryStream(dataChunk.data)); var ms = new MemoryStream(); for (int j = 0; j < nChunks; j++) { string outfile = f + "." + j + ".dmw"; FileStream fs = new FileStream(outfile, FileMode.Create, FileAccess.Write, FileShare.None); fs.WriteByte(0x20); fs.WriteByte(0x00); fs.WriteByte(0x00); fs.WriteByte(0x00); fs.WriteByte(0x1F); for (int i = 0; i < 32; i++) { short ssample = br.ReadInt16(); int sample = ssample + 16; if (sample > 31) { sample = 31; //clamp in case we overdrived or whatever } if (sample < 0) { throw new InvalidOperationException("oops minus 0"); } fs.WriteByte((byte)sample); sw.Write("{0}{1}", (byte)sample, i == 31?"":" "); } sw.WriteLine(); fs.Close(); } sw.Close(); rm.WriteFile(f); } }
public void Load(Stream stream) { try { RiffSource = null; var rm = new RiffMaster(); rm.LoadStream(stream); RiffSource = rm; //analyze the file to make sure its an OK wave file if (rm.riff.type != "WAVE") { throw new Blob_WaveFile_Exception("Not a RIFF WAVE file"); } var fmt = rm.riff.subchunks.FirstOrDefault(chunk => chunk.tag == "fmt ") as RiffMaster.RiffSubchunk_fmt; if (fmt == null) { throw new Blob_WaveFile_Exception("Not a valid RIFF WAVE file (missing fmt chunk"); } if (1 != rm.riff.subchunks.Count(chunk => chunk.tag == "data")) { //later, we could make a Stream which would make an index of data chunks and walk around them throw new Blob_WaveFile_Exception("Multi-data-chunk WAVE files not supported"); } if (fmt.format_tag != RiffMaster.RiffSubchunk_fmt.FORMAT_TAG.WAVE_FORMAT_PCM) { throw new Blob_WaveFile_Exception("Not a valid PCM WAVE file (only PCM is supported)"); } if (fmt.channels != 2 || fmt.bitsPerSample != 16 || fmt.samplesPerSec != 44100) { throw new Blob_WaveFile_Exception("Not a CDA format WAVE file (conversion not yet supported)"); } //acquire the start of the data chunk var dataChunk = rm.riff.subchunks.FirstOrDefault(chunk => chunk.tag == "data") as RiffMaster.RiffSubchunk; waveDataStreamPos = dataChunk.Position; mDataLength = dataChunk.Length; } catch (Exception) { Dispose(); throw; } }