FraserOfSmeg
FraserOfSmeg

Reputation: 1148

How to read bytes from a .wav file vb.net

I'm trying to do some work with .wav files and I've been able to play the files and play sound randomly by creating an array of bytes (see code for both below) I'd like to know if there's a method I can use to get the bytes from a .wav file. My thinking it if I could get the bytes from the .wav file I should be able to play the sound as I am doing with random noise. This should then allow me to work out how to modify the sound.

Playing a .wav file:

    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

Play random noise using direct sound:

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

What I'm trying to do is get the byte array from a .wav file so I could play it the same way as I do the random noise.

Thanks in advance!

UPDATE:

I've written the following code for using the bytes read from the .wav file:

    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)

The error appears in this line:

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

The error is: "Value does not fall within the expected range."

I believe I've read the correct bits from the array for each variable. I have also taken note of the endianness. Again thanks in advance :)

Upvotes: 3

Views: 4483

Answers (1)

the_lotus
the_lotus

Reputation: 12748

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.

Upvotes: 2

Related Questions