Skip to content

Commit

Permalink
review tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bejager committed Apr 11, 2024
1 parent b8c3166 commit 4bb83a7
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 40 deletions.
45 changes: 21 additions & 24 deletions binding/python/test_orca.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def _test_word_equal(self, word: Orca.WordAlignment, word_truth: Orca.WordAlignm
self._test_equal_timestamp(word.start_sec, word_truth.start_sec)
self._test_equal_timestamp(word.end_sec, word_truth.end_sec)

for i, phoneme in enumerate(word.phonemes):
phoneme_truth = word_truth.phonemes[i]
self.assertEqual(len(word.phonemes), len(word_truth.phonemes))
for phoneme, phoneme_truth in zip(word.phonemes, word_truth.phonemes):
self._test_phoneme_equal(phoneme, phoneme_truth)

@staticmethod
Expand Down Expand Up @@ -108,30 +108,31 @@ def test_synthesize_alignment_exact(self) -> None:
pcm, alignments = orca.synthesize(test_data.text_alignment, random_state=test_data.random_state)
self.assertGreater(len(pcm), 0)

for i, word in enumerate(alignments):
word_truth = test_data.alignments[i]
self.assertTrue(len(alignments) == len(test_data.alignments))
for word, word_truth in zip(alignments, test_data.alignments):
self._test_word_equal(word, word_truth)

def test_synthesize_alignment(self) -> None:
for i, orca in enumerate(self.orcas):
if self.EXACT_ALIGNMENT_TEST_MODEL_IDENTIFIER not in self.model_paths[i]:
if self.EXACT_ALIGNMENT_TEST_MODEL_IDENTIFIER in self.model_paths[i]:
continue

pcm, alignments = orca.synthesize(test_data.text_alignment, random_state=test_data.random_state)
self.assertGreater(len(pcm), 0)
pcm, alignments = orca.synthesize(test_data.text_alignment, random_state=test_data.random_state)
self.assertGreater(len(pcm), 0)

previous_word_end_sec = 0
previous_phoneme_end_sec = 0
for word in alignments:
self.assertTrue(word.start_sec == previous_word_end_sec)
self.assertTrue(word.end_sec > word.start_sec)
previous_word_end_sec = word.end_sec
previous_word_end_sec = 0
previous_phoneme_end_sec = 0
for word in alignments:
self.assertTrue(word.start_sec == previous_word_end_sec)
self.assertTrue(word.end_sec > word.start_sec)
previous_word_end_sec = word.end_sec

for phoneme in word.phonemes:
self.assertTrue(phoneme.start_sec == previous_phoneme_end_sec)
self.assertTrue(phoneme.start_sec >= word.start_sec)
self.assertTrue(phoneme.end_sec <= word.end_sec)
self.assertTrue(phoneme.end_sec > phoneme.start_sec)
previous_phoneme_end_sec = phoneme.end_sec
for phoneme in word.phonemes:
self.assertTrue(phoneme.start_sec == previous_phoneme_end_sec)
self.assertTrue(phoneme.start_sec >= word.start_sec)
self.assertTrue(phoneme.end_sec <= word.end_sec)
self.assertTrue(phoneme.end_sec > phoneme.start_sec)
previous_phoneme_end_sec = phoneme.end_sec

def test_streaming_synthesis(self) -> None:
for i, orca in enumerate(self.orcas):
Expand Down Expand Up @@ -162,12 +163,8 @@ def test_synthesize_speech_rate(self) -> None:

self.assertLess(len(pcm_fast), len(pcm_slow))

try:
with self.assertRaises(OrcaError):
_ = orca.synthesize(test_data.text, speech_rate=9999)
except OrcaError:
pass
else:
self.fail("Expected OrcaError")

def test_synthesize_to_file(self) -> None:
for orca in self.orcas:
Expand Down
8 changes: 3 additions & 5 deletions binding/python/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import json
import os
from dataclasses import dataclass
from typing import List, Sequence, Set
from typing import List, Sequence

import soundfile

Expand All @@ -33,7 +33,7 @@ class TestData:

def read_wav_file(path: str) -> Sequence[int]:
pcm, _ = soundfile.read(path)
pcm = list((pcm * 32768))
pcm = list(pcm * 32768)
return pcm


Expand All @@ -47,10 +47,8 @@ def get_test_data() -> TestData:
with open(data_file_path, encoding="utf8") as data_file:
test_data = json.loads(data_file.read())

alignment_data = test_data["alignments"]

alignments = []
for word_data in alignment_data:
for word_data in test_data["alignments"]:
phonemes = []
for phoneme_data in word_data["phonemes"]:
phoneme = Orca.PhonemeAlignment(
Expand Down
3 changes: 3 additions & 0 deletions demo/llm/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sounddevice
tiktoken
openai
6 changes: 1 addition & 5 deletions demo/llm/src/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ def _tokenize(self, text: str) -> Sequence[str]:
return tokens

def _chat(self, user_input: str) -> Generator[str, None, None]:
try:
text_index = int(user_input)
sentence = self._sentences[text_index]
except ValueError:
sentence = self._sentences[random.randint(0, len(self._sentences) - 1)]
sentence = self._sentences[random.randint(0, len(self._sentences) - 1)]

for i in self._tokenize(text=sentence):
time.sleep(self._tokens_delay)
Expand Down
4 changes: 2 additions & 2 deletions demo/python/orca_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ def main():
pcm, alignment = orca.synthesize(args.text)
processing_time = time.time() - start
length_sec = len(pcm) / orca.sample_rate
with wave.open(args.output_path, 'wb') as output_file:
with wave.open(args.output_path, "wb") as output_file:
output_file.setnchannels(1)
output_file.setsampwidth(2)
output_file.setframerate(orca.sample_rate)
output_file.writeframes(struct.pack('%dh' % len(pcm), *pcm))
output_file.writeframes(struct.pack(f"{len(pcm)}h", *pcm))
print(
f"Orca took {processing_time:.2f} seconds to synthesize {length_sec:.2f} seconds of speech which is "
f"~{length_sec / processing_time:.0f} times faster than real-time.")
Expand Down
3 changes: 0 additions & 3 deletions demo/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
../../binding/python/dist/pvorca-0.1.4-py3-none-any.whl
pvorca==0.1.4
sounddevice
tiktoken
openai
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2018-2023 Picovoice Inc.
Copyright 2024 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
file accompanying this source.
Expand Down

0 comments on commit 4bb83a7

Please sign in to comment.