3

我正在尝试使用 .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)

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

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

4

1 回答 1

2

You can use the File.ReadAllBytes to read all the data of the file. Or use a FileStream to read the file. Then you can use the Serializer.Serialize to put the data into a class.

于 2013-05-23T19:10:11.613 回答