mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-01-23 05:39:39 -05:00
This commit imports the C# engine code and game files, excluding C++ source code. [Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
67 lines
1.1 KiB
C#
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();
|
|
}
|
|
}
|
|
}
|
|
}
|