Thread: Get WAV file length

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    Coimbra, Portugal
    Posts
    85

    Get WAV file length

    Hello to all.

    I've been working on a project and I'd like to know if you could tell me a way to determine the WAV file length (In Seconds, Mils or whatever, as long as I can convert it to seconds). I can't use the WIN32 API because this application is linux-specific. (I am using SDL_Mixer to handle sound but I can use another lib if needed)

    Thanks in Advance.

    Jorl17

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Isn't that information in the file header somewhere?
    Just fread a few bytes from the start of the file, then extract those which encode the duration.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    It might be in the header. I am not sure.

    If it is not in the header, however, then I know one way you can. The will most definitely be a sampling rate in the header. This sampling rate tells you how many samples are taken per second. There will also be some header that says how big a sample is (1 byte....2 bytes...etc.). From that information, you can tell how long the WAV file is just by reading in the file, looking at how many bytes you read in, and then looking at the sampling rate and doing some simple algebra.
    My Website

    "Circular logic is good because it is."

  4. #4
    Registered User
    Join Date
    Mar 2008
    Location
    Coimbra, Portugal
    Posts
    85
    Final EDIT
    Well, I googled something on the File Format and nothing...went downstairs to eat and had a flash of light...
    Code:
    infile.get_num_samples()/(bitsPerSample/8)/(sampleRate)
    Based on the example I was provided with I believe that is the code. I tested it on multiple files and it did work, so now i can move my project forward and you can close this thread.

    Thanks to all of you:

    Jorl17


    EDIT

    I found http://people.msoe.edu/~taylor/examples/wav.htm and figured out that it is part of another open-source project.

    With this I can get all that information, I'd be pleased if someone could just tell me the maths I'd have to do to calculate. I know that this seems quite rude (probably) v- asking for someone to do our work - but...well, I just didn't figure it out and I only come in the forum as a last resource.

    Thanks;





    Hello.

    Probably it is in the header...and I did some googling before I posted, was trying to find some usefull source-codes to look at but all I could see was functions using the WIN32 API...I think that in one of them there was some var like WAVHEADER or something, so that's probably it...

    Anyways, I'll be googling for "WAV Content" or "WAV constitution +header +C++" or something like that to find some usefull information.

    If you can give me a source-code sample (from 5 lines to 100.000.000 lines, I don't care) just paste it here if possible and (if possible) a minimalist description but - hey, that's asking for too much I guess..

    Thanks a lot!
    Last edited by Jorl17; 05-08-2008 at 08:44 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Probably, since you seem to have all the information you need except the will to do it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    For the majority of wave files (PCM waves) that should work. However, every now and then I come across some oddball wave that has its data compressed. If you just forget about those, you're not missing much, but if you do care, then I think you're supposed to look inside the "Facts" chunk. (Quite a few apps out there that read ".wav" files fail with these oddball wave files)
    The `fact'' chunk is required if the waveform data is contained in a `wavl'' LIST chunk and for all compressed audio formats. The chunk is not required for PCM files using the `data'' chunk format.
    If you don't already have it, you should think about getting this wordfile and this html file which contain info on the .wav format and various compressions used in it. The word file contains quite a bit of useful info.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    read the header, then count the samples and divide by the sample rate (which is in the header). That gives you seconds. I forget if the number of samples is int eh header or not. looking now, no i dont htink it is, so you will have to parse the whole file counting samples.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM