Skip to content

Commit

Permalink
modify RecorderSpeechRecognizer, set default speex encoding to C++ lib
Browse files Browse the repository at this point in the history
  • Loading branch information
olami-developers authored and olami-developers committed Apr 17, 2018
1 parent ade7f04 commit 3d44740
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
7 changes: 4 additions & 3 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ android {
minSdkVersion 17
targetSdkVersion 25
versionCode 1
versionName "2.5.0"
versionName "2.5.1"

archivesBaseName = "olami-android-client"
version = android.defaultConfig.versionName

externalNativeBuild {
cmake {
cppFlags "-std=c++11 -frtti -fexceptions"
Expand All @@ -36,12 +37,12 @@ android {
}
}

sourceSets { main { jni.srcDirs = ['src/main/jni', 'src/main/jni/'] } }

lintOptions {
abortOnError false
}

sourceSets { main { jni.srcDirs = ['src/main/jni', 'src/main/jni/'] } }

libraryVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ public class KeepRecordingSpeechRecognizer extends SpeechRecognizerBase {

private VoiceVolume mVoiceVolume = new VoiceVolume();

private RecognizeState mRecognizeState = null;
private Codec mSpeexEncoder = null;

private RecognizeState mRecognizeState = null;

/**
* Recognize process state
*/
Expand Down
26 changes: 23 additions & 3 deletions lib/src/main/java/ai/olami/android/RecorderSpeechRecognizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import ai.olami.android.jni.Codec;
import ai.olami.cloudService.APIConfiguration;
import ai.olami.cloudService.APIResponse;
import ai.olami.cloudService.CookieSet;
Expand Down Expand Up @@ -75,8 +76,10 @@ public class RecorderSpeechRecognizer extends SpeechRecognizerBase{

private VoiceVolume mVoiceVolume = new VoiceVolume();

private RecordState mRecordState;
private RecognizeState mRecognizeState;
private Codec mSpeexEncoder = null;

private RecordState mRecordState = null;
private RecognizeState mRecognizeState = null;

/**
* Recording state
Expand Down Expand Up @@ -482,7 +485,19 @@ private void doSending() throws Exception {
byte[] audioData = (byte[]) mRecordDataQueue.take();
mIsFinal = (isRecodingStopped() && (mRecordDataQueue.isEmpty()));
length += ((audioData.length / getFrameSize()) * FRAME_LENGTH_MILLISECONDS);
mRecognizer.appendAudioFramesData(audioData);
if (getAudioCompressLibraryType() == AUDIO_COMPRESS_LIBRARY_TYPE_CPP) {
if (mSpeexEncoder == null) {
mSpeexEncoder = new Codec();
mSpeexEncoder.open(1, 10);
}
byte[] encBuffer = new byte[audioData.length];
int encSize = mSpeexEncoder.encodeByte(audioData, 0, audioData.length, encBuffer);
mRecognizer.setAudioType(SpeechRecognizer.AUDIO_TYPE_PCM_SPEEX);
mRecognizer.appendSpeexAudioFramesData(encBuffer, encSize);
} else {
mRecognizer.setAudioType(SpeechRecognizer.AUDIO_TYPE_PCM_RAW);
mRecognizer.appendAudioFramesData(audioData);
}
if ((length >= getUploadAudioLengthMilliseconds()) || mIsFinal) {
APIResponse response = mRecognizer.flushToUploadAudio(mCookie, mIsFinal);
if (response.ok()) {
Expand Down Expand Up @@ -514,6 +529,11 @@ private void doSending() throws Exception {
}
}

if (mSpeexEncoder != null) {
mSpeexEncoder.close();
mSpeexEncoder = null;
}

synchronized (mRecordDataQueue) {
mRecordDataQueue.clear();
mRecordDataQueue = null;
Expand Down

0 comments on commit 3d44740

Please sign in to comment.