| 285 | } |
| 286 | |
| 287 | U32 KDspAudioSdl::writeAudio(U8* data, U32 len) { |
| 288 | U32 bytesPerSecond = want.freq * want.channels * bytesPerSampleWant(); |
| 289 | U32 delay = bytesPerSecond / 8; |
| 290 | |
| 291 | if (!KSystem::soundEnabled) { |
| 292 | static U32 timeSinceLastWrite; |
| 293 | |
| 294 | U32 elapsedTime = KSystem::getMilliesSinceStart() - timeSinceLastWrite; |
| 295 | U32 bytesToRemove = bytesPerSecond * elapsedTime / 1000; |
| 296 | |
| 297 | bytesToRemove = std::min((U32)this->audioBuffer.size(), bytesToRemove); |
| 298 | this->audioBuffer.erase(this->audioBuffer.begin(), this->audioBuffer.begin() + bytesToRemove); |
| 299 | |
| 300 | if (this->audioBuffer.size() > delay) { |
| 301 | return -K_EWOULDBLOCK; |
| 302 | } |
| 303 | U32 blockSize = bytesPerSampleWant() * want.channels; |
| 304 | len = std::min(len, ((delay - (U32)this->audioBuffer.size()) & ~(blockSize - 1))); |
| 305 | audioBuffer.insert(this->audioBuffer.end(), data, data + len); |
| 306 | timeSinceLastWrite = KSystem::getMilliesSinceStart(); |
| 307 | return len; |
| 308 | } |
| 309 | SDL_LockAudio(); |
| 310 | if (this->audioBuffer.size() > delay) { |
| 311 | SDL_UnlockAudio(); |
| 312 | return -K_EWOULDBLOCK; |
| 313 | } |
| 314 | U32 blockSize = bytesPerSampleWant() * want.channels; |
| 315 | len = std::min(len, ((delay - (U32)this->audioBuffer.size()) & ~(blockSize - 1))); |
| 316 | audioBuffer.insert(this->audioBuffer.end(), data, data + len); |
| 317 | SDL_UnlockAudio(); |
| 318 | return len; |
| 319 | } |
| 320 | |
| 321 | static U32 nextId = 0; |
| 322 | BOXEDWINE_MUTEX KDspAudio::mutex; |
no test coverage detected