Skip to content

Commit

Permalink
remove duplicate synth_to_bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
willwade committed Sep 27, 2024
1 parent cedbdf2 commit 726c53b
Showing 1 changed file with 0 additions and 28 deletions.
28 changes: 0 additions & 28 deletions tts_wrapper/tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ def _convert_mp3_to_pcm(self, mp3_data: bytes) -> bytes:
:return: Raw PCM data as bytes (int16).
"""
from soundfile import read # type: ignore
from io import BytesIO

# Use soundfile to read MP3 data
mp3_fp = BytesIO(mp3_data)
Expand Down Expand Up @@ -111,7 +110,6 @@ def _convert_audio(
target_format = "wav"
if target_format not in ["mp3", "flac", "wav"]:
raise ValueError(f"Unsupported format: {target_format}")
from io import BytesIO

# Create an in-memory file object
output = BytesIO()
Expand Down Expand Up @@ -172,32 +170,6 @@ def synth_to_bytes(self, text: Any) -> bytes:
"""
pass

def synth_to_bytestream(self, text: Any, format: Optional[str] = "wav") -> BytesIO:
"""
Synthesizes text to an in-memory bytestream in the specified audio format.
:param text: The text to synthesize.
:param format: The audio format (e.g., 'wav', 'mp3', 'flac'). Default: 'wav'.
:return: A BytesIO object containing the audio data.
"""
try:
# Synthesize text to raw PCM bytes
audio_bytes = self.synth_to_bytes(text)
pcm_data = np.frombuffer(audio_bytes, dtype=np.int16)

# Convert PCM data to the desired audio format
format = format if format else "wav"
converted_audio = self._convert_audio(pcm_data, format, self.audio_rate)

# Wrap the converted audio bytes in a BytesIO bytestream
bytestream = BytesIO(converted_audio)
bytestream.seek(0) # Reset stream position to the beginning

return bytestream
except Exception as e:
logging.error(f"Error in synth_to_bytestream: {e}")
raise

def synth_to_file(
self, text: Any, filename: str, format: Optional[str] = "wav"
) -> None:
Expand Down

0 comments on commit 726c53b

Please sign in to comment.