-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoPlayer.cs
More file actions
48 lines (37 loc) · 1.5 KB
/
DemoPlayer.cs
File metadata and controls
48 lines (37 loc) · 1.5 KB
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
46
47
48
using Demo.Generator;
using OldBit.Beep;
namespace Demo;
/// <summary>
/// Demonstrates how to play a sequence of notes.
/// </summary>
public class DemoPlayer(AudioFormat audioFormat, int sampleRate, int channelCount, WaveType waveType, int volume)
{
private const float NoteC5 = 523.25f;
private const float NoteE5 = 659.25f;
private const float NoteG5 = 783.99f;
public async Task PlayAsync()
{
var playerC5 = Task.Run(async () => { await StartPlayerAsync(NoteC5); });
await Task.Delay(TimeSpan.FromSeconds(1));
var playerE5 = Task.Run(async () => { await StartPlayerAsync(NoteE5); });
await Task.Delay(TimeSpan.FromSeconds(1));
var playerG5 = Task.Run(async () => { await StartPlayerAsync(NoteG5); });
await Task.WhenAll(playerC5, playerE5, playerG5);
}
private async Task StartPlayerAsync(float note)
{
var waveGenerator = WaveGeneratorFactory.CreateWaveGenerator(audioFormat, waveType, sampleRate, channelCount);
var audioData = waveGenerator.Generate(note, TimeSpan.FromSeconds(3)).ToArray();
using var audioPlayer = new AudioPlayer(audioFormat, sampleRate, channelCount,
new PlayerOptions
{
BufferSizeInBytes = 32768,
ThrowOnUnsupportedPlatform = true,
});
audioPlayer.Volume = volume;
audioPlayer.Start();
await audioPlayer.EnqueueAsync(audioData);
await Task.Delay(3100);
audioPlayer.Stop();
}
}