声卡采集解决思路

声卡采集
声卡采集怎么写呢,给个实现代码!

------解决方案--------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Threading;
using System.IO;
using Microsoft.DirectX.DirectSound;
using Microsoft.DirectX;

namespace DSound
{
public partial class Form3 : Form
{
#region 用户变量
private string strRecSaveFile = string.Empty;//文件保存路径
private Notify myNotify = null;//缓冲区提示事件
private FileStream fsWav = null;//保存的文件流
private int iNotifyNum = 16;//通知的个数
private int iBufferOffset = 0;//本次数据起始点, 上一次数据的终点。
private int iSampleSize = 0;//所采集到的数据大小
private int iNotifySize = 0;//通知所在区域大小
private int iBufferSize = 0;//缓冲区大小
private BinaryWriter mWriter;
private Capture capture = null;//捕捉设备对象
private CaptureBuffer capturebuffer = null;//捕捉缓冲区
private AutoResetEvent notifyevent = null;
private Thread notifythread = null;
private WaveFormat mWavFormat;//PCM格式
#endregion

/// <summary>
/// 设置PCM格式;
/// </summary>
/// <returns></returns>
private WaveFormat SetWaveFormat()
{
WaveFormat format = new WaveFormat();
format.FormatTag = WaveFormatTag.Pcm;//设置音频类型
format.SamplesPerSecond = 22050;//采样率(单位:赫兹)典型值:11025、22050、44100Hz
format.BitsPerSample = 16;//采样位数
format.Channels = 1;//声道
format.BlockAlign = (short)(format.Channels * (format.BitsPerSample / 8));//单位采样点的字节数
format.AverageBytesPerSecond = format.BlockAlign * format.SamplesPerSecond;
return format;
//按照以上采样规格,可知采样1秒钟的字节数为22050*2=55100B 约为 53K
}
/// <summary>
/// 创建wave文件;
/// </summary>
/// <param name="strFileName"></param>
private void CreateWaveFile(string strFileName)
{
fsWav = new FileStream(strFileName, FileMode.CreateNew);
mWriter = new BinaryWriter(fsWav);
char[] ChunkRiff = { 'R', 'I', 'F', 'F' };
char[] ChunkType = { 'W', 'A', 'V', 'E' };
char[] ChunkFmt = { 'f', 'm', 't', ' ' };
char[] ChunkData = { 'd', 'a', 't', 'a' };
short shPad = 1; // File padding
int nFormatChunkLength = 0x10; // Format chunk length.
int nLength = 0; // File length, minus first 8 bytes of RIFF description. This will be filled in later.
short shBytesPerSample = 0; // Bytes per sample.
// 一个样本点的字节数目
if (8 == mWavFormat.BitsPerSample && 1 == mWavFormat.Channels)
shBytesPerSample = 1;
else if ((8 == mWavFormat.BitsPerSample && 2 == mWavFormat.Channels) || (16 == mWavFormat.BitsPerSample && 1 == mWavFormat.Channels))
shBytesPerSample = 2;
else if (16 == mWavFormat.BitsPerSample && 2 == mWavFormat.Channels)
shBytesPerSample = 4;
// RIFF 块
mWriter.Write(ChunkRiff);
mWriter.Write(nLength);
mWriter.Write(ChunkType);
// WAVE块
mWriter.Write(ChunkFmt);
mWriter.Write(nFormatChunkLength);
mWriter.Write(shPad);
mWriter.Write(mWavFormat.Channels);
mWriter.Write(mWavFormat.SamplesPerSecond);
mWriter.Write(mWavFormat.AverageBytesPerSecond);
mWriter.Write(shBytesPerSample);