mirror of
https://github.com/DarthAffe/KeyboardAudioVisualizer.git
synced 2025-12-13 07:38:44 +00:00
60 lines
1.6 KiB
C#
60 lines
1.6 KiB
C#
using CSCore;
|
|
using CSCore.SoundIn;
|
|
using CSCore.Streams;
|
|
|
|
namespace KeyboardAudioVisualizer.AudioCapture
|
|
{
|
|
public class CSCoreAudioInput : IAudioInput
|
|
{
|
|
#region Properties & Fields
|
|
|
|
private WasapiCapture _capture;
|
|
private SoundInSource _soundInSource;
|
|
private SingleBlockNotificationStream _stream;
|
|
|
|
private readonly float[] _readBuffer = new float[2048];
|
|
|
|
public int SampleRate => _soundInSource?.WaveFormat?.SampleRate ?? -1;
|
|
|
|
#endregion
|
|
|
|
#region Event
|
|
|
|
public event AudioData DataAvailable;
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
public void Initialize()
|
|
{
|
|
_capture = new WasapiLoopbackCapture();
|
|
_capture.Initialize();
|
|
_soundInSource = new SoundInSource(_capture) { FillWithZeros = false };
|
|
|
|
_stream = _soundInSource.WaveFormat.SampleRate == 44100
|
|
? new SingleBlockNotificationStream(_soundInSource.ToStereo().ToSampleSource())
|
|
: new SingleBlockNotificationStream(_soundInSource.ChangeSampleRate(44100).ToStereo().ToSampleSource());
|
|
|
|
_soundInSource.DataAvailable += OnSoundDataAvailable;
|
|
|
|
_capture.Start();
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_capture?.Stop();
|
|
_capture?.Dispose();
|
|
}
|
|
|
|
private void OnSoundDataAvailable(object sender, DataAvailableEventArgs dataAvailableEventArgs)
|
|
{
|
|
int readCount;
|
|
while ((readCount = _stream.Read(_readBuffer, 0, _readBuffer.Length)) > 0)
|
|
DataAvailable?.Invoke(_readBuffer, 0, readCount);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|