var outputFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "NAudio"); Directory.CreateDirectory(outputFolder); var outputFilePath = Path.Combine(outputFolder,"recorded.wav");
var waveIn = new WaveInEvent();
WaveFileWriter writer = null; MuLawChatCodec _codec = new MuLawChatCodec(); bool closing = false; var f = new Form(); var buttonRecord = new Button() { Text = "Record" }; var buttonStop = new Button() { Text = "Stop", Left = buttonRecord.Right, Enabled = false }; f.Controls.AddRange(new Control[] { buttonRecord, buttonStop });
buttonRecord.Click += (s, a) => { writer = new WaveFileWriter(outputFilePath, waveIn.WaveFormat); waveIn.StartRecording(); buttonRecord.Enabled = false; buttonStop.Enabled = true; };
buttonStop.Click += (s, a) => waveIn.StopRecording();
public WaveFormat RecordFormat => new WaveFormat(8000, 16, 1);
publicbyte[] Encode(byte[] data, int offset, int length) { var encoded = newbyte[length / 2]; int outIndex = 0; for (int n = 0; n < length; n += 2) { encoded[outIndex++] = MuLawEncoder.LinearToMuLawSample(BitConverter.ToInt16(data, offset + n)); } return encoded; }
publicbyte[] Decode(byte[] data, int offset, int length) { var decoded = newbyte[length * 2]; int outIndex = 0; for (int n = 0; n < length; n++) { short decodedSample = MuLawDecoder.MuLawToLinearSample(data[n + offset]); decoded[outIndex++] = (byte)(decodedSample & 0xFF); decoded[outIndex++] = (byte)(decodedSample >> 8); } return decoded; }