Skip to content

Commit

Permalink
fixed interrupt
Browse files Browse the repository at this point in the history
  • Loading branch information
matt200-ok committed Dec 27, 2024
1 parent 4078ff6 commit a8a2ec8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ dependencies {
implementation 'ai.picovoice:android-voice-processor:1.0.2'
implementation 'ai.picovoice:porcupine-android:3.0.1'
implementation 'ai.picovoice:cheetah-android:2.0.0'
implementation 'ai.picovoice:picollm-android:1.2.0'
implementation 'ai.picovoice:picollm-android:1.2.3'
implementation 'ai.picovoice:orca-android:1.0.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.media.AudioAttributes;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.net.Uri;
import android.os.Bundle;
Expand Down Expand Up @@ -293,6 +293,8 @@ private void runWakeWordSTT(short[] frame) {
try {
int keywordIndex = porcupine.process(frame);
if (keywordIndex == 0) {
interrupt();

llmPromptText = new StringBuilder();
updateUIState(UIState.STT);
}
Expand Down Expand Up @@ -509,16 +511,26 @@ private void runLLM(String prompt) {

ttsPlaybackExecutor.submit(() -> {
try {
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build();

AudioFormat audioFormat = new AudioFormat.Builder()
.setSampleRate(orca.getSampleRate())
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
.setChannelMask(AudioFormat.CHANNEL_OUT_MONO)
.build();

ttsOutput = new AudioTrack(
AudioManager.STREAM_MUSIC,
orca.getSampleRate(),
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT,
audioAttributes,
audioFormat,
AudioTrack.getMinBufferSize(
orca.getSampleRate(),
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT),
AudioTrack.MODE_STREAM);
AudioTrack.MODE_STREAM,
0);

ttsOutput.play();
} catch (Exception e) {
Expand All @@ -535,22 +547,24 @@ private void runLLM(String prompt) {

while (isQueueingPcm.get() || !pcmQueue.isEmpty()) {
short[] pcm = pcmQueue.poll();
if (pcm != null && pcm.length > 0 && currentState == UIState.LLM_TTS) {
if (pcm != null && pcm.length > 0 && ttsOutput.getPlayState() == AudioTrack.PLAYSTATE_PLAYING) {
ttsOutput.write(pcm, 0, pcm.length);
}
}

ttsOutput.stop();
if (ttsOutput.getPlayState() == AudioTrack.PLAYSTATE_PLAYING) {
ttsOutput.flush();
ttsOutput.stop();
}
ttsOutput.release();
});
}

private void interrupt() {
try {
picollm.interrupt();
if (ttsOutput != null) {
if (ttsOutput != null && ttsOutput.getPlayState() == AudioTrack.PLAYSTATE_PLAYING) {
ttsOutput.stop();
ttsOutput.release();
}
} catch (PicoLLMException e) {
onEngineProcessError(e.getMessage());
Expand Down Expand Up @@ -584,7 +598,6 @@ private void onEngineProcessError(String message) {
mainHandler.post(() -> chatText.setText(message));
}


private void startWakeWordListening() {
if (voiceProcessor.hasRecordAudioPermission(this)) {
try {
Expand Down

0 comments on commit a8a2ec8

Please sign in to comment.