dcsimg
Results 1 to 8 of 8

Thread: Reading Frequencies From Wave (*.wav) Files

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    2

    Reading Frequencies From Wave (*.wav) Files

    Hi guys. I'm Machez Nolan, of part-French and Carribean descent (any doubts about my name now?) and I am a part-time novice programmer.

    As in the heading to this message, I wish to know if it is possible to open a wave (*.wav) file and read the frequencies from it and print it into the form?

    Supposing the public functions have been declared (for those who didn't get it; well, this is the bit I want) previously, I wan to know if a program can be made such that;

    Code:
    Private Sub Form1_Load()
    Do While eof = False ' EOF: End Of File
    Print freq, sec
    Cls
    Loop
    End Sub
    Well, that ain't exactly it, but that gives a rough look of how the code should look. Thanks once again, all of you, for your help.

  2. #2
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    Re: Reading Frequencies From Wave (*.wav) Files

    Code:
    Option Explicit
    Private Type WAVEHEADER_RIFF
     RIFF As Long
     riffBlockSize As Long
     riffBlockType As Long
    End Type
    Private Type WAVEHEADER_FORMAT
     wfBlockType As Long
     wfBlockSize As Long
     wFormatTag As Integer
     nChannels As Integer
     nSamplesPerSec As Long
     nAvgBytesPerSec As Long
     nBlockAlign As Integer
     wBitsPerSample As Integer
    End Type
    Private Sub Form_Load()
     Dim hFile As Long
     Dim LenFile As Long
     Dim Seconds As Long
     Dim wr As WAVEHEADER_RIFF
     Dim wf As WAVEHEADER_FORMAT
     Dim File As String
     hFile = FreeFile
     '******** change this *********
     File = "C:\Led Zeppelin - Gallows Pole.wav"
     Open File For Binary As hFile
     LenFile = LOF(hFile)
     Get #hFile, , wr
     Get #hFile, , wf
     Close hFile
     Seconds = LenFile \ wf.nAvgBytesPerSec
     Debug.Print "Frequency: " & wf.nSamplesPerSec
     Debug.Print "Length Seconds: " & Seconds
     Debug.Print "Formatted Time: " & SecsToMS(Seconds)
    End Sub
    
    
    Private Function SecsToMS(Secs As Long) As String
     SecsToMS = "00:00"
     Mid$(SecsToMS, 1, 2) = Right$("0" & Secs \ 60, 2)
     Mid$(SecsToMS, 4, 2) = Right$("0" & Secs Mod 60, 2)
    End Function

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    2

    Reply To Answerer

    Thanks, Sir. It has helped me exactly how I want it. One doubt, did you actually make it up within about three hours?

  4. #4
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    Re: Reading Frequencies From Wave (*.wav) Files

    Not sure what you are asking. The code is from a class I wrote which does this and a few
    other things. I just picked out the relevant parts for this post.

  5. #5
    Registered User
    Join Date
    Nov 2019
    Posts
    1

    Re: Reading Frequencies From Wave (*.wav) Files

    Quote Originally Posted by VBClassicRocks View Post
    Code:
    Option Explicit
    Private Type WAVEHEADER_RIFF
     RIFF As Long
     riffBlockSize As Long
     riffBlockType As Long
    End Type
    Private Type WAVEHEADER_FORMAT
     wfBlockType As Long
     wfBlockSize As Long
     wFormatTag As Integer
     nChannels As Integer
     nSamplesPerSec As Long
     nAvgBytesPerSec As Long
     nBlockAlign As Integer
     wBitsPerSample As Integer
    End Type
    Private Sub Form_Load()
     Dim hFile As Long
     Dim LenFile As Long
     Dim Seconds As Long
     Dim wr As WAVEHEADER_RIFF
     Dim wf As WAVEHEADER_FORMAT
     Dim File As String
     hFile = FreeFile
     '******** change this *********
     File = "C:\Led Zeppelin - Gallows Pole.wav"
     Open File For Binary As hFile
     LenFile = LOF(hFile)
     Get #hFile, , wr
     Get #hFile, , wf
     Close hFile
     Seconds = LenFile \ wf.nAvgBytesPerSec
     Debug.Print "Frequency: " & wf.nSamplesPerSec
     Debug.Print "Length Seconds: " & Seconds
     Debug.Print "Formatted Time: " & SecsToMS(Seconds)
    End Sub
    
    
    Private Function SecsToMS(Secs As Long) As String
     SecsToMS = "00:00"
     Mid$(SecsToMS, 1, 2) = Right$("0" & Secs \ 60, 2)
     Mid$(SecsToMS, 4, 2) = Right$("0" & Secs Mod 60, 2)
    End Function
    Hi Sir,

    Can you please guide me on how to run this code?Is there any userform or anything that i need to create first? I tried to run this code by assigning to one macro but nothing happens. Thank you

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,622

    Re: Reading Frequencies From Wave (*.wav) Files

    one macro?
    Are you trying this in some version of VBA?
    It was written to run in the VB6 IDE, and the results printed in the debug (aka Immediate) window.

    If you're running it from VBA, then I expect you will need to change the Debug.Print statements to something else to display the information somewhere, like labels or textboxes for instance, or in Cells possibly if you're using a Macro in Excel.

    If you are talking VBA, then you should be able to run the macro from the macro editor (essentially the VBA IDE), which should have an Immediate Window available, that will display the results of the Debug.Print.
    If it doesn't display anything in the Immediate Window, then you could step through the execution of the macro to see where it goes wrong.
    Last edited by passel; Nov 14th, 2019 at 10:03 PM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  7. #7
    Registered User
    Join Date
    Dec 2019
    Posts
    1

    Re: Reading Frequencies From Wave (*.wav) Files

    hi sir, i need your advice, i tried upload the above coding in vba, when i run the program , the result only show 1 frequency which is 44100. any wav file which i insert will have the same value. only the length of the wav file correct. is it possible to have frequency for each second of a audio wav file? thank you.. if possible.. please guide me on the formula in the coding.
    this is example what i have got:
    ...................
    Frequency: 44100
    Length Seconds: 1
    Formatted Time: 00:01

    Frequency: 44100
    Length Seconds: 63
    Formatted Time: 01:03
    ....................

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,622

    Re: Reading Frequencies From Wave (*.wav) Files

    The frequency that is returned is the frequency that the waveform was sampled at, i.e. based on the desired Nyquist frequency, to create the Pulse Code Modulated data that defines the waveform.

    If you have a waveform that you are trying to determine the frequency of the waveform that was recorded, not the sampling rate, then you would need to run a fast Fourier transform algorithm on a portion of the data to convert waveform over time into frequency.

    I don't have experience in these matters. You should search for code that implement FFTs for what you need.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width