-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathGoogleVoiceSpeech.cs
More file actions
218 lines (165 loc) · 6.95 KB
/
Copy pathGoogleVoiceSpeech.cs
File metadata and controls
218 lines (165 loc) · 6.95 KB
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
// Copyright (c) 2016 steele of lowkeysoft.com
// http://lowkeysoft.com
//
// This software is provided 'as-is', without any express or implied warranty. In
// no event will the authors be held liable for any damages arising from the use
// of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not claim
// that you wrote the original software. If you use this software in a product,
// an acknowledgment in the product documentation would be appreciated but is not
// required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
// =============================================================================
//
// Acquired from https://github.com/steelejay/LowkeySpeech
//
using UnityEngine;
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Web;
[RequireComponent (typeof (AudioSource))]
public class GoogleVoiceSpeech : MonoBehaviour {
public GUIText TextBox;
struct ClipData
{
public int samples;
}
const int HEADER_SIZE = 44;
private int minFreq;
private int maxFreq;
private bool micConnected = false;
//A handle to the attached AudioSource
private AudioSource goAudioSource;
public string apiKey;
// Use this for initialization
void Start () {
//Check if there is at least one microphone connected
if(Microphone.devices.Length <= 0)
{
//Throw a warning message at the console if there isn't
Debug.LogWarning("Microphone not connected!");
}
else //At least one microphone is present
{
//Set 'micConnected' to true
micConnected = true;
//Get the default microphone recording capabilities
Microphone.GetDeviceCaps(null, out minFreq, out maxFreq);
//According to the documentation, if minFreq and maxFreq are zero, the microphone supports any frequency...
if(minFreq == 0 && maxFreq == 0)
{
//...meaning 44100 Hz can be used as the recording sampling rate
maxFreq = 44100;
}
//Get the attached AudioSource component
goAudioSource = this.GetComponent<AudioSource>();
}
}
void OnGUI()
{
//If there is a microphone
if(micConnected)
{
//If the audio from any microphone isn't being recorded
if(!Microphone.IsRecording(null))
{
//Case the 'Record' button gets pressed
if(GUI.Button(new Rect(Screen.width/2-100, Screen.height/2-25, 200, 50), "Record"))
{
//Start recording and store the audio captured from the microphone at the AudioClip in the AudioSource
goAudioSource.clip = Microphone.Start( null, true, 7, maxFreq); //Currently set for a 7 second clip
}
}
else //Recording is in progress
{
//Case the 'Stop and Play' button gets pressed
if(GUI.Button(new Rect(Screen.width/2-100, Screen.height/2-25, 200, 50), "Stop and Play!"))
{
float filenameRand = UnityEngine.Random.Range (0.0f, 10.0f);
string filename = "testing" + filenameRand;
Microphone.End(null); //Stop the audio recording
Debug.Log( "Recording Stopped");
if (!filename.ToLower().EndsWith(".wav")) {
filename += ".wav";
}
var filePath = Path.Combine("testing/", filename);
filePath = Path.Combine(Application.persistentDataPath, filePath);
Debug.Log("Created filepath string: " + filePath);
// Make sure directory exists if user is saving to sub dir.
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
SavWav.Save (filePath, goAudioSource.clip); //Save a temporary Wav File
Debug.Log( "Saving @ " + filePath);
//Insert your API KEY here.
string apiURL = "https://speech.googleapis.com/v1/speech:recognize?&key=";
string Response;
Debug.Log( "Uploading " + filePath);
Response = HttpUploadFile (apiURL, filePath, "file", "audio/wav; rate=44100");
Debug.Log ("Response String: " +Response);
var jsonresponse = SimpleJSON.JSON.Parse(Response);
if (jsonresponse != null) {
string resultString = jsonresponse ["result"] [0].ToString ();
var jsonResults = SimpleJSON.JSON.Parse (resultString);
string transcripts = jsonResults ["alternative"] [0] ["transcript"].ToString ();
Debug.Log ("transcript string: " + transcripts );
TextBox.text = transcripts;
}
//goAudioSource.Play(); //Playback the recorded audio
File.Delete(filePath); //Delete the Temporary Wav file
}
GUI.Label(new Rect(Screen.width/2-100, Screen.height/2+25, 200, 50), "Recording in progress...");
}
}
else // No microphone
{
//Print a red "Microphone not connected!" message at the center of the screen
GUI.contentColor = Color.red;
GUI.Label(new Rect(Screen.width/2-100, Screen.height/2-25, 200, 50), "Microphone not connected!");
}
}
public string HttpUploadFile(string url, string file, string paramName, string contentType) {
System.Net.ServicePointManager.ServerCertificateValidationCallback += (o, certificate, chain, errors) => true;
Debug.Log(string.Format("Uploading {0} to {1}", file, url));
Byte[] bytes = File.ReadAllBytes(file);
String file64 = Convert.ToBase64String(bytes,
Base64FormattingOptions.None);
Debug.Log(file64);
try
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{ \"config\": { \"languageCode\" : \"en-US\" }, \"audio\" : { \"content\" : \"" + file64 + "\"}}";
Debug.Log(json);
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Debug.Log(httpResponse);
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
Debug.Log("Response:" + result);
}
} catch (WebException ex) {
var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
Debug.Log(resp);
}
return "empty";
}
}