Directx 将directdx转换为slimdx

Directx 将directdx转换为slimdx,directx,slimdx,Directx,Slimdx,嘿,我在从directx转换到slimdx时遇到问题。我的操作系统是windows 7 64位。下面是无法工作的旧directx代码: Imports Microsoft.DirectX.DirectSound Imports System.Threading Imports System.Collections.Specialized Public Class VU_Sound_Form Private Const SAMPLES As Integer = 8 Privat

嘿,我在从directx转换到slimdx时遇到问题。我的操作系统是windows 7 64位。下面是无法工作的旧directx代码:

Imports Microsoft.DirectX.DirectSound
Imports System.Threading
Imports System.Collections.Specialized

Public Class VU_Sound_Form

    Private Const SAMPLES As Integer = 8
    Private Shared SAMPLE_FORMAT_ARRAY As Integer() = {SAMPLES, 2, 1}
    Public Shared audioDevices As CaptureDevicesCollection
    Private Shared m_deviceNames As StringCollection

    Private deviceIndex As Integer = -1
    Private buffer As Microsoft.DirectX.DirectSound.CaptureBuffer
    Private liveVolumeThread As System.Threading.Thread
    Private m_frameDelay As Integer = 20
    Private Range_Division As Integer = 10

    'FrameDelay - the time in milliseconds between animation frames, measured in milliseconds. 
    'Values between 10 and 30 generally give good results, default is 20.


    Private Sub VU_Sound_Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim audioDevices As New CaptureDevicesCollection
        Dim x As Integer = 0
        MsgBox(audioDevices.Count.ToString())
        While x < audioDevices.Count
            ComboBox1.Items.Add(audioDevices.Item(x).Description)
            '  x = x + 1
        End While
        ComboBox1.SelectedIndex = 0
        Start()
    End Sub


    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Start()
    End Sub


    Public Sub Start()
        [Stop]()
        Dim audioDevices As New CaptureDevicesCollection
        deviceIndex = ComboBox1.SelectedIndex
        If deviceIndex <> -1 Then
            ' initialize the capture buffer and start the animation thread
            Dim cap As New Capture(audioDevices(deviceIndex).DriverGuid)
            Dim desc As New CaptureBufferDescription()
            Dim wf As New WaveFormat()
            wf.BitsPerSample = 16
            wf.SamplesPerSecond = 44100
            wf.Channels = 2
            wf.BlockAlign = CShort(wf.Channels * wf.BitsPerSample / 8)
            wf.AverageBytesPerSecond = wf.BlockAlign * wf.SamplesPerSecond
            wf.FormatTag = WaveFormatTag.Pcm

            desc.Format = wf
            desc.BufferBytes = SAMPLES * wf.BlockAlign

            buffer = New Microsoft.DirectX.DirectSound.CaptureBuffer(desc, cap)
            buffer.Start(True)

            ' Start a seperate thread to read the buffer and update the progress bars
            liveVolumeThread = New Thread(AddressOf updateProgress)  'Thread starts at updateProgress
            Control.CheckForIllegalCrossThreadCalls = False ' This is needed otherwise the form will not update
            liveVolumeThread.Priority = ThreadPriority.Lowest ' Thread works in the background
            liveVolumeThread.Start()

        End If
    End Sub

    Public Sub [Stop]()
        If liveVolumeThread IsNot Nothing Then
            liveVolumeThread.Abort()
            liveVolumeThread.Join()
            liveVolumeThread = Nothing
        End If

        If buffer IsNot Nothing Then
            If buffer.Capturing Then
                buffer.[Stop]()
            End If

            buffer.Dispose()
            buffer = Nothing
        End If
    End Sub



    Public Sub updateProgress()

        While True
            Dim tempFrameDelay As Integer = m_frameDelay
            Dim samples__1 As Array = buffer.Read(0, GetType(Int16), LockFlag.FromWriteCursor, SAMPLE_FORMAT_ARRAY)

            Dim leftGoal As Integer = 0
            Dim rightGoal As Integer = 0

            ' Convert the 8 samples to positive values and sum them togather 
            For i As Integer = 0 To SAMPLES - 1
                If CType(samples__1.GetValue(i, 0, 0), Int16) < 0 Then
                    leftGoal -= CType(samples__1.GetValue(i, 0, 0), Int16)
                Else
                    leftGoal += CType(samples__1.GetValue(i, 0, 0), Int16)
                End If
                If CType(samples__1.GetValue(i, 1, 0), Int16) < 0 Then
                    rightGoal -= CType(samples__1.GetValue(i, 1, 0), Int16)
                Else
                    rightGoal += CType(samples__1.GetValue(i, 1, 0), Int16)
                End If
            Next

            ' Calculate the average of the 8 samples
            leftGoal = CInt(Math.Abs(leftGoal \ SAMPLES))
            rightGoal = CInt(Math.Abs(rightGoal \ SAMPLES))

            ' Convert values to deecibels
            If leftGoal = 0 Then leftGoal = 1
            If rightGoal = 0 Then rightGoal = 1
            leftGoal = (100 + (20 * Math.Log10(leftGoal / 32768)))
            rightGoal = (100 + (20 * Math.Log10(rightGoal / 32768)))

            'By adding 100 sets the display range from 0 to 100 
            'By limiting the progreess bars to 74-100 gives a viewed range of +10dB to -26dB




            'Trap the range between 74-100, giving a 26dB meter
            If leftGoal < 74 Then leftGoal = 74
            If rightGoal < 74 Then rightGoal = 74 ' Set the display range to minimum -10dB
            If leftGoal > 100 Then leftGoal = 100
            If rightGoal > 100 Then rightGoal = 100

            Dim range1 As Double = leftGoal - ProgressBar1.Value ' calculates the difference between new and the current progress bar value 
            Dim range2 As Double = rightGoal - ProgressBar2.Value

            ' Assign the exact current value of the progress bar
            Dim exactValue1 As Double = ProgressBar1.Value
            Dim exactValue2 As Double = ProgressBar2.Value

            Dim stepSize1 As Double = range1 / Range_Division
            Dim absStepSize1 As Double = Math.Abs(stepSize1)

            Dim stepSize2 As Double = range2 / Range_Division
            Dim absStepSize2 As Double = Math.Abs(stepSize2)

            If ProgressBar1.Value < leftGoal Then    ' Display the peak
                ProgressBar1.Value = leftGoal
            End If

            If ProgressBar1.Value > leftGoal Then ' decrement the value by the Range_Division
                If absStepSize1 < Math.Abs(leftGoal - ProgressBar1.Value) Then
                    exactValue1 += stepSize1
                    ProgressBar1.Value = Math.Truncate(exactValue1)
                Else
                    ProgressBar1.Value = leftGoal
                End If
            End If

            If ProgressBar2.Value < rightGoal Then
                ProgressBar2.Value = rightGoal
            End If

            If ProgressBar2.Value > rightGoal Then ' decrement the value by the Range_Division
                If absStepSize2 < Math.Abs(rightGoal - ProgressBar2.Value) Then
                    exactValue2 += stepSize2
                    ProgressBar2.Value = Math.Truncate(exactValue2)
                Else
                    ProgressBar2.Value = rightGoal
                End If
            End If
            Thread.Sleep(m_frameDelay)
        End While
    End Sub


    Private Sub ProgressBar1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProgressBar1.Click

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click



    End Sub
