要在 Linux 上播放音效,OpenAL 是個跨平台又直覺的選擇。本文把整個播放流程包成一個 SoundPlay 類別,支援多音效併發、音量調整、暫停與續播。
#ifndef SOUNDPLAY_H_
#define SOUNDPLAY_H_
#include <al.h>
#include <alc.h>
#include <alut.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#define numBuffer 4 // 可同時播放 4 個聲音
#define numSource 4
using namespace std;
class SoundPlay {
public:
SoundPlay();
virtual ~SoundPlay();
void PlaySound(string fileName, int index, float volume);
void SetVolume(int index, float volume);
void ReleaseSound(int index);
void MusicPause(int index);
void MusicContinue(int index);
bool CheckPlayState(int i);
private:
void PlayLongSound(const char* fileName, int index);
void OpenDevice();
void CloseDevice();
string GetALCErrorString(ALenum err);
private:
ALuint buffers[numBuffer];
ALuint sources[numSource];
ALCcontext* cc;
ALCdevice* dev;
bool checkstate;
ALenum result;
};
#endif /* SOUNDPLAY_H_ */
建構時先呼叫 alutInit,再開啟裝置;解構時關閉裝置:
SoundPlay::SoundPlay()
{
alutInit(NULL, 0);
if ((result = alGetError()) != AL_NO_ERROR)
cout << "alutInit--" << result << endl;
OpenDevice();
}
SoundPlay::~SoundPlay()
{
CloseDevice();
}
void SoundPlay::OpenDevice()
{
ALCchar DeviceName[] = "ALSA Software"; // 亦可為 DirectSound3D
dev = alcOpenDevice(DeviceName);
cc = alcCreateContext(dev, NULL);
alcMakeContextCurrent(cc);
}
void SoundPlay::CloseDevice()
{
ALCcontext* context = alcGetCurrentContext();
ALCdevice* device = alcGetContextsDevice(context);
alcMakeContextCurrent(NULL);
alcDestroyContext(context);
alcCloseDevice(device);
}
真正的播放發生在 PlayLongSound:產生 source 與 buffer、把 WAV 讀進記憶體、灌進 buffer,再把 buffer 掛到 source 上播放。index 為 0 時開啟循環:
alGenSources(1, &sources[index]);
alGenBuffers(1, &buffers[index]);
ALvoid *data;
ALsizei size = 0, freq = 0;
ALenum format;
ALboolean loop;
alutLoadWAVFile((ALbyte *)fileName, &format, &data, &size, &freq, &loop);
alBufferData(buffers[index], format, data, size, freq);
alutUnloadWAV(format, data, size, freq);
alSourcei(sources[index], AL_BUFFER, buffers[index]);
if (index == 0)
alSourcei(sources[index], AL_LOOPING, AL_TRUE);
alSourcePlay(sources[index]);
對外介面就顯得乾淨多了:
void SoundPlay::PlaySound(string fileName, int index, float volume)
{
alGenSources(1, &sources[index]);
alGenBuffers(1, &buffers[index]);
ReleaseSound(index);
PlayLongSound(fileName.data(), index);
alSourcef(sources[index], AL_GAIN, volume);
}
void SoundPlay::SetVolume(int index, float volume) // volume 取值 0~1
{
alSourcef(sources[index], AL_GAIN, volume);
}
void SoundPlay::ReleaseSound(int index)
{
alDeleteSources(1, &sources[index]);
alDeleteBuffers(1, &buffers[index]);
}
void SoundPlay::MusicPause(int index)
{
alSourcePause(sources[index]);
alSourceStop(sources[index]);
}
void SoundPlay::MusicContinue(int index)
{
alSourcePlay(sources[index]);
}
bool SoundPlay::CheckPlayState(int i)
{
ALint state;
alGetSourcei(sources[i], AL_SOURCE_STATE, &state);
if (state != AL_PLAYING) {
checkstate = false;
return true; // 已停止
}
return false;
}
使用方式相當樸素:
int main()
{
SoundPlay sp;
sp.PlaySound("Media/Music/BGSound1.wav", 0, 100);
while (1) {}
return 0;
}
實務上要留意兩點:PlaySound 傳入的 volume 若超過 1 並不會被夾住,可能出現破音;alutLoadWAVFile 在較新的 OpenAL Soft 中已被標記為老舊 API,新專案建議改用 stl 或 libsndfile 自行解碼。