RecordDlg.cs
//**********************************************************
//* Direct X Sound Recorder *
//* Author: D. Zouchinski *
//* http://zouchinski.co.uk *
//* Copyright @ 2009-2011 *
//**********************************************************
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using Microsoft.DirectX.DirectSound;
namespace SoundRecorder
{
public partial class RecordDlg : Form
{
public class DeviceInfo
{
protected DeviceInformation info;
public DeviceInfo(DeviceInformation di)
{
info = di;
}
public Guid DriverGuid { get { return info.DriverGuid; } }
public override string ToString()
{
return info.Description;
}
}
private Timer timer = new Timer();
private SoundCapture capture;
private WaveFile wavFile;
public RecordDlg()
{
InitializeComponent();
textOutput.Text = Properties.Settings.Default.Location;
textOutput.TextChanged += textOutput_TextChanged;
timer.Tick += new EventHandler(timer_Tick);
}
void textOutput_TextChanged(object sender, EventArgs e)
{
Properties.Settings.Default.Location = textOutput.Text;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
CaptureDevicesCollection devices = new CaptureDevicesCollection();
foreach (DeviceInformation info in devices)
{
int line = comboInput.Items.Add(new DeviceInfo(info));
if (info.DriverGuid == Properties.Settings.Default.Device)
comboInput.SelectedIndex = line;
}
}
private void StartCapture(Guid guid)
{
leftIndicator.Reset();
rightIndicator.Reset();
capture = new SoundCapture(guid);
if (capture != null)
{
capture.Start();
timer.Interval = 100;
timer.Start();
}
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
StopCapture();
Properties.Settings.Default.Save();
}
private void StopCapture()
{
if (capture != null)
{
if (wavFile != null)
wavFile.Close();
wavFile = null;
capture.Stop();
timer.Stop();
capture.Dispose();
capture = null;
}
leftIndicator.Reset();
rightIndicator.Reset();
}
void timer_Tick(object sender, EventArgs e)
{
short[] data = capture.Pull();
if (wavFile != null)
wavFile.Write(data);
leftIndicator.Update(LeftChannel(data));
rightIndicator.Update(RightChannel(data));
if (wavFile != null)
this.Text = "Recorded " + wavFile.TimeInSeconds().ToString("f1") + " sec";
else
this.Text = "Paused";
}
private static int GetPeak(short[] data)
{
int peak = 0;
if (data != null && data.Length > 0)
{
foreach (int value in data)
{
if (peak < Math.Abs(value))
peak = Math.Abs(value);
}
}
return peak;
}
private static short[] LeftChannel(short[] data)
{
if (data == null)
return null;
short[] left = new short[data.Length / 2];
int index = 0;
foreach (short sample in data)
{
if (index % 2 == 0)
left[index / 2] = sample;
index++;
}
return left;
}
private static short[] RightChannel(short[] data)
{
if (data == null)
return null;
short[] right = new short[data.Length / 2];
int index = 0;
foreach (short sample in data)
{
if (index % 2 != 0)
right[index / 2] = sample;
index++;
}
return right;
}
private static int GetLevel(short[] data)
{
int level = 0;
if (data != null && data.Length > 0)
{
foreach (int value in data)
level += Math.Abs(value);
level /= data.Length;
}
return level;
}
private void comboInput_SelectedIndexChanged(object sender, EventArgs e)
{
StopCapture();
DeviceInfo info = comboInput.SelectedItem as DeviceInfo;
if (info != null)
{
StartCapture(info.DriverGuid);
Properties.Settings.Default.Device = info.DriverGuid;
}
}
private void buttonSelect_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog dlg = new FolderBrowserDialog())
{
dlg.SelectedPath = textOutput.Text;
if (dlg.ShowDialog(this) == DialogResult.OK)
textOutput.Text = dlg.SelectedPath;
}
}
private void buttonOpen_Click(object sender, EventArgs e)
{
if (Directory.Exists(textOutput.Text))
System.Diagnostics.Process.Start(textOutput.Text);
}
private void buttonStart_Click(object sender, EventArgs e)
{
if (capture == null)
return;
if (wavFile == null)
{
wavFile = new WaveFile(NextFile(), capture.GetFormat());
}
else
{
wavFile.Close();
wavFile = null;
}
if (wavFile != null)
buttonStart.Text = "Stop";
else
buttonStart.Text = "Start";
}
private static string NextFile()
{
int index = Properties.Settings.Default.FileIndex + 1;
Properties.Settings.Default.FileIndex = index;
string location = Properties.Settings.Default.Location;
string prefix = Properties.Settings.Default.Prefix;
return Path.Combine(location, prefix + index.ToString("00000") + ".wav");
}
private void buttonClose_Click(object sender, EventArgs e)
{
Close();
}
}
}