Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
laves committed Oct 30, 2023
1 parent bce4b76 commit 553705c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 38 deletions.
73 changes: 46 additions & 27 deletions binding/python/_octopus.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ def size(self) -> int:
def to_bytes(self) -> bytes:
return self._to_bytes(self.handle, self.size)

@classmethod
def create_owned(cls, handle: c_void_p, size: int) -> 'OctopusMetadata':
return cls.from_bytes(cls._to_bytes(handle, size))

@classmethod
def from_bytes(cls, metadata_bytes: bytes) -> 'OctopusMetadata':
byte_ptr = (c_byte * len(metadata_bytes)).from_buffer_copy(metadata_bytes)
Expand Down Expand Up @@ -216,21 +212,33 @@ def __init__(
self._delete_func.argtypes = [POINTER(self.COctopus)]
self._delete_func.restype = None

self._index_size_func = library.pv_octopus_index_size
self._index_size_func.argtypes = [
POINTER(self.COctopus),
c_int32,
POINTER(c_int32)]
self._index_size_func.restype = self.PicovoiceStatuses

self._index_func = library.pv_octopus_index
self._index_func.argtypes = [
POINTER(self.COctopus),
POINTER(c_short),
c_int32,
POINTER(c_void_p),
POINTER(c_int32)]
c_void_p]
self._index_func.restype = self.PicovoiceStatuses

self._index_file_size_func = library.pv_octopus_index_file_size
self._index_file_size_func.argtypes = [
POINTER(self.COctopus),
c_char_p,
POINTER(c_int32)]
self._index_file_size_func.restype = self.PicovoiceStatuses

self._index_file_func = library.pv_octopus_index_file
self._index_file_func.argtypes = [
POINTER(self.COctopus),
c_char_p,
POINTER(c_void_p),
POINTER(c_int32)]
c_void_p]
self._index_file_func.restype = self.PicovoiceStatuses

self._search_func = library.pv_octopus_search
Expand All @@ -243,17 +251,17 @@ def __init__(
POINTER(c_int32)]
self._search_func.restype = self.PicovoiceStatuses

self._matches_delete_func = library.pv_octopus_matches_delete
self._matches_delete_func.argtypes = [POINTER(self.CMatch)]
self._matches_delete_func.restype = None

version_func = library.pv_octopus_version
version_func.argtypes = []
version_func.restype = c_char_p
self._version = version_func().decode('utf-8')

self._sample_rate = library.pv_sample_rate()

self._pv_free = library.pv_free
self._pv_free.argtypes = [c_void_p]
self._pv_free.restype = None

def delete(self) -> None:
"""Releases resources acquired by Octopus."""

Expand All @@ -268,24 +276,29 @@ def index_audio_data(self, pcm: Sequence[int]) -> OctopusMetadata:
:return metadata: An immutable metadata object.
"""

c_metadata = c_void_p()
metadata_size = c_int32()
status = self._index_size_func(
self._handle,
c_int32(len(pcm)),
byref(metadata_size))
if status is not self.PicovoiceStatuses.SUCCESS:
raise self._PICOVOICE_STATUS_TO_EXCEPTION[status](
message='Index size failed',
message_stack=self._get_error_stack())

metadata_bytes = create_string_buffer(metadata_size.value)
metadata_bytes_ptr = cast(metadata_bytes, c_void_p)
status = self._index_func(
self._handle,
(c_short * len(pcm))(*pcm),
c_int32(len(pcm)),
byref(c_metadata),
byref(metadata_size))
metadata_bytes_ptr)
if status is not self.PicovoiceStatuses.SUCCESS:
raise self._PICOVOICE_STATUS_TO_EXCEPTION[status](
message='Index failed',
message_stack=self._get_error_stack())

metadata = OctopusMetadata.create_owned(c_metadata, metadata_size.value)
self._pv_free(c_metadata)

return metadata
return OctopusMetadata(metadata_bytes_ptr, metadata_size.value)

def index_audio_file(self, path: str) -> OctopusMetadata:
"""
Expand All @@ -298,23 +311,28 @@ def index_audio_file(self, path: str) -> OctopusMetadata:
if not os.path.exists(path):
raise OctopusIOError("Couldn't find input file at `%s`." % path)

c_metadata = c_void_p()
metadata_size = c_int32()

