C ++ OpenAL不会播放WAV文件

时间:2016-04-25 01:21:52

标签: c++ audio openal

所以我编写了一个方法,用于接受.wav文件的文件路径,完成缓冲区和源的创建,以及播放声音。显然,当我运行程序时,实际上不会播放声音。我的错误检查都没有触发,因此该文件没有任何问题。过去几个小时我一直在搜索谷歌并空手而归,所以我想在这里问一下。这是代码:

FILE* fp = NULL;

fp = fopen("audioFile.wav", "rb");

char type[4];
DWORD size, chunkSize;
short formatType, channels;
DWORD sampleRate, avgBytesPerSec;
short bytesPerSample, bitsPerSample;
DWORD dataSize;

fread(type, sizeof(char), 4, fp);
if (type[0] != 'R' || type[1] != 'I' || type[2] != 'F' || type[3] != 'F')
    fatalError("OpenAL Error: No RIFF");

fread(&size, sizeof(DWORD), 1, fp);
fread(type, sizeof(char), 4, fp);
if (type[0] != 'W' || type[1] != 'A' || type[2] != 'V' || type[3] != 'E')
    fatalError("OpenAL Error: Not a WAVE file");

fread(type, sizeof(char), 4, fp);
if (type[0] != 'f' || type[1] != 'm' || type[2] != 't' || type[3] != ' ')
    fatalError("OpenAL Error: Not a fmt");

fread(&chunkSize, sizeof(DWORD), 1, fp);
fread(&formatType, sizeof(short), 1, fp);
fread(&channels, sizeof(short), 1, fp);
fread(&sampleRate, sizeof(DWORD), 1, fp);
fread(&avgBytesPerSec, sizeof(DWORD), 1, fp);
fread(&bytesPerSample, sizeof(short), 1, fp);
fread(&bitsPerSample, sizeof(short), 1, fp);

fread(type, sizeof(char), 4, fp);
if (type[0] != 'd' || type[1] != 'a' || type[2] != 't' || type[3] != 'a')
    fatalError("OpenAL Error: Missing DATA");

fread(&dataSize, sizeof(DWORD), 1, fp);

ALCdevice* device;
ALCcontext* context;

device = alcOpenDevice(NULL);
if (!device)
    fatalError("OpenAL Error: No sound device detected");

context = alcCreateContext(device, NULL);
alcMakeContextCurrent(context);
if (!context)
    fatalError("OpenAL Error: No sound context");

unsigned char* buf = new unsigned char[dataSize];
fread(buf, sizeof(BYTE), dataSize, fp);

ALuint source;
ALuint buffer;
ALuint frequency = sampleRate;
ALenum format = 0;

alGenBuffers(1, &buffer);
alGenSources(1, &source);

if (bitsPerSample == 8)
{
    if (channels == 1)
        format = AL_FORMAT_MONO8;
    else if (channels == 2)
        format = AL_FORMAT_STEREO8;
}
else if (bitsPerSample == 16)
{
    if (channels == 1)
        format = AL_FORMAT_MONO16;
    else if (channels == 2)
        format = AL_FORMAT_STEREO16;
}

alBufferData(buffer, format, buf, dataSize, frequency);

ALfloat SourcePos[] = { 0.0f, 0.0f, 0.0f };
ALfloat SourceVel[] = { 0.0f, 0.0f, 0.0f };
ALfloat ListenerPos[] = { 0.0f, 0.0f, 0.0f };
ALfloat ListenerVel[] = { 0.0f, 0.0f, 0.0f };
ALfloat ListenerOri[] = { 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f };

// Listener
alListenerfv(AL_POSITION, ListenerPos);
alListenerfv(AL_VELOCITY, ListenerVel);
alListenerfv(AL_ORIENTATION, ListenerOri);

// Source
alSourcei(source, AL_BUFFER, buffer);
alSourcef(source, AL_PITCH, 1.0f);
alSourcef(source, AL_GAIN, 1.0f);
alSourcefv(source, AL_POSITION, SourcePos);
alSourcefv(source, AL_VELOCITY, SourceVel);
alSourcei(source, AL_LOOPING, AL_FALSE);

alSourcePlay(source);

fclose(fp);
delete[] buf;

alDeleteSources(1, &source);
alDeleteBuffers(1, &buffer);
alcMakeContextCurrent(NULL);
alcDestroyContext(context);
alcCloseDevice(device);

0 个答案:

没有答案