41 lines
1.2 KiB
C#
41 lines
1.2 KiB
C#
using NAudio.Wave;
|
|
using System;
|
|
|
|
namespace Desktop
|
|
{
|
|
public class BeeperDevice
|
|
{
|
|
private WaveOutEvent _waveOut;
|
|
private BufferedWaveProvider _buffer;
|
|
|
|
public BeeperDevice()
|
|
{
|
|
_waveOut = new WaveOutEvent();
|
|
_waveOut.DesiredLatency = 50; // 100ms latency to prevent buffer stutter
|
|
|
|
// 44.1kHz, 1 channel (Mono), Float format
|
|
_buffer = new BufferedWaveProvider(WaveFormat.CreateIeeeFloatWaveFormat(44100, 1));
|
|
_buffer.BufferDuration = TimeSpan.FromSeconds(1);
|
|
_buffer.DiscardOnBufferOverflow = true;
|
|
|
|
_waveOut.Init(_buffer);
|
|
_waveOut.Play();
|
|
}
|
|
|
|
public void AddSample(bool isHigh)
|
|
{
|
|
//Buffer overrun check and dump
|
|
if (_buffer.BufferedDuration.TotalMilliseconds > 100)
|
|
{
|
|
_buffer.ClearBuffer();
|
|
}
|
|
|
|
// Convert the boolean into a physical sound wave (-0.2 or +0.2)
|
|
float sampleValue = isHigh ? 0.2f : -0.2f;
|
|
|
|
// Convert the float to bytes and drop it in the pipe
|
|
byte[] bytes = BitConverter.GetBytes(sampleValue);
|
|
_buffer.AddSamples(bytes, 0, 4);
|
|
}
|
|
}
|
|
} |