Question

I have a very specific question. I'd like to know precisely how to draw a waveform for a .wav file. Even more specifically I need help on getting the waveform x and y co-ordinates from the wav file, the process of drawing these points is not the subject of this question. I have no relevant code to post as all of my attempts thus far have been based on other peoples work and I am not able to get them working.

Thanks.

I've started working from the code in the first link below. The original code comes up with a number of errors as things are not defined, although this is relatively easy to fix.

Private Sub cmdDefault_Click(……) Handles cmdDefault.Click
Try
SoundDevice = New Microsoft.DirectX.DirectSound.Device
SoundDevice.SetCooperativeLevel(Me.Handle_
, Microsoft.DirectX.DirectSound.CooperativeLevel.Normal)
SbufferOriginal = New _ Microsoft.DirectX.DirectSound._
SecondaryBuffer(SoundFile, SoundDevice)
SbufferOriginal.Play(0,_
Microsoft.DirectX.DirectSound.BufferPlayFlags.Looping)
Catch ex As Exception
End Try
End Sub

I've basically added a 'Dim x as y' for each undefined variable. The only variable the type for which isn't clear is "SoundFile", I've found it should be of the type "Microsoft.DirectX.DirectSound.BufferDescription". So the above becomes:

 Private Sub cmdDefault_Click(……) Handles cmdDefault.Click
        Try
            Dim SoundDevice = New Microsoft.DirectX.DirectSound.Device
            SoundDevice.SetCooperativeLevel(Me.Handle, Microsoft.DirectX.DirectSound.CooperativeLevel.Normal)
            Dim SbufferOriginal = New Microsoft.DirectX.DirectSound.SecondaryBuffer(Microsoft.DirectX.DirectSound.BufferDescription, SoundDevice)
            SbufferOriginal.Play(0, Microsoft.DirectX.DirectSound.BufferPlayFlags.Looping)
        Catch ex As Exception
        End Try
    End Sub

The issue is I'm lost as where to go from here. THIS IS MY QUESTION.

Was it helpful?

Solution

Here SoundFile can be a BufferDescription, but from the context I would guess that it is most likely here either a Stream (could be FileStream, MemoryStream, etc) or a string for the file path to the .wav file. See the various constructor overloads for SecondaryBuffer.

The only other incomplete thing about the code you've posted is that it has scrapped the Sub arguments for the sake of brevity (it is a Click handler so it needs the standard arguments). To make this work you would only need to do something like this :

Private Sub PlaySound(ByVal SoundFilePath as String)
  Try
    SoundDevice = New Microsoft.DirectX.DirectSound.Device
    SoundDevice.SetCooperativeLevel(Me.Handle, _
       Microsoft.DirectX.DirectSound.CooperativeLevel.Normal)

    SbufferOriginal = New _ Microsoft.DirectX.DirectSound._
       SecondaryBuffer(SoundFilePath, SoundDevice)
    SbufferOriginal.Play(0, _
        Microsoft.DirectX.DirectSound.BufferPlayFlags.Looping)
  Catch ex As Exception
     'do something for exception
  End Try
End Sub

Where you would call it like

PlaySound("C:\MySounds\Foo.wav")

This should probably get you started. If you continue working through the tutorial you originally posted (where this code came from) and post back here with questions when you get stuck you should be able to work to a point where you can get the visualizations you are after. Most of what you need is in that tutorial.

OTHER TIPS

Right, I've gotten the above code working now. I've moved on to this piece of code:

   Sub New(ByVal SoundFilePathName As String)
       mWAVFileName = SoundFilePathName
       mOpen = OpenWAVStream(mWAVFileName)
       '******************* MAIN WORK HERE ******************
       'Parse the WAV file and read the
       If mOpen Then
              'Read the Header Data in THIS ORDER
              'Each Read results in the File Pointer Moving
              mChunkID = ReadChunkID(mWAVStream)
              mChunkSize = ReadChunkSize(mWAVStream)
              mFormatID = ReadFormatID(mWAVStream)
              mSubChunkID = ReadSubChunkID(mWAVStream)
              mSubChunkSize = ReadSubChunkSize(mWAVStream)
              mAudioFormat = ReadAudioFormat(mWAVStream)
              mNumChannels = ReadNumChannels(mWAVStream)
              mSampleRate = ReadSampleRate(mWAVStream)
              mByteRate = ReadByteRate(mWAVStream)
              mBlockAlign = ReadBlockAlign(mWAVStream)
              mBitsPerSample = ReadBitsPerSample(mWAVStream)
              mSubChunkIDTwo = ReadSubChunkIDTwo(mWAVStream)
              mSubChunkSizeTwo = ReadSubChunkSizeTwo(mWAVStream)
              mWaveSoundData = ReadWAVSampleData(mWAVStream)
              mWAVStream.Close()
       End If
   End Sub

The issue I'm having hear is knowing what type each of the variable are (mOpen, mChunkID, mChunkSize, mFormatID etc.). I've guessed the mWAVFileName is a string as it's the filename of the WAV file. OpenWAVEStream isn't defined either but I'm assuming it is a function I have to write to open the file (or maybe check if the file can be opened). Which would imply that mOpen is either the data from the file or a Boolean value representing if the file can be opened. ReadChunkID, ReadFormatID etc... are some functions the first (ReadChunkID) has been written and shared so I think I might be able to work out the other function if I can get the types of the variables down.So what I'm after is the types of the variables, or some advice on how to find them.

Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top