Skip to content

Commit

Permalink
performance: speed up b64 operations (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl authored Dec 23, 2024
1 parent a5573a8 commit 32f32fe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
16 changes: 13 additions & 3 deletions hivemind_listener/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import base64
import queue
import subprocess
import threading
import time
from dataclasses import dataclass, field
from queue import Queue
from shutil import which
from tempfile import NamedTemporaryFile
from typing import Dict, List, Tuple, Optional, Union

import click
import pybase64
import speech_recognition as sr
from ovos_bus_client import MessageBusClient
from ovos_bus_client.message import Message
Expand Down Expand Up @@ -318,7 +319,12 @@ def get_b64_tts(self, message: Optional[Message] = None) -> str:
# cast to str() to get a path, as it is a AudioFile object from tts cache
with open(wav, "rb") as f:
audio = f.read()
return base64.b64encode(audio).decode("utf-8")

s = time.monotonic()
encoded = pybase64.b64encode(audio).decode("utf-8")
LOG.debug(f"b64 encoding took: {time.monotonic() - s} seconds")

return encoded

def transcribe_b64_audio(self, message: Optional[Message] = None) -> List[Tuple[str, float]]:
"""
Expand All @@ -332,7 +338,11 @@ def transcribe_b64_audio(self, message: Optional[Message] = None) -> List[Tuple[
"""
b64audio = message.data["audio"]
lang = message.data.get("lang", self.plugins.stt.lang)
wav_data = base64.b64decode(b64audio)

s = time.monotonic()
wav_data = pybase64.b64decode(b64audio)
LOG.debug(f"b64 decoding took: {time.monotonic() - s} seconds")

audio = bytes2audiodata(wav_data)
return self.plugins.stt.transcribe(audio, lang)

Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ ovos-simple-listener
hivemind_bus_client>=0.1.0,<1.0.0
ovos-plugin-manager<1.0.0
hivemind-core>=0.1.0,<1.0.0
click
click
pybase64

0 comments on commit 32f32fe

Please sign in to comment.