0

First of all, let me say that I have been reading another similar questions but I can´t find the solution for my problem in them.

I'm using the "OpenAL" library to play WAV files by creating an AudioBuffer and then an AudioSource, but I think this does not matter. I created a class called AudioBuffer which has a static method to get all the info and then return a pointer to a object created inside it. What I'm trying to do is to read a WAV file. To do so, I first read the header to get the values of each field and then build a buffer with the "data size" I read before and store in it the whole data field. The problem is that when I try to load WAV file, it just won't play. Here is the function I use to both load a WAV file and read its fields:

typedef struct {
char chunk_id[4];
uint32_t chunk_size;
char format[4];
} wave_header;

typedef struct {
char id[4];
uint32_t size;
} riff_chunk_header;

typedef struct {
uint16_t audio_format;
uint16_t num_channels;
uint32_t sample_rate;
uint32_t byte_rate;
uint16_t block_align;
uint16_t bits_per_sample;
} wave_fmt_chunk;

AudioBuffer* AudioBuffer::load(const char* filename) {

wave_header w_header;
riff_chunk_header r_c_header;
wave_fmt_chunk w_f_chunk;
short extra_params_size = 0;
bool data = false;
char bloque[1];
int data_size = 0;

AudioBuffer *audiobuffer = new AudioBuffer(1);

std::ifstream in(filename, std::ios::binary);

if (in.is_open()) {

    printf("Fichero abierto correctamente.\n");

    in.read(w_header.chunk_id, 4);

    if (strncmp(w_header.chunk_id, "RIFF", 4) != 0) {
        printf("El fichero no es de tipo WAV.\n");
        return nullptr;
    }
    else {
        printf("Fichero WAV valido.\n");
    }

    in.read(reinterpret_cast<char *>(&w_header.chunk_size), 4);
    in.read(w_header.format, 4);

    in.read(r_c_header.id, 4);
    in.read(reinterpret_cast<char *>(&r_c_header.size), 4); //FmtChunkSize

    in.read(reinterpret_cast<char *>(&w_f_chunk.audio_format), 2);
    in.read(reinterpret_cast<char *>(&w_f_chunk.num_channels), 2);
    in.read(reinterpret_cast<char *>(&w_f_chunk.sample_rate), 4);
    in.read(reinterpret_cast<char *>(&w_f_chunk.byte_rate), 4);
    in.read(reinterpret_cast<char *>(&w_f_chunk.block_align), 2);
    in.read(reinterpret_cast<char *>(&w_f_chunk.bits_per_sample), 2);

    if (r_c_header.size > 16) {
        in.read(reinterpret_cast<char *>(&extra_params_size), 2);
        in.ignore(extra_params_size); //Ignoramos los bytes de parámetros adicionales.
    }

    while (!data) {
        in.read(bloque, 1);
        if (bloque[0] == 'd') {
            in.read(bloque, 1);
            if (bloque[0] == 'a') {
                in.read(bloque, 1);
                if (bloque[0] == 't') {
                    in.read(bloque, 1);
                    if (bloque[0] == 'a')
                        data = true; //Se ha encontrado "data".
                }
            }
        }

    }

    //Una vez encontrado "data"
    in.read(reinterpret_cast<char *>(&data_size), 4); //Leemos el tamaño del bloque data.

    char *m_data = new char[data_size]; //Buffer con el tamaño de los datos.
    in.read(m_data, data_size); //Rellenamos el buffer con los datos.

    //Generamos el buffer de OpenAL.
    alGenBuffers(1, audiobuffer->buffer);

    if (w_f_chunk.bits_per_sample == 8) {
        if (w_f_chunk.num_channels == 1) {
            alBufferData(audiobuffer->buffer[0], AL_FORMAT_MONO8, m_data, data_size, w_f_chunk.sample_rate);
        }
        else {
            alBufferData(audiobuffer->buffer[0], AL_FORMAT_STEREO8, m_data, data_size, w_f_chunk.sample_rate);
        }
    }
    else if (w_f_chunk.bits_per_sample == 16) {
        if (w_f_chunk.num_channels == 1) {
            alBufferData(audiobuffer->buffer[0], AL_FORMAT_MONO16, m_data, data_size, w_f_chunk.sample_rate);
        }
        else {
            alBufferData(audiobuffer->buffer[0], AL_FORMAT_STEREO16, m_data, data_size, w_f_chunk.sample_rate);
        }
    }

    return audiobuffer;
}
else {
    printf("El fichero no se pudo abrir. Ruta incorrecta.\n");
    return nullptr;
}
}

Sorry if some variable names and comments are in Spanish, but I think it's easy to understand.

  • First I open the file the function got by parameter and print if it was successfully opened.
  • Then I look for the "RIFF" string which tells me if it is a valid WAV file.
  • After that, I read the values for each field.

The WAV header structure I'm following is this: enter image description here

I'm assuming the last 2 elements only appear depending on the "AudioFormat" field value. If it is equal to 1, those elements won't appear. Otherwise, they may or may not appear. For knowing this, I'm comparing the "FmtChunkSize"field value:

  • If it is equal to 16, then the last 2 fields are definitely not present.
  • If it is greater than 16, then I have to read the value of "ExtraParamsSize" field and skip those bytes when reading.

Then, I'm looking for the "data" string. When I have finally found it, I read its size (the next 4 bytes) and create a buffer of that size. From alGenBuffers(1, audiobuffer->buffer); on I'm just creating OpenAL buffers (this is not where my problem is).

Debugging I found that the AudioFormat value is 1 (so it shouldn't have the last two fields) BUT FmtChunkSize is greater than 16 (so it should have the last two fields... Kind of conflicting...) Therefore, I might think that my problem is that I'm not taking into account the endianness, but if so I don0t know how to read the values properly then.

The WAV file I'm loading is OK because other people I know have successfully played it with their code.

Sorry If i haven't explained myself very well, and sorry for the question size too, but I thought it might be helpful for you to know which WAV header structure I was following.

Any help would be appreciated, thank you very much in advance.

1
  • What do you mean by it won't play? Are you sure you don't have another (non-data) chunk after the one you read? Feb 3, 2019 at 15:16

1 Answer 1

0

A wav file works a little differently than what you think.

You have chunks of data, each must be read together. They all follow the same pattern: 4 characters, a size on 4 bytes and possibly additional data.

The first block should be fixed, that's the file type, with then the size of the file (-8 bytes, so that's the remaining size of the file) and the file format.

Then, there are the usual (possibly) chunks. You have a type (4 characters), then the size of the chunk (-8 bytes) and the relevant data.

In your case, the second chunk seems to be "fmt". You should only care about this chunk size to know if you have more than 16 bytes of information or not. That's what decides things.

Then you have the "data" chunk. Same pattern, 4 characters, then the size of the sound data and the data itself.

But you could get other chunks, like "bext", so you need to read all other chunks, not just "data".

As I've said, they follow the same pattern. 4 characters, 4 bytes for size and then some bytes (of size size) that are attached to that chunk. If you follow the rules, then you will be able to read your file.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.