/// <summary> /// Write the stored audio data as a WAVE file. /// </summary> public void SaveAsWav(string filePath) { // Make sure we have data. if (String.IsNullOrEmpty(_tempfile)) { _cbWritten = 0; return; } if (!File.Exists(_tempfile)) { _tempfile = null; _cbWritten = 0; return; } if (_cbWritten == 0) { File.Delete(_tempfile); _tempfile = null; return; } FileInfo fi = new FileInfo(_tempfile); Debug.Assert(fi.Length == _cbWritten); WaveFileWriter writer = new WaveFileWriter(filePath); writer.WriteFileHeader((int)fi.Length); WaveFormatChunk format = new WaveFormatChunk(); format.chunkId = "fmt "; format.chunkSize = 16; // size of the struct in bytes - 8 format.audioFormat = WAV_FMT_PCM; format.channelCount = _channelCount; format.sampleRate = _sampleRate; format.byteRate = (uint)(_sampleRate * _channelCount * _bitsPerFrame / 8); format.blockAlign = (ushort)(_channelCount * _bitsPerFrame / 8); format.bitsPerSample = _bitsPerFrame; writer.WriteFormatChunk(format); writer.WriteDataHeader((int)fi.Length); byte[] data = File.ReadAllBytes(_tempfile); Debug.Assert(data.Length == _cbWritten); writer.WriteData(data); writer.Close(); // Clean up the temporary data from the recording process. File.Delete(_tempfile); _tempfile = null; _cbWritten = 0; }