Thread: .wav file length in seconds

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    19

    .wav file length in seconds

    Hello all,

    I'm trying to put together something that can read a .wav file, and figure out the length in seconds.

    Could anyone show me a simple way of doing it?

    I know that the equation is t = size / byte rate.

    However, I have no clue how to get those data.

    Also, if anyone can tell me how to make it so that it can read any audio file I would greatly appreciate it.

    Thanks.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    .wav files are actually very heterogeneous, meaning not all files named .wav have a header that's the same.

    "t = size/byte rate" is not quite going to cut it. You have some more factors:
    -frequency in cycles per second
    -number of channels (1, 2, 4, 8...)
    -a bit value (8, 16, 24, 32...) determining the depth (or resolution) per cycle per channel

    That's information you either have to know or obtain from the header.

    Also, if anyone can tell me how to make it so that it can read any audio file I would greatly appreciate it.
    Sure, find a library that will do it for you. Those will be platform specific, and to be honest I cannot name one.
    Last edited by MK27; 02-10-2010 at 08:55 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your question really revolves around audio, rather than a code problem. I'm not trying to chase you off, but you'll get better answers in a newsgroup or forum dedicated to PC audio, than you will here.

    I know the Ubuntu forum has had a real expert on PC audio with that OS. I'm sure other Linux Distro's and Windows (especially in the newgroups), has the same thing.

    Good luck.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It is a code problem in the sense that you do need to write code in order to create or playback a .wav file.

    There is actually nothing special beyond the header about wavs. They are not (normally) encrypted or compressed. They are actually most often just raw PCM (Pulse-code modulation) data. Depending on the complexity of the header, I think the data can be re-organized but like I said, that is usually not the case in my (extremely limited) experience.
    Last edited by MK27; 02-10-2010 at 08:49 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MK27 View Post
    "t = size/byte rate" is not quite going to cut it. You have some more factors:
    Yeah it will. The WAV "fmt" header contains a field, nBytesPerSecond. The field is actually redundant because it can be computed from sample rate, bits-per-sample, and number of channels, but it's there.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by brewbuck View Post
    Yeah it will. The WAV "fmt" header contains a field, nBytesPerSecond. The field is actually redundant because it can be computed from sample rate, bits-per-sample, and number of channels, but it's there.
    Hallelujah.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    19
    Hello all,

    Thanks for the responses you've given me.

    I'm using a language called CSound with C, so playing and creating wav files is not a problem.

    I wanted the C part of the program to get the necessary data from the wav file to get the exact duration, so that it can be used for stretching/shortening the audio.

    I found a couple things online but they're very complicated. I'm actually doing it for a class so maybe it's better if I don't get too far ahead....

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MK27 View Post
    Hallelujah.
    Yes, my mind indeed is a compendium of the most interesting facts in the universe.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM