-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathApp.LFE.cs
More file actions
221 lines (159 loc) · 6.08 KB
/
Copy pathApp.LFE.cs
File metadata and controls
221 lines (159 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using System.Runtime.InteropServices;
using System.Windows;
using SharpDX.DirectSound;
using SharpDX.Multimedia;
namespace MarvinsAIRA
{
public partial class App : Application
{
private bool _lfe_initialized = false;
private DirectSoundCapture? _lfe_directSoundCapture;
private CaptureBuffer? _lfe_captureBuffer;
private AutoResetEvent _lfe_autoResetEvent = new( false );
private float[,] _lfe_magnitude = new float[ 2, FFB_SAMPLES_PER_FRAME ];
private int _lfe_magnitudeIndex = 0;
private bool _lfe_keepThreadAlive = false;
private bool _lfe_threadRunning = false;
private const int LFE_360HZ_TO_8KHZ_SCALE = 22;
private const int LFE_FRAME_SIZE_IN_SAMPLES = FFB_SAMPLES_PER_FRAME * LFE_360HZ_TO_8KHZ_SCALE;
private const int LFE_BYTES_PER_SAMPLE = 2;
private const int LFE_FRAME_SIZE_IN_BYTES = LFE_FRAME_SIZE_IN_SAMPLES * LFE_BYTES_PER_SAMPLE;
private const int LFE_CAPTURE_BUFFER_FREQUENCY = 8000;
private const int LFE_CAPTURE_BUFFER_BITS_PER_SAMPLE = LFE_BYTES_PER_SAMPLE * 8;
private const int LFE_CAPTURE_BUFFER_SAMPLES = 360 * LFE_360HZ_TO_8KHZ_SCALE;
private const int LFE_CAPTURE_BUFFER_SIZE_IN_BYTES = LFE_CAPTURE_BUFFER_SAMPLES * LFE_BYTES_PER_SAMPLE;
public void InitializeLFE()
{
WriteLine( "InitializeLFE called.", true );
if ( _lfe_initialized )
{
UninitializeLFE();
}
WriteLine( "...enumerating recording devices (for LFE)..." );
DeviceInformation? selectedDeviceInformation = null;
SerializableDictionary<Guid, string> lfeDeviceList = [];
var deviceInformationList = DirectSoundCapture.GetDevices();
foreach ( var deviceInformation in deviceInformationList )
{
if ( deviceInformation.DriverGuid != Guid.Empty )
{
WriteLine( $"...we found the {deviceInformation.Description} device..." );
lfeDeviceList.Add( deviceInformation.DriverGuid, deviceInformation.Description );
if ( deviceInformation.DriverGuid == Settings.SelectedLFEDeviceGuid )
{
WriteLine( "...we found the selected recording device..." );
selectedDeviceInformation = deviceInformation;
}
}
}
Settings.UpdateLFEDeviceList( lfeDeviceList );
if ( Settings.LFEToFFBEnabled )
{
if ( selectedDeviceInformation != null )
{
_lfe_initialized = true;
WriteLine( "...initializing direct sound capture..." );
_lfe_directSoundCapture = new DirectSoundCapture( selectedDeviceInformation.DriverGuid );
WriteLine( "...direct sound capture initialized..." );
WriteLine( "...initializing capture buffer..." );
var captureBufferDescription = new CaptureBufferDescription
{
Format = new WaveFormat( LFE_CAPTURE_BUFFER_FREQUENCY, LFE_CAPTURE_BUFFER_BITS_PER_SAMPLE, 1 ),
BufferBytes = LFE_CAPTURE_BUFFER_SIZE_IN_BYTES
};
_lfe_captureBuffer = new CaptureBuffer( _lfe_directSoundCapture, captureBufferDescription );
WriteLine( "...capture buffer initialized..." );
WriteLine( "...initializing notification positions..." );
var notificationPositionArray = new NotificationPosition[ LFE_CAPTURE_BUFFER_SAMPLES / ( FFB_SAMPLES_PER_FRAME * LFE_360HZ_TO_8KHZ_SCALE ) ];
for ( var i = 0; i < notificationPositionArray.Length; i++ )
{
notificationPositionArray[ i ] = new()
{
Offset = i * LFE_FRAME_SIZE_IN_BYTES,
WaitHandle = _lfe_autoResetEvent
};
}
_lfe_captureBuffer.SetNotificationPositions( notificationPositionArray );
WriteLine( "...notification positions initialized..." );
WriteLine( "...starting capture..." );
_lfe_captureBuffer.Start( true );
WriteLine( "...capture started..." );
WriteLine( "...starting LFE thread..." );
Task.Run( LFEThread );
WriteLine( "...LFE thread started..." );
}
}
}
private void UninitializeLFE()
{
WriteLine( "UninitializeLFE called.", true );
if ( _lfe_threadRunning )
{
WriteLine( "...terminating LFE thread..." );
_lfe_keepThreadAlive = false;
_lfe_autoResetEvent.Set();
while ( _lfe_threadRunning )
{
Thread.Sleep( 0 );
}
WriteLine( "...LFE thread terminated..." );
}
if ( _lfe_captureBuffer != null )
{
WriteLine( "...uninitializing capture buffer..." );
_lfe_captureBuffer.Stop();
_lfe_captureBuffer.Dispose();
_lfe_captureBuffer = null;
WriteLine( "...capture buffer uninitialized..." );
}
for ( var i = 0; i < FFB_SAMPLES_PER_FRAME; i++ )
{
_lfe_magnitude[ 0, i ] = 0;
_lfe_magnitude[ 1, i ] = 0;
}
_lfe_initialized = false;
}
private void StopLFE()
{
UninitializeLFE();
}
private void LFEThread()
{
_lfe_keepThreadAlive = true;
_lfe_threadRunning = true;
var byteSpan = new Span<byte>( new byte[ LFE_FRAME_SIZE_IN_BYTES ] );
while ( _lfe_keepThreadAlive )
{
var signalReceived = _lfe_autoResetEvent?.WaitOne( 250 ) ?? false;
if ( signalReceived && _lfe_keepThreadAlive )
{
if ( _lfe_captureBuffer != null )
{
var currentCapturePosition = _lfe_captureBuffer.CurrentCapturePosition;
currentCapturePosition = ( currentCapturePosition / LFE_FRAME_SIZE_IN_BYTES ) * LFE_FRAME_SIZE_IN_BYTES;
var magnitudeIndex = ( _lfe_magnitudeIndex + 1 ) % 2;
var currentReadPosition = ( currentCapturePosition + LFE_CAPTURE_BUFFER_SIZE_IN_BYTES - LFE_FRAME_SIZE_IN_BYTES ) % LFE_CAPTURE_BUFFER_SIZE_IN_BYTES;
var dataStream = _lfe_captureBuffer.Lock( currentReadPosition, LFE_FRAME_SIZE_IN_BYTES, LockFlags.None, out var secondPart );
dataStream.Read( byteSpan );
var shortSpan = MemoryMarshal.Cast<byte, short>( byteSpan );
var sampleOffset = 0;
for ( var i = 0; i < FFB_SAMPLES_PER_FRAME; i++ )
{
var amplitudeSum = 0f;
for ( var j = 0; j < LFE_360HZ_TO_8KHZ_SCALE; j++ )
{
amplitudeSum += shortSpan[ sampleOffset ] / (float) short.MinValue;
sampleOffset++;
}
_lfe_magnitude[ magnitudeIndex, i ] = amplitudeSum / LFE_360HZ_TO_8KHZ_SCALE;
}
_lfe_captureBuffer.Unlock( dataStream, secondPart );
_lfe_magnitudeIndex = magnitudeIndex;
}
}
}
_lfe_threadRunning = false;
}
}
}