1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
| #include "SYOpenALPlayer.hpp"
#pragma mark - Private #pragma mark -- 更新 OpenAL 缓存 void SYOpenALPlayer::refreshQueueBuffer() { ALint processedNum = numOfBuffProcessed(); while (processedNum--) { ALuint buffID; alSourceUnqueueBuffers(m_sourceID, 1, &buffID); alDeleteBuffers(1, &buffID); } }
#pragma mark -- 检出 OpenAL 状态 ALenum SYOpenALPlayer::checkALError() { ALenum errState = AL_NO_ERROR; errState = alGetError(); switch (errState) { case AL_NO_ERROR: std::cout << "AL_NO_ERROR" << std::endl; break; case AL_INVALID_NAME: std::cout << "AL_INVALID_NAME : Invalid Name paramater passed to AL call" << std::endl; break; case AL_INVALID_ENUM: std::cout << "AL_INVALID_ENUM : Invalid parameter passed to AL call" << std::endl; break; case AL_INVALID_VALUE: std::cout << "AL_INVALID_VALUE : Invalid enum parameter value" << std::endl; break; case AL_INVALID_OPERATION: std::cout << "AL_INVALID_OPERATION : Illegal call" << std::endl; break; case AL_OUT_OF_MEMORY: std::cout << "AL_OUT_OF_MEMORY : No mojo" << std::endl; break; default: std::cout << "Unknown error code" << std::endl; break; } return errState; }
#pragma mark - Public #pragma mark -- 构造函数 SYOpenALPlayer::SYOpenALPlayer() { m_fillBufLen = 0; m_totalBufLen = BUFF_NUM * BUFF_SIZE; m_initBuf = (ALubyte*)malloc(m_totalBufLen); }
#pragma mark -- 析构函数 SYOpenALPlayer::~SYOpenALPlayer() { clearOpenAL(); }
#pragma mark -- 初始化 OpenAL bool SYOpenALPlayer::initOpenAL() { m_device = alcOpenDevice(NULL); m_context = alcCreateContext(m_device, NULL); alcMakeContextCurrent(m_context); alGenSources(1, &m_sourceID); alGenBuffers(BUFF_NUM, m_buffers); return true; }
#pragma mark -- 配置 OpenAL bool SYOpenALPlayer::configOpenAL(ALuint channels, ALuint bits, ALuint frequency) { ALenum format; if (8 == bits) { if (1 == channels) { format = AL_FORMAT_MONO8; } else if (2 == channels) { format = AL_FORMAT_STEREO8; } else { format = 0; } } else if (16 == bits) { if (1 == channels) { format = AL_FORMAT_MONO16; } else if (2 == channels) { format = AL_FORMAT_STEREO16; } else { format = 0; } } else { format = 0; } if (0 == format) { std::cout << "Incompatible format : channels = " << channels << "bits = " << bits << std::endl; return false; } m_format = format; m_frequency = frequency; return true; }
#pragma mark -- 设置是否循环播放 void SYOpenALPlayer::setLoop(ALint isLoop) { alSourcei(m_sourceID, AL_LOOPING, isLoop); }
#pragma mark -- 设置音量大小 void SYOpenALPlayer::setVolume(ALfloat volume) { alSourcef(m_sourceID, AL_GAIN, volume); }
#pragma mark -- 设置播放速度 void SYOpenALPlayer::setSpeed(ALfloat speed) { alSpeedOfSound(1.0f); }
#pragma mark -- 获取 OpenAL 已经处理(播放)完毕的缓冲个数 ALint SYOpenALPlayer::numOfBuffProcessed() { ALint bufNum; alGetSourcei(m_sourceID, AL_BUFFERS_PROCESSED, &bufNum); return bufNum; }
#pragma mark -- 获取音源状态 SourceState SYOpenALPlayer::sourceState() { ALint val; SourceState state; alGetSourcei(m_sourceID, AL_SOURCE_STATE, &val); switch (val) { case AL_INITIAL: state = SourceInitial; break; case AL_PLAYING: state = SourcePlaying; break; case AL_PAUSED: state = SourcePaused; break; case AL_STOPPED: state = SourceStopped; break; default: state = SourceInitial; break; } return state; }
#pragma mark -- 打开音频缓存进行播放 void SYOpenALPlayer::openAudio(ALubyte* buffer, ALuint length) { if (NULL == buffer || 0 == length) { std::cout << "Can not open audio !" << std::endl; return; } if (0 == m_fillBufLen || m_totalBufLen > m_fillBufLen) { ALint needLen = m_totalBufLen - m_fillBufLen; ALint cpyLen = length > needLen ? needLen : length; ALint remainLen = length - cpyLen; memcpy(m_initBuf, buffer, cpyLen); m_fillBufLen += cpyLen; if (m_totalBufLen == m_fillBufLen) { for (ALint i = 0; i < BUFF_NUM; i++) { std::cout << "填第 " << i << "个缓冲区" << std::endl; alBufferData(m_buffers[i], m_format, m_initBuf + i * BUFF_SIZE, BUFF_SIZE, m_frequency); } std::cout << "所有缓冲区入队列 !" << std::endl; alSourceQueueBuffers(m_sourceID, BUFF_NUM, m_buffers); free(m_initBuf); m_initBuf = NULL; playSound(); } if (0 < remainLen) { ALuint bufId; alGenBuffers(1, &bufId); alBufferData(bufId, m_format, buffer + cpyLen, remainLen, m_frequency); alSourceQueueBuffers(m_sourceID, 1, &bufId); } } else { refreshQueueBuffer(); ALuint loopBufID; alGenBuffers(1, &loopBufID); alBufferData(loopBufID, m_format, buffer, (ALsizei)length, m_frequency); alSourceQueueBuffers(m_sourceID, 1, &loopBufID); playSound(); } }
#pragma mark -- 开启播放 void SYOpenALPlayer::playSound() { if (SourcePlaying != sourceState()) { alSourcePlay(m_sourceID); } }
#pragma mark -- 暂停播放 void SYOpenALPlayer::pauseSound() { if (SourcePaused != sourceState()) { alSourcePause(m_sourceID); } }
#pragma mark -- 停止播放 void SYOpenALPlayer::stopSound() { if (SourceStopped != sourceState()) { alSourceStop(m_sourceID); } }
#pragma mark -- 清理 OpenAL void SYOpenALPlayer::clearOpenAL() { alDeleteSources(1, &m_sourceID); alDeleteBuffers(BUFF_NUM, m_buffers); alcDestroyContext(m_context); alcCloseDevice(m_device); }
|