The NAudio NuGet-Package is quite easy to use. I have a working snipped here that you can modify if you want:
void Main()
{
var sine = new SineGenerator();
var waveOut = new WaveOut();
waveOut.DesiredLatency = 100;
waveOut.Init(sine);
waveOut.Play();
var random = new Random();
while (true)
{
Thread.Sleep(100);
sine.Frequency = random.Next(100, 500);
}
waveOut.Stop();
}
public class SineGenerator : IWaveProvider
{
private WaveFormat waveFormat;
private int sample;
public SineGenerator()
: this(44100, 1)
{
Frequency = 1000;
Amplitude = 0.25f;
}
public float Frequency { get; set;}
public float Amplitude { get; set; }
public SineGenerator(int sampleRate, int channels)
{
SetWaveFormat(sampleRate, channels);
}
public void SetWaveFormat(int sampleRate, int channels)
{
this.waveFormat = WaveFormat.CreateIeeeFloatWaveFormat(sampleRate, channels);
}
public int Read(byte[] buffer, int offset, int count)
{
WaveBuffer waveBuffer = new WaveBuffer(buffer);
int samplesRequired = count / 4;
int samplesRead = this.Read(waveBuffer.FloatBuffer, offset / 4, samplesRequired);
return samplesRead * 4;
}
public int Read(float[] buffer, int offset, int sampleCount)
{
int sampleRate = WaveFormat.SampleRate;
int channels = WaveFormat.Channels;
double p = 2.0 * Math.PI / channels * Frequency;
for (int n = 0; n < sampleCount; n += channels)
{
float value = (float)(Amplitude * Math.Sin((sample * p) / sampleRate));
var b = Convert.ToByte((value + 0.5) * 255);
value = (float)b / 255;
for (int c = 0; c < channels; c++)
{
buffer[n + offset + c] = value;
}
sample += channels;
if (sample == int.MaxValue) sample = 0;
}
return sampleCount;
}
public WaveFormat WaveFormat
{
get { return waveFormat; }
}
}
See also this thread for more solutions.