End Class
导入Microsoft.DirectX.DirectSound
导入系统线程
导入System.Collections.Specialized
公开课预习表
私有常量样本为整数=8
私有共享样本\格式\数组为整数()={SAMPLES,2,1}
作为CaptureDevicesCollection的公共共享音频设备
作为StringCollection的专用共享m_设备名称
私有设备索引为整数=-1
专用缓冲区,如Microsoft.DirectX.DirectSound.CaptureBuffer
私有liveVolumeThread为System.Threading.Thread
专用m_帧延迟为整数=20
专用范围除以整数=10
'FrameDelay-动画帧之间的时间(以毫秒为单位),以毫秒为单位。
“10到30之间的值通常会给出良好的结果,默认值为20。
私有子VU_Sound_Form_Load(ByVal sender作为对象,ByVal e作为System.EventArgs)处理Me.Load
Dim音频设备作为新的CaptureDevicesCollection
尺寸x为整数=0
MsgBox(audioDevices.Count.ToString())
而x<音频设备。计数
ComboBox1.Items.Add(audioDevices.Item(x).Description)
'x=x+1
结束时
ComboBox1.SelectedIndex=0
开始()
端接头
私有子Combox1\u SelectedIndexChanged(ByVal sender作为对象,ByVal e作为System.EventArgs)处理Combox1.SelectedIndexChanged
开始()
端接头
公共子启动()
[停止]()
Dim音频设备作为新的CaptureDevicesCollection
deviceIndex=ComboBox1.SelectedIndex
如果设备索引为-1,则
'初始化捕获缓冲区并启动动画线程
作为新捕获的调暗盖(音频设备(deviceIndex).DriverGuid)
作为新CaptureBufferDescription()的Dim desc
作为新波形的Dim wf()
wf.BitsPerSample=16
wf.SamplesPerSecond=44100
wf.Channels=2
wf.BlockAlign=CShort(wf.Channels*wf.BitsPerSample/8)
wf.AverageBytesPerSecond=wf.BlockAlign*wf.SamplesPerSecond
wf.FormatTag=WaveFormatTag.Pcm
描述格式=wf
desc.BufferBytes=SAMPLES*wf.BlockAlign
buffer=新的Microsoft.DirectX.DirectSound.CaptureBuffer(desc,cap)
buffer.Start(True)
'启动一个单独的线程来读取缓冲区并更新进度条
liveVolumeThread=新线程(updateProgress的地址)'线程从updateProgress开始
Control.CheckForIllegalCrossThreadCalls=False'这是必需的,否则表单将不会更新
liveVolumeThread.Priority=ThreadPriority.lower“线程在后台工作
liveVolumeThread.Start()
如果结束
端接头
公共分[站]()
如果liveVolumeThread不是什么,那么
liveVolumeThread.Abort()
liveVolumeThread.Join()
liveVolumeThread=无
如果结束
如果缓冲区不是空的,那么
如果是缓冲区,那么捕获
缓冲区。[停止]()
如果结束
buffer.Dispose()
缓冲区=无
如果结束
端接头
公共子updateProgress()
虽然是真的
Dim tempFrameDelay作为整数=m_frameDelay
Dim samples\uuu 1 As Array=buffer.Read(0,GetType(Int16),LockFlag.FromWriteCursor,SAMPLE\uFormat\uArray)
Dim leftGoal作为整数=0
Dim rightGoal作为整数=0
'将8个样本转换为正值,并将它们相加
对于i,作为整数=0到采样数-1
如果CType(samples__1.GetValue(i,0,0),Int16)<0,那么
leftGoal-=CType(示例1.GetValue(i,0,0),Int16)
其他的
leftGoal+=CType(示例1.GetValue(i,0,0),Int16)
如果结束
如果CType(samples_uu1.GetValue(i,1,0),Int16)<0,那么
rightGoal-=CType(示例1.GetValue(i,1,0),Int16)
其他的
rightGoal+=CType(示例1.GetValue(i,1,0),Int16)
如果结束
下一个
'计算8个样本的平均值
leftGoal=CInt(Math.Abs(leftGoal\SAMPLES))
rightGoal=CInt(Math.Abs(rightGoal\SAMPLES))
'将值转换为Deecibel
如果leftGoal=0,则leftGoal=1
如果rightGoal=0,则rightGoal=1
leftGoal=(100+(20*Math.Log10(leftGoal/32768)))
rightGoal=(100+(20*Math.Log10(rightGoal/32768)))
'通过添加100将显示范围设置为0到100
'通过将进度条限制在74-100,可提供+10dB至-26dB的查看范围
'将范围限制在74-100之间,给出一个26分贝的米
如果leftGoal<74,则leftGoal=74
如果rightGoal<74,则rightGoal=74'将显示范围设置为最小-10dB
如果leftGoal>100,则leftGoal=100
如果rightGoal>100,则rightGoal=100
Dim range1 As Double=leftGoal-ProgressBar1.Value'计算新进度条值与当前进度条值之间的差值
Dim range2为Double=rightGoal-ProgressBar2.Value
'指定进度条的确切当前值
将exactValue1标注为Double=ProgressBar1.Value
将exactValue2标注为Double=ProgressBar2.Value
尺寸stepSize1为双=量程1/量程分区
尺寸absStepSize1为双精度=数学参数Abs(stepSize1)
尺寸步长2为双=量程2/量程分割
Dim Abs stepSize2为Double=Math.Abs(stepSize2)
如果ProgressBar1.ValueError   1   Overload resolution failed because no accessible 'New' accepts this number of arguments.    C:\Users\UltimateSoul\Documents\Visual Studio 2010\Projects\WindowsApplication3\WindowsApplication3\Form1.vb    43  17  WindowsApplication3

