Thread: How to change " sample rate" of a wav file

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    1

    Unhappy How to change " sample rate" of a wav file

    Hello everybody!
    I'm have a problem with my excersice. I don't know how to change the " sample rate" of a wav file by VC++ ( or C++).
    Ex: you have a wav file 44.1 kHz, you want to convert to 48 kHz.
    Anyone can help me? Thanks a lot!

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    To decrease the sample rate, you have to drop samples or average them. To increase the sample rate, you have to interpolate new samples. To change the sample rate in not-integer factors like in your example, you have to do really ugly things.
    To get the samples, you have to parse the WAV file.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    This sounds kind-of difficult for an "exercise" (homework assignment?) unless you're already studing audio files.

    It isn't too hard if you know something about digital audio, and if you know how to load the PCM data into an array. I've never used them, but there are there are WinAPI functions (and DirectX/DirectSound functions) to do this (if you use VC++ or another Windows compiler).

    Once you've got an array, you just need to create a new array (with more samples if you are up-sampling). Then, you can simply interpolate to find the new sample values. (Each 48kHz sample will lie somewhere in-between two 44.1kHz samples in the time-domain.)

    NOTE - Upsampling is easier than downsampling. If you downsample, you must low-pass filter to prevent aliasing. I assume that's way beyond the scope of this exercise... It's not trivial to write a filter, although you can use a "cookbook" algorithm or a filter library.

    Of course, you need to change the sample rate in the WAV file header when you save the file, or it will play-back too slow.

    If you're not familiar with digital (sampled) audio, there is an introduction on the Audacity site. It shows how you "connect the dots" to create a "wave". From that, you should be able to visualize how a 48kHz file has more "dots" than a 44.1kHz file.

    You can find the WAV file specs at wotsit.org. It can be a bit tricky, depending on the bit-depth and number of channels.

    Here is a link about resampling.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. 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
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM