1
0
mirror of https://github.com/salexdv/speechrecognizer.git synced 2024-11-16 17:52:26 +02:00

Непрерывное и разовое распознавание

This commit is contained in:
salexdv 2023-04-24 15:15:04 +03:00
parent deacf021d8
commit 077a15c393
15 changed files with 111 additions and 16 deletions

View File

@ -20,6 +20,7 @@ static const wchar_t *g_MethodNames[] =
{
L"Delay",
L"StartSpeechRecognition",
L"StartEndlessSpeechRecognition",
L"StopSpeechRecognition",
L"PlaySoundAlert",
L"RequestServiceStatus"
@ -30,6 +31,7 @@ static const wchar_t *g_MethodNamesRu[] =
{
L"Пауза",
L"ЗапуститьРаспознаваниеРечи",
L"ЗапуститьПостоянноеРаспознаваниеРечи",
L"ОстановитьРаспознаваниеРечи",
L"ВоспроизвестиЗвуковоеОповещение",
L"ЗапроситьСтатусСервиса"
@ -373,6 +375,10 @@ bool AddInNative::CallAsProc(const long lMethodNum, tVariant* paParams, const lo
javaMainApp.startSpeechRecognition();
return true;
}
case eMethStartEndlessSpeechRecognition: {
javaMainApp.startEndlessSpeechRecognition();
return true;
}
case eMethStopSpeechRecognition: {
javaMainApp.stopSpeechRecognition();
return true;

View File

@ -34,6 +34,7 @@ public:
{
eMethDelay = 0,
eMethStartSpeechRecognition,
eMethStartEndlessSpeechRecognition,
eMethStopSpeechRecognition,
eMethPlaySoundAlert,
eMethRequestServiceStatus,

View File

@ -102,7 +102,7 @@
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>Gcc_4_9</PlatformToolset>
<AndroidAPILevel>android-27</AndroidAPILevel>
<AndroidAPILevel>android-28</AndroidAPILevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='X86|ARM'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
@ -134,7 +134,8 @@
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>Gcc_4_9</PlatformToolset>
<AndroidAPILevel>android-27</AndroidAPILevel>
<AndroidAPILevel>android-28</AndroidAPILevel>
<UseOfStl>c++_shared</UseOfStl>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='X86|x86'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>

View File

@ -63,6 +63,16 @@ void MainApp::startSpeechRecognition() const
}
}
void MainApp::startEndlessSpeechRecognition() const
{
if (obj)
{
JNIEnv* env = getJniEnv();
jmethodID methID = env->GetMethodID(cc, "startEndlessSpeechRecognition", "()V");
env->CallVoidMethod(obj, methID);
}
}
void MainApp::stopSpeechRecognition() const
{
if (obj)

View File

@ -24,6 +24,7 @@ public:
void Initialize(IAddInDefBaseEx*);
void startSpeechRecognition() const;
void startEndlessSpeechRecognition() const;
void stopSpeechRecognition() const;
void playSoundAlert() const;
void requestServiceStatus() const;

View File

@ -1,5 +1,5 @@
{
"ndkFolder": "C:\\Users\\SM\\AppData\\Local\\Android\\Sdk\\ndk\\21.0.6113669",
"ndkFolder": "C:\\Users\\SM\\AppData\\Local\\Android\\Sdk\\ndk\\20.1.5948944",
"messages": [
{
"level": "INFO",
@ -23,7 +23,15 @@
},
{
"level": "INFO",
"message": "Considering C:\\Users\\SM\\AppData\\Local\\Android\\Sdk\\ndk\\21.0.6113669 in SDK ndk folder"
"message": "Considering C:\\Users\\SM\\AppData\\Local\\Android\\Sdk\\ndk\\20.1.5948944 in SDK ndk folder"
},
{
"level": "INFO",
"message": "Considering C:\\Users\\SM\\AppData\\Local\\Android\\Sdk\\ndk\\24.0.8215888 in SDK ndk folder"
},
{
"level": "INFO",
"message": "Considering C:\\Users\\SM\\AppData\\Local\\Android\\Sdk\\ndk\\25.2.9519653 in SDK ndk folder"
},
{
"level": "INFO",
@ -31,7 +39,15 @@
},
{
"level": "INFO",
"message": "No user requested version, choosing C:\\Users\\SM\\AppData\\Local\\Android\\Sdk\\ndk\\21.0.6113669 which is version 21.0.6113669"
"message": "Rejected C:\\Users\\SM\\AppData\\Local\\Android\\Sdk\\ndk\\24.0.8215888 in SDK ndk folder because that location has no source.properties"
},
{
"level": "INFO",
"message": "Rejected C:\\Users\\SM\\AppData\\Local\\Android\\Sdk\\ndk\\25.2.9519653 in SDK ndk folder because that location has no source.properties"
},
{
"level": "INFO",
"message": "No user requested version, choosing C:\\Users\\SM\\AppData\\Local\\Android\\Sdk\\ndk\\20.1.5948944 which is version 20.1.5948944"
}
]
}

View File

@ -7,8 +7,11 @@ public final class Intents {
// Outgoing intents
public static final String START_RECOGNIZER = "com.github.salexdv.speechrecognizer.START";
public static final String START_ENDLESS_RECOGNIZER = "com.github.salexdv.speechrecognizer.START_ENDLESS_RECOGNIZER";
public static final String STOP_RECOGNIZER = "com.github.salexdv.speechrecognizer.STOP";
public static final String STATUS_RECOGNIZER = "com.github.salexdv.speechrecognizer.STATUS";
public static final String PLAY_BEEP = "com.github.salexdv.speechrecognizer.BEEP";
// Incoming intents
public static final String RECOGNIZER_ONREADY = "com.github.salexdv.speechrecognizer.ON_READY";
public static final String RECOGNIZER_ONBEGIN = "com.github.salexdv.speechrecognizer.ON_BEGININIG";

View File

@ -57,9 +57,7 @@ public class MainApp implements Runnable {
public void playSoundAlert() {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
MediaPlayer mp = MediaPlayer.create(m_Activity.getApplicationContext(), notification);
mp.start();
recognizerRequest(Intents.PLAY_BEEP);
}
@ -81,8 +79,7 @@ public class MainApp implements Runnable {
}
public void startSpeechRecognition()
{
private void startRecognition(String startIntent) {
if (m_Receiver == null)
{
@ -129,12 +126,26 @@ public class MainApp implements Runnable {
m_Activity.registerReceiver(m_Receiver, filter);
recognizerRequest(Intents.START_RECOGNIZER);
recognizerRequest(startIntent);
}
}
public void startSpeechRecognition()
{
this.startRecognition(Intents.START_RECOGNIZER);
}
public void startEndlessSpeechRecognition()
{
this.startRecognition(Intents.START_ENDLESS_RECOGNIZER);
}
public void stopSpeechRecognition()
{

View File

@ -11,7 +11,7 @@ android {
minSdkVersion 28
targetSdkVersion 30
versionCode 1
versionName "1.0"
versionName "1.1"
ndk.abiFilters 'armeabi-v7a','arm64-v8a','x86','x86_64'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
archivesBaseName = "mobile1c_speechrecognizer"

View File

@ -7,11 +7,13 @@ public class AudioController {
private Context context;
private AudioManager audioManager;
private int streamVolume;
public AudioController (Context applicationContext) {
context = applicationContext;
audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
this.streamVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
}
@ -20,6 +22,7 @@ public class AudioController {
audioManager.adjustStreamVolume(AudioManager.STREAM_NOTIFICATION, AudioManager.ADJUST_MUTE, 0);
audioManager.adjustStreamVolume(AudioManager.STREAM_ALARM, AudioManager.ADJUST_MUTE, 0);
audioManager.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_MUTE, 0);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_PLAY_SOUND);
}
@ -28,6 +31,7 @@ public class AudioController {
audioManager.adjustStreamVolume(AudioManager.STREAM_NOTIFICATION, AudioManager.ADJUST_UNMUTE, 0);
audioManager.adjustStreamVolume(AudioManager.STREAM_ALARM, AudioManager.ADJUST_UNMUTE, 0);
audioManager.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_UNMUTE, 0);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, this.streamVolume, AudioManager.FLAG_PLAY_SOUND);
}

View File

@ -7,8 +7,10 @@ public final class Intents {
// Incoming intents
public static final String START_RECOGNIZER = "com.github.salexdv.speechrecognizer.START";
public static final String START_ENDLESS_RECOGNIZER = "com.github.salexdv.speechrecognizer.START_ENDLESS_RECOGNIZER";
public static final String STOP_RECOGNIZER = "com.github.salexdv.speechrecognizer.STOP";
public static final String STATUS_RECOGNIZER = "com.github.salexdv.speechrecognizer.STATUS";
public static final String PLAY_BEEP = "com.github.salexdv.speechrecognizer.BEEP";
// Outgoing intents
public static final String RECOGNIZER_ONREADY = "com.github.salexdv.speechrecognizer.ON_READY";
public static final String RECOGNIZER_ONBEGIN = "com.github.salexdv.speechrecognizer.ON_BEGININIG";

View File

@ -191,7 +191,7 @@ public class MainActivity extends AppCompatActivity {
((Button)findViewById(R.id.testService)).setText(R.string.stop_test);
sendRequest(Intents.START_RECOGNIZER);
sendRequest(Intents.START_ENDLESS_RECOGNIZER);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

View File

@ -82,8 +82,13 @@ public class RecService extends Service {
switch (intent.getAction())
{
case Intents.START_RECOGNIZER:
case Intents.START_ENDLESS_RECOGNIZER:_RECOGNIZER:
try {
if (SpeechRecognizer.isRecognitionAvailable(context)) {
if (intent.getAction() == Intents.START_ENDLESS_RECOGNIZER)
recognizer.enableEndlessRecognition();
else
recognizer.disableEndlessRecognition();
recognizer.start();
}
else {
@ -106,6 +111,8 @@ public class RecService extends Service {
Log.d(recognizerTAG, status);
sendResponce(Intents.RECOGNIZER_STATUS, status);
break;
case Intents.PLAY_BEEP:
recognizer.beep();
default:
break;
}
@ -116,8 +123,10 @@ public class RecService extends Service {
IntentFilter filter = new IntentFilter();
filter.addAction(Intents.START_RECOGNIZER);
filter.addAction(Intents.START_ENDLESS_RECOGNIZER);
filter.addAction(Intents.STOP_RECOGNIZER);
filter.addAction(Intents.STATUS_RECOGNIZER);
filter.addAction(Intents.PLAY_BEEP);
registerReceiver(receiver, filter);
return START_NOT_STICKY;

View File

@ -1,6 +1,7 @@
package com.github.salexdv.speechrecognizer.android_service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
@ -15,6 +16,7 @@ public class Recognizer implements RecognitionListener {
private String recognizerTAG;
private SpeechRecognizer recognizer;
private boolean recStarted;
private boolean endlessRecognition;
private Intent speechRecognizerIntent;
private Context context;
private AudioController audioController;
@ -28,6 +30,12 @@ public class Recognizer implements RecognitionListener {
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.EXTRA_LANGUAGE_MODEL);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE, true);
Long voiceTimeout = new Long(15000);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, voiceTimeout);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, voiceTimeout);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS,voiceTimeout);
audioController = new AudioController(context);
@ -68,7 +76,8 @@ public class Recognizer implements RecognitionListener {
Log.d(recognizerTAG, error);
recognizer.destroy();
sendResponse(Intents.RECOGNIZER_ERROR, error);
start();
if (this.endlessRecognition)
start();
}
}
@ -79,7 +88,8 @@ public class Recognizer implements RecognitionListener {
Log.d(recognizerTAG, "onResults (" + recognitionResult + ")");
sendResponse(Intents.RECOGNIZER_RESULT, recognitionResult);
recognizer.destroy();
start();
if (this.endlessRecognition)
start();
}
@Override
@ -105,6 +115,18 @@ public class Recognizer implements RecognitionListener {
}
public void enableEndlessRecognition() {
this.endlessRecognition = true;
}
public void disableEndlessRecognition() {
this.endlessRecognition = false;
}
public void start() {
audioController.mute();
@ -119,7 +141,6 @@ public class Recognizer implements RecognitionListener {
public void stop() {
if (recStarted) {
audioController.unmute();
recognizer.cancel();
recognizer.stopListening();
recognizer.destroy();
@ -129,6 +150,16 @@ public class Recognizer implements RecognitionListener {
}
public void beep() {
audioController.unmute();
MediaPlayer.create(context, R.raw.beep).start();
if (recStarted)
audioController.mute();
}
public void destroy() {
recognizer.destroy();

Binary file not shown.