Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
qgi
file 94 lines (73 sloc) 2.029 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
/*
* Importer.c
* Player
*
* Created by qgi on 28.03.11.
*
*/

#include "Importer.h"

#define PCM 0x0001

//forward declarations
ALenum alGetBufferFormat(unsigned short usBitsPerSample, unsigned short usChannels);

#pragma pack(1)
struct WAVE_FORMAT
{
unsigned int uHeaderLength;
unsigned short int usFormat;
unsigned short int usChannels;
unsigned int uSamplingRate;
unsigned int uBytesPerSecond;
unsigned short int usBlockAlign;
unsigned short int usBitsPerSample;
} waveFormat;

ERR_IMPORT readWavFile(const char szFileName[], ALuint uBuffer)
{

char *pDataBuffer;
unsigned int uDataBufferLength;
FILE* pFile;
long lFileSize;
ALenum alBufferFormat;

pFile = fopen(szFileName, "rb");
if (!pFile)
return ERR_FILE_IO;

lFileSize = lGetFileSize(pFile);

if(!bSkipPast(pFile, lFileSize, "WAVE") || !bSkipPast(pFile, lFileSize, "fmt "))
return ERR_HEADER;

fread(&waveFormat, sizeof(char), sizeof(waveFormat), pFile);

if (waveFormat.usFormat != PCM)
return ERR_FORMAT;

alBufferFormat = alGetBufferFormat(waveFormat.usBitsPerSample, waveFormat.usChannels);
if (alBufferFormat == AL_NONE)
return ERR_FORMAT;

if (!bSkipPast(pFile, lFileSize, "data"))
ERR_DATA;

fread(&uDataBufferLength, sizeof(uDataBufferLength), 1, pFile);

if (uDataBufferLength<1)
return ERR_DATA;

pDataBuffer = (char*) malloc (uDataBufferLength);
if (pDataBuffer==NULL)
return ERR_MEM;

fread(pDataBuffer, sizeof(char), uDataBufferLength, pFile);
alBufferData(uBuffer, alBufferFormat, pDataBuffer, uDataBufferLength, waveFormat.uSamplingRate);
free(pDataBuffer);
fclose(pFile);

return ERR_OK;
}

ALenum alGetBufferFormat(unsigned short usBitsPerSample, unsigned short usChannels)
{

if (usBitsPerSample == 8)
{
if (usChannels == 1)
return AL_FORMAT_MONO8;
else if (usChannels == 2)
return AL_FORMAT_STEREO8;
}
else if (usBitsPerSample == 16)
{
if (usChannels == 1)
return AL_FORMAT_MONO16;
else if (usChannels == 2)
return AL_FORMAT_STEREO16;
}
return AL_NONE;

}
Something went wrong with that request. Please try again.