Files
sbox-public/engine/Sandbox.Engine/Systems/Audio/AudioMeter.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

67 lines
1.1 KiB
C#

namespace Sandbox.Audio;
/// <summary>
/// Allows the capture and monitor of an audio source
/// </summary>
public class AudioMeter
{
public struct Frame
{
public float MaxLevelLeft { get; set; }
public float MaxLevelRight { get; set; }
public float MaxLevel => Math.Max( MaxLevelLeft, MaxLevelRight );
/// <summary>
/// The amount of individual voices playing
/// </summary>
public int VoiceCount { get; set; }
}
List<Frame> Frames = new( 64 );
internal AudioMeter()
{
Add( new Frame() );
}
internal void Add( MultiChannelBuffer mix, int voiceCount )
{
Frame f = new Frame();
f.VoiceCount = voiceCount;
f.MaxLevelLeft = mix.Get( AudioChannel.Left ).LevelMax;
if ( mix.ChannelCount > 1 )
{
f.MaxLevelRight = mix.Get( AudioChannel.Right ).LevelMax;
}
else
{
f.MaxLevelLeft = f.MaxLevelRight;
}
Add( f );
}
void Add( in Frame frame )
{
lock ( Frames )
{
if ( Frames.Count > 64 )
Frames.RemoveAt( Frames.Count - 1 );
Frames.Insert( 0, frame );
}
}
public Frame Current
{
get
{
lock ( Frames )
{
return Frames.LastOrDefault();
}
}
}
}