using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; using Microsoft.DirectX.DirectSound; namespace USBGlowLightDirectX { public partial class Form1 : Form { WaveFormat waveFormat = default(WaveFormat); CaptureBuffer _dwCapBuffer = default(CaptureBuffer); AutoResetEvent _resetEvent = new AutoResetEvent(false); Notify _notify = default(Notify); Int32 _dwOutputBufferSize; Int32 _dwCaptureBufferSize; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //Setup the Capturing Format waveFormat.SamplesPerSecond = 96000; waveFormat.BitsPerSample = 16; waveFormat.Channels = 2; waveFormat.FormatTag = WaveFormatTag.Pcm; waveFormat.BlockAlign = (short)(waveFormat.Channels * (waveFormat.BitsPerSample / 8)); waveFormat.AverageBytesPerSecond = waveFormat.SamplesPerSecond * waveFormat.BlockAlign; var _dwNotifySize = Math.Max(4096, waveFormat.AverageBytesPerSecond / 8); _dwNotifySize -= _dwNotifySize % waveFormat.BlockAlign; _dwCaptureBufferSize = _dwNotifySize; _dwOutputBufferSize = _dwNotifySize / 2; //Setup the Capture Buffer var cap = default(Capture); var cdc = new CaptureDevicesCollection(); for (int i = 0; i < cdc.Count; i++) { if (cdc[i].Description.ToLower().Contains("Stereo Mix".ToLower())) { cap = new Capture(cdc[i].DriverGuid); break; } } var capDesc = new CaptureBufferDescription { Format = waveFormat, BufferBytes = _dwCaptureBufferSize }; _dwCapBuffer = new CaptureBuffer(capDesc, cap); //This is the Capture Buffer //setup buffer notifications to allow buffer to reset when full var bpn1 = new BufferPositionNotify(); bpn1.Offset = _dwCapBuffer.Caps.BufferBytes / 2 - 1; bpn1.EventNotifyHandle = _resetEvent.SafeWaitHandle.DangerousGetHandle(); var bpn2 = new BufferPositionNotify(); bpn2.Offset = _dwCapBuffer.Caps.BufferBytes - 1; bpn2.EventNotifyHandle = _resetEvent.SafeWaitHandle.DangerousGetHandle(); _notify.SetNotificationPositions(new BufferPositionNotify[] { bpn1, bpn2 }); } private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (this.checkBox1.Checked == true) { int offset = 0; Thread _dwCaptureThread = new Thread((ThreadStart)delegate { _dwCapBuffer.Start(true); while (this.checkBox1.Checked == true) { _resetEvent.WaitOne(); _dwCapBuffer.Read(offset, typeof(short), LockFlag.None, _dwOutputBufferSize); offset = (offset + _dwOutputBufferSize) % _dwCaptureBufferSize; } _dwCapBuffer.Stop(); }); _dwCaptureThread.Start(); } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)