Add AudioDataInfo.Length

This commit is contained in:
Wojtek Figat
2023-04-19 15:15:28 +02:00
parent 1e1c296300
commit 552ce3e495
5 changed files with 28 additions and 14 deletions

View File

@@ -44,6 +44,11 @@ const Char* ToString(AudioFormat value)
}
}
float AudioDataInfo::GetLength() const
{
return (float)NumSamples / (float)Math::Max(1U, SampleRate * NumChannels);
}
Array<AudioListener*> Audio::Listeners;
Array<AudioSource*> Audio::Sources;
Array<AudioDevice> Audio::Devices;

View File

@@ -0,0 +1,12 @@
// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
namespace FlaxEngine
{
partial struct AudioDataInfo
{
/// <summary>
/// Gets the length of the audio data (in seconds).
/// </summary>
public float Length => (float)NumSamples / (float)Mathf.Max(1U, SampleRate * NumChannels);
}
}

View File

@@ -150,11 +150,6 @@ AudioClip::~AudioClip()
ASSERT(_streamingTask == nullptr);
}
float AudioClip::GetLength() const
{
return AudioHeader.Info.NumSamples / static_cast<float>(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++)
{

View File

@@ -107,7 +107,6 @@ public:
/// <summary>
/// Gets the audio data format.
/// </summary>
/// <returns>The value.</returns>
API_PROPERTY() FORCE_INLINE AudioFormat Format() const
{
return AudioHeader.Format;
@@ -116,7 +115,6 @@ public:
/// <summary>
/// Gets the audio data info metadata.
/// </summary>
/// <returns>The value.</returns>
API_PROPERTY() FORCE_INLINE const AudioDataInfo& Info() const
{
return AudioHeader.Info;
@@ -125,7 +123,6 @@ public:
/// <summary>
/// Returns true if the sound source is three dimensional (volume and pitch varies based on listener distance and velocity).
/// </summary>
/// <returns>The value.</returns>
API_PROPERTY() FORCE_INLINE bool Is3D() const
{
return AudioHeader.Is3D;
@@ -134,7 +131,6 @@ public:
/// <summary>
/// Returns true if the sound is using data streaming.
/// </summary>
/// <returns>The value.</returns>
API_PROPERTY() FORCE_INLINE bool IsStreamable() const
{
return AudioHeader.Streamable;
@@ -143,7 +139,6 @@ public:
/// <summary>
/// Returns true if the sound data is during streaming by an async task.
/// </summary>
/// <returns>The streaming task existence value flag.</returns>
API_PROPERTY() FORCE_INLINE bool IsStreamingTaskActive() const
{
return _streamingTask != nullptr;
@@ -152,8 +147,10 @@ public:
/// <summary>
/// Gets the length of the audio clip (in seconds).
/// </summary>
/// <returns>The value.</returns>
API_PROPERTY() float GetLength() const;
API_PROPERTY() float GetLength() const
{
return AudioHeader.Info.GetLength();
}
public:

View File

@@ -30,7 +30,7 @@ const Char* ToString(AudioFormat value);
/// <summary>
/// Meta-data describing a chunk of audio.
/// </summary>
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.
/// </summary>
API_FIELD() uint32 BitDepth;
/// <summary>
/// Gets the length of the audio data (in seconds).
/// </summary>
float GetLength() const;
};