/// <summary>Copy wav data to a memorystream with auto conversion</summary> /// <param name="file">file name</param> /// <param name="stream">destination stream</param> /// <returns>bool -success</returns> public bool CopyStream(string file, ref MemoryStream stream) { try { // try reading the header GetHeader(file, ref _tWaveFormat); // Houston, we have a problem.. this requires conversion if (_tWaveFormat.wBitsPerSample == 0 || _tWaveFormat.wFormatTag != WAVE_FORMAT_PCM || (this.Convert16Bit == true && _tWaveFormat.wBitsPerSample == 8) || (this.Convert2Channels == true && _tWaveFormat.nChannels == 1)) { AcmStreamOut acm = new AcmStreamOut(); // conversion options acm.Convert16Bit = this.Convert16Bit; acm.Convert2Channels = this.Convert2Channels; // fetch the converted stream SND_RESULT ret = acm.PreConvert(file, ref stream); if (ret != SND_RESULT.SND_ERR_END_OF_STREAM || stream.Length == 0) { return(false); } else { // get the header and set props _tWaveFormat = acm.GetHeader(); this.Length = acm.DataLength; this.Channels = _tWaveFormat.nChannels; this.SamplesPerSecond = _tWaveFormat.nSamplesPerSec; this.BitsPerSample = _tWaveFormat.wBitsPerSample; this.AvgBytesPerSecond = _tWaveFormat.nAvgBytesPerSec; acm.Close(); stream.Position = 0; } } else { // does not require conversion int offset = WF_OFFSET_DATA; // open a filestream and reader FileStream fs = new FileStream(file, FileMode.Open); BinaryReader br = new BinaryReader(fs); // copy to byte array int len = (int)this.Length; byte[] bt = new byte[len]; fs.Position = offset; br.Read(bt, 0, bt.Length); fs.Close(); // write to stream stream = new MemoryStream(bt.Length); stream.Write(bt, 0, bt.Length); stream.Position = 0; } return(true); } catch { return(false); } }
/// <summary>Read converted byte stream</summary> /// <summary>If reading real time, first call must be proceeded by Create()</summary> /// <param name="data">input buffer</param> /// <param name="size">buffer size</param> /// <returns>SND_RESULT</returns> public SND_RESULT Read(ref byte[] data, uint size) { uint nread = 0; SND_RESULT ret = StreamRead(ref data, size, ref nread); if (nread == 0) { return(SND_RESULT.SND_ERR_END_OF_STREAM); } return(ret); }