Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for AAC audio encoding #299

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies = [
"ctranslate2>=4.5.0",
"fastapi>=0.115.6",
"faster-whisper>=1.1.1",
"ffmpeg-python>=0.2.0",
"huggingface-hub[hf-transfer]>=0.25.1",
"kokoro-onnx[gpu]>=0.3.6,<0.4.0",
"numpy>=2.1.1",
Expand Down
38 changes: 35 additions & 3 deletions src/speaches/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import numpy as np
import soundfile as sf
import ffmpeg

from speaches.config import SAMPLES_PER_SECOND

Expand Down Expand Up @@ -50,9 +51,40 @@ def convert_audio_format(
subtype=subtype,
endian=endian,
)
converted_audio_bytes_buffer = io.BytesIO()
sf.write(converted_audio_bytes_buffer, data, samplerate=sample_rate, format=audio_format)
return converted_audio_bytes_buffer.getvalue()

if audio_format == "aac":
try:
# Write to WAV in memory
wav_buffer = io.BytesIO()
sf.write(wav_buffer, data, samplerate=sample_rate, format='WAV')
wav_bytes = wav_buffer.getvalue()

# Convert WAV to AAC using ffmpeg
input_stream = ffmpeg.input('pipe:', format='wav')
output_stream = ffmpeg.output(
input_stream,
'pipe:',
acodec='aac',
ab='192k',
f='adts' # AAC container format
)

out_bytes, _ = ffmpeg.run(
output_stream,
input=wav_bytes, # Use the WAV bytes
capture_stdout=True,
capture_stderr=True
)

return out_bytes

except ffmpeg.Error as e:
logger.error(f"FFmpeg conversion failed: {e.stderr.decode()}")
raise
else:
converted_audio_bytes_buffer = io.BytesIO()
sf.write(converted_audio_bytes_buffer, data, samplerate=sample_rate, format=audio_format)
return converted_audio_bytes_buffer.getvalue()


def audio_samples_from_file(file: BinaryIO) -> NDArray[np.float32]:
Expand Down
6 changes: 3 additions & 3 deletions src/speaches/routers/speech.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
OPENAI_SUPPORTED_SPEECH_VOICE_NAMES = ("alloy", "echo", "fable", "onyx", "nova", "shimmer")

# https://platform.openai.com/docs/guides/text-to-speech/supported-output-formats
type ResponseFormat = Literal["mp3", "flac", "wav", "pcm"]
SUPPORTED_RESPONSE_FORMATS = ("mp3", "flac", "wav", "pcm")
UNSUPORTED_RESPONSE_FORMATS = ("opus", "aac")
type ResponseFormat = Literal["mp3", "flac", "wav", "pcm", "aac"]
SUPPORTED_RESPONSE_FORMATS = ("mp3", "flac", "wav", "pcm", "aac")
UNSUPORTED_RESPONSE_FORMATS = ("opus")

MIN_SAMPLE_RATE = 8000
MAX_SAMPLE_RATE = 48000
Expand Down
23 changes: 23 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.