Error   2   'CaptureBuffer' is a type and cannot be used as an expression.  C:\Users\UltimateSoul\Documents\Visual Studio 2010\Projects\WindowsApplication3\WindowsApplication3\Form1.vb    56  22  WindowsApplication3


'Imports Microsoft.DirectX.DirectSound
Imports System.Threading
Imports System.Collections.Specialized
Imports SlimDX.DirectSound
Imports SlimDX.Multimedia

'Imports SlimDX.DirectSound


Public Class Form1

    Private Const SAMPLES As Integer = 8
    Private Shared SAMPLE_FORMAT_ARRAY As Integer() = {SAMPLES, 2, 1}
    Public Shared audioDevices As DeviceCollection
    Private Shared m_deviceNames As StringCollection

    Private deviceIndex As Integer = -1
    Private buffer As Microsoft.DirectX.DirectSound.CaptureBuffer
    Private liveVolumeThread As System.Threading.Thread
    Private m_frameDelay As Integer = 20
    Private Range_Division As Integer = 10

    'FrameDelay - the time in milliseconds between animation frames, measured in milliseconds. 
    'Values between 10 and 30 generally give good results, default is 20.


    Private Sub VU_Sound_Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub


    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Start()
    End Sub


    Public Sub Start()
        [Stop]()
        Dim audioDevices As New DeviceCollection
        deviceIndex = ComboBox1.SelectedIndex
        If deviceIndex <> -1 Then
            ' initialize the capture buffer and start the animation thread
            Dim cap As New CaptureBuffer(audioDevices(deviceIndex).DriverGuid)
            Dim desc As New CaptureBufferDescription()
            Dim wf As New WaveFormat()
            wf.BitsPerSample = 16
            wf.SamplesPerSecond = 44100
            wf.Channels = 2
            wf.BlockAlignment = CShort(wf.Channels * wf.BitsPerSample / 8)
            wf.AverageBytesPerSecond = wf.BlockAlignment * wf.SamplesPerSecond
            wf.FormatTag = WaveFormatTag.Pcm

            desc.Format = wf
            desc.BufferBytes = SAMPLES * wf.BlockAlignment

            buffer = CaptureBuffer(cap, desc)
            buffer.Start(True)

            ' Start a seperate thread to read the buffer and update the progress bars
            liveVolumeThread = New Thread(AddressOf updateProgress)  'Thread starts at updateProgress
            Control.CheckForIllegalCrossThreadCalls = False ' This is needed otherwise the form will not update
            liveVolumeThread.Priority = ThreadPriority.Lowest ' Thread works in the background
            liveVolumeThread.Start()

        End If
    End Sub

    Public Sub [Stop]()
        If liveVolumeThread IsNot Nothing Then
            liveVolumeThread.Abort()
            liveVolumeThread.Join()
            liveVolumeThread = Nothing
        End If

        If buffer IsNot Nothing Then
            If buffer.Capturing Then
                buffer.[Stop]()
            End If

            buffer.Dispose()
            buffer = Nothing
        End If
    End Sub



    Public Sub updateProgress()

        While True
            Dim tempFrameDelay As Integer = m_frameDelay
            Dim samples__1 As Array = buffer.Read(0, GetType(Int16), LockFlags.FromWriteCursor, SAMPLE_FORMAT_ARRAY)

            Dim leftGoal As Integer = 0
            Dim rightGoal As Integer = 0

            ' Convert the 8 samples to positive values and sum them togather 
            For i As Integer = 0 To SAMPLES - 1
                If CType(samples__1.GetValue(i, 0, 0), Int16) < 0 Then
                    leftGoal -= CType(samples__1.GetValue(i, 0, 0), Int16)
                Else
                    leftGoal += CType(samples__1.GetValue(i, 0, 0), Int16)
                End If
                If CType(samples__1.GetValue(i, 1, 0), Int16) < 0 Then
                    rightGoal -= CType(samples__1.GetValue(i, 1, 0), Int16)
                Else
                    rightGoal += CType(samples__1.GetValue(i, 1, 0), Int16)
                End If
            Next

            ' Calculate the average of the 8 samples
            leftGoal = CInt(Math.Abs(leftGoal \ SAMPLES))
            rightGoal = CInt(Math.Abs(rightGoal \ SAMPLES))

            ' Convert values to deecibels
            If leftGoal = 0 Then leftGoal = 1
            If rightGoal = 0 Then rightGoal = 1
            leftGoal = (100 + (20 * Math.Log10(leftGoal / 32768)))
            rightGoal = (100 + (20 * Math.Log10(rightGoal / 32768)))

            'By adding 100 sets the display range from 0 to 100 
            'By limiting the progreess bars to 74-100 gives a viewed range of +10dB to -26dB




            'Trap the range between 74-100, giving a 26dB meter
            If leftGoal < 74 Then leftGoal = 74
            If rightGoal < 74 Then rightGoal = 74 ' Set the display range to minimum -10dB
            If leftGoal > 100 Then leftGoal = 100
            If rightGoal > 100 Then rightGoal = 100

            Dim range1 As Double = leftGoal - ProgressBar1.Value ' calculates the difference between new and the current progress bar value 
            Dim range2 As Double = rightGoal - ProgressBar2.Value

            ' Assign the exact current value of the progress bar
            Dim exactValue1 As Double = ProgressBar1.Value
            Dim exactValue2 As Double = ProgressBar2.Value

            Dim stepSize1 As Double = range1 / Range_Division
            Dim absStepSize1 As Double = Math.Abs(stepSize1)

            Dim stepSize2 As Double = range2 / Range_Division
            Dim absStepSize2 As Double = Math.Abs(stepSize2)

            If ProgressBar1.Value < leftGoal Then    ' Display the peak
                ProgressBar1.Value = leftGoal
            End If

            If ProgressBar1.Value > leftGoal Then ' decrement the value by the Range_Division
                If absStepSize1 < Math.Abs(leftGoal - ProgressBar1.Value) Then
                    exactValue1 += stepSize1
                    ProgressBar1.Value = Math.Truncate(exactValue1)
                Else
                    ProgressBar1.Value = leftGoal
                End If
            End If

            If ProgressBar2.Value < rightGoal Then
                ProgressBar2.Value = rightGoal
            End If

            If ProgressBar2.Value > rightGoal Then ' decrement the value by the Range_Division
                If absStepSize2 < Math.Abs(rightGoal - ProgressBar2.Value) Then
                    exactValue2 += stepSize2
                    ProgressBar2.Value = Math.Truncate(exactValue2)
                Else
                    ProgressBar2.Value = rightGoal
                End If
            End If
            Thread.Sleep(m_frameDelay)
        End While
    End Sub


    Private Sub ProgressBar1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProgressBar1.Click

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim audioDevices As New DeviceCollection
        Dim x As Integer = 0
        MsgBox(audioDevices.Count.ToString())
        While x < audioDevices.Count
            ComboBox1.Items.Add(audioDevices.Item(x).Description)
            '  x = x + 1
        End While
        ComboBox1.SelectedIndex = 0
        Start()


    End Sub
End Class
Dim cap As New CaptureBuffer(audioDevices(deviceIndex).DriverGuid)
buffer = CaptureBuffer(cap, desc)