|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft. All rights reserved. |
| 3 | +// Licensed under the MIT license. See LICENSE.md file in the project root for full license information. |
| 4 | +// |
| 5 | +// <code> |
| 6 | +package com.microsoft.cognitiveservices.speech.samples.quickstart; |
| 7 | + |
| 8 | +import android.content.res.AssetManager; |
| 9 | +import android.support.v4.app.ActivityCompat; |
| 10 | +import android.support.v7.app.AppCompatActivity; |
| 11 | +import android.os.Bundle; |
| 12 | +import android.util.Log; |
| 13 | +import android.view.View; |
| 14 | +import android.widget.Button; |
| 15 | +import android.widget.TextView; |
| 16 | + |
| 17 | +import com.microsoft.cognitiveservices.speech.AudioDataStream; |
| 18 | +import com.microsoft.cognitiveservices.speech.KeywordRecognitionModel; |
| 19 | +import com.microsoft.cognitiveservices.speech.SpeechConfig; |
| 20 | +import com.microsoft.cognitiveservices.speech.SpeechRecognitionResult; |
| 21 | +import com.microsoft.cognitiveservices.speech.SpeechRecognizer; |
| 22 | +import com.microsoft.cognitiveservices.speech.audio.AudioConfig; |
| 23 | +import com.microsoft.cognitiveservices.speech.KeywordRecognizer; |
| 24 | +import com.microsoft.cognitiveservices.speech.KeywordRecognitionResult; |
| 25 | +import com.microsoft.cognitiveservices.speech.ResultReason; |
| 26 | +import com.microsoft.cognitiveservices.speech.audio.PushAudioInputStream; |
| 27 | + |
| 28 | +import java.io.InputStream; |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.concurrent.ExecutorService; |
| 31 | +import java.util.concurrent.Executors; |
| 32 | +import java.util.concurrent.Future; |
| 33 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 34 | + |
| 35 | +import static android.Manifest.permission.*; |
| 36 | + |
| 37 | +public class MainActivity extends AppCompatActivity { |
| 38 | + |
| 39 | + private ExecutorService es = Executors.newFixedThreadPool(3); |
| 40 | + |
| 41 | + // Replace below with your own subscription key |
| 42 | + private static String speechSubscriptionKey = "YourSubscriptionKey"; |
| 43 | + // Replace below with your own service region (e.g., "westus"). |
| 44 | + private static String serviceRegion = "YourServiceRegion"; |
| 45 | + |
| 46 | + @Override |
| 47 | + protected void onCreate(Bundle savedInstanceState) { |
| 48 | + super.onCreate(savedInstanceState); |
| 49 | + setContentView(R.layout.activity_main); |
| 50 | + |
| 51 | + // Note: we need to request the permissions |
| 52 | + int requestCode = 5; // unique code for the permission request |
| 53 | + ActivityCompat.requestPermissions(MainActivity.this, new String[]{RECORD_AUDIO, INTERNET}, requestCode); |
| 54 | + } |
| 55 | + |
| 56 | + void enableButton(final boolean enabled) { |
| 57 | + final Button btn = (Button) this.findViewById(R.id.button); |
| 58 | + runOnUiThread(() -> { |
| 59 | + btn.setEnabled(enabled); |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + void updateText(final String text) { |
| 64 | + final TextView txt = (TextView) this.findViewById(R.id.hello); // 'hello' is the ID of your text view |
| 65 | + runOnUiThread(() -> { |
| 66 | + txt.setText(text); |
| 67 | + }); |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + final AtomicBoolean stopPumping = new AtomicBoolean(); |
| 72 | + private Runnable pump(final AudioDataStream src, final PushAudioInputStream dst) |
| 73 | + { |
| 74 | + return () -> { |
| 75 | + try { |
| 76 | + final byte[] buffer = new byte[300]; |
| 77 | + while (!stopPumping.get()) { |
| 78 | + if (!src.canReadData(300)) |
| 79 | + { |
| 80 | + continue; |
| 81 | + } |
| 82 | + long count = src.readData(buffer); |
| 83 | + if (count == 0) { |
| 84 | + break; |
| 85 | + } |
| 86 | + if (count < 300) { |
| 87 | + dst.write(Arrays.copyOf(buffer, (int) count)); |
| 88 | + } else { |
| 89 | + dst.write(buffer); |
| 90 | + } |
| 91 | + } |
| 92 | + } catch (Exception ex) |
| 93 | + { |
| 94 | + Log.e("SpeechSDKDemo", "Pump got an exception"); |
| 95 | + } |
| 96 | + }; |
| 97 | + } |
| 98 | + |
| 99 | + public void onSpeechButtonClicked(View v) { |
| 100 | + enableButton(false); |
| 101 | + updateText("Say \"Computer\""); |
| 102 | + Runnable asyncTask = () -> { |
| 103 | + try { |
| 104 | + AssetManager am = getAssets(); |
| 105 | + AudioConfig config = AudioConfig.fromDefaultMicrophoneInput(); |
| 106 | + assert(config != null); |
| 107 | + |
| 108 | + KeywordRecognizer reco = new KeywordRecognizer(config); |
| 109 | + assert(reco != null); |
| 110 | + |
| 111 | + InputStream is = am.open("kws.table"); |
| 112 | + KeywordRecognitionModel model = KeywordRecognitionModel.fromStream(is, "computer", false); |
| 113 | + Future<KeywordRecognitionResult> task = reco.recognizeOnceAsync(model); |
| 114 | + assert(task != null); |
| 115 | + |
| 116 | + KeywordRecognitionResult result = task.get(); |
| 117 | + assert(result != null); |
| 118 | + |
| 119 | + if (result.getReason() == ResultReason.RecognizedKeyword) { |
| 120 | + updateText("Recognized " + result.getText()); |
| 121 | + } |
| 122 | + else { |
| 123 | + updateText("Error: got the wrong sort of recognition."); |
| 124 | + reco.close(); |
| 125 | + enableButton(true); |
| 126 | + return; |
| 127 | + } |
| 128 | + AudioDataStream audioDataStream = AudioDataStream.fromResult(result); |
| 129 | + |
| 130 | + PushAudioInputStream srInputStream = PushAudioInputStream.create(); |
| 131 | + AudioConfig srAudioConfig = AudioConfig.fromStreamInput(srInputStream); |
| 132 | + SpeechConfig srConfig = SpeechConfig.fromSubscription(speechSubscriptionKey, serviceRegion); |
| 133 | + SpeechRecognizer speechRecognizer = new SpeechRecognizer(srConfig, srAudioConfig); |
| 134 | + /* We pump the audio into the push stream */ |
| 135 | + stopPumping.set(false); |
| 136 | + es.execute(pump(audioDataStream, srInputStream)); |
| 137 | + SpeechRecognitionResult srResult = speechRecognizer.recognizeOnceAsync().get(); |
| 138 | + stopPumping.set(true); |
| 139 | + if (srResult.getReason() == ResultReason.RecognizedSpeech) { |
| 140 | + updateText("Recognized Speech " + srResult.getText()); |
| 141 | + } |
| 142 | + else { |
| 143 | + updateText("Error: got the wrong sort of recognition."); |
| 144 | + } |
| 145 | + audioDataStream.detachInput(); |
| 146 | + audioDataStream.close(); |
| 147 | + reco.close(); |
| 148 | + config.close(); |
| 149 | + speechRecognizer.close(); |
| 150 | + srAudioConfig.close(); |
| 151 | + srConfig.close(); |
| 152 | + srInputStream.close(); |
| 153 | + } catch (Exception ex) { |
| 154 | + Log.e("SpeechSDKDemo", "unexpected " + ex.getMessage()); |
| 155 | + enableButton(true); |
| 156 | + assert(false); |
| 157 | + } |
| 158 | + enableButton(true); |
| 159 | + }; |
| 160 | + es.execute(asyncTask); |
| 161 | + } |
| 162 | +} |
| 163 | +// </code> |
0 commit comments