为了在 Ubuntu18 上编写播放 ogg vorbis 文件的代码,我安装了 openal 和 alut,然后我下载了 libogg-1.3.3 和 libvorbis-1.3.6 并执行了以下操作:
./configure
make
sudo make install
据我所知,没有错误,它们似乎安装在我的 linux 系统中。
我复制了一个演示。我可以附加整个代码,它不是很大。作为参考,代码在这里:https ://www.gamedev.net/articles/programming/general-and-gameplay-programming/introduction-to-ogg-vorbis-r2031/
#include <AL/al.h>
#include <AL/alut.h>
#include <vorbis/vorbisfile.h>
#include <cstdio>
#include <iostream>
#include <vector>
constexpr int BUFFER_SIZE=32768; // 32 KB buffers
using namespace std;
void loadOgg(const char fileName[],
vector < char > &buffer,
ALenum &format, ALsizei &freq) {
int endian = 0; // 0 for Little-Endian, 1 for Big-Endian
int bitStream;
long bytes;
char array[BUFFER_SIZE]; // Local fixed size array
FILE *f = fopen(fileName, "rb");
vorbis_info *pInfo;
OggVorbis_File oggFile;
ov_open(f, &oggFile, NULL, 0);
pInfo = ov_info(&oggFile, -1);
format = pInfo->channels == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
freq = pInfo->rate;
// decode the OGG file and put the raw audio dat into the buffer
do {
// Read up to a buffer's worth of decoded sound data
bytes = ov_read(&oggFile, array, BUFFER_SIZE, endian, 2, 1, &bitStream);
// Append to end of buffer
buffer.insert(buffer.end(), array, array + bytes);
} while (bytes > 0);
ov_clear(&oggFile); // release the file resources at the end
}
int main(int argc, char *argv[]) {
ALint state; // The state of the sound source
ALuint bufferID; // The OpenAL sound buffer ID
ALuint sourceID; // The OpenAL sound source
ALenum format; // The sound data format
ALsizei freq; // The frequency of the sound data
vector<char> buffer; // The sound buffer data from file
// Initialize the OpenAL library
alutInit(&argc, argv);
// Create sound buffer and source
alGenBuffers(1, &bufferID);
alGenSources(1, &sourceID);
// Set the source and listener to the same location
alListener3f(AL_POSITION, 0.0f, 0.0f, 0.0f);
alSource3f(sourceID, AL_POSITION, 0.0f, 0.0f, 0.0f);
loadOgg(argv[1], buffer, format, freq);
}
编译:
g++ -g playSound.cc -lopenal -lalut -logg -lvorbis
没有错误抱怨找不到任何库,但是有一个未定义的符号 ov_open、ov_info、ov_read
因为我假设这些符号在 libogg 或 libvorbis 中,所以我尝试删除 -logg -lvorbis (结果相同),然后作为健全性检查,我尝试链接到一个假库(-lfugu),这给了我一个错误,因为 fugu 确实不存在。很明显,该链接正在查找 ogg 和 vorbis 库,但没有定义这些符号。