TranscribeLive

 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
using var client = new AssemblyAIClient(apiKey);

// - You need to have `sox` installed on your system. If not, run:
//   macOS: brew install sox (macOS)
//   Linux: sudo apt-get install sox libsox-fmt-all
//   Windows: manually download and install sox, and add sox to your PATH environment variable.
//            https://sourceforge.net/projects/sox/

// 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
// {
//     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 doesn't work on Windows
    OperatingSystem.IsWindows() ? "-t waveaudio default" : "--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();
soxProcess.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();