当它不应该在Windows上跳转到偏移量

时间:2011-10-22 18:03:57

标签: c++ openal

我在Windows上遇到OpenAL问题。

ALuint source;
ALuint buffer;

alGenSources(1, &source);
DJAudioSource audioSource = {source, 0,0,0, 0,0,0};

alSourcef(source, AL_PITCH, 1);
alSourcef(source, AL_GAIN, 1);
alSourcefv(source, AL_POSITION, audioSource.pos);
alSourcefv(source, AL_VELOCITY, audioSource.vel);
alSourcei(source, AL_LOOPING, AL_TRUE);

alGenBuffers(1, &buffer);

FILE *file = fopen(path, "rb");

RiffHeader rh;
WaveFormatHeader wfh;
DataHeader dh;

fread(&rh,  1, sizeof(RiffHeader),          file);
fread(&wfh, 1, sizeof(WaveFormatHeader),    file);
fread(&dh,  1, sizeof(DataHeader),          file);

unsigned char *buf = (unsigned char*)malloc(dh.SubChunkSize);
fread(buf,  1, dh.SubChunkSize,             file);

fclose(file);

alBufferData(buffer, AL_FORMAT_STEREO16, buf, dh.SubChunkSize, wfh.SampleRate);
alSourcei(source, AL_BUFFER, buffer);

free(but);
sources.push_back(audioSource);

我没有错误检查,因为我知道我正在测试它的文件在Mac OS X上的Xcode中运行时有效。这里是我的代码中使用的结构。

// Size of 24.
typedef struct WaveFormatHeader {
    char SubChunkID[4];
    unsigned int SubChunkSize;

    unsigned short AudioFormat;
    unsigned short Channels;
    unsigned int SampleRate;
    unsigned int ByteRate;

    unsigned short BlockAlign;
    unsigned short BitsPerSample;
} WaveFormatHeader;

// Size of 8.
typedef struct DataHeader {
    char SubChunkID[4];
    unsigned int SubChunkSize;
} DataHeader;

我遇到的问题是这个。当我在Mac OS X上运行该程序时,它播放音频很好,并且循环也很好。当我在Windows上运行它时,它开始通过文件的大约四分之三。它播放到最后,然后跳到歌曲的四分之一(大概是因为该歌曲的四分之一已经过去了)。然后播放到大约一半并停止。在歌曲已经过去的一半之后,它将从四分之三的时间再次开始。

在我的load()函数中,我正在使用此行播放音频。

alSourcePlay(audioManager.sources.back().source);

audioManager是一个自定义类对象,它包含一个源向量 - 不是OpenAL源,而是一个DJAudioSource结构,其中包含一个ALuint源,等等。

typedef struct DJAudioSource {
    ALuint source;
    ALfloat pos[3];
    ALfloat vel[3];
} DJAudioSource;

有谁知道为什么会发生这种情况和/或如何阻止它?如果我错过了这个问题的任何重要信息,我会道歉。

0 个答案:

没有答案