| 359 | } |
| 360 | |
| 361 | void VideoBackendPlayer::UpdateAudioBuffer(Span<byte> data, TimeSpan time, TimeSpan duration) |
| 362 | { |
| 363 | PROFILE_CPU(); |
| 364 | PROFILE_MEM(Video); |
| 365 | ZoneText(DebugUrl, DebugUrlLen); |
| 366 | AudioBufferTime = time; |
| 367 | AudioBufferDuration = duration; |
| 368 | if (!AudioBackend::Instance) |
| 369 | return; |
| 370 | |
| 371 | // Setup audio source |
| 372 | if (AudioSource == 0) |
| 373 | { |
| 374 | AudioSource = AudioBackend::Source::Add(AudioInfo, Vector3::Zero, Quaternion::Identity, AudioVolume, 1.0f, AudioPan, false, IsAudioSpatial, AudioAttenuation, AudioMinDistance, 1.0f); |
| 375 | IsAudioPlayPending = 1; |
| 376 | } |
| 377 | else |
| 378 | { |
| 379 | // Get the processed buffers count |
| 380 | int32 numProcessedBuffers = 0; |
| 381 | AudioBackend::Source::GetProcessedBuffersCount(AudioSource, numProcessedBuffers); |
| 382 | if (numProcessedBuffers > 0) |
| 383 | { |
| 384 | // Unbind processed buffers from the source |
| 385 | AudioBackend::Source::DequeueProcessedBuffers(AudioSource); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | // Get audio buffer |
| 390 | uint32 bufferId = AudioBuffers[NextAudioBuffer]; |
| 391 | if (bufferId == 0) |
| 392 | { |
| 393 | bufferId = AudioBackend::Buffer::Create(); |
| 394 | AudioBuffers[NextAudioBuffer] = bufferId; |
| 395 | } |
| 396 | NextAudioBuffer = (NextAudioBuffer + 1) % ARRAY_COUNT(AudioBuffers); |
| 397 | |
| 398 | // Update audio buffer |
| 399 | AudioDataInfo dataInfo = AudioInfo; |
| 400 | const uint32 samplesPerSecond = dataInfo.SampleRate * dataInfo.NumChannels; |
| 401 | const uint32 maxSamplesInData = (uint32)data.Length() * 8 / dataInfo.BitDepth; |
| 402 | const uint32 maxSamplesInDuration = (uint32)Math::CeilToInt(samplesPerSecond * duration.GetTotalSeconds()); |
| 403 | dataInfo.NumSamples = Math::Min(maxSamplesInData, maxSamplesInDuration); |
| 404 | AudioBackend::Buffer::Write(bufferId, data.Get(), dataInfo); |
| 405 | |
| 406 | // Append audio buffer |
| 407 | AudioBackend::Source::QueueBuffer(AudioSource, bufferId); |
| 408 | if (IsAudioPlayPending) |
| 409 | { |
| 410 | IsAudioPlayPending = 0; |
| 411 | AudioBackend::Source::Play(AudioSource); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | void VideoBackendPlayer::Tick() |
| 416 | { |