tags:

views:

46

answers:

3

Hello,

gcc 4.4.2 c89

I have a wave file: 8000 Hz 16 bit

I am wondering if there anyway I can load the raw data of this wave file into a buffer.

Many thanks for any advice

+1  A: 

Yes, you're looking for reading a binary file in C. Something like this:

FILE* f;
char buf[MAX_FILE_SIZE];
int n;

f = fopen("filename.bin", "rb");
if (f)
{
    n = fread(buf, sizeof(char), MAX_FILE_SIZE, f);
}
else
{
    // error opening file
}

This reads a buffer of bytes. From it you can build your data. Reading multi-byte data directly is more tricky because you run into issues of representation and endianness.

Two key C functions are used:

  • fopen that opens a file in binary mode (the "rb" flag)
  • fread that reads block data (useful for binary streams). Documented here.
Eli Bendersky
Thanks. What do you mean by representation and endianness?
robUK
@robUK: I mean - how are multi-byte values stored in the memory (which is an array of bytes, basically). Endianness is an important issue you must read about - http://en.wikipedia.org/wiki/Endianness
Eli Bendersky
+1  A: 

If you want to process the sound samples, you would be best to use a library that interprets the sound data for you. For example libsndfile.

Craig McQueen
+1  A: 

The best way IMHO would be to use a linked list with elements holding a large(1024 or more) fixed size char arrays.

the100rabh