Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SpeechRecognizer.java « SpeechRecognizer « Cluster « com « src - github.com/ClusterM/android-speech-recognition.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 69e79672789e4737a1680f22914b9e9dd15f6eff (plain)
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package com.Cluster.SpeechRecognizer;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javaFlacEncoder.FLACEncoder;
import javaFlacEncoder.FLACStreamOutputStream;
import javaFlacEncoder.StreamConfiguration;

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.util.Log;

public class SpeechRecognizer
{
	final String API_KEY = "YOUR_API_KEY";
	final String TAG = "SpeechRecognizer";
	// ×àñòîòà äèñêðåòèçàöèè
	final int SAMPLE_RATE = 8000;
	// Äëèòåëüíîñòü àíàëèçèðóåìûõ ñýìïëîâ
	final int TIMER_INTERVAL = 100;
	// Âðåìÿ, â òå÷åíèè êîòîðîãî àíàëèçèðóåòñÿ ãðîìêîñòü
	final int MAX_ANALYZE_LENGTH = 3000;
	// Ñîîòíîøåíèå ãðîìêîñòè ðå÷è ê ãðîìêîñòè ôîíà
	final float VOLUME_RATIO = 1.25f;
	// Ìàêñèìàëüíîå êîë-âî îøèáîê, ïîñëå êîòîðîãî óâåëè÷èâàåòñÿ àìïëèòóäà
	// ñðàáàòûâàíèÿ
	final float MAX_ERROR_COUNT = 3;
	// Åñëè â òå÷åíèè ýòîãî âðåìåíè íåò îøèáîê, ñáðàñûâàåì ñ÷¸ò÷èê îøèáîê
	final int MAX_NO_ERROR_TIME = 10000;

	// Ñëóøàòåëü
	VoiceRecognizedListener voiceRecognizedListener = null;
	// Êàêîé ïðîöåíò çíà÷åíèé ãðîìêîñòè äîëæåí ïðåâûøàòü óðîâåíü îïðåäåëåíèÿ
	float detectRatio = 0.25f;
	// Ñêîëüêî íóæíî ìîë÷àòü ïåðåä ðàñïîçíàâàíèåì ôðàçû â ìèëëèñåêóíäàõ
	int maxSilenceLength = 500;
	// Ìèíèìàëüíàÿ äëèíà ôðàçû â ìèëëèñåêóíäàõ
	int minRecordLength = 500; 
	// Ìàêñèìàëüíàÿ äëèíà ôðàçû â ìèëëèñåêóíäàõ
	int maxRecordLength = 5000;
	// ßçûêà
	String language = "en-US";
	// Ìàêñèìàëüíîå êîë-âî ðåçóëüòàòîâ
	int maxResults = 5;

	// Ãðîìêîñòü, ñ êîòîðîé äîëæåí çâó÷àòü ãîëîñ
	int detectLevel = 32767;
	// Ïèøåì ëè çâóê â äàííûõ ìîìåíò
	boolean recording = false;
	// Äëèíà çàïèñè
	int recordLength = 0;
	// Äëèíà òèøèíû
	int silenceLength = 0;
	// Ñêîëüêî çâóêà ìû ïðîàíàëèçèðîâàëè íà ãðîìêîñòü
	int analyzeLength = 0;
	// Êîëè÷åñòâî îøèáîê ðàñïîçíàâàíèÿ ïîäðÿä
	int errorCount = 0;
	// Âðåìÿ, â òå÷åíèè êîòîðîãî íåò îøèáîê
	int noErrorTimer = 0;
	// Ìàêñèìàëüíàÿ ãðîìêîñòü ôîíà, êîãäà ÷åëîâåê ìîë÷èò
	int maxSilenceLevel = 0;
	// Ìàêñèìàëüíàÿ ãðîìêîñòü
	int maxLevel = 0;
	
	AudioRecord aRecorder;
	byte[] buffer;
	ByteArrayOutputStream record = new ByteArrayOutputStream();

	public float getDetectRatio()
	{
		return detectRatio;
	}

