-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
63 lines (56 loc) · 1.67 KB
/
Program.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
using System.Diagnostics;
using AssemblyAI.Realtime;
// Set up the cancellation token, so we can stop the program with Ctrl+C
var cts = new CancellationTokenSource();
var ct = cts.Token;
Console.CancelKeyPress += (sender, e) => cts.Cancel();
// Set up the realtime transcriber
await using var transcriber = new RealtimeTranscriber(new RealtimeTranscriberOptions
{
ApiKey = Environment.GetEnvironmentVariable("ASSEMBLYAI_API_KEY"),
SampleRate = 16_000
});
transcriber.PartialTranscriptReceived.Subscribe(transcript =>
{
if (transcript.Text == "") return;
Console.WriteLine($"Partial transcript: {transcript.Text}");
});
transcriber.FinalTranscriptReceived.Subscribe(transcript =>
{
Console.WriteLine($"Final transcript: {transcript.Text}");
});
await transcriber.ConnectAsync();
var soxArguments = string.Join(' ', [
"--default-device",
"--no-show-progress",
"--rate 16000",
"--channels 1",
"--encoding signed-integer",
"--bits 16",
"--type wav",
"-" // pipe
]);
Console.WriteLine($"sox {soxArguments}");
using var soxProcess = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "sox",
Arguments = soxArguments,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
soxProcess.Start();
soxProcess.BeginErrorReadLine();
var soxOutputStream = soxProcess.StandardOutput.BaseStream;
var buffer = new Memory<byte>(new byte[4096]);
while (await soxOutputStream.ReadAsync(buffer, ct) > 0)
{
if (ct.IsCancellationRequested) break;
await transcriber.SendAudioAsync(buffer);
}
soxProcess.Kill();
await transcriber.CloseAsync();