@@ -11,26 +11,25 @@ namespace VoiceCraft.Network.Audio.Effects
1111 public class DirectionalEffect : IAudioEffect
1212 {
1313 public static int SampleRate => Constants . SampleRate ;
14-
14+
1515 public EffectType EffectType => EffectType . Directional ;
16-
17- [ JsonIgnore ]
18- public ushort Bitmask { get ; set ; }
19-
16+
17+ [ JsonIgnore ] public ushort Bitmask { get ; set ; }
18+
2019 public event Action < IAudioEffect > ? OnDisposed ;
21-
20+
2221 public float WetDry
2322 {
2423 get ;
2524 set => field = Math . Clamp ( value , 0.0f , 1.0f ) ;
2625 } = 1.0f ;
27-
26+
2827 public IAudioEffectProcessor GetProcessor ( VoiceCraftEntity entity ) =>
2928 new DirectionalEffectProcessor ( this , entity ) ;
3029
3130 public void Update ( IAudioEffect audioEffect )
3231 {
33- if ( audioEffect is not DirectionalEffect directionalEffect )
32+ if ( audioEffect is not DirectionalEffect directionalEffect )
3433 throw new ArgumentException ( "Unexpected Audio Effect Type!" , nameof ( audioEffect ) ) ;
3534 Bitmask = directionalEffect . Bitmask ;
3635 WetDry = directionalEffect . WetDry ;
@@ -59,12 +58,11 @@ public void Dispose()
5958 }
6059 }
6160 }
62-
61+
6362 public class DirectionalEffectProcessor : IAudioEffectProcessor
6463 {
6564 private readonly DirectionalEffect _effect ;
66- private readonly SampleLerpVolume _lerpVolume1 ;
67- private readonly SampleLerpVolume _lerpVolume2 ;
65+ private readonly SampleLerpVolume [ ] _lerpVolume ;
6866 public IAudioEffect Effect => _effect ;
6967 public VoiceCraftEntity Entity { get ; }
7068 public event Action < IAudioEffectProcessor > ? OnDisposed ;
@@ -73,8 +71,11 @@ public DirectionalEffectProcessor(DirectionalEffect effect, VoiceCraftEntity ent
7371 {
7472 _effect = effect ;
7573 Entity = entity ;
76- _lerpVolume1 = new SampleLerpVolume ( Constants . SampleRate , TimeSpan . FromMilliseconds ( 20 ) ) ;
77- _lerpVolume2 = new SampleLerpVolume ( Constants . SampleRate , TimeSpan . FromMilliseconds ( 20 ) ) ;
74+ _lerpVolume =
75+ [
76+ new SampleLerpVolume ( Constants . SampleRate , TimeSpan . FromMilliseconds ( 20 ) ) ,
77+ new SampleLerpVolume ( Constants . SampleRate , TimeSpan . FromMilliseconds ( 20 ) )
78+ ] ;
7879 Effect . OnDisposed += _ => Dispose ( ) ;
7980 }
8081
@@ -87,20 +88,18 @@ public void Process(VoiceCraftEntity to, Span<float> buffer)
8788 to . Rotation . Y * Math . PI / 180 ) ;
8889 var left = ( float ) Math . Max ( 0.5 - Math . Cos ( rot ) * 0.5 , 0.2 ) ;
8990 var right = ( float ) Math . Max ( 0.5 + Math . Cos ( rot ) * 0.5 , 0.2 ) ;
90-
91- _lerpVolume1 . TargetVolume = left ;
92- _lerpVolume2 . TargetVolume = right ;
9391
94- for ( var i = 0 ; i < buffer . Length ; i += 2 )
92+ _lerpVolume [ 0 ] . TargetVolume = left ;
93+ _lerpVolume [ 1 ] . TargetVolume = right ;
94+
95+ for ( var i = 0 ; i < buffer . Length ; i ++ )
9596 {
96- var leftOutput = _lerpVolume1 . Transform ( buffer [ i ] ) ;
97- var rightOutput = _lerpVolume2 . Transform ( buffer [ i + 1 ] ) ;
98-
99- buffer [ i ] = leftOutput * _effect . WetDry + buffer [ i ] * ( 1.0f - _effect . WetDry ) ;
100- buffer [ i + 1 ] = rightOutput * _effect . WetDry + buffer [ i + 1 ] * ( 1.0f - _effect . WetDry ) ;
101-
102- _lerpVolume1 . Step ( ) ;
103- _lerpVolume2 . Step ( ) ;
97+ for ( var c = 0 ; c < 2 && c + i < buffer . Length ; c ++ )
98+ {
99+ var output = _lerpVolume [ c ] . Transform ( buffer [ i ] ) ;
100+ buffer [ i ] = output * _effect . WetDry + buffer [ i ] * ( 1.0f - _effect . WetDry ) ;
101+ _lerpVolume [ c ] . Step ( ) ;
102+ }
104103 }
105104 }
106105
@@ -116,7 +115,7 @@ public void Dispose()
116115 }
117116 }
118117 }
119-
118+
120119 [ JsonSourceGenerationOptions ( WriteIndented = true ) ]
121120 [ JsonSerializable ( typeof ( DirectionalEffect ) , GenerationMode = JsonSourceGenerationMode . Metadata ) ]
122121 public partial class DirectionalEffectGenerationContext : JsonSerializerContext ;
0 commit comments