	public void setDetectRatio(float detectRatio)
	{
		this.detectRatio = detectRatio;
	}

	public int getMaxSilenceLength()
	{
		return maxSilenceLength;
	}

	public void setMaxSilenceLength(int maxSilenceLength)
	{
		this.maxSilenceLength = maxSilenceLength;
	}

	public int getMinRecordLength()
	{
		return minRecordLength;
	}

	public void setMinRecordLength(int minRecordLength)
	{
		this.minRecordLength = minRecordLength;
	}

	public int getMaxRecordLength()
	{
		return maxRecordLength;
	}

	public void setMaxRecordLength(int maxRecordLength)
	{
		this.maxRecordLength = maxRecordLength;
	}

	public String getLanguage()
	{
		return language;
	}

	public void setLanguage(String language)
	{
		this.language = language;
	}

	public int getMaxResults()
	{
		return maxResults;
	}

	public void setMaxResults(int maxResults)
	{
		this.maxResults = maxResults;
	}

	public void setVoiceRecognizedListener(VoiceRecognizedListener voiceRecognizedListener)
	{
		this.voiceRecognizedListener = voiceRecognizedListener;
	}

	public int getDetectLevel()
	{
		return detectLevel;
	}

	public void start() throws Exception
	{
		if (aRecorder != null)
			return; // Óæå çàïóùåíû
		// Ïàðàìåòðû çàõâàòà
		int audioSource = MediaRecorder.AudioSource.MIC;
		final int channelConfig = AudioFormat.CHANNEL_IN_MONO;
		int nChannels = 1;
		int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
		int bSamples = 16;
		int framePeriod = SAMPLE_RATE * TIMER_INTERVAL / 1000;
		int bufferSize = framePeriod * 2 * bSamples * nChannels / 8;
		if (bufferSize < AudioRecord.getMinBufferSize(SAMPLE_RATE, channelConfig, audioFormat))
		{
			bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE, channelConfig, audioFormat);
			Log.w(TAG, "Increasing buffer size to " + Integer.toString(bufferSize));
		}

