.Net API contributed examples

DeepSpeechWPF

This examples demonstrates using the .Net Framework DeepSpeech NuGet to build a graphical Windows application using DeepSpeech

  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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
using CSCore;
using CSCore.CoreAudioAPI;
using CSCore.SoundIn;
using CSCore.Streams;
using DeepSpeechClient.Interfaces;
using Microsoft.Win32; 
using System;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace DeepSpeechWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly IDeepSpeech _sttClient;
         
        private const uint BEAM_WIDTH = 500;
        private const float LM_ALPHA = 0.75f;
        private const float LM_BETA = 1.85f;



        #region Streaming

        private readonly WasapiCapture _audioCapture;

        private MMDeviceCollection _audioCaptureDevices;
        private SoundInSource _soundInSource;
        private IWaveSource _convertedSource;

        /// <summary>
        /// Queue that prevents feeding data to the inference engine if it is busy.
        /// </summary>
        private ConcurrentQueue<short[]> _bufferQueue = new ConcurrentQueue<short[]>();

        private int _threadSafeBoolBackValue = 0;

        /// <summary>
        /// Lock to process items in the queue one at time.
        /// </summary>
        public bool IsBusy
        {
            get => (Interlocked.CompareExchange(ref _threadSafeBoolBackValue, 1, 1) == 1);
            set
            {
                if (value) Interlocked.CompareExchange(ref _threadSafeBoolBackValue, 1, 0);
                else Interlocked.CompareExchange(ref _threadSafeBoolBackValue, 0, 1);
            }
        }
        #endregion

        public MainWindow()
        {
            InitializeComponent();
            _sttClient = new DeepSpeechClient.DeepSpeech();

            //if you want to record from the mic change to this
            //_audioCapture = new WasapiCapture();

            //we capture the windows audio output
            _audioCapture = new WasapiLoopbackCapture();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            LoadAvailableCaptureDevices();

            Task.Run(()=>
            {
                try
                {
                    _sttClient.CreateModel("output_graph.pbmm", "alphabet.txt", BEAM_WIDTH);
                    Dispatcher.Invoke(() => { EnableControls(); });
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    Close();
                }
            });
        }

        /// <summary>
        /// Loads all the available audio capture devices.
        /// </summary>
        private void LoadAvailableCaptureDevices()
        {
            DataFlow dataFlow = DataFlow.Render;  //Use render to get output devices

            // Use capture to get input devices such a microphone
            // DataFlow dataFlow = DataFlow.Capture;

            _audioCaptureDevices = MMDeviceEnumerator.EnumerateDevices(dataFlow, DeviceState.Active); //we get only enabled devices
            foreach (var device in _audioCaptureDevices)
            {
                cbxAudioInputs.Items.Add(device.FriendlyName);
            }
            if (_audioCaptureDevices.Count > 0)
            {
                cbxAudioInputs.SelectedIndex = 0;
            }
        }

        private void EnableControls()
        {
            btnEnableLM.IsEnabled = true;
            btnOpenFile.IsEnabled = true;
            btnStartRecording.IsEnabled = true;
        }

        private async void BtnTranscript_Click(object sender, RoutedEventArgs e)
        {
            txtResult.Text = string.Empty;
            btnTranscript.IsEnabled = false;
            lblStatus.Content = "Running inference...";
            Stopwatch watch = new Stopwatch();
            var waveBuffer = new NAudio.Wave.WaveBuffer(File.ReadAllBytes(txtFileName.Text));
            using (var waveInfo = new NAudio.Wave.WaveFileReader(txtFileName.Text))
            {
                Console.WriteLine("Running inference....");

                watch.Start();
                await Task.Run(() =>
                {
                    string speechResult = _sttClient.SpeechToText(waveBuffer.ShortBuffer, Convert.ToUInt32(waveBuffer.MaxSize / 2), 16000);
                    watch.Stop();
                    Dispatcher.Invoke(() =>
                    {
                        txtResult.Text = $"Audio duration: {waveInfo.TotalTime.ToString()} {Environment.NewLine}" +
                            $"Inference took: {watch.Elapsed.ToString()} {Environment.NewLine}" +
                            $"Recognized text: {speechResult}";
                    });
                });
            }
            waveBuffer.Clear();
            lblStatus.Content = string.Empty;
            btnTranscript.IsEnabled = true;
        }

        private async void BtnEnableLM_Click(object sender, RoutedEventArgs e)
        {
            lblStatus.Content = "Loading LM.....";
            btnEnableLM.IsEnabled = false;
            await Task.Run(() =>
            {
                try
                {
                    _sttClient.EnableDecoderWithLM("lm.binary", "trie", LM_ALPHA, LM_BETA);
                    Dispatcher.Invoke(() => lblStatus.Content = "LM loaded.");
                }
                catch (Exception ex)
                {
                    Dispatcher.Invoke(() => btnEnableLM.IsEnabled = true);
                    MessageBox.Show(ex.Message);
                }
            });
        }

        private void BtnOpenFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog
            {
                Filter = "wav Files |*.wav",
                Multiselect = false,
                Title = "Please select a wav file."
            };

            if ((bool)dialog.ShowDialog())
            {
                txtFileName.Text = dialog.FileName;
                btnTranscript.IsEnabled = true;
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            _sttClient.Dispose();
            base.OnClosing(e);
        }

        private void CbxAudioInputs_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            btnStartRecording.IsEnabled = false;
            btnStopRecording.IsEnabled = false;
            if (_audioCapture.RecordingState == RecordingState.Recording)
            {
                _audioCapture.Stop();
                _soundInSource.Dispose();
                _convertedSource.Dispose();
                _audioCapture.DataAvailable -= _capture_DataAvailable;
                _sttClient.FreeStream(); //this a good example of FreeStream, the user changed the audio input, so we no longer need the current stream
            }
            if (_audioCaptureDevices!=null)
            {
                _audioCapture.Device = _audioCaptureDevices[cbxAudioInputs.SelectedIndex]; 
            }
            InitilizeAudioCapture();
        }

       

        /// <summary>
        /// Initializes the recorder and setup the native stream.
        /// </summary>
        private void InitilizeAudioCapture()
        {
            _audioCapture.Initialize();
            _audioCapture.DataAvailable += _capture_DataAvailable;
            _soundInSource = new SoundInSource(_audioCapture) { FillWithZeros = false };  
            //create a source, that converts the data provided by the
            //soundInSource to required by the deepspeech model
            _convertedSource = _soundInSource
               .ChangeSampleRate(16000) // sample rate
               .ToSampleSource()
               .ToWaveSource(16); //bits per sample
             
            _convertedSource = _convertedSource.ToMono();
            btnStartRecording.IsEnabled = true;
            
        }

        private void _capture_DataAvailable(object sender, DataAvailableEventArgs e)
        {
            //read data from the converedSource
            //important: don't use the e.Data here
            //the e.Data contains the raw data provided by the 
            //soundInSource which won't have the deepspeech required audio format
            byte[] buffer = new byte[_convertedSource.WaveFormat.BytesPerSecond / 2];

            int read;
            //int bytesReadIndex = 0;
            //keep reading as long as we still get some data
            while ((read = _convertedSource.Read(buffer, 0, buffer.Length)) > 0)
            { 
                short[] sdata = new short[(int)Math.Ceiling(Convert.ToDecimal(read / 2))];
                Buffer.BlockCopy(buffer, 0, sdata, 0, read);
                _bufferQueue.Enqueue(sdata);
                Task.Run(() => OnNewData());
            } 
        }


        private void BtnStartRecording_Click(object sender, RoutedEventArgs e)
        {
            _sttClient.CreateStream(16000);
            _audioCapture.Start();
            btnStartRecording.IsEnabled = false;
            btnStopRecording.IsEnabled = true;
        }

        private async void BtnStopRecording_Click(object sender, RoutedEventArgs e)
        {
            btnStartRecording.IsEnabled = false;
            btnStopRecording.IsEnabled = false;
            _audioCapture.Stop();
            await Task.Run(async () =>
            {
                while (!_bufferQueue.IsEmpty && IsBusy) //we wait for all the queued buffers to be processed
                {
                    await Task.Delay(90);
                }
                string sttResult = _sttClient.FinishStream();
                Dispatcher.Invoke(() => txtResult.Text = sttResult);
            });
            btnStartRecording.IsEnabled = true;
        }

        /// <summary>
        /// Starts processing data from the queue.
        /// </summary>
        private void OnNewData()
        {
            while (!IsBusy && !_bufferQueue.IsEmpty)
            {
                if (_bufferQueue.TryDequeue(out short[] buffer))
                {
                    IsBusy = true;
                    _sttClient.FeedAudioContent(buffer, Convert.ToUInt32(buffer.Length));
                    IsBusy = false;
                }
            }
        }
    }
}

Full source code available under examples/net_framework/DeepSpeechWPF/.