Skip to content

Commit

Permalink
minor clean
Browse files Browse the repository at this point in the history
  • Loading branch information
albho committed May 2, 2024
1 parent e2cef54 commit c0305f8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public OrcaStream(long stream) {
* `getValidCharacters()`. Custom pronunciations can be embedded in the text via the
* syntax `{word|pronunciation}`. The pronunciation is expressed in ARPAbet format,
* e.g.: `I {liv|L IH V} in {Sevilla|S EH V IY Y AH}`.
* @return The output audio. If none is available, null is returned.
* @throws OrcaException if there is an error while synthesizing audio.
*/
public short[] synthesize(String text) throws OrcaException {
Expand All @@ -60,7 +61,7 @@ public short[] synthesize(String text) throws OrcaException {
}
if (stream == 0) {
throw new OrcaInvalidStateException(
"Attempted to call OrcaStream synthesize after close."
"Attempted to call OrcaStream synthesize without an open stream."
);
}

Expand All @@ -72,6 +73,7 @@ public short[] synthesize(String text) throws OrcaException {
/**
* Flushes remaining text. The returned audio contains the speech representation of the text.
*
* @return Any remaining output audio. If none is available, null is returned.
* @throws OrcaException if there is an error while synthesizing audio.
*/
public short[] flush() throws OrcaException {
Expand All @@ -82,7 +84,7 @@ public short[] flush() throws OrcaException {
}
if (stream == 0) {
throw new OrcaInvalidStateException(
"Attempted to call OrcaStream flush after close."
"Attempted to call OrcaStream flush without an open stream."
);
}

Expand Down Expand Up @@ -160,7 +162,7 @@ public void delete() {
* syntax `{word|pronunciation}`. The pronunciation is expressed in ARPAbet format,
* e.g.: `I {liv|L IH V} in {Sevilla|S EH V IY Y AH}`.
* @param params Global parameters for synthesized text. See 'OrcaSynthesizeParams' for details.
* @return The output audio.
* @return The output audio and alignments data.
* @throws OrcaException if there is an error while synthesizing audio.
*/
public OrcaAudio synthesize(String text, OrcaSynthesizeParams params) throws OrcaException {
Expand Down Expand Up @@ -189,6 +191,7 @@ public OrcaAudio synthesize(String text, OrcaSynthesizeParams params) throws Orc
* @param outputPath Absolute path to the output audio file. The output file is saved as
* `WAV (.wav)` and consists of a single mono channel.
* @param params Global parameters for synthesized text. See 'OrcaSynthesizeParams' for details.
* @return The alignments data.
* @throws OrcaException if there is an error while synthesizing audio to file.
*/
public OrcaWord[] synthesizeToFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,45 +283,45 @@ public void testSynthesizeAlignment() throws OrcaException {
Objects.equals(modelFileUsed, EXACT_ALIGNMENT_TEST_MODEL_IDENTIFIER));
}

@Test
public void testSynthesizeToFileAlignment() throws OrcaException {
final File outputFile = new File(
appContext.getFilesDir(),
"text.wav");
OrcaWord[] result = orca.synthesizeToFile(
textAlignment,
outputFile.getAbsolutePath(),
new OrcaSynthesizeParams.Builder()
.setRandomState(randomState)
.build());
outputFile.delete();

final OrcaWord[] synthesizeTestData = new OrcaWord[alignments.size()];
for (int i = 0; i < alignments.size(); i++) {
final JsonObject testData = alignments.get(i).getAsJsonObject();
final String word = testData.get("word").getAsString();
final float startSec = testData.get("start_sec").getAsFloat();
final float endSec = testData.get("end_sec").getAsFloat();
final JsonArray phonemesJson = testData.getAsJsonArray("phonemes");
final OrcaPhoneme[] phonemes = new OrcaPhoneme[phonemesJson.size()];
for (int j = 0; j < phonemesJson.size(); j++) {
final JsonObject phonemeJson = phonemesJson.get(j).getAsJsonObject();
phonemes[j] = new OrcaPhoneme(
phonemeJson.get("phoneme").getAsString(),
phonemeJson.get("start_sec").getAsFloat(),
phonemeJson.get("end_sec").getAsFloat());
}
synthesizeTestData[i] = new OrcaWord(
word,
startSec,
endSec,
phonemes);
@Test
public void testSynthesizeToFileAlignment() throws OrcaException {
final File outputFile = new File(
appContext.getFilesDir(),
"text.wav");
OrcaWord[] result = orca.synthesizeToFile(
textAlignment,
outputFile.getAbsolutePath(),
new OrcaSynthesizeParams.Builder()
.setRandomState(randomState)
.build());
outputFile.delete();

final OrcaWord[] synthesizeTestData = new OrcaWord[alignments.size()];
for (int i = 0; i < alignments.size(); i++) {
final JsonObject testData = alignments.get(i).getAsJsonObject();
final String word = testData.get("word").getAsString();
final float startSec = testData.get("start_sec").getAsFloat();
final float endSec = testData.get("end_sec").getAsFloat();
final JsonArray phonemesJson = testData.getAsJsonArray("phonemes");
final OrcaPhoneme[] phonemes = new OrcaPhoneme[phonemesJson.size()];
for (int j = 0; j < phonemesJson.size(); j++) {
final JsonObject phonemeJson = phonemesJson.get(j).getAsJsonObject();
phonemes[j] = new OrcaPhoneme(
phonemeJson.get("phoneme").getAsString(),
phonemeJson.get("start_sec").getAsFloat(),
phonemeJson.get("end_sec").getAsFloat());
}
validateMetadata(
result,
synthesizeTestData,
Objects.equals(modelFileUsed, EXACT_ALIGNMENT_TEST_MODEL_IDENTIFIER));
synthesizeTestData[i] = new OrcaWord(
word,
startSec,
endSec,
phonemes);
}
validateMetadata(
result,
synthesizeTestData,
Objects.equals(modelFileUsed, EXACT_ALIGNMENT_TEST_MODEL_IDENTIFIER));
}

@Test
public void testSynthesizeSpeechRate() throws OrcaException {
Expand Down Expand Up @@ -360,7 +360,6 @@ public void testSynthesizeRandomState() throws OrcaException {
final OrcaAudio randomState1 = orca.synthesize(
text,
new OrcaSynthesizeParams.Builder()
.setSpeechRate(0.7f)
.setRandomState(1)
.build());
assertTrue(randomState1.getPcm().length > 0);
Expand All @@ -369,7 +368,6 @@ public void testSynthesizeRandomState() throws OrcaException {
final OrcaAudio randomState2 = orca.synthesize(
text,
new OrcaSynthesizeParams.Builder()
.setSpeechRate(1.3f)
.setRandomState(2)
.build());
assertTrue(randomState2.getPcm().length > 0);
Expand Down

0 comments on commit c0305f8

Please sign in to comment.