-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMicrophone.cs
More file actions
206 lines (174 loc) · 4.36 KB
/
Microphone.cs
File metadata and controls
206 lines (174 loc) · 4.36 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
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
198
199
200
201
202
203
204
205
206
using System.Runtime.InteropServices;
using PortAudioSharp;
namespace OpenAI;
/// <summary>
/// Implements a Microphone using PortAudio
/// </summary>
public sealed class Microphone : IDisposable
{
/// <summary>
///
/// </summary>
static Microphone()
{
PortAudio.Initialize();
}
/// <summary>
///
/// </summary>
public static void Terminate()
{
PortAudio.Terminate();
}
/// <summary>
///
/// </summary>
public Action<byte[], int>? PushCallback { get; set; }
/// <summary>
/// Sample rate
/// </summary>
public int SampleRate { get; set; } = 48_000;
/// <summary>
/// Chunk size
/// </summary>
public int ChunkSize { get; set; } = 32_000;
/// <summary>
/// Number of channels
/// </summary>
public int Channels { get; set; } = 1;
/// <summary>
/// Input device index
/// </summary>
public int DeviceIndex { get; set; } = PortAudio.NoDevice;
/// <summary>
/// Sample Format
/// </summary>
public SampleFormat SampleFormat { get; set; } = SampleFormat.Int16;
/// <summary>
///
/// </summary>
public bool IsMuted { get; private set; }
private PortAudioSharp.Stream? _stream;
private CancellationTokenSource? _exitToken;
/// <summary>
/// Start begins the listening on the microphone
/// </summary>
/// <returns></returns>
public bool Start()
{
if (_stream != null)
{
return false;
}
// reset exit token
_exitToken?.Dispose();
_exitToken = new CancellationTokenSource();
// Get the device info
if (DeviceIndex == PortAudio.NoDevice)
{
DeviceIndex = PortAudio.DefaultInputDevice;
if (DeviceIndex == PortAudio.NoDevice)
{
return false;
}
}
DeviceInfo info = PortAudio.GetDeviceInfo(DeviceIndex);
// Create the stream
_stream = new PortAudioSharp.Stream(
inParams: new StreamParameters
{
device = DeviceIndex,
channelCount = Channels,
sampleFormat = SampleFormat,
suggestedLatency = info.defaultLowInputLatency,
hostApiSpecificStreamInfo = IntPtr.Zero,
},
outParams: null,
sampleRate: SampleRate,
framesPerBuffer: (uint)ChunkSize,
streamFlags: StreamFlags.ClipOff,
callback: Сallback,
userData: IntPtr.Zero
);
// Start the stream
_stream.Start();
return true;
}
/// <summary>
///
/// </summary>
public void Stop()
{
// Check if we have a stream
if (_stream == null)
{
return;
}
// signal stop
_exitToken?.Cancel();
// Stop the stream
_stream.Stop();
_stream.Dispose();
_stream = null;
_exitToken?.Dispose();
_exitToken = null;
}
private StreamCallbackResult Сallback(
nint input,
nint output,
uint frameCount,
ref StreamCallbackTimeInfo timeInfo,
StreamCallbackFlags statusFlags,
nint userDataPtr)
{
// Check if the input is null
if (input == IntPtr.Zero)
{
return StreamCallbackResult.Continue;
}
// Check if the exit token is set
if (_exitToken is { IsCancellationRequested: true })
{
return StreamCallbackResult.Abort;
}
// copy and send the data
byte[] buf = new byte[frameCount * sizeof(Int16)];
if (IsMuted)
{
buf = new byte[buf.Length];
}
else
{
Marshal.Copy(input, buf, 0, buf.Length);
}
PushCallback?.Invoke(buf, buf.Length);
return StreamCallbackResult.Continue;
}
/// <summary>
///
/// </summary>
public void Mute()
{
if (_stream != null)
{
return;
}
IsMuted = true;
}
/// <summary>
///
/// </summary>
public void Unmute()
{
if (_stream != null)
{
return;
}
IsMuted = false;
}
/// <inheritdoc/>
public void Dispose()
{
Stop();
}
}