| 1262 | } |
| 1263 | |
| 1264 | static Result UpdateInternal(SoundSystem* sound) |
| 1265 | { |
| 1266 | DM_PROFILE(__FUNCTION__); |
| 1267 | |
| 1268 | if (IsAudioInterrupted()) |
| 1269 | { |
| 1270 | // We can't play sounds when Audio was interrupted by OS event (Phone call, Alarm etc) |
| 1271 | return RESULT_OK; |
| 1272 | } |
| 1273 | |
| 1274 | DM_MUTEX_OPTIONAL_SCOPED_LOCK(sound->m_Mutex); |
| 1275 | |
| 1276 | // feed buffers for active streams |
| 1277 | uint16_t i = 0; |
| 1278 | while (i < sound->m_InstancesActive.Size()) { |
| 1279 | uint16_t instance_index = sound->m_InstancesActive[i]; |
| 1280 | SoundInstance &instance = sound->m_Instances[instance_index]; |
| 1281 | ALuint source = sound->m_Sources[instance.m_SourceIndex]; |
| 1282 | if (instance.m_BufferCount) { |
| 1283 | // drain any processed buffers |
| 1284 | ALuint source = sound->m_Sources[instance.m_SourceIndex]; |
| 1285 | ALint buffers_processed = 0; |
| 1286 | alGetSourcei(source, AL_BUFFERS_PROCESSED, |
| 1287 | &buffers_processed); |
| 1288 | assert(buffers_processed <= instance.m_BufferCount); |
| 1289 | while (buffers_processed) { |
| 1290 | ALuint buffer; |
| 1291 | alSourceUnqueueBuffers(source, 1, &buffer); |
| 1292 | checkForAlErrors("alSourceUnqueueBuffers"); |
| 1293 | --buffers_processed; |
| 1294 | --instance.m_BufferCount; |
| 1295 | } |
| 1296 | } |
| 1297 | ALenum state; |
| 1298 | alGetSourcei(source, AL_SOURCE_STATE, &state); |
| 1299 | if (instance.m_Playing) { |
| 1300 | // feed new buffers and handle looping |
| 1301 | while (instance.m_BufferCount < sound->m_BuffersPerSource) { |
| 1302 | dmSoundCodec::Result r = Feed(&instance); |
| 1303 | if (r == dmSoundCodec::RESULT_OK) { |
| 1304 | // noop |
| 1305 | } else if (r == dmSoundCodec::RESULT_END_OF_STREAM) { |
| 1306 | break; |
| 1307 | } else { |
| 1308 | // error |
| 1309 | dmLogError("AL source feed error: %i\n", (int)r); |
| 1310 | instance.m_Playing = false; |
| 1311 | break; |
| 1312 | } |
| 1313 | } |
| 1314 | if (instance.m_BufferCount && state != AL_PLAYING) { |
| 1315 | alSourcePlay(source); |
| 1316 | checkForAlErrors("alSourcePlay"); |
| 1317 | } |
| 1318 | } else { |
| 1319 | // if we're no longer playing this instance, we still need to wait for its buffers to finish |
| 1320 | if (state != AL_PLAYING) { |
| 1321 | // setting AL_BUFFER to AL_NONE will unqueue any buffers |
no test coverage detected