]> wolfpit.net Git - hardware/bopit/.git/blob - BopIt/MainForm.cs
Integrated speech recognition
[hardware/bopit/.git] / BopIt / MainForm.cs
1 ´╗┐using Google.Cloud.Speech.V1;
2 using System;
3 using System.Collections.Generic;
4 using System.ComponentModel;
5 using System.Data;
6 using System.Drawing;
7 using System.IO;
8 using System.IO.Ports;
9 using System.Linq;
10 using System.Text;
11 using System.Threading;
12 using System.Threading.Tasks;
13 using System.Windows.Forms;
14 using System.Windows.Forms.DataVisualization.Charting;
15 using Timer = System.Timers.Timer;
16
17 namespace BopIt
18 {
19 public partial class MainForm : Form
20 {
21 private Timer progressReset = new Timer();
22 private Series wave = new Series();
23
24 public MainForm()
25 {
26 InitializeComponent();
27 }
28
29 private void MainForm_Load(object sender, EventArgs args)
30 {
31 var waveIn = new NAudio.Wave.WaveInEvent();
32 waveIn.DeviceNumber = 0;
33 waveIn.WaveFormat = new NAudio.Wave.WaveFormat(16000, 1);
34 waveIn.DataAvailable +=
35 (object waveSender, NAudio.Wave.WaveInEventArgs e) =>
36 {
37 //Dynamic waveform buffer offset, i want to fucking die btw
38 short[] waveValues = new short[(int)(e.BytesRecorded * .55)];
39 for (int i = 0 ; i < e.BytesRecorded; i += 2)
40 waveValues[(i / 2) + ((waveValues.Length - e.BytesRecorded / 2) / 2)] = (short)(BitConverter.ToInt16(e.Buffer, i) / 50);
41 try
42 {
43 waveform.Invoke((MethodInvoker)(() => waveform.Series[0].Points.DataBindY(waveValues)));
44 UpdateVolumeMeter(Math.Abs(waveValues[waveValues.Length / 2]));
45 }
46 catch (Exception ex)
47 {
48 Console.WriteLine(ex.Message);
49 }
50 };
51 waveIn.StartRecording();
52
53
54
55 new Thread((ThreadStart)delegate
56 {
57 while (true)
58 StreamingMicRecognizeAsync(10).Wait();
59 }).Start();
60
61 //Volume meter de-incrimenter
62 #region
63 progressReset.Interval = 50;
64 progressReset.Elapsed += (o, arg) =>
65 {
66 if (progressBar.Value > 0)
67 progressBar.Invoke((MethodInvoker)(() => progressBar.Value--));
68
69 };
70 progressReset.Start();
71 #endregion
72
73 wave.ChartType = SeriesChartType.Area;
74 wave.Color = Color.Blue;
75 }
76
77 private void UpdateVolumeMeter(int data)
78 {
79 if(data < progressBar.Maximum && data > progressBar.Value)
80 progressBar.Invoke((MethodInvoker)(() => progressBar.Value = data));
81 }
82 private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
83 {
84 progressReset.Dispose();
85 }
86
87 private string currentSentance = String.Empty;
88 private string lastTrigger = String.Empty;
89 async Task<object> StreamingMicRecognizeAsync(int seconds)
90 {
91 if (NAudio.Wave.WaveIn.DeviceCount < 1)
92 {
93 Console.WriteLine("No Mic");
94 return -1;
95 }
96 var speech = SpeechClient.Create();
97 var streamingCall = speech.StreamingRecognize();
98
99 // Write the initial request with the config.
100 await streamingCall.WriteAsync(
101 new StreamingRecognizeRequest()
102 {
103 StreamingConfig = new StreamingRecognitionConfig()
104 {
105 Config = new RecognitionConfig()
106 {
107 Encoding =
108 RecognitionConfig.Types.AudioEncoding.Linear16,
109 SampleRateHertz = 16000,
110 LanguageCode = "en",
111 },
112 InterimResults = true,
113 }
114 });
115 Task printResponses = Task.Run(async () =>
116 {
117 while (await streamingCall.ResponseStream.MoveNext(
118 default(CancellationToken)))
119 {
120 foreach (var result in streamingCall.ResponseStream
121 .Current.Results)
122 {
123 var sentanceResult = result.Alternatives.Last().Transcript;
124 var newWordList = sentanceResult.Split(' ');
125 var currentWordList = currentSentance.Split(' ');
126
127 if (newWordList.Length > currentWordList.Length ||
128 newWordList.First() != currentWordList.First())
129 {
130 currentSentance = sentanceResult;
131 textBox1.Invoke((MethodInvoker)(() => textBox1.AppendText(newWordList.Last() + Environment.NewLine)));
132 if ( newWordList.Last() != lastTrigger)
133 {
134 textBox1.Invoke((MethodInvoker)(() => textBox1.AppendText("TRIGGER: " + newWordList.Last() + Environment.NewLine)));
135 lastTrigger = newWordList.Last();
136 }
137 Console.WriteLine(newWordList.Last());
138 }
139 }
140 }
141 });
142
143 object writeLock = new object();
144 bool writeMore = true;
145 var waveIn = new NAudio.Wave.WaveInEvent();
146 waveIn.DeviceNumber = 0;
147 waveIn.WaveFormat = new NAudio.Wave.WaveFormat(16000, 1);
148 waveIn.DataAvailable +=
149 (object sender, NAudio.Wave.WaveInEventArgs args) =>
150 {
151 lock (writeLock)
152 {
153 if (!writeMore) return;
154 streamingCall.WriteAsync(
155 new StreamingRecognizeRequest()
156 {
157 AudioContent = Google.Protobuf.ByteString
158 .CopyFrom(args.Buffer, 0, args.BytesRecorded)
159 }).Wait();
160 }
161 };
162 waveIn.StartRecording();
163 Console.WriteLine("Reconnecting stream");
164 await Task.Delay(TimeSpan.FromSeconds(seconds));
165 // Stop recording and shut down.
166 waveIn.StopRecording();
167 lock (writeLock) writeMore = false;
168 await streamingCall.WriteCompleteAsync();
169 await printResponses;
170 return 0;
171 }
172
173 private static bool IsTrigger(string word)
174 {
175 foreach (var trigger in File.ReadAllLines("beeMovie.txt"))
176 if (word == trigger)
177 return true;
178 return false;
179 }
180 }
181 }