如何从.wav文件vb.net读取字节

时间:2013-05-23 18:37:50

标签: vb.net bytearray directsound wav

我正在尝试使用.wav文件做一些工作,我已经能够播放文件并通过创建一个字节数组随机播放声音(请参阅下面的代码)我想知道是否有我可以用来从.wav文件中获取字节的方法。我想,如果我能从.wav文件中获取字节,我应该能够播放声音,因为我正在使用随机噪声。这应该让我可以解决如何修改声音。

播放.wav文件:

    Dim SoundDevice = New Microsoft.DirectX.DirectSound.Device
Dim SbufferOriginal = New Microsoft.DirectX.DirectSound.SecondaryBuffer(SoundFilePath, SoundDevice)
Private Sub PlaySound()
    Try
        SbufferOriginal = New Microsoft.DirectX.DirectSound.SecondaryBuffer(SoundFilePath, SoundDevice)
        SoundDevice.SetCooperativeLevel(Me.Handle, Microsoft.DirectX.DirectSound.CooperativeLevel.Normal)
        SbufferOriginal.Play(0, Microsoft.DirectX.DirectSound.BufferPlayFlags.Looping)
    Catch ex As Exception
        'do something for exception
    End Try
End Sub

使用直接声音播放随机噪音:

DSdev.SetCooperativeLevel(Me.Handle, CooperativeLevel.Normal)

DSformat = New WaveFormat()
DSformat.BitsPerSample = 8
DSformat.Channels = 1
DSformat.BlockAlign = 1
DSformat.FormatTag = WaveFormatTag.Pcm
DSformat.SamplesPerSecond = 8000
DSformat.AverageBytesPerSecond = DSformat.SamplesPerSecond *
DSformat.BlockAlign

'buffer description
DSdes = New BufferDescription(DSformat)
DSdes.BufferBytes = 3 * DSformat.AverageBytesPerSecond

'create the buffer
DSbuffer = New Microsoft.DirectX.DirectSound.SecondaryBuffer(DSdes, DSdev)

'generate ramdom data (white noise)
Dim rawsamples(22050) As Byte
Dim rnd1 = New System.Random()
Dim tmepno As Integer = 150





For j = 0 To 5
    DSbuffer.Stop()
    Dim i As Integer
    For i = 0 To 22050
        rawsamples(i) = 250
        tmepno += 1
        If tmepno = 255 Then
            tmepno = 150
        End If
        'rnd1.Next(255)
    Next i

    ' load audio samples to secondary buffer
    DSbuffer.Write(0, rawsamples, LockFlag.EntireBuffer)

    'play audio buffer
    DSbuffer.Play(0, BufferPlayFlags.Default)
    Threading.Thread.Sleep(250)
Next

我要做的是从.wav文件中获取字节数组,这样我就能像播放随机噪声那样播放它。

提前致谢!

更新:

我已经编写了以下代码来使用从.wav文件中读取的字节:

    Dim justsounddata(bytearray.GetLength(0) - 44 - 1) As Byte
    Dim bitstring As String
    For i = 0 To justsounddata.GetLength(0) - 1
        'RichTextBox1.AppendText(bytearray(i))
        justsounddata(justsounddata.GetLength(0) - 1 - i) = bytearray(i + 44)
        bitstring &= bytearray(i)
    Next

    RichTextBox1.Text = bitstring

    Dim workingvalue As String



    DSdev.SetCooperativeLevel(Me.Handle, CooperativeLevel.Normal)

    DSformat = New WaveFormat()

    workingvalue = Mid(bitstring, 35, 2)
    workingvalue = StrReverse(workingvalue)
    DSformat.BitsPerSample = workingvalue
    'CInt(bitspersample)

    workingvalue = Mid(bitstring, 23, 2)
    workingvalue = StrReverse(workingvalue)
    DSformat.Channels = workingvalue

    workingvalue = Mid(bitstring, 33, 2)
    workingvalue = StrReverse(workingvalue)
    DSformat.BlockAlign = workingvalue

    workingvalue = Mid(bitstring, 9, 4)
    'workingvalue = StrReverse(workingvalue)
    DSformat.FormatTag = workingvalue

    workingvalue = Mid(bitstring, 25, 4)
    workingvalue = StrReverse(workingvalue)
    DSformat.SamplesPerSecond = workingvalue

    'CInt(samplesspersecond)
    DSformat.AverageBytesPerSecond = DSformat.SamplesPerSecond * DSformat.BlockAlign
    'CInt(bitrate)

    'buffer description
    DSdes = New BufferDescription(DSformat)
    DSdes.BufferBytes = 3 * DSformat.AverageBytesPerSecond

    'create the buffer
    DSbuffer = New Microsoft.DirectX.DirectSound.SecondaryBuffer(DSdes, DSdev)

    'generate ramdom data (white noise)
    Dim rawsamples(22050) As Byte
    Dim rnd1 = New System.Random()
    Dim tmepno As Integer = 150


    ' load audio samples to secondary buffer
    'DSbuffer.Write(0, rawsamples, LockFlag.EntireBuffer)

    DSbuffer.Write(0, justsounddata, LockFlag.EntireBuffer)

    'play audio buffer
    '
    DSbuffer.Play(0, BufferPlayFlags.Default)

此行中出现错误:

DSbuffer = New Microsoft.DirectX.DirectSound.SecondaryBuffer(DSdes, DSdev)

错误是:“值不在预期范围内。”

我相信我已经从每个变量的数组中读取了正确的位。我也注意到了字节序。再次感谢提前:))

1 个答案:

答案 0 :(得分:2)

您可以使用File.ReadAllBytes来读取文件的所有数据。或使用FileStream来读取文件。然后,您可以使用Serializer.Serialize将数据放入类中。