From 552ce3e495d3fc5b3cf40b1e0e867a78ad260548 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 19 Apr 2023 15:15:28 +0200 Subject: [PATCH] Add `AudioDataInfo.Length` --- Source/Engine/Audio/Audio.cpp | 5 +++++ Source/Engine/Audio/Audio.cs | 12 ++++++++++++ Source/Engine/Audio/AudioClip.cpp | 7 +------ Source/Engine/Audio/AudioClip.h | 11 ++++------- Source/Engine/Audio/Types.h | 7 ++++++- 5 files changed, 28 insertions(+), 14 deletions(-) create mode 100644 Source/Engine/Audio/Audio.cs diff --git a/Source/Engine/Audio/Audio.cpp b/Source/Engine/Audio/Audio.cpp index 14639a6a2..dd8eae629 100644 --- a/Source/Engine/Audio/Audio.cpp +++ b/Source/Engine/Audio/Audio.cpp @@ -44,6 +44,11 @@ const Char* ToString(AudioFormat value) } } +float AudioDataInfo::GetLength() const +{ + return (float)NumSamples / (float)Math::Max(1U, SampleRate * NumChannels); +} + Array Audio::Listeners; Array Audio::Sources; Array Audio::Devices; diff --git a/Source/Engine/Audio/Audio.cs b/Source/Engine/Audio/Audio.cs new file mode 100644 index 000000000..61a535357 --- /dev/null +++ b/Source/Engine/Audio/Audio.cs @@ -0,0 +1,12 @@ +// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. + +namespace FlaxEngine +{ + partial struct AudioDataInfo + { + /// + /// Gets the length of the audio data (in seconds). + /// + public float Length => (float)NumSamples / (float)Mathf.Max(1U, SampleRate * NumChannels); + } +} diff --git a/Source/Engine/Audio/AudioClip.cpp b/Source/Engine/Audio/AudioClip.cpp index 858fc5cc5..34cf60441 100644 --- a/Source/Engine/Audio/AudioClip.cpp +++ b/Source/Engine/Audio/AudioClip.cpp @@ -150,11 +150,6 @@ AudioClip::~AudioClip() ASSERT(_streamingTask == nullptr); } -float AudioClip::GetLength() const -{ - return AudioHeader.Info.NumSamples / static_cast(Math::Max(1U, AudioHeader.Info.SampleRate * AudioHeader.Info.NumChannels)); -} - float AudioClip::GetBufferStartTime(int32 bufferIndex) const { ASSERT(IsLoaded()); @@ -164,7 +159,7 @@ float AudioClip::GetBufferStartTime(int32 bufferIndex) const int32 AudioClip::GetFirstBufferIndex(float time, float& offset) const { ASSERT(IsLoaded()); - ASSERT(time >= 0 && time <= GetLength()); + time = Math::Clamp(time, 0.0f, GetLength()); for (int32 i = 0; i < _totalChunks; i++) { diff --git a/Source/Engine/Audio/AudioClip.h b/Source/Engine/Audio/AudioClip.h index f661c34c1..a8f064afe 100644 --- a/Source/Engine/Audio/AudioClip.h +++ b/Source/Engine/Audio/AudioClip.h @@ -107,7 +107,6 @@ public: /// /// Gets the audio data format. /// - /// The value. API_PROPERTY() FORCE_INLINE AudioFormat Format() const { return AudioHeader.Format; @@ -116,7 +115,6 @@ public: /// /// Gets the audio data info metadata. /// - /// The value. API_PROPERTY() FORCE_INLINE const AudioDataInfo& Info() const { return AudioHeader.Info; @@ -125,7 +123,6 @@ public: /// /// Returns true if the sound source is three dimensional (volume and pitch varies based on listener distance and velocity). /// - /// The value. API_PROPERTY() FORCE_INLINE bool Is3D() const { return AudioHeader.Is3D; @@ -134,7 +131,6 @@ public: /// /// Returns true if the sound is using data streaming. /// - /// The value. API_PROPERTY() FORCE_INLINE bool IsStreamable() const { return AudioHeader.Streamable; @@ -143,7 +139,6 @@ public: /// /// Returns true if the sound data is during streaming by an async task. /// - /// The streaming task existence value flag. API_PROPERTY() FORCE_INLINE bool IsStreamingTaskActive() const { return _streamingTask != nullptr; @@ -152,8 +147,10 @@ public: /// /// Gets the length of the audio clip (in seconds). /// - /// The value. - API_PROPERTY() float GetLength() const; + API_PROPERTY() float GetLength() const + { + return AudioHeader.Info.GetLength(); + } public: diff --git a/Source/Engine/Audio/Types.h b/Source/Engine/Audio/Types.h index a19f2f492..193b10045 100644 --- a/Source/Engine/Audio/Types.h +++ b/Source/Engine/Audio/Types.h @@ -30,7 +30,7 @@ const Char* ToString(AudioFormat value); /// /// Meta-data describing a chunk of audio. /// -API_STRUCT() struct AudioDataInfo +API_STRUCT(NoDefault) struct AudioDataInfo { DECLARE_SCRIPTING_TYPE_MINIMAL(AudioDataInfo); @@ -53,4 +53,9 @@ DECLARE_SCRIPTING_TYPE_MINIMAL(AudioDataInfo); /// The number of bits per sample. /// API_FIELD() uint32 BitDepth; + + /// + /// Gets the length of the audio data (in seconds). + /// + float GetLength() const; };