OpenAL 在 Windows 上不应该跳转到偏移量
我在 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;
有谁知道为什么会发生这种情况和/或如何阻止它?如果我错过了这个问题中的任何重要信息,我深表歉意。
I'm experiencing a problem with OpenAL on Windows.
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);
I don't have error checking because I know that the file I am testing it with works when I run it in Xcode on Mac OS X. And here are the structs used in my code.
// 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;
The problem I am experiencing is this. When I run the program on Mac OS X, it plays the audio fine, and loops fine too. When I run it on Windows, it starts roughly three quarters of the way through the file. It plays to the end, then jumps to one quarter of the way through the song (presumably because one quarter of the song has passed at that point). It then plays until about halfway through and stops. After the duration of half the song has passed, it starts again from three quarters of the way through.
I am using this line to play the audio after it has been loaded, in my load() function.
alSourcePlay(audioManager.sources.back().source);
audioManager is a custom class object that contains a vector of sources - not an OpenAL source but a DJAudioSource struct, which contains an ALuint source in it, amongst other things.
typedef struct DJAudioSource {
ALuint source;
ALfloat pos[3];
ALfloat vel[3];
} DJAudioSource;
Does anyone know why this is happening and/or how to stop it? And I apologise if I've missed any crucial information out of this question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论