status = self._index_file_func(
status = self._index_file_size_func(
self._handle,
path.encode('utf-8'),
byref(c_metadata),
byref(metadata_size))
if status is not self.PicovoiceStatuses.SUCCESS:
raise self._PICOVOICE_STATUS_TO_EXCEPTION[status](
message='Index failed',
message='Index file size failed',
message_stack=self._get_error_stack())

metadata = OctopusMetadata.create_owned(c_metadata, metadata_size.value)
self._pv_free(c_metadata)
metadata_bytes = create_string_buffer(metadata_size.value)
metadata_bytes_ptr = cast(metadata_bytes, c_void_p)
status = self._index_file_func(
self._handle,
path.encode('utf-8'),
metadata_bytes)
if status is not self.PicovoiceStatuses.SUCCESS:
raise self._PICOVOICE_STATUS_TO_EXCEPTION[status](
message='Index file failed',
message_stack=self._get_error_stack())

return metadata
return OctopusMetadata(metadata_bytes_ptr, metadata_size.value)

Match = namedtuple('Match', ['start_sec', 'end_sec', 'probability'])

Expand Down Expand Up @@ -363,6 +381,7 @@ def search(self, metadata: OctopusMetadata, phrases: Iterable[str]) -> Dict[str,
probability=c_phrase_matches[i].probability)
phrase_matches.append(match)
matches[phrase] = phrase_matches
self._matches_delete_func(c_phrase_matches)

return matches

Expand Down
16 changes: 9 additions & 7 deletions binding/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
INCLUDE_FILES = ('../../LICENSE', '__init__.py', '_factory.py', '_octopus.py', '_util.py')
INCLUDE_LIBS = ('linux', 'mac', 'windows')

# os.system('git clean -dfx')
os.system('git clean -dfx')

package_folder = os.path.join(os.path.dirname(__file__), 'pvoctopus')
os.mkdir(package_folder)
Expand All @@ -16,18 +16,20 @@
shutil.copy(os.path.join(os.path.dirname(__file__), rel_path), package_folder)
manifest_in += "include pvoctopus/%s\n" % os.path.basename(rel_path)

os.mkdir(os.path.join(package_folder, 'lib'))
model_subdir = 'lib/common/param'
model_file = 'octopus_params.pv'
os.makedirs(os.path.join(package_folder, model_subdir))
shutil.copy(
os.path.join(os.path.dirname(__file__), '../..', model_subdir, model_file),
os.path.join(package_folder, model_subdir, model_file))
manifest_in += "include pvoctopus/%s/%s\n" % (model_subdir, model_file)

for platform in INCLUDE_LIBS:
shutil.copytree(
os.path.join(os.path.dirname(__file__), '../../lib', platform),
os.path.join(package_folder, 'lib', platform))
manifest_in += "recursive-include pvoctopus/lib/%s/ *\n" % platform

shutil.copy(
os.path.join(os.path.dirname(__file__), '../../lib/common/param/octopus_params.pv'),
os.path.join(package_folder, 'lib/common/param/octopus_params.pv'))
manifest_in += "include pvoctopus/lib/common/param/octopus_params.pv\n"

with open(os.path.join(os.path.dirname(__file__), 'MANIFEST.in'), 'w') as f:
f.write(manifest_in)

Expand Down
12 changes: 9 additions & 3 deletions binding/python/test_octopus.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
["es", {"manzana": [(5.184, 5.984, 1)]}],
["fr", {"perroquet": [(4.352, 5.184, 0.952)]}],
["it", {"porcospino": [(0.480, 1.728, 1)]}],
["ja", {"りんご": [(0.960, 1.664, 1)]}],
["ja", {"りんご": [(0.990, 1.634, 1)]}],
["ko", {"아이스크림": [(6.592, 7.520, 0.961)]}],
["pt", {"porco espinho": [(0.480, 1.792, 1)]}],
]
Expand Down Expand Up @@ -188,7 +188,10 @@ def test_version(self):
def test_message_stack(self):
error = None
try:
o = self._create_octopus()
o = Octopus(
access_key='invalid',
library_path=default_library_path(self._relative),
model_path=get_model_path_by_language(self._relative))
self.assertIsNone(o)
except OctopusError as e:
error = e.message_stack
Expand All @@ -197,7 +200,10 @@ def test_message_stack(self):
self.assertGreater(len(error), 0)

try:
o = self._create_octopus()
o = Octopus(
access_key='invalid',
library_path=default_library_path(self._relative),
model_path=get_model_path_by_language(self._relative))
self.assertIsNone(o)
except OctopusError as e:
self.assertEqual(len(error), len(e.message_stack))
Expand Down
2 changes: 1 addition & 1 deletion binding/python/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def get_audio_path_by_language(relative: str, language: str = 'en') -> str:


def get_model_path_by_language(relative: str, language: str = 'en'):
model_file_path = 'lib/common/octopus_params%s.pv' % ('' if language == 'en' else ('_%s' % language))
model_file_path = 'lib/common/param/octopus_params%s.pv' % ('' if language == 'en' else ('_%s' % language))
return os.path.join(
os.path.dirname(__file__),
relative,
Expand Down

0 comments on commit 553705c

Please sign in to comment.