MCPcopy Create free account
hub / github.com/FlaxEngine/FlaxEngine / UpdateAudioBuffer

Method UpdateAudioBuffer

Source/Engine/Video/Video.cpp:361–413  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

359}
360
361void 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
415void VideoBackendPlayer::Tick()
416{

Callers 2

ReadStreamFunction · 0.80
ReadCodecOutputFunction · 0.80

Calls 8

QueueBufferFunction · 0.85
AddFunction · 0.50
CreateFunction · 0.50
CeilToIntFunction · 0.50
MinFunction · 0.50
WriteFunction · 0.50
LengthMethod · 0.45
GetMethod · 0.45

Tested by

no test coverage detected