// ReSharper disable once UnusedParameter.Local public static void Main(string[] args) { var start = DateTime.Now; Debug.WriteLine("Starting at {0}", start); var builder = new StringBuilder(); var language = Environment.GetEnvironmentVariable("LANG") ?? "en"; Console.WriteLine(language); using (var stream = File.Open(SampleAudio, FileMode.Open, FileAccess.Read)) { try { /* * The API constructor is passed the websockets URL, callbacks for the messages it might receive, * the language to transcribe (as a .NET CultureInfo object) and stream to read data from. */ var config = new SmRtApiConfig(language) { OutputLocale = "en-GB", AddTranscriptCallback = s => builder.Append(s), AddTranscriptMessageCallback = s => Console.WriteLine(ToJson(s)), // The v2 appliances don't have partial transcripts, but rather "low-latency finals", so skip this bit. //AddPartialTranscriptMessageCallback = s => Console.WriteLine(ToJson(s)), ErrorMessageCallback = s => Console.WriteLine(ToJson(s)), WarningMessageCallback = s => Console.WriteLine(ToJson(s)), CustomDictionaryPlainWords = new[] { "speechmagic" }, CustomDictionarySoundsLikes = new Dictionary <string, IEnumerable <string> >(), Insecure = true, BlockSize = 8192, }; // We can do this here, or earlier. It's not used until .Run() is called on the API object. config.CustomDictionarySoundsLikes["gnocchi"] = new[] { "nokey", "noki" }; var api = new SmRtApi(RtUrl, stream, config ); // Run() will block until the transcription is complete. Console.WriteLine($"Connecting to {RtUrl}"); api.Run(); Console.WriteLine(builder.ToString()); } catch (AggregateException e) { Console.WriteLine(e); } } var finish = DateTime.Now; Debug.WriteLine("Starting at {0} -- {1}", finish, finish - start); Console.ReadLine(); }
// ReSharper disable once UnusedParameter.Local public static void Main(string[] args) { var originalColor = Console.ForegroundColor; var x = Console.CursorLeft; var y = Console.CursorTop; using (var rtsp = new RtspStream("rtsp://192.168.128.30:8554/test")) { try { rtsp.Go(); /* * The API constructor is passed the websockets URL, callbacks for the messages it might receive, * the language to transcribe (as a .NET CultureInfo object) and stream to read data from. */ var config = new SmRtApiConfig("en") { AddTranscriptCallback = s => { Console.SetCursorPosition(x, y); Console.Write(s); x = Console.CursorLeft; y = Console.CursorTop; }, AddPartialTranscriptMessageCallback = s => { Console.SetCursorPosition(x, y); Console.ForegroundColor = ConsoleColor.Cyan; Console.Write(s.transcript); Console.ForegroundColor = originalColor; }, ErrorMessageCallback = s => Console.WriteLine(ToJson(s)), WarningMessageCallback = s => Console.WriteLine(ToJson(s)), Insecure = true, }; var api = new SmRtApi(RtUrl, rtsp, config ); // Run() will block until the transcription is complete. Console.WriteLine($"Connecting to {RtUrl}"); api.Run(); } catch (AggregateException e) { Console.WriteLine(e); } } Console.WriteLine("End of stream"); Console.ReadLine(); }
/// <summary> /// Transcribe raw audio from a stream /// </summary> /// <param name="wsUrl">A websocket endpoint e.g. wss://192.168.1.10:9000/</param> /// <param name="stream">A stream to read input from</param> /// <param name="configuration">Configuration object (model, audio properties)</param> public SmRtApi(string wsUrl, Stream stream, SmRtApiConfig configuration) { Configuration = configuration; WsUrl = new Uri(wsUrl); _stream = stream; var src = new CancellationTokenSource(); CancelToken = src.Token; }
private static void RunTest(string language) { string expectedTranscript; var sampleAudioSource = Path.Combine(TestContext.CurrentContext.TestDirectory, SampleAudio); using (var transcript = File.OpenText(Path.Combine(TestContext.CurrentContext.TestDirectory, "expected_transcript.txt"))) { expectedTranscript = transcript.ReadToEnd(); } var builder = new StringBuilder(); using (var stream = File.Open(sampleAudioSource, FileMode.Open, FileAccess.Read)) { try { var config = new SmRtApiConfig(language); { config.AddTranscriptCallback = s => builder.Append(s); }; var api = new SmRtApi("wss://api.rt.speechmatics.io:9000/", stream, config ); // Run() will block until the transcription is complete. api.Run(); Console.WriteLine(builder.ToString()); } catch (AggregateException e) { Console.WriteLine(e); throw; } // TODO: Word error rate comparison //Assert.AreEqual(expectedTranscript, builder.ToString(), "Unexpected transcript in the bagging area"); } }
// ReSharper disable once UnusedParameter.Local public static void Main(string[] args) { // NAudio has two (three?) audio capture APIs // This is the first one I tried, but apparently the wasapi one is lower level and // therefore better. // // var waveSource = new WaveInEvent {WaveFormat = new WaveFormat(44100, 16, 1)}; // This is an example, but experiment shows that making the value too low will // result in incomplete buffers to send to the RT appliance, leading to bad // transcripts. // waveSource.BufferMilliseconds = 100; // waveSource.DataAvailable += WaveSourceOnDataAvailable; // waveSource.StartRecording(); var selectedDevice = ChooseDevice(); var wasapiClient = new WasapiCapture(selectedDevice); var sampleRate = wasapiClient.WaveFormat.SampleRate; var channels = wasapiClient.WaveFormat.Channels; IsStereo = channels == 2; Console.WriteLine("Sample rate {0}", sampleRate); Console.WriteLine("Bits per sample {0}", wasapiClient.WaveFormat.BitsPerSample); Console.WriteLine("Channels {0}", channels); Console.WriteLine("Encoding {0}", wasapiClient.WaveFormat.Encoding); wasapiClient.DataAvailable += WaveSourceOnDataAvailable; var t = new Task(() => { wasapiClient.StartRecording(); }); using (var stream = AudioStream) { try { /* * The API constructor is passed the websockets URL, callbacks for the messages it might receive, * the language to transcribe and stream to read data from. */ // Make sure the sampleRate matches the value in the wasapiClient object var config = new SmRtApiConfig("en", sampleRate, AudioFormatType.Raw, AudioFormatEncoding.PcmF32Le) { AddTranscriptCallback = Console.Write, // AddPartialTranscriptMessageCallback = s => Console.Write("* " + s.transcript), ErrorMessageCallback = s => Console.WriteLine(ToJson(s)), WarningMessageCallback = s => Console.WriteLine(ToJson(s)), Insecure = true, BlockSize = 8192 }; var api = new SmRtApi(RtUrl, stream, config ); // Start recording audio t.Start(); // Run() will block until the transcription is complete. Console.WriteLine($"Connecting to {RtUrl}"); api.Run(); } catch (AggregateException e) { Console.WriteLine(e); } } Console.WriteLine("End of stream"); Console.ReadLine(); }