打印本文 打印本文 关闭窗口 关闭窗口
VB.NET实现DirectSound9 (7) 录音
作者:武汉SEO闵涛  文章来源:敏韬网  点击数8740  更新时间:2009/4/23 19:00:44  文章录入:mintao  责任编辑:mintao

关键字: VB.NET DirectX 9 DirectSound 录音  riff文件格式             作者:董含君

下午看了微软提供的例子,居然把录音定位成Beginner级别
晕哦,虽说我认为这个例子是微软提供的最”直接”的例子,但是步骤超多.而且还牵扯到多线程开辟缓冲区回调riff文件格式 IO 输出等等.由于录音的复杂性,以及微软这个例子的直接性,坚持原创的我最终还是复制了大量的代码.(希望不要骂我....)

OK,先来说录音的步骤,里面牵扯到riff或者使用技巧的地方,有注释.我仅仅说步骤.附带截图一张

程序运行截图

首先需要说明与往常不同的概念

1 声卡(或者windows)把音频设备分成2个部分,一个是录音设备(Capture),另一个是回放设备(PlayBack)

2 前面我们用的是Device创建回放设备,这次需要使用Capture创建录音设备,录音设备不像回放,设备的能力往往很关键(回放虽说也很关键,但是设备能力基本上差别不大),所以不能单单使用一个默认就行,需要用户指定,甚至用枚举的办法逐个测试性能(看看是否支持这种格式)

3 利用    Dim CapList As New CaptureDevicesCollection
得到list之后
        Dim info As DeviceInformation
        ''''''''''''先得到可以使用的信息
        '''''''''''' 设备信息由这个集合提供
        For Each info In CapList
            ListBox1.Items.Add(info.Description)
        Next
枚举出所有设备

4 创建Capture的时候,需要指定使用那个设备了,(回放的时候也可以指定,但是我们用的是默认的)
        ''''利用选择的设备
        Cap = New Capture(CapList(ListBox1.SelectedIndex).DriverGuid)

5 尝试所有支持的类型
就是一个for 循环里面嵌套下面的try语句
try
Cap=new capture(....)
catch
''''''''''''''''''''失败的时候继续尝试下一个
end try

6 完成设备的准备工作之后,DirectSound初始化完成

7 录音,
第一步创建riff(理解成riff即可)
第二步创建录音用的缓冲区(包括文件缓冲区以及CaptureBuffer)
第三步创建新的线程用于捕获数据

8 停止
停止capture
讲缓冲区内容写入磁盘
修改riff的文件信息
释放资源

9 播放,利用最简单的办法播放文件


大体步骤就这些,录音的时候函数之间的关系比较复杂.但是没有更简单的办法.

注释比较详细,关于riff的文件操作方法内部也有注释.或者直接复制到你的程序直接用也行.

以下是源代码

Imports Microsoft.DirectX.DirectSound

Imports System.IO

Imports System.Threading

 

Public Class Form1

    Inherits System.Windows.Forms.Form

 

    Private Structure FormatInfo

        Public format As WaveFormat

 

        Public Overrides Function ToString() As String

            Return ConvertWaveFormatToString(format)

        End Function ''''ToString

    End Structure ''''FormatInfo

 

    Dim DevPlay As New Device

    Dim BufPlay As SecondaryBuffer

 

    Dim Formats As New ArrayList

    Dim Cap As Capture

    Dim CapList As New CaptureDevicesCollection

    Private InputFormatSupported(19) As Boolean

    Public InputFormat As WaveFormat

    Private WaveFile As FileStream = Nothing

    Private Writer As BinaryWriter = Nothing

    Public applicationNotify As Notify = Nothing

    Public applicationBuffer As CaptureBuffer = Nothing

    Public NotifySize As Integer = 0

    Private NotifyThread As Thread = Nothing

    Public NotificationEvent As AutoResetEvent = Nothing

    Public PositionNotify(NumberRecordNotifications) As BufferPositionNotify

    Public Const NumberRecordNotifications As Integer = 16

    Public CaptureBufferSize As Integer = 0

    Public NextCaptureOffset As Integer = 0

    Private SampleCount As Integer = 0

 

 

#Region " Windows 窗体设计器生成的代码 "

 

    Public Sub New()

        MyBase.New()

 

        ''''该调用是 Windows 窗体设计器所必需的。

        InitializeComponent()

 

        ''''在 InitializeComponent() 调用之后添加任何初始化

 

    End Sub

 

    ''''窗体重写 dispose 以清理组件列表。

    Prote

[1] [2] [3] [4] [5] [6] [7] [8] [9] [10]  ...  下一页 >> 

打印本文 打印本文 关闭窗口 关闭窗口