Read WAV file using IReadFile

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

Read WAV file using IReadFile

Post by LunaRebirth »

I'm trying to convert the following code from using fopen() to using IReadFile::read

Code: Select all

typedef struct
{
    char  riff[4];//'RIFF'
    unsigned int riffSize;
    char  wave[4];//'WAVE'
    char  fmt[4];//'fmt '
    unsigned int fmtSize;
    unsigned short format;
    unsigned short channels;
    unsigned int samplesPerSec;
    unsigned int bytesPerSec;
    unsigned short blockAlign;
    unsigned short bitsPerSample;
    char  data[4];//'data'
    unsigned int dataSize;
} BasicWAVEHeader;
 
char* readWAV(char* filename,BasicWAVEHeader* header,IrrlichtDevice* device)
{
    char* buffer = 0;
    FILE* file = fopen(filename,"rb");
    if (!file)
    {
        device->getLogger()->log("Couldn't open.");
        return 0;
    }
 
    if (fread(header,sizeof(BasicWAVEHeader),1,file))
    {
        if (!(//these things *must* be valid with this basic header
                    memcmp("RIFF",header->riff,4) ||
                    memcmp("WAVE",header->wave,4) ||
                    memcmp("fmt ",header->fmt,4)  ||
                    memcmp("data",header->data,4)
                ))
        {
 
            buffer = (char*)malloc(header->dataSize);
            if (buffer)
            {
                if (fread(buffer,header->dataSize,1,file))
                {
                    fclose(file);
                    return buffer;
                }
                free(buffer);
            }
        }
    }
    fclose(file);
    return 0;
}
I am still having issues playing sounds with OpenAL, and this is my last resort. fopen() cannot find files packaged in the APK, so I must use the file system.

Is it possible to do, even with the BasicWAVEHeader struct?
CuteAlien
Admin
Posts: 9628
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Read WAV file using IReadFile

Post by CuteAlien »

Should be possible. fopen is IFilesSystem::createAndOpenFile. And the returned IReadFile has a read function which you can use instead of fread. You drop that IReadFile to close it (instead of fclose).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

Re: Read WAV file using IReadFile

Post by LunaRebirth »

CuteAlien wrote:Should be possible. fopen is IFilesSystem::createAndOpenFile. And the returned IReadFile has a read function which you can use instead of fread. You drop that IReadFile to close it (instead of fclose).
The part I'm concerned on is the fread.
Would

Code: Select all

file->read(header, sizeof(BasicWAVEHeader))
work the same without fread's "count" argument?
CuteAlien
Admin
Posts: 9628
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Read WAV file using IReadFile

Post by CuteAlien »

You can find some discussions on the net about the rationale for the count parameter (https://stackoverflow.com/questions/295 ... -arguments), but basically you never need it and it's generally set to 1 anyway.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply