首页 > 开发 > .Net > 正文

VB.NET实现DirectSound9 (7) 录音

2020-02-03 15:23:38
字体:
来源:转载
供稿:网友
  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • 关键字: 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 以清理组件列表。

    protected overloads overrides sub dispose(byval disposing as boolean)

    if disposing then

    if not (components is nothing) then

    components.dispose()

    end if

    end if

    mybase.dispose(disposing)

    end sub



    'windows 窗体设计器所必需的

    private components as system.componentmodel.icontainer



    '注意: 以下过程是 windows 窗体设计器所必需的

    '可以使用 windows 窗体设计器修改此过程。

    '不要使用代码编辑器修改它。

    friend withevents listbox1 as system.windows.forms.listbox

    friend withevents listbox2 as system.windows.forms.listbox

    friend withevents textbox1 as system.windows.forms.textbox

    friend withevents button1 as system.windows.forms.button

    friend withevents button2 as system.windows.forms.button

    friend withevents button3 as system.windows.forms.button

    friend withevents button4 as system.windows.forms.button

    friend withevents label1 as system.windows.forms.label

    <system.diagnostics.debuggerstepthrough()> private sub initializecomponent()

    me.listbox1 = new system.windows.forms.listbox

    me.listbox2 = new system.windows.forms.listbox

    me.textbox1 = new system.windows.forms.textbox

    me.button1 = new system.windows.forms.button

    me.button2 = new system.windows.forms.button

    me.button3 = new system.windows.forms.button

    me.button4 = new system.windows.forms.button

    me.label1 = new system.windows.forms.label

    me.suspendlayout()

    '

    'listbox1

    '

    me.listbox1.itemheight = 12

    me.listbox1.location = new system.drawing.point(16, 16)

    me.listbox1.name = "listbox1"

    me.listbox1.size = new system.drawing.size(216, 64)

    me.listbox1.tabindex = 1

    '

    'listbox2

    '

    me.listbox2.itemheight = 12

    me.listbox2.location = new system.drawing.point(16, 88)

    me.listbox2.name = "listbox2"

    me.listbox2.size = new system.drawing.size(216, 100)

    me.listbox2.tabindex = 2

    '

    'textbox1

    '

    me.textbox1.location = new system.drawing.point(24, 208)

    me.textbox1.name = "textbox1"

    me.textbox1.size = new system.drawing.size(208, 21)

    me.textbox1.tabindex = 3

    me.textbox1.text = "c:/0001.wav"

    '

    'button1

    '

    me.button1.location = new system.drawing.point(24, 240)

    me.button1.name = "button1"

    me.button1.size = new system.drawing.size(64, 24)

    me.button1.tabindex = 4

    me.button1.text = "recode"

    '

    'button2

    '

    me.button2.location = new system.drawing.point(96, 240)

    me.button2.name = "button2"

    me.button2.size = new system.drawing.size(72, 24)

    me.button2.tabindex = 5

    me.button2.text = "stop"

    '

    'button3

    '

    me.button3.location = new system.drawing.point(176, 240)

    me.button3.name = "button3"

    me.button3.size = new system.drawing.size(80, 24)

    me.button3.tabindex = 6

    me.button3.text = "play"

    '

    'button4

    '

    me.button4.location = new system.drawing.point(272, 240)

    me.button4.name = "button4"

    me.button4.size = new system.drawing.size(96, 24)

    me.button4.tabindex = 7

    me.button4.text = "disposebuff"

    '

    'label1

    '

    me.label1.location = new system.drawing.point(248, 16)

    me.label1.name = "label1"

    me.label1.size = new system.drawing.size(264, 160)

    me.label1.tabindex = 8

    me.label1.text = "声音格式"

    '

    'form1

    '

    me.autoscalebasesize = new system.drawing.size(6, 14)

    me.clientsize = new system.drawing.size(536, 277)

    me.controls.add(me.label1)

    me.controls.add(me.button4)

    me.controls.add(me.button3)

    me.controls.add(me.button2)

    me.controls.add(me.button1)

    me.controls.add(me.textbox1)

    me.controls.add(me.listbox2)

    me.controls.add(me.listbox1)

    me.name = "form1"

    me.text = "form1"

    me.resumelayout(false)



    end sub



    #end region





    private sub form1_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load

    dim info as deviceinformation

    '''先得到可以使用的信息

    ''' 设备信息由这个集合提供

    for each info in caplist

    listbox1.items.add(info.description)

    next

    end sub



    private sub listbox1_selectedindexchanged(byval sender as system.object, byval e as system.eventargs) handles listbox1.selectedindexchanged

    '利用选择的设备

    cap = new capture(caplist(listbox1.selectedindex).driverguid)

    '''枚举支持的格式

    '''尝试各种格式,只能用try catch 了

    dim fmt as waveformat

    dim testcapture as capturebuffer

    dim capturedesc as new capturebufferdescription

    'listbox2.items.clear()

    scanavailableinputformats(cap)

    fillformatlistbox()

    end sub

    sub scanavailableinputformats(byval cap as capture)

    '-----------------------------------------------------------------------------

    ' name: scanavailableinputformats()

    ' desc: tests to see if 20 different standard wave formats are supported by

    ' the capture device

    '-----------------------------------------------------------------------------

    dim format as new waveformat

    dim dscheckboxd as new capturebufferdescription

    dim pdscapturebuffer as capturebuffer = nothing



    ' this might take a second or two, so throw up the hourglass

    cursor = cursors.waitcursor



    format.formattag = waveformattag.pcm



    ' try 20 different standard formats to see if they are supported

    dim iindex as integer

    for iindex = 0 to 19

    getwaveformatfromindex(iindex, format)



    ' to test if a capture format is supported, try to create a

    ' new capture buffer using a specific format. if it works

    ' then the format is supported, otherwise not.

    dscheckboxd.bufferbytes = format.averagebytespersecond

    dscheckboxd.format = format



    try

    pdscapturebuffer = new capturebuffer(dscheckboxd, cap)

    inputformatsupported(iindex) = true

    catch

    inputformatsupported(iindex) = false

    end try

    pdscapturebuffer.dispose()

    next iindex

    cursor = cursors.default

    end sub 'scanavailableinputformats

    private sub getwaveformatfromindex(byval index as integer, byref format as waveformat)

    '-----------------------------------------------------------------------------

    ' name: getwaveformatfromindex()

    ' desc: returns 20 different wave formats based on index

    '-----------------------------------------------------------------------------

    dim samplerate as integer = index / 4

    dim itype as integer = index mod 4



    select case samplerate

    case 0

    format.samplespersecond = 48000

    case 1

    format.samplespersecond = 44100

    case 2

    format.samplespersecond = 22050

    case 3

    format.samplespersecond = 11025

    case 4

    format.samplespersecond = 8000

    end select



    select case itype

    case 0

    format.bitspersample = 8

    format.channels = 1

    case 1

    format.bitspersample = 16

    format.channels = 1

    case 2

    format.bitspersample = 8

    format.channels = 2

    case 3

    format.bitspersample = 16

    format.channels = 2

    end select



    format.blockalign = cshort(format.channels * (format.bitspersample / 8))

    format.averagebytespersecond = format.blockalign * format.samplespersecond

    end sub 'getwaveformatfromindex

    private shared function convertwaveformattostring(byval format as waveformat) as string

    '-----------------------------------------------------------------------------

    ' name: convertwaveformattostring()

    ' desc: converts a wave format to a text string

    '-----------------------------------------------------------------------------

    return format.samplespersecond.tostring() + " hz, " + format.bitspersample.tostring() + "-bit " + iif(format.channels = 1, "mono", "stereo")

    end function 'convertwaveformattostring

    sub fillformatlistbox()

    '-----------------------------------------------------------------------------

    ' name: fillformatlistbox()

    ' desc: fills the format list box based on the availible formats

    '-----------------------------------------------------------------------------

    dim info as new formatinfo

    dim strformatname as string = string.empty

    dim format as new waveformat



    dim iindex as integer

    for iindex = 0 to inputformatsupported.length - 1

    if true = inputformatsupported(iindex) then

    ' turn the index into a waveformat then turn that into a

    ' string and put the string in the listbox

    getwaveformatfromindex(iindex, format)

    info.format = format

    formats.add(info)

    end if

    next iindex

    listbox2.datasource = formats

    end sub 'fillformatlistbox

    sub createriff()

    '*************************************************************************

    '

    '

    ' 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.

    wavefile = new filestream(textbox1.text, filemode.create)

    writer = new binarywriter(wavefile)



    ' set up file with riff chunk info.

    dim chunkriff as char() = {"r", "i", "f", "f"}

    dim chunktype as char() = {"w", "a", "v", "e"}

    dim chunkfmt as char() = {"f", "m", "t", " "}

    dim chunkdata as char() = {"d", "a", "t", "a"}



    dim shpad as short = 1 ' file padding

    dim nformatchunklength as integer = &h10 ' format chunk length.

    dim nlength as integer = 0 ' file length, minus first 8 bytes of riff description. this will be filled in later.

    dim shbytespersample as short = 0 ' bytes per sample.

    ' figure out how many bytes there will be per sample.

    if 8 = inputformat.bitspersample and 1 = inputformat.channels then

    shbytespersample = 1

    elseif 8 = inputformat.bitspersample and 2 = inputformat.channels or (16 = inputformat.bitspersample and 1 = inputformat.channels) then

    shbytespersample = 2

    elseif 16 = inputformat.bitspersample and 2 = inputformat.channels then

    shbytespersample = 4

    end if

    ' fill in the riff info for the wave file.

    writer.write(chunkriff)

    writer.write(nlength)

    writer.write(chunktype)



    ' fill in the format info for the wave file.

    writer.write(chunkfmt)

    writer.write(nformatchunklength)

    writer.write(shpad)

    writer.write(inputformat.channels)

    writer.write(inputformat.samplespersecond)

    writer.write(inputformat.averagebytespersecond)

    writer.write(shbytespersample)

    writer.write(inputformat.bitspersample)



    ' now fill in the data chunk.

    writer.write(chunkdata)

    writer.write(cint(0)) ' the sample length will be written in later.

    end sub 'createriff

    sub createcapturebuffer()

    '-----------------------------------------------------------------------------

    ' name: createcapturebuffer()

    ' desc: creates a capture buffer and sets the format

    '-----------------------------------------------------------------------------

    dim dscheckboxd as new capturebufferdescription



    if not nothing is applicationnotify then

    applicationnotify.dispose()

    applicationnotify = nothing

    end if

    if not nothing is applicationbuffer then

    applicationbuffer.dispose()

    applicationbuffer = nothing

    end if



    if 0 = inputformat.channels then

    return

    end if

    ' set the notification size

    notifysize = iif(1024 > inputformat.averagebytespersecond / 8, 1024, inputformat.averagebytespersecond / 8)

    notifysize -= notifysize mod inputformat.blockalign



    ' set the buffer sizes

    capturebuffersize = notifysize * numberrecordnotifications



    ' create the capture buffer

    dscheckboxd.bufferbytes = capturebuffersize

    inputformat.formattag = waveformattag.pcm

    dscheckboxd.format = inputformat ' set the format during creatation

    applicationbuffer = new capturebuffer(dscheckboxd, cap)

    nextcaptureoffset = 0



    initnotifications()

    end sub 'createcapturebuffer



    sub initnotifications()

    '-----------------------------------------------------------------------------

    ' name: initnotifications()

    ' desc: inits the notifications on the capture buffer which are handled

    ' in the notify thread.

    '-----------------------------------------------------------------------------

    if nothing is applicationbuffer then

    throw new argumentnullexception

    end if

    ' create a thread to monitor the notify events

    if nothing is notifythread then

    notifythread = new thread(new threadstart(addressof waitthread))

    notifythread.start()



    ' create a notification event, for when the sound stops playing

    notificationevent = new autoresetevent(false)

    end if



    ' setup the notification positions

    dim i as integer

    for i = 0 to numberrecordnotifications - 1

    positionnotify(i).offset = notifysize * i + notifysize - 1

    positionnotify(i).eventnotifyhandle = notificationevent.handle

    next i



    applicationnotify = new notify(applicationbuffer)



    ' tell directsound when to notify the app. the notification will come in the from

    ' of signaled events that are handled in the notify thread.

    applicationnotify.setnotificationpositions(positionnotify, numberrecordnotifications)

    end sub 'initnotifications

    private sub waitthread()

    while created

    'sit here and wait for a message to arrive

    notificationevent.waitone(timeout.infinite, true)

    recordcaptureddata()

    end while

    end sub 'waitthread

    sub recordcaptureddata()

    '-----------------------------------------------------------------------------

    ' name: recordcaptureddata()

    ' desc: copies data from the capture buffer to the output buffer

    '-----------------------------------------------------------------------------

    dim capturedata as byte() = nothing

    dim readpos as integer

    dim capturepos as integer

    dim locksize as integer



    if nothing is applicationbuffer or nothing is wavefile then

    return

    end if

    applicationbuffer.getcurrentposition(capturepos, readpos)

    locksize = readpos - nextcaptureoffset

    if locksize < 0 then

    locksize += capturebuffersize

    end if

    ' block align lock size so that we are always write on a boundary

    locksize -= locksize mod notifysize



    if 0 = locksize then

    return

    end if

    ' read the capture buffer.

    capturedata = ctype(applicationbuffer.read(nextcaptureoffset, gettype(byte), lockflag.none, locksize), byte())



    ' write the data into the wav file

    writer.write(capturedata, 0, capturedata.length)



    ' update the number of samples, in bytes, of the file so far.

    samplecount += capturedata.length



    ' move the capture offset along

    nextcaptureoffset += capturedata.length

    nextcaptureoffset = nextcaptureoffset mod capturebuffersize ' circular buffer

    end sub 'recordcaptureddata



    private sub listbox2_selectedindexchanged(byval sender as system.object, byval e as system.eventargs) handles listbox2.selectedindexchanged

    inputformat = ctype(formats(listbox2.selectedindex), formatinfo).format

    label1.text = ctype(listbox2.selecteditem, formatinfo).format.tostring

    end sub

    sub startorstoprecord(byval startrecording as boolean)

    '-----------------------------------------------------------------------------

    ' name: startorstoprecord()

    ' desc: starts or stops the capture buffer from recording

    '-----------------------------------------------------------------------------

    if startrecording then

    ' create a capture buffer, and tell the capture

    ' buffer to start recording

    createcapturebuffer()

    applicationbuffer.start(true)

    else

    ' stop the capture and read any data that

    ' was not caught by a notification

    if nothing is applicationbuffer then

    return

    end if

    ' stop the buffer, and read any data that was not

    ' caught by a notification

    applicationbuffer.stop()



    recordcaptureddata()



    writer.seek(4, seekorigin.begin) ' seek to the length descriptor of the riff file.

    writer.write(cint(samplecount + 36)) ' write the file length, minus first 8 bytes of riff description.

    writer.seek(40, seekorigin.begin) ' seek to the data length descriptor of the riff file.

    writer.write(samplecount) ' write the length of the sample data in bytes.

    writer.close() ' close the file now.

    writer = nothing ' set the writer to null.

    wavefile = nothing ' set the filestream to null.

    end if

    end sub 'startorstoprecord





    private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click

    createriff()

    startorstoprecord(true)



    end sub



    private sub button2_click(byval sender as system.object, byval e as system.eventargs) handles button2.click

    startorstoprecord(false)



    end sub



    private sub button3_click(byval sender as system.object, byval e as system.eventargs) handles button3.click

    devplay.setcooperativelevel(me, cooperativelevel.priority)

    bufplay = new secondarybuffer(textbox1.text, devplay)

    bufplay.play(0, bufferplayflags.default)



    end sub



    private sub button4_click(byval sender as system.object, byval e as system.eventargs) handles button4.click

    bufplay.stop()

    bufplay.dispose()



    end sub

    private sub mainform_closing(byval sender as object, byval e as system.componentmodel.canceleventargs) handles mybase.closing

    if not nothing is notificationevent then

    notificationevent.set()

    end if

    if not nothing is applicationbuffer then

    if applicationbuffer.capturing then

    startorstoprecord(false)

    end if

    end if

    end

    end sub 'mainform_closing



    end class



    =======================================
    ok directsound最后的部分写完了.总体感觉微软的封装还是有道理的

    既要体现出易用性,而且还要充分发挥硬件的能力.如果你觉得某些特性用不到(很多其实都用不到)

    你可以自己在directsound的基础上制作自己的声效引擎方便自己使用.

    现在也存在不少优秀的声效引擎(在模拟器里面经常见到),至于是否自己开发,就要看你的兴趣了

    如果您对directsound感兴趣,可以来我的blog或者留言版发表看法.

    http://blog.csdn.net/a11s

    下一个目标,托管的directplay
    (可能跟dsound中间插入ddraw一样,dplay的时候顺便搞定dinput)

    ==========end directsound 9==================



    发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表