-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudio.cs
197 lines (151 loc) · 5.69 KB
/
Audio.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using OpenTK.Audio.OpenAL;
using NAudio.Wave;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using static System.Console;
namespace Parsing
{
public static class Audio
{
public static double[] Record(double duration, int sampleRate = 44_100)
{
var device = ALC.CaptureOpenDevice(null, sampleRate, ALFormat.Mono16, 1024);
CheckAlcError();
var deviceName = ALC.GetString(ALDevice.Null, AlcGetString.CaptureDefaultDeviceSpecifier);
WriteLine($"recording {duration}s on {deviceName}");
ALC.CaptureStart(device);
CheckAlcError();
int samplesCount = (int)(sampleRate * duration);
var samples = new short[samplesCount];
int samplesRead = 0;
while (samplesRead < samples.Length)
{
int samplesAvailable = ALC.GetAvailableSamples(device);
CheckAlcError();
if (samplesAvailable > 512)
{
int samplesToRead = Math.Min(samplesAvailable, samples.Length - samplesRead);
ALC.CaptureSamples(device, ref samples[samplesRead], samplesToRead);
CheckAlcError();
samplesRead += samplesToRead;
}
Thread.Yield();
}
ALC.CaptureStop(device);
CheckAlcError();
if (!ALC.CaptureCloseDevice(device))
Error.WriteLine($"could not close {deviceName}");
CheckAlcError();
WriteLine("recording finished");
return samples
.Select(s => (double)s / short.MaxValue)
.ToArray();
static void CheckAlcError()
{
var error = AL.GetError();
if (error != ALError.NoError)
throw new Exception(AL.GetErrorString(error));
}
}
public static (WaveFormat format, int framesRead, double[] samples) File(string path, double length)
{
using var wfr = new WaveFileReader(path);
int framesToRead = (int) (length * wfr.WaveFormat.SampleRate);
var samples = new List<double>();
var frame = wfr.ReadNextSampleFrame();
int framesRead = 1;
while(frame != null && framesRead <= framesToRead)
{
foreach (var sample in frame)
samples.Add(sample);
frame = wfr.ReadNextSampleFrame();
framesRead++;
}
// one greater than actually read
framesRead--;
return (wfr.WaveFormat, framesRead, samples.ToArray());
}
}
public class Recording
{
private readonly string deviceName;
private readonly int sampleRate;
private readonly double frameLength;
private readonly Action<double[]> handle;
private bool running = false;
private ALCaptureDevice device;
public Recording(string deviceName, int sampleRate, double frameLength, Action<double[]> handle)
{
this.deviceName = deviceName;
this.sampleRate = sampleRate;
this.frameLength = frameLength;
this.handle = handle;
}
public Task Start()
{
int frameSize = (int)(frameLength * sampleRate);
var task = new Task(() =>
{
device = ALC.CaptureOpenDevice(deviceName, sampleRate, ALFormat.Mono16, 1024);
CheckAlcError();
ALC.CaptureStart(device);
CheckAlcError();
running = true;
while (running)
{
short[] samples = CaptureFrame(frameSize);
if (!running)
break; // break loop to avoid handling of stopped capture
new Task(() => Handle(samples)).Start();
}
ALC.CaptureStop(device);
CheckAlcError();
if (!ALC.CaptureCloseDevice(device))
Error.WriteLine($"could not close {deviceName}");
CheckAlcError();
});
task.Start();
return task;
}
public void Stop()
{
running = false;
}
private static void CheckAlcError()
{
var error = AL.GetError();
if (error != ALError.NoError)
throw new Exception(AL.GetErrorString(error));
}
private short[] CaptureFrame(int frameSize)
{
int samplesCount = frameSize;
var samples = new short[samplesCount];
int samplesRead = 0;
while (running && samplesRead < samples.Length) // break capture loop if stop requested
{
int samplesAvailable = ALC.GetAvailableSamples(device);
CheckAlcError();
if (samplesAvailable > 512)
{
int samplesToRead = Math.Min(samplesAvailable, samples.Length - samplesRead);
ALC.CaptureSamples(device, ref samples[samplesRead], samplesToRead);
CheckAlcError();
samplesRead += samplesToRead;
}
//Thread.Yield(); //??
}
return samples;
}
private void Handle(short[] samples)
{
double[] samplesNormalized = samples
.Select(s => (double)s / short.MaxValue)
.ToArray();
handle(samplesNormalized);
}
}
}