|
32 | 32 | import java.util.concurrent.BlockingQueue; |
33 | 33 | import java.util.concurrent.LinkedBlockingQueue; |
34 | 34 |
|
| 35 | +import ai.olami.android.jni.Codec; |
35 | 36 | import ai.olami.cloudService.APIConfiguration; |
36 | 37 | import ai.olami.cloudService.APIResponse; |
37 | 38 | import ai.olami.cloudService.CookieSet; |
@@ -75,8 +76,10 @@ public class RecorderSpeechRecognizer extends SpeechRecognizerBase{ |
75 | 76 |
|
76 | 77 | private VoiceVolume mVoiceVolume = new VoiceVolume(); |
77 | 78 |
|
78 | | - private RecordState mRecordState; |
79 | | - private RecognizeState mRecognizeState; |
| 79 | + private Codec mSpeexEncoder = null; |
| 80 | + |
| 81 | + private RecordState mRecordState = null; |
| 82 | + private RecognizeState mRecognizeState = null; |
80 | 83 |
|
81 | 84 | /** |
82 | 85 | * Recording state |
@@ -482,7 +485,19 @@ private void doSending() throws Exception { |
482 | 485 | byte[] audioData = (byte[]) mRecordDataQueue.take(); |
483 | 486 | mIsFinal = (isRecodingStopped() && (mRecordDataQueue.isEmpty())); |
484 | 487 | length += ((audioData.length / getFrameSize()) * FRAME_LENGTH_MILLISECONDS); |
485 | | - mRecognizer.appendAudioFramesData(audioData); |
| 488 | + if (getAudioCompressLibraryType() == AUDIO_COMPRESS_LIBRARY_TYPE_CPP) { |
| 489 | + if (mSpeexEncoder == null) { |
| 490 | + mSpeexEncoder = new Codec(); |
| 491 | + mSpeexEncoder.open(1, 10); |
| 492 | + } |
| 493 | + byte[] encBuffer = new byte[audioData.length]; |
| 494 | + int encSize = mSpeexEncoder.encodeByte(audioData, 0, audioData.length, encBuffer); |
| 495 | + mRecognizer.setAudioType(SpeechRecognizer.AUDIO_TYPE_PCM_SPEEX); |
| 496 | + mRecognizer.appendSpeexAudioFramesData(encBuffer, encSize); |
| 497 | + } else { |
| 498 | + mRecognizer.setAudioType(SpeechRecognizer.AUDIO_TYPE_PCM_RAW); |
| 499 | + mRecognizer.appendAudioFramesData(audioData); |
| 500 | + } |
486 | 501 | if ((length >= getUploadAudioLengthMilliseconds()) || mIsFinal) { |
487 | 502 | APIResponse response = mRecognizer.flushToUploadAudio(mCookie, mIsFinal); |
488 | 503 | if (response.ok()) { |
@@ -514,6 +529,11 @@ private void doSending() throws Exception { |
514 | 529 | } |
515 | 530 | } |
516 | 531 |
|
| 532 | + if (mSpeexEncoder != null) { |
| 533 | + mSpeexEncoder.close(); |
| 534 | + mSpeexEncoder = null; |
| 535 | + } |
| 536 | + |
517 | 537 | synchronized (mRecordDataQueue) { |
518 | 538 | mRecordDataQueue.clear(); |
519 | 539 | mRecordDataQueue = null; |
|
0 commit comments