Beeper implemented but has noise on the port - to fix

This commit is contained in:
2026-04-21 16:22:30 +01:00
parent dcbb505145
commit b6eb77318d
4 changed files with 65 additions and 8 deletions

41
Desktop/BeeperDevice.cs Normal file
View File

@@ -0,0 +1,41 @@
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);
}
}
}