views:

696

answers:

7

How do you programmatically compress a WAV file to another format (PCM, 11,025 KHz sampling rate, etc.)?

A: 

I'd look into audacity... I'm pretty sure they don't have a command line utility that can do it, but they may have a library...

Update:

It looks like they use libsndfile, which is released under the LGPL. I for one, would probably just try using that.

chills42
I'm not planning to use Audacity. I've been doing some research. What can you say about Windows Audio Compression Manager?
+1  A: 

Use sox (Sound eXchange : universal sound sample translator) in Linux: SoX is a command line program that can convert most popular audio files to most other popular audio file formats. It can optionally change the audio sample data type and apply one or more sound effects to the file during this translation.

Brian Carlton
A: 

If you mean how do you compress the PCM data to a different audio format then there are a variety of libraries you can use to do this, depending on the platform(s) that you want to support. If you just want to change the sample rate of the PCM data then you need a sample rate conversion algorithm instead, which is a completely different problem. Can you be more specific in your requirements?

Stu Mackellar
i have to convert a WAV file into: ID: RIFFFormat: WAVESub ID1: fmtSize 1: 16Audio Format: 1Number of Channels: 1Sample Rate:11025Byte Rate: 11025Block Align: 1Bits Per Sample: 8Sub ID2: data
That is a wav file. Are you saying you want to change an existing wav file to a wav file with different properties?
Stu Mackellar
+1  A: 

You're asking about resampling, and more specifically downsampling, not compression. While both processes are lossy (meaning that you will suffer loss of information), downsampling works on raw samples instead of in the frequency domain.

If you are interested in doing compression, then you should look into lame or OGG vorbis libraries; you are no doubt familiar with MP3 and OGG technology, though I have a feeling from your question that you are interested in getting back a PCM file with a lower sampling rate.

In that case, you need a resampling library, of which there are a few possibilites. The most widely known is libsamplerate, which I honestly would not recommend due to quality issues not only within the generated audio files, but also of the stability of the code used in the library itself. The other non-commercial possibility is sox, as a few others have mentioned. Depending on the nature of your program, you can either exec sox as a separate process, or you can call it from your own code by using it as a library. I personally have not tried this approach, but I'm working on a product now where we use sox (for upsampling, actually), and we're quite happy with the results.

The other option is to write your own sample rate conversion library, which can be a significant undertaking, but, if you only are interested in converting with an integer factor (ie, from 44.1kHz to 22kHz, or from 44.1kHz to 11kHz), then it is actually very easy, since you only need to strip out every Nth sample.

Nik Reiman
A: 

To Stu Mackellar: Yes.

A: 

Thanks for the reply sqook.. I have another question: I'm trying sox (.exe) right now. Now my requirement is to convert files into these properties..

ID: RIFF Format: WAVE Sub ID1: fmt Size 1: 16 Audio Format: 1 Number of Channels: 1 Sample Rate:11025 Byte Rate: 11025 Block Align: 1 Bits Per Sample: 8 Sub ID2: data

sox has converted some files successfully into the above properties (i used rate 11.025k to do that) but others are not. Are there any way I could get the above results all the time?

Sorry I'm just newbie at this.. :)

A: 

In Windows, you can make use of the Audio Compression Manager to convert between files (the acm... functions). You will also need a working knowledge of the WAVEFORMAT structure, and WAV file formats. Unfortunately, to write all this yourself will take some time, which is why it may be a good idea to investigate some of the open source options suggested by others.

I have written a my own open source .NET audio library called NAudio that can convert WAV files from one format to another, making use of the ACM codecs that are installed on your machine. I know you have tagged this question with C++, but if .NET is acceptable then this may save you some time. Have a look at the NAudioDemo project for an example of converting files.

Mark Heath