c#录音功能怎么实现?

Python011

c#录音功能怎么实现?,第1张

C#中使用DirectSound录音

一.声卡录音的基本原理

为了实现一个录音的基本过程,至少需要以下对象的支持:

1. 录音设备,对我们的PC设备就是声卡。这个录音设备可以进行的操作应该有开始和关闭。

2. 缓冲区,也就是录制的声音放在哪里的问题。

二.DirectSound对录音的描述模型

1. DirectSound对录音的支持类

Ø Capture,设备对象,可以看作是声卡的描述。

Ø CaptureBuffer,缓冲区对象,存放录入的音频数据。

Ø Notify,事件通知对象,由于录音是一个长时间的过程,因此使用一个缓冲队列(多个缓冲区)接收数据,每当一个缓冲区满的时候,系统使用这个对象通知应用程序取走这个缓冲区,并继续录音。

以上三个对象是进行录音操作的主要对象,由于在C++中对DirectSound的操作DirectX帮助文档中已经有很详细的说明,这里就不再赘述了。本文是针对Managed Code。除了以上三个主要的DirectSound类,还需要以下几个辅助类。

Ø WaveFormat,描述了进行录制的声音波形的格式,例如采样率,单声道还是立体声,每个采样点的长度等等。

Ø Thread,线程类,由于录音的过程是需要不断处理缓冲区满的事件,因此新建一个线程对此进行单独处理。

Ø AutoResetEvent,通知的事件,当缓冲区满的时候,使用该事件作为通知事件。

三.代码解析(SoundRecord类)

1.需要引用的程序集

using System

using System.Windows.Forms

using System.Threading

using System.IO

// 对DirectSound的支持

using Microsoft.DirectX

using Microsoft.DirectX.DirectSound

2. SoundRecord的成员数据

public const int cNotifyNum = 16 // 缓冲队列的数目

private int mNextCaptureOffset = 0 // 该次录音缓冲区的起始点

private int mSampleCount = 0 // 录制的样本数目

private int mNotifySize = 0// 每次通知大小

private int mBufferSize = 0// 缓冲队列大小

private string mFileName = string.Empty// 文件名

private FileStream mWaveFile = null// 文件流

private BinaryWriter mWriter = null// 写文件

private Capture mCapDev = null // 音频捕捉设备

private CaptureBuffer mRecBuffer = null// 缓冲区对象

private Notify mNotify = null // 消息通知对象

private WaveFormat mWavFormat // 录音的格式

private Thread mNotifyThread = null// 处理缓冲区消息的线程

private AutoResetEvent mNotificationEvent = null // 通知事件

3. 对外操作的函数

/// <summary>

/// 构造函数,设定录音设备,设定录音格式.

/// </summary>

public SoundRecord()

{

// 初始化音频捕捉设备

InitCaptureDevice()

// 设定录音格式

mWavFormat = CreateWaveFormat()

}

/// <summary>

/// 设定录音结束后保存的文件,包括路径

/// </summary>

/// <param name="filename">保存wav文件的路径名</param>

public void SetFileName(string filename)

{

mFileName = filename

}

/// <summary>

/// 开始录音

/// </summary>

public void RecStart()

{

// 创建录音文件

CreateSoundFile()

// 创建一个录音缓冲区,并开始录音

CreateCaptureBuffer()

// 建立通知消息,当缓冲区满的时候处理方法

InitNotifications()

mRecBuffer.Start(true)

}

/// <summary>

/// 停止录音

/// </summary>

public void RecStop()

{

// 关闭通知消息

if (null != mNotificationEvent)

mNotificationEvent.Set()

// 停止录音

mRecBuffer.Stop()

// 写入缓冲区最后的数据

RecordCapturedData()

// 回写长度信息

mWriter.Seek(4, SeekOrigin.Begin)

mWriter.Write((int)(mSampleCount + 36)) // 写文件长度

mWriter.Seek(40, SeekOrigin.Begin)

mWriter.Write(mSampleCount) // 写数据长度

mWriter.Close()

mWaveFile.Close()

mWriter = null

mWaveFile = null

}

4.内部调用函数

/// <summary>

/// 初始化录音设备,此处使用主录音设备.

/// </summary>

/// <returns>调用成功返回true,否则返回false</returns>

private bool InitCaptureDevice()

{

// 获取默认音频捕捉设备

CaptureDevicesCollection devices = new CaptureDevicesCollection()// 枚举音频捕捉设备

Guid deviceGuid = Guid.Empty // 音频捕捉设备的ID

if (devices.Count>0)

deviceGuid = devices[0].DriverGuid

else

{

MessageBox.Show("系统中没有音频捕捉设备")

return false

}

// 用指定的捕捉设备创建Capture对象

try

{

mCapDev = new Capture(deviceGuid)

}

catch (DirectXException e)

{

MessageBox.Show(e.ToString())

return false

}

return true

}

/// <summary>

/// 创建录音格式,此处使用16bit,16KHz,Mono的录音格式

/// </summary>

/// <returns>WaveFormat结构体</returns>

private WaveFormat CreateWaveFormat()

{

WaveFormat format = new WaveFormat()

format.FormatTag = WaveFormatTag.Pcm // PCM

format.SamplesPerSecond = 16000 // 16KHz

format.BitsPerSample = 16 // 16Bit

format.Channels = 1 // Mono

format.BlockAlign = (short)(format.Channels * (format.BitsPerSample / 8))

format.AverageBytesPerSecond = format.BlockAlign * format.SamplesPerSecond

return format

}

/// <summary>

/// 创建录音使用的缓冲区

/// </summary>

private void CreateCaptureBuffer()

{

// 缓冲区的描述对象

CaptureBufferDescription bufferdescription = new CaptureBufferDescription()

if (null != mNotify)

{

mNotify.Dispose()

mNotify = null

}

if (null != mRecBuffer)

{

mRecBuffer.Dispose()

mRecBuffer = null

}

// 设定通知的大小,默认为1s钟

mNotifySize = (1024 >mWavFormat.AverageBytesPerSecond / 8) ? 1024 : (mWavFormat.AverageBytesPerSecond / 8)

mNotifySize -= mNotifySize % mWavFormat.BlockAlign

// 设定缓冲区大小

mBufferSize = mNotifySize * cNotifyNum

// 创建缓冲区描述

bufferdescription.BufferBytes = mBufferSize

bufferdescription.Format = mWavFormat // 录音格式

// 创建缓冲区

mRecBuffer = new CaptureBuffer(bufferdescription, mCapDev)

mNextCaptureOffset = 0

}

/// <summary>

/// 初始化通知事件,将原缓冲区分成16个缓冲队列,在每个缓冲队列的结束点设定通知点.

/// </summary>

/// <returns>是否成功</returns>

private bool InitNotifications()

{

if (null == mRecBuffer)

{

MessageBox.Show("未创建录音缓冲区")

return false

}

// 创建一个通知事件,当缓冲队列满了就激发该事件.

mNotificationEvent = new AutoResetEvent(false)

// 创建一个线程管理缓冲区事件

if (null == mNotifyThread)

{

mNotifyThread = new Thread(new ThreadStart(WaitThread))

mNotifyThread.Start()

}

// 设定通知的位置

BufferPositionNotify[] PositionNotify = new BufferPositionNotify[cNotifyNum + 1]

for (int i = 0i <cNotifyNumi++)

{

PositionNotify[i].Offset = (mNotifySize * i) + mNotifySize - 1

PositionNotify[i].EventNotifyHandle = mNotificationEvent.Handle

}

mNotify = new Notify(mRecBuffer)

mNotify.SetNotificationPositions(PositionNotify, cNotifyNum)

return true

}

/// <summary>

/// 将录制的数据写入wav文件

/// </summary>

private void RecordCapturedData()

{

byte[] CaptureData = null

int ReadPos

int CapturePos

int LockSize

mRecBuffer.GetCurrentPosition(out CapturePos, out ReadPos)

LockSize = ReadPos - mNextCaptureOffset

if (LockSize <0)

LockSize += mBufferSize

// 对齐缓冲区边界,实际上由于开始设定完整,这个操作是多余的.

LockSize -= (LockSize % mNotifySize)

if (0 == LockSize)

return

// 读取缓冲区内的数据

CaptureData = (byte[])mRecBuffer.Read(mNextCaptureOffset, typeof(byte), LockFlag.None, LockSize)

// 写入Wav文件

mWriter.Write(CaptureData, 0, CaptureData.Length)

// 更新已经录制的数据长度.

mSampleCount += CaptureData.Length

// 移动录制数据的起始点,通知消息只负责指示产生消息的位置,并不记录上次录制的位置

mNextCaptureOffset += CaptureData.Length

mNextCaptureOffset %= mBufferSize// Circular buffer

}

/// <summary>

/// 接收缓冲区满消息的处理线程

/// </summary>

private void WaitThread()

{

while(true)

{

// 等待缓冲区的通知消息

mNotificationEvent.WaitOne(Timeout.Infinite, true)

// 录制数据

RecordCapturedData()

}

}

/// <summary>

/// 创建保存的波形文件,并写入必要的文件头.

/// </summary>

private void CreateSoundFile()

{

/**************************************************************************

Here is where the file will be created. A

wave file is a RIFF file, which has chunks

of data that describe what the file contains.

A wave RIFF file is put together like this:

The 12 byte RIFF chunk is constructed like this:

Bytes 0 - 3 : 'R' 'I' 'F' 'F'

Bytes 4 - 7 : Length of file, minus the first 8 bytes of the RIFF description.

(4 bytes for "WAVE" + 24 bytes for format chunk length +

8 bytes for data chunk description + actual sample data size.)

Bytes 8 - 11: 'W' 'A' 'V' 'E'

The 24 byte FORMAT chunk is constructed like this:

Bytes 0 - 3 : 'f' 'm' 't' ' '

Bytes 4 - 7 : The format chunk length. This is always 16.

Bytes 8 - 9 : File padding. Always 1.

Bytes 10- 11: Number of channels. Either 1 for mono, or 2 for stereo.

Bytes 12- 15: Sample rate.

Bytes 16- 19: Number of bytes per second.

Bytes 20- 21: Bytes per sample. 1 for 8 bit mono, 2 for 8 bit stereo or

16 bit mono, 4 for 16 bit stereo.

Bytes 22- 23: Number of bits per sample.

The DATA chunk is constructed like this:

Bytes 0 - 3 : 'd' 'a' 't' 'a'

Bytes 4 - 7 : Length of data, in bytes.

Bytes 8 -...: Actual sample data.

***************************************************************************/

// Open up the wave file for writing.

mWaveFile = new FileStream(mFileName, FileMode.Create)

mWriter = new BinaryWriter(mWaveFile)

// Set up file with RIFF chunk info.

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)

mWriter.Write(mWavFormat.BitsPerSample)

// 数据块

mWriter.Write(ChunkData)

mWriter.Write((int)0) // The sample length will be written in later.

}

5.外部窗体调用方式

声明部分:

private SoundRecord recorder = null // 录音

窗体构造函数:

recorder = new SoundRecord()

启动录音按钮:

private void btnStart_Click(object sender, System.EventArgs e)

{

//

// 录音设置

//

string wavfile = null

wavfile = “test.wav”

recorder.SetFileName(wavfile)

recorder.RecStart()

}

中止录音按钮:

private void btnStop_Click(object sender, System.EventArgs e)

{

recorder.RecStop()

recorder = null

}

6.需要添加的外部引用文件

在系统的System32目录下添加以下两个引用文件,如果没有,在DirectX的开发包内可以找到。

Microsoft.DirectX.dll

Microsoft.DirectX.DirectSound.dll

#include <conio.h>

#include <math.h>

#include <process.h>

#define N 5//N个点

#define T 3 //T次拟合

#define W 1//权函数

#define PRECISION 0.00001

float pow_n(float a,int n)

{

int i

if(n==0)

return(1)

float res=a

for(i=1i<ni++)

{

res*=a

}

return(res)

}

void mutiple(float a[][N],float b[][T+1],float c[][T+1])

{

float res=0

int i,j,k

for(i=0i<T+1i++)

for(j=0j<T+1j++)

{

res=0

for(k=0k<Nk++)

{

res+=a[i][k]*b[k][j]

c[i][j]=res

}

}

}

void matrix_trans(float a[][T+1],float b[][N])

{

int i,j

for(i=0i<Ni++)

{

for(j=0j<T+1j++)

{

b[j][i]=a[i][j]

}

}

}

void init(float x_y[][2],int n)

{

int i

printf("请输入%d个已知点:\n",N)

for(i=0i<ni++)

{

printf("(x%d y%d):",i,i)

scanf("%f %f",&x_y[i][0],&x_y[i][1])

}

}

void get_A(float matrix_A[][T+1],float x_y[][2],int n)

{

int i,j

for(i=0i<Ni++)

{

for(j=0j<T+1j++)

{

matrix_A[i][j]=W*pow_n(x_y[i][0],j)

}

}

}

void print_array(float array[][T+1],int n)

{

int i,j

for(i=0i<ni++)

{

for(j=0j<T+1j++)

{

printf("%-g",array[i][j])

}

printf("\n")

}

}

void convert(float argu[][T+2],int n)

{

int i,j,k,p,t

float rate,temp

for(i=1i<ni++)

{

for(j=ij<nj++)

{

if(argu[i-1][i-1]==0)

{

for(p=ip<np++)

{

if(argu[p][i-1]!=0)

break

}

if(p==n)

{

printf("方程组无解!\n")

exit(0)

}

for(t=0t<n+1t++)

{

temp=argu[i-1][t]

argu[i-1][t]=argu[p][t]

argu[p][t]=temp

}

}

rate=argu[j][i-1]/argu[i-1][i-1]

for(k=i-1k<n+1k++)

{

argu[j][k]-=argu[i-1][k]*rate

if(fabs(argu[j][k])<=PRECISION)

argu[j][k]=0

}

}

}

}

void compute(float argu[][T+2],int n,float root[])

{

int i,j

float temp

for(i=n-1i>=0i--)

{

temp=argu[i][n]

for(j=n-1j>ij--)

{

temp-=argu[i][j]*root[j]

}

root[i]=temp/argu[i][i]

}

}

void get_y(float trans_A[][N],float x_y[][2],float y[],int n)

{

int i,j

float temp

for(i=0i<ni++)

{

temp=0

for(j=0j<Nj++)

{

temp+=trans_A[i][j]*x_y[j][1]

}

y[i]=temp

}

}

void cons_formula(float coef_A[][T+1],float y[],float coef_form[][T+2])

{

int i,j

for(i=0i<T+1i++)

{

for(j=0j<T+2j++)

{

if(j==T+1)

coef_form[i][j]=y[i]

else

coef_form[i][j]=coef_A[i][j]

}

}

}

void print_root(float a[],int n)

{

int i,j

printf("%d个点的%d次拟合的多项式系数为:\n",N,T)

for(i=0i<ni++)

{

printf("a[%d]=%g,",i+1,a[i])

}

printf("\n")

printf("拟合曲线方程为:\ny(x)=%g",a[0])

for(i=1i<ni++)

{

printf(" + %g",a[i])

for(j=0j<ij++)

{

printf("*X")

}

}

printf("\n")

}

void process()

{

float x_y[N][2],matrix_A[N][T+1],trans_A[T+1][N],coef_A[T+1][T+1],coef_formu[T+1][T+2],y[T+1],a[T+1]

init(x_y,N)

get_A(matrix_A,x_y,N)

printf("矩阵A为:\n")

print_array(matrix_A,N)

matrix_trans(matrix_A,trans_A)

mutiple(trans_A,matrix_A,coef_A)

printf("法矩阵为:\n")

print_array(coef_A,T+1)

get_y(trans_A,x_y,y,T+1)

cons_formula(coef_A,y,coef_formu)

convert(coef_formu,T+1)

compute(coef_formu,T+1,a)

print_root(a,T+1)

}

void main()

{

process()

}

]]>

</Content>

<PostDateTime>2007-4-19 19:23:57</PostDateTime>

</Reply>

<Reply>

<PostUserNickName></PostUserNickName>

<rank>一级(初级)</rank>

<ranknum>user1</ranknum>

<credit>100</credit>

<ReplyID>40389872</ReplyID>

<TopicID>5478010</TopicID>

<PostUserId>1526752</PostUserId>

<PostUserName>jiangxc2004</PostUserName>

<Point>0</Point>

<Content>

<![CDATA[

你可以改一下

不从终端输入,直接在程序中给出参数

请输入5个已知点:

(x0 y0):-2 -0.1

(x1 y1):-1 0.1

(x2 y2):0 0.4

(x3 y3):1 0.9

(x4 y4):2 1.6

矩阵A为:

1 -2 4 -8

1 -1 1 -1

1 0 0 0

1 1 1 1

1 2 4 8

法矩阵为:

5 0 10 0

0 10 0 34

10 0 34 0

0 34 0 130

5个点的3次拟合的多项式系数为:

a[1]=0.408571, a[2]=0.391667, a[3]=0.0857143, a[4]=0.00833333,

拟合曲线方程为:

y(x)=0.408571 + 0.391667*X + 0.0857143*X*X + 0.00833333*X*X*X

]]>

</Content>

<PostDateTime>2007-4-19 19:26:11</PostDateTime>

</Reply>

<Reply>

<PostUserNickName></PostUserNickName>

<rank>一级(初级)</rank>

<ranknum>user1</ranknum>

<credit>100</credit>

<ReplyID>40390406</ReplyID>

<TopicID>5478010</TopicID>

<PostUserId>1526752</PostUserId>

<PostUserName>jiangxc2004</PostUserName>

<Point>0</Point>

<Content>

<![CDATA[

这样就可以直接调用process()函数了!

二次拟合的话就把宏 T 成2

拟合点的数目 N 也可以修改!

也可以去到注释的部分进行返回值的调用!

另外,团IDC网上有许多产品团购,便宜有口碑