Skip to content

Commit

Permalink
minor fix android demo threading states
Browse files Browse the repository at this point in the history
  • Loading branch information
albho committed Aug 20, 2024
1 parent aad2148 commit a57e8cc
Showing 1 changed file with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ private void validateText(String text) {
} else {
Set<Character> invalidChars = new HashSet<>();
Matcher m = validationRegex.matcher(text);
while(m.find()) {
while (m.find()) {
invalidChars.add(text.charAt(m.start()));
}

Expand Down Expand Up @@ -444,8 +444,11 @@ private void runStreamSynthesis(final String text) {
CountDownLatch streamingAudioLatch = new CountDownLatch(1);

executor.submit(() -> {
mainHandler.post(() -> {
streamTextView.setText("");
});

isStreamingText.set(true);
streamingSynthesisLatch.countDown();

String[] words = text.split(" ");
for (String word : words) {
Expand All @@ -455,6 +458,7 @@ private void runStreamSynthesis(final String text) {
textStream.add(finalWord);
streamTextView.append(finalWord);
});
streamingSynthesisLatch.countDown();
try {
Thread.sleep(100);
} catch (InterruptedException ignored) { }
Expand All @@ -466,7 +470,6 @@ private void runStreamSynthesis(final String text) {
executorStreamingSynthesis.submit(() -> {
try {
mainHandler.post(() -> {
streamTextView.setText("");
streamSecsTextView.setText("Seconds of audio synthesized: 0.000s");
synthesizeButton.setEnabled(false);
});
Expand Down Expand Up @@ -537,7 +540,7 @@ private void runStreamSynthesis(final String text) {
audioTrack.play();

streamingAudioLatch.await();
while(isQueueingStreamingPcm.get() || !pcmQueue.isEmpty()) {
while (isQueueingStreamingPcm.get() || !pcmQueue.isEmpty()) {
if (!pcmQueue.isEmpty()) {
short[] pcm = pcmQueue.poll();
if (pcm != null && pcm.length > 0) {
Expand All @@ -549,7 +552,10 @@ private void runStreamSynthesis(final String text) {
audioTrack.stop();
audioTrack.release();

mainHandler.post(() -> setUIState(UIState.EDIT));
mainHandler.post(() -> {
setUIState(UIState.EDIT);
streamTextView.setText("");
});
} catch (Exception e) {
mainHandler.post(() -> displayError(e.toString()));
}
Expand Down

0 comments on commit a57e8cc

Please sign in to comment.