-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTonePlayer.cs
45 lines (38 loc) · 1.13 KB
/
TonePlayer.cs
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
using System;
using System.Threading.Tasks;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;
public class TonePlayer : IDisposable
{
private readonly WaveOutEvent waveOut;
private readonly SignalGenerator signalGenerator;
private readonly VolumeSampleProvider volumeProvider;
public TonePlayer()
{
waveOut = new WaveOutEvent();
signalGenerator = new SignalGenerator();
volumeProvider = new VolumeSampleProvider(signalGenerator);
}
public async Task PlayPureToneAsync(int frequencyInHz, int durationInMillis, float volume)
{
await Task.Run(() =>
{
signalGenerator.Frequency = frequencyInHz;
signalGenerator.Gain = 0.2f; // Adjust the gain as needed
signalGenerator.Type = SignalGeneratorType.Sin;
volumeProvider.Volume = volume;
waveOut.Init(volumeProvider);
waveOut.Play();
Task.Delay(durationInMillis).Wait();
waveOut.Stop();
});
}
public void Stop()
{
waveOut?.Stop();
}
public void Dispose()
{
waveOut?.Dispose();
}
}