Skip to content

Commit

Permalink
update ui
Browse files Browse the repository at this point in the history
  • Loading branch information
albho committed May 7, 2024
1 parent 289154d commit c96aa78
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,14 @@ public void onStreamSwitchClick(View view) {
try {
if (orcaStream == null) {
orcaStream = orca.streamOpen(new OrcaSynthesizeParams.Builder().build());
runOnUiThread(() -> {
streamSecsTextView.setText("");
streamSecsTextView.setVisibility(View.VISIBLE);
});
} else {
orcaStream.close();
orcaStream = null;
runOnUiThread(() -> streamSecsTextView.setVisibility(View.INVISIBLE));
}
} catch (OrcaException e) {
onOrcaException(e);
Expand Down Expand Up @@ -398,7 +403,6 @@ private void stopStreamPlay() {
setUIState(UIState.EDIT);
infoTextView.setText("");
streamTextView.setVisibility(View.INVISIBLE);
streamSecsTextView.setVisibility(View.INVISIBLE);
synthesizeEditText.setVisibility(View.VISIBLE);
});
} catch (Exception e) {
Expand All @@ -418,16 +422,23 @@ private void runStreamSynthesis(final String text) {
AtomicBoolean isStreamingText = new AtomicBoolean(false);
ArrayList<String> textStream = new ArrayList<>();

AtomicBoolean isQueueingStreamingPcm = new AtomicBoolean(false);
ConcurrentLinkedQueue<short[]> pcmQueue = new ConcurrentLinkedQueue<>();
CountDownLatch latch = new CountDownLatch(1);

executor.submit(() -> {
isStreamingText.set(true);

String[] words = text.split(" ");
for (String word : words) {
word += " ";
try {
textStream.add(word);
String finalWord = word;
mainHandler.post(() -> {
textStream.add(finalWord);
streamTextView.append(finalWord);
});
Thread.sleep(100);
streamTextView.append(word);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Expand All @@ -436,15 +447,11 @@ private void runStreamSynthesis(final String text) {
isStreamingText.set(false);
});

AtomicBoolean isQueueingStreamingPcm = new AtomicBoolean(false);
ConcurrentLinkedQueue<short[]> pcmQueue = new ConcurrentLinkedQueue<>();
CountDownLatch latch = new CountDownLatch(1);

executorStreamingSynthesis.submit(() -> {
try {
mainHandler.post(() -> {
streamTextView.setText("");
streamSecsTextView.setText("");
streamSecsTextView.setText("Seconds of audio synthesized: 0.000s");
synthesizeButton.setEnabled(false);
});

Expand All @@ -461,7 +468,8 @@ private void runStreamSynthesis(final String text) {
if (pcm != null && pcm.length > 0) {
pcmQueue.add(pcm);
secs += (float) pcm.length / orca.getSampleRate();
streamSecsTextView.setText("Seconds of audio synthesized: " + String.format("%.3f", secs) + "s");
float finalSecs = secs;
mainHandler.post(() -> streamSecsTextView.setText("Seconds of audio synthesized: " + String.format("%.3f", finalSecs) + "s"));
if (numIterations == STREAMING_NUM_AUDIO_WAIT_CHUNKS) {
latch.countDown();
isPcmPlayStarted = true;
Expand All @@ -479,7 +487,8 @@ private void runStreamSynthesis(final String text) {
if (flushedPcm != null && flushedPcm.length > 0) {
pcmQueue.add(flushedPcm);
secs += (float) flushedPcm.length / orca.getSampleRate();
streamSecsTextView.setText("Seconds of audio synthesized: " + String.format("%.3f", secs) + "s");
float finalSecs = secs;
mainHandler.post(() -> streamSecsTextView.setText("Seconds of audio synthesized: " + String.format("%.3f", finalSecs) + "s"));
}

if (!isPcmPlayStarted) {
Expand Down
179 changes: 93 additions & 86 deletions demo/android/OrcaDemo/orca-demo-app/src/main/res/layout/orca_demo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,93 @@
android:foregroundTint="#002A1F65"
tools:context=".MainActivity">

<ToggleButton
android:id="@+id/synthesizeButton"
android:theme="@style/ButtonStyle"
android:textColor="@android:color/white"
android:layout_width="160dp"
android:layout_height="80dp"
android:layout_marginStart="1dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="40dp"
android:onClick="onSynthesizeClick"
android:textOff="SYNTHESIZE"
android:textOn="STOP"
<androidx.constraintlayout.widget.Barrier
android:id="@+id/textViewBarrier"
app:barrierDirection="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="infoTextView,errorTextView"/>

<androidx.appcompat.widget.SwitchCompat
android:id="@+id/streamSwitch"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:onClick="onStreamSwitchClick"
android:text="Streaming Synthesis"
android:textSize="16sp"
app:layout_constraintBottom_toTopOf="@+id/streamTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
tools:ignore="UseSwitchCompatOrMaterialXml"/>

<TextView
android:id="@+id/streamTextView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="20dp"
android:background="#eeeeee"
android:gravity="top|start"
android:importantForAutofill="no"
android:padding="10dp"
android:scrollbars="vertical"
android:textSize="20sp"
android:enabled="false"
app:layout_constraintBottom_toBottomOf="parent"
android:visibility="invisible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/textViewBarrier" />

<ProgressBar
android:id="@+id/synthesizeProgress"
<TextView
android:id="@+id/streamSecsTextView"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginTop="20dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:textSize="16sp"
android:visibility="invisible"
android:layout_width="160dp"
android:layout_height="80dp"
android:layout_marginStart="1dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/streamTextView"
app:layout_constraintBottom_toTopOf="@+id/infoTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
app:layout_constraintStart_toStartOf="parent" />

<EditText
android:id="@+id/synthesizeEditText"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="20dp"
android:background="#eeeeee"
android:gravity="top|start"
android:hint="Enter some text you want to synthesize..."
android:importantForAutofill="no"
android:inputType="textMultiLine"
android:layout_margin="20dp"
android:padding="10dp"
app:layout_constraintBottom_toTopOf="@+id/textViewBarrier"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="@+id/textViewBarrier"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="#eeeeee"
android:scrollbars="vertical"
android:gravity="top|start"
android:hint="Enter some text you want to synthesize..."/>
app:layout_constraintTop_toTopOf="@+id/streamTextView"
app:layout_constraintVertical_bias="0.0" />

<ProgressBar
android:id="@+id/synthesizeProgress"
android:visibility="invisible"
android:layout_width="160dp"
android:layout_height="80dp"
android:layout_marginStart="1dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>

<TextView
android:id="@+id/numCharsTextView"
Expand All @@ -63,15 +104,8 @@
android:enabled="false"
android:text=""
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="@+id/synthesizeEditText"
app:layout_constraintTop_toBottomOf="@+id/synthesizeEditText"/>

<androidx.constraintlayout.widget.Barrier
android:id="@+id/textViewBarrier"
app:barrierDirection="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="infoTextView,errorTextView"/>
app:layout_constraintEnd_toEndOf="@+id/streamTextView"
app:layout_constraintTop_toBottomOf="@+id/streamTextView"/>

<TextView
android:id="@+id/infoTextView"
Expand All @@ -81,12 +115,13 @@
android:layout_marginEnd="8dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:padding="8dp"
android:text=""
android:textAlignment="center"
app:layout_constraintTop_toBottomOf="@+id/numCharsTextView"
app:layout_constraintBottom_toTopOf="@+id/synthesizeButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/streamSecsTextView" />

<TextView
android:id="@+id/errorTextView"
Expand All @@ -101,55 +136,27 @@
android:text="TextView"
android:textColor="#FFFFFF"
android:visibility="invisible"
app:layout_constraintTop_toBottomOf="@+id/numCharsTextView"
app:layout_constraintBottom_toTopOf="@+id/synthesizeButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>

<androidx.appcompat.widget.SwitchCompat
android:id="@+id/streamSwitch"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:onClick="onStreamSwitchClick"
android:text="Streaming Synthesis"
android:textSize="16sp"
app:layout_constraintBottom_toTopOf="@+id/synthesizeEditText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
tools:ignore="UseSwitchCompatOrMaterialXml"/>
app:layout_constraintTop_toBottomOf="@+id/streamSecsTextView" />

<TextView
android:id="@+id/streamSecsTextView"
android:layout_width="0dp"
android:layout_height="64dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:textSize="16sp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="@+id/synthesizeEditText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>

<TextView
android:id="@+id/streamTextView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="20dp"
android:background="#eeeeee"
android:gravity="top|start"
android:importantForAutofill="no"
android:padding="10dp"
android:scrollbars="vertical"
<ToggleButton
android:id="@+id/synthesizeButton"
android:theme="@style/ButtonStyle"
android:textColor="@android:color/white"
android:layout_width="160dp"
android:layout_height="80dp"
android:layout_marginStart="1dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="40dp"
android:onClick="onSynthesizeClick"
android:textOff="SYNTHESIZE"
android:textOn="STOP"
android:textSize="20sp"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="@+id/streamSecsTextView"
android:enabled="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/streamSwitch"
app:layout_constraintVertical_bias="0.0"/>
app:layout_constraintStart_toStartOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit c96aa78

Please sign in to comment.