		// Çàïóñêàåì çàõâàò àóäèî
		aRecorder = new AudioRecord(audioSource, SAMPLE_RATE, channelConfig, audioFormat, bufferSize);
		if (aRecorder.getState() != AudioRecord.STATE_INITIALIZED)
		{
			Log.e(TAG, "AudioRecord initialization failed");
			aRecorder = null;
			throw new Exception("AudioRecord initialization failed");
		}
		aRecorder.setRecordPositionUpdateListener(updateListener);
		aRecorder.setPositionNotificationPeriod(framePeriod);
		buffer = new byte[framePeriod * bSamples / 8 * nChannels];
		aRecorder.startRecording();
		// Íóæíî äëÿ íåêîòîðûõ ñòàðûõ âåðñèé Android
		aRecorder.read(buffer, 0, buffer.length);
		Log.i(TAG, "Started");
	}

	public void stop()
	{
		if (aRecorder != null)
		{
			aRecorder.stop();
			aRecorder = null;
			Log.i(TAG, "Stopped");
		}
	}

	private AudioRecord.OnRecordPositionUpdateListener updateListener = new AudioRecord.OnRecordPositionUpdateListener()
	{
		@Override
		public void onPeriodicNotification(AudioRecord recorder)
		{
			if (aRecorder == null)
				return; // Stopped
			// ×èòàåì äàííûå
			int len = aRecorder.read(buffer, 0, buffer.length);
			int maxAmplitude = 0;
			int detects = 0;
			// Ïðîõîäèìñÿ âäîëü çàïèñàííîãî ñýìïëà
			for (int p = 0; p < len - 1; p += 2)
			{
				// WTF. Êîíâåðòèðóåì little-endian signed bytes â int
				int level = buffer[p + 1] * 256 + ((buffer[p] >= 0) ? buffer[p] : (256 + buffer[p]));
				int amplitude = Math.abs(level);
				if (amplitude > maxAmplitude)
					maxAmplitude = amplitude;
				if (amplitude > detectLevel)
					detects++;
			}

			// Àíàëèç ôîíîâîé ãðîìîêñòè
			// Çàïîìèíàåì ìàêñèìàëüíóþ ñðåäíþþ ãðîìêîñòü
			if (maxAmplitude > maxLevel)
			{
				maxLevel = maxAmplitude;
			}
			// Åñëè äîñòàòî÷íî òèõî, òî ñìîòðèì - íå ïîíèçèòü ëè ãðîìêîñòü
			// ñðàáàòûâàíèÿ?
			if (detects == 0)
			{
				if (analyzeLength >= MAX_ANALYZE_LENGTH)
				{
					if (maxSilenceLevel * VOLUME_RATIO < detectLevel)
					{
						Log.i(TAG, "Decreasing detect level from " + detectLevel + " to " + maxSilenceLevel * VOLUME_RATIO);
						detectLevel = (int) (maxSilenceLevel * VOLUME_RATIO);
					}
					analyzeLength = 0;
					maxSilenceLevel = 0;
				} else
				{
					if (maxAmplitude > maxSilenceLevel)
						maxSilenceLevel = maxAmplitude;
					analyzeLength += TIMER_INTERVAL;
				}
			}

			// Log.d(TAG, "Data: " + len + ", max: " + maxAmplitude +
			// ", detects: " + detects);

			// Äîñòèãíóòî ëè íóæíîå êîëè÷åñòâî ïðåâûøåíèé àìïëèòóäû
			// ñðàáàòûâàíèÿ?
			boolean voiceDetected = (detects > len / 2 * detectRatio);

			if (!recording) // Åñëè çàïèñü íå èä¸ò...
			{
				if (voiceDetected) // È ìû îáíàðóæèëè âñïûøêó àìïëèòóäû
				{
					// Çàïóñêàåì çàïèñü
					recording = true;
					recordLength = TIMER_INTERVAL;
					Log.d(TAG, "Voice record started");
				} else
				{
					// Åñëè çàïèñü íå èä¸ò, ìû âñåãäà äåðæèì â çàïàñå îäèí ñåìïë
					record.reset();
					record.write(buffer, 0, len);
					// Òèøèíà, îøèáîê òî÷íî íåò
					noErrorTimer++;
					if (noErrorTimer >= MAX_NO_ERROR_TIME) errorCount = 0;
				}
			}
			if (recording) // Åñëè çàïèñü èä¸ò (èëè íà÷àëàñü òîëüêî ÷òî)
			{
				// Ïèøåì çâóê â áóôåð
				recordLength += TIMER_INTERVAL;
				record.write(buffer, 0, len);

				// Åñëè â ýòîò ðàç ãîëîñ íå îáíàðóæèëè
				// Èëè åñëè ïèøåì åãî óæå ñëèøêîì äîëãî
				if (!voiceDetected || (maxRecordLength < recordLength))
				{
					// Ñ÷èòàåì êàê äîëãî
					silenceLength += TIMER_INTERVAL;
					if ((silenceLength >= maxSilenceLength) || (maxRecordLength < recordLength))
					{
						// Ïîðà ïðåêðàæàòü çàïèñü
						recording = false;
						Log.d(TAG, "Voice record stopped, length: " + (recordLength - silenceLength));
						if (recordLength - silenceLength >= minRecordLength)
						{
							try
							{
								new Thread(new ProceedRecordThread(record.toByteArray())).start();
							} catch (Exception e)
							{
								e.printStackTrace();
							}
						}
					}
				} else
					silenceLength = 0; // Íå ìîë÷àò, îáíóëÿåì ñ÷¸ò÷èê òèøèíû
			}			
		}

		@Override
		public void onMarkerReached(AudioRecord recorder)
		{
			// Íå íóæíî
		}
	};

	class ProceedRecordThread implements Runnable
	{
		byte[] record;

		public ProceedRecordThread(byte[] record)
		{
			this.record = record;
		}

		public void run()
		{
			try
			{
				byte[] flac = flacEncode(record);
				String results[] = request(flac, 5);
				if ((voiceRecognizedListener != null) && (results.length > 0))
					voiceRecognizedListener.onVoiceRecognized(results);
			} catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}

	byte[] flacEncode(byte[] sampleData) throws IOException
	{
		Log.d(TAG, "Encoding...");
		FLACEncoder flacEncoder = new FLACEncoder();
		ByteArrayOutputStream flacData = new ByteArrayOutputStream();
		FLACStreamOutputStream flacOutputStream = new FLACStreamOutputStream(flacData);
		// FLACFileOutputStream flacOutputStream = new
		// FLACFileOutputStream("/mnt/sdcard/test.flac");
		StreamConfiguration streamConfiguration = new StreamConfiguration();
		streamConfiguration.setSampleRate(SAMPLE_RATE);
		streamConfiguration.setBitsPerSample(16);
		streamConfiguration.setChannelCount(1);
		flacEncoder.setStreamConfiguration(streamConfiguration);
		flacEncoder.setOutputStream(flacOutputStream);
		flacEncoder.openFLACStream();
		int[] sampleDataInt = new int[sampleData.length / 2];
		for (int p = 0; p < sampleData.length - 1; p += 2)
		{
			// WTF, Java? Two little-endian signed bytes to int conversion
			sampleDataInt[p / 2] = sampleData[p + 1] * 256 + ((sampleData[p] >= 0) ? sampleData[p] : (256 + sampleData[p]));
		}
		flacEncoder.addSamples(sampleDataInt, sampleDataInt.length);
		flacEncoder.encodeSamples(sampleDataInt.length, false);
		flacEncoder.encodeSamples(flacEncoder.samplesAvailableToEncode(), true);
		flacOutputStream.close();
		Log.d(TAG, "Encoded");
		return flacData.toByteArray();
	}

	public String[] request(byte[] flac, int maxResults) throws Exception
	{
		Log.d(TAG, "Requesting...");
		final String googleurl = "https://www.google.com/speech-api/v2/recognize?output=json";
		StringBuilder sb = new StringBuilder(googleurl);
		sb.append("&key=" + API_KEY);
		sb.append("&lang=" + language);
		sb.append("&maxresults=" + maxResults);
		sb.append("&pfilter=0"); // ;)

		URL url = new URL(sb.toString());
		URLConnection urlCon = url.openConnection();
		urlCon.setDoOutput(true);
		urlCon.setUseCaches(false);

		urlCon.setRequestProperty("Content-Type", "audio/x-flac; rate=" + SAMPLE_RATE);
		OutputStream outputStream = urlCon.getOutputStream();
		outputStream.write(flac);
		outputStream.close();
		BufferedReader br = new BufferedReader(new InputStreamReader(urlCon.getInputStream(), Charset.forName("UTF-8")));

		StringBuilder result = new StringBuilder();
		String line;
		while ((line = br.readLine()) != null)
		{
			result.append(line);
		}
		br.close();
		// Log.d(TAG, "Response: " + result.toString());
		Pattern regex = Pattern.compile("\"transcript\":\"(.+?)\"");
		Matcher m = regex.matcher(result.toString());
		List<String> results = new ArrayList<String>();
		while (m.find())
		{
			String r = m.group(1);
			results.add(r);
			Log.d(TAG, "Result: " + r);
		}
		// Ðåçóëüòàò ïóñòîé, ìîæåò ñëèøêîì ãðîìêî?
		if (results.isEmpty())
		{
			Log.d(TAG, "Google can't understand you");
			errorCount++;
			noErrorTimer = 0;
			if (errorCount >= MAX_ERROR_COUNT) // À áûâàåò èíà÷å?
			{
				if (maxLevel > detectLevel)
				{
					Log.i(TAG, "Increasing detect level from " + detectLevel + " to " + maxLevel);
					detectLevel = (int) (maxLevel);
				}
				errorCount = 0;
				maxLevel = 0;
			}
		} else
		{
			errorCount = 0;
			maxLevel = 0;
		}
		return (String[]) results.toArray(new String[results.size()]);
	}

	public interface VoiceRecognizedListener
	{
		void onVoiceRecognized(String[] results);
	}
}