Skip to content
Merged
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
27 changes: 18 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ The official Python library for the Fish Audio API
```bash
pip install fish-audio-sdk

# With audio playback utilities
# To play audio without FFmpeg (adds sounddevice and soundfile)
pip install "fish-audio-sdk[utils]"
```

Expand All @@ -56,15 +56,12 @@ client = FishAudio(api_key="your_api_key")

```python
from fishaudio import FishAudio
from fishaudio.utils import play, save
from fishaudio.utils import save

client = FishAudio()

# Generate audio
# Generate audio (MP3 by default) and save it to a file
audio = client.tts.convert(text="Hello, world!")

# Play or save
play(audio)
save(audio, "output.mp3")
```

Expand All @@ -73,19 +70,31 @@ save(audio, "output.mp3")
```python
import asyncio
from fishaudio import AsyncFishAudio
from fishaudio.utils import play, save
from fishaudio.utils import save


async def main():
client = AsyncFishAudio()
audio = await client.tts.convert(text="Hello, world!")
play(audio)
save(audio, "output.mp3")


asyncio.run(main())
```

**Playing the audio (optional):** `play` runs the `ffplay` program from [FFmpeg](https://ffmpeg.org/download.html), which must be on your `PATH`; without it, `play` raises `DependencyError`. To play without FFmpeg, install the `[utils]` extra (on Linux, `sounddevice` also needs the PortAudio library, such as `libportaudio2`) and pass `use_ffmpeg=False`.

```python
from fishaudio.utils import play

with open("output.mp3", "rb") as f:
audio = f.read()

play(audio) # needs ffplay on PATH
# Or, without FFmpeg, after pip install "fish-audio-sdk[utils]":
# play(audio, use_ffmpeg=False)
```

## Core Features

### Text-to-Speech
Expand Down Expand Up @@ -144,7 +153,7 @@ config = TTSConfig(
prosody=Prosody(speed=1.2, volume=-5),
reference_id="933563129e564b19a115bedd57b7406a",
format="wav",
latency="balanced",
latency="balanced", # or "normal" (quality), "low" (earlier first audio on s3-pro)
)

# Reuse across generations
Expand Down
20 changes: 10 additions & 10 deletions src/fishaudio/resources/tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def stream(
reference_id: Voice reference ID (overrides config.reference_id if provided)
references: Reference audio samples (overrides config.references if provided)
format: Audio format - "mp3", "wav", "pcm", or "opus" (overrides config.format if provided)
latency: Latency mode - "normal" or "balanced" (overrides config.latency if provided)
latency: Latency mode - "normal", "balanced" or "low" (overrides config.latency if provided)
speed: Speech speed multiplier, e.g. 1.5 for 1.5x speed (overrides config.prosody.speed if provided)
config: TTS configuration (audio settings, voice, model parameters)
model: TTS model to use. Deprecated models ("s1", which will be
Expand Down Expand Up @@ -273,7 +273,7 @@ def convert(
reference_id: Voice reference ID (overrides config.reference_id if provided)
references: Reference audio samples (overrides config.references if provided)
format: Audio format - "mp3", "wav", "pcm", or "opus" (overrides config.format if provided)
latency: Latency mode - "normal" or "balanced" (overrides config.latency if provided)
latency: Latency mode - "normal", "balanced" or "low" (overrides config.latency if provided)
speed: Speech speed multiplier, e.g. 1.5 for 1.5x speed (overrides config.prosody.speed if provided)
config: TTS configuration (audio settings, voice, model parameters)
model: TTS model to use. Deprecated models ("s1", which will be
Expand Down Expand Up @@ -339,7 +339,7 @@ def stream_with_timestamps(
format: Audio format - "mp3", "wav", "pcm", or "opus" (overrides config.format if provided).
"opus" is the best fit for playing audio while it streams; "wav" and "pcm"
are uncompressed, which keeps sample positions exact.
latency: Latency mode - "normal" or "balanced" (overrides config.latency if provided)
latency: Latency mode - "normal", "balanced" or "low" (overrides config.latency if provided)
speed: Speech speed multiplier, e.g. 1.5 for 1.5x speed (overrides config.prosody.speed if provided)
config: TTS configuration (audio settings, voice, model parameters)
model: TTS model to use. Deprecated models ("s1", which will be
Expand Down Expand Up @@ -428,7 +428,7 @@ def convert_with_timestamps(
format: Audio format - "mp3", "wav", "pcm", or "opus" (overrides config.format if provided).
"opus" is the best fit for playing audio while it streams; "wav" and "pcm"
are uncompressed, which keeps sample positions exact.
latency: Latency mode - "normal" or "balanced" (overrides config.latency if provided)
latency: Latency mode - "normal", "balanced" or "low" (overrides config.latency if provided)
speed: Speech speed multiplier, e.g. 1.5 for 1.5x speed (overrides config.prosody.speed if provided)
config: TTS configuration (audio settings, voice, model parameters)
model: TTS model to use. Deprecated models ("s1", which will be
Expand Down Expand Up @@ -493,7 +493,7 @@ def stream_websocket(
reference_id: Voice reference ID (overrides config.reference_id if provided)
references: Reference audio samples (overrides config.references if provided)
format: Audio format - "mp3", "wav", "pcm", or "opus" (overrides config.format if provided)
latency: Latency mode - "normal" or "balanced" (overrides config.latency if provided)
latency: Latency mode - "normal", "balanced" or "low" (overrides config.latency if provided)
speed: Speech speed multiplier, e.g. 1.5 for 1.5x speed (overrides config.prosody.speed if provided)
config: TTS configuration (audio settings, voice, model parameters)
model: TTS model to use. Deprecated models ("s1", which will be
Expand Down Expand Up @@ -654,7 +654,7 @@ async def stream(
reference_id: Voice reference ID (overrides config.reference_id if provided)
references: Reference audio samples (overrides config.references if provided)
format: Audio format - "mp3", "wav", "pcm", or "opus" (overrides config.format if provided)
latency: Latency mode - "normal" or "balanced" (overrides config.latency if provided)
latency: Latency mode - "normal", "balanced" or "low" (overrides config.latency if provided)
speed: Speech speed multiplier, e.g. 1.5 for 1.5x speed (overrides config.prosody.speed if provided)
config: TTS configuration (audio settings, voice, model parameters)
model: TTS model to use. Deprecated models ("s1", which will be
Expand Down Expand Up @@ -751,7 +751,7 @@ async def convert(
reference_id: Voice reference ID (overrides config.reference_id if provided)
references: Reference audio samples (overrides config.references if provided)
format: Audio format - "mp3", "wav", "pcm", or "opus" (overrides config.format if provided)
latency: Latency mode - "normal" or "balanced" (overrides config.latency if provided)
latency: Latency mode - "normal", "balanced" or "low" (overrides config.latency if provided)
speed: Speech speed multiplier, e.g. 1.5 for 1.5x speed (overrides config.prosody.speed if provided)
config: TTS configuration (audio settings, voice, model parameters)
model: TTS model to use. Deprecated models ("s1", which will be
Expand Down Expand Up @@ -818,7 +818,7 @@ async def stream_with_timestamps(
format: Audio format - "mp3", "wav", "pcm", or "opus" (overrides config.format if provided).
"opus" is the best fit for playing audio while it streams; "wav" and "pcm"
are uncompressed, which keeps sample positions exact.
latency: Latency mode - "normal" or "balanced" (overrides config.latency if provided)
latency: Latency mode - "normal", "balanced" or "low" (overrides config.latency if provided)
speed: Speech speed multiplier, e.g. 1.5 for 1.5x speed (overrides config.prosody.speed if provided)
config: TTS configuration (audio settings, voice, model parameters)
model: TTS model to use. Deprecated models ("s1", which will be
Expand Down Expand Up @@ -908,7 +908,7 @@ async def convert_with_timestamps(
format: Audio format - "mp3", "wav", "pcm", or "opus" (overrides config.format if provided).
"opus" is the best fit for playing audio while it streams; "wav" and "pcm"
are uncompressed, which keeps sample positions exact.
latency: Latency mode - "normal" or "balanced" (overrides config.latency if provided)
latency: Latency mode - "normal", "balanced" or "low" (overrides config.latency if provided)
speed: Speech speed multiplier, e.g. 1.5 for 1.5x speed (overrides config.prosody.speed if provided)
config: TTS configuration (audio settings, voice, model parameters)
model: TTS model to use. Deprecated models ("s1", which will be
Expand Down Expand Up @@ -973,7 +973,7 @@ async def stream_websocket(
reference_id: Voice reference ID (overrides config.reference_id if provided)
references: Reference audio samples (overrides config.references if provided)
format: Audio format - "mp3", "wav", "pcm", or "opus" (overrides config.format if provided)
latency: Latency mode - "normal" or "balanced" (overrides config.latency if provided)
latency: Latency mode - "normal", "balanced" or "low" (overrides config.latency if provided)
speed: Speech speed multiplier, e.g. 1.5 for 1.5x speed (overrides config.prosody.speed if provided)
config: TTS configuration (audio settings, voice, model parameters)
model: TTS model to use. Deprecated models ("s1", which will be
Expand Down
6 changes: 4 additions & 2 deletions src/fishaudio/types/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,7 @@ def warn_if_deprecated_model(
# Model state types
ModelState = Literal["created", "training", "trained", "failed"]

# Latency modes
LatencyMode = Literal["normal", "balanced"]
# Latency modes. "low" gives earlier first audio on s3-pro. It is not available
# on s3-pro-free, which accepts it without a speed-up; s2.1-pro showed no
# speed-up either.
LatencyMode = Literal["low", "normal", "balanced"]
4 changes: 3 additions & 1 deletion src/fishaudio/types/tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ class TTSConfig(BaseModel):
normalize: Whether to normalize/clean the input text. Default: True
chunk_length: Characters per generation chunk. Range: 100-300. Default: 200.
Lower values = faster initial response, higher values = better quality
latency: Generation mode. Options: "normal" (higher quality), "balanced" (faster). Default: "balanced"
latency: Generation mode. Options: "normal" (higher quality), "balanced" (faster),
"low" (earlier first audio on s3-pro). Default: "balanced"
reference_id: Voice model ID from fish.audio (e.g., "802e3bc2b27e49c2995d23ef70e6ac89").
Find IDs in voice URLs or via voices.list()
references: List of reference audio samples for instant voice cloning. Default: []
Expand Down Expand Up @@ -129,6 +130,7 @@ class TTSRequest(BaseModel):
reference_id: Voice model ID for using a specific voice. Default: None
normalize: Whether to normalize/clean the input text. Default: True
latency: Generation mode. Options: "normal", "balanced". Default: "balanced"
"low" is also accepted; it gives earlier first audio on s3-pro.
Comment on lines 132 to +133
prosody: Speech speed and volume settings. Default: None
top_p: Nucleus sampling for token selection. Range: 0.0-1.0. Default: 0.7
temperature: Randomness in generation. Range: 0.0-1.0. Default: 0.7
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/test_tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,21 @@ def test_convert_with_latency_parameter(self, tts_client, mock_client_wrapper):
payload = ormsgpack.unpackb(call_args[1]["content"])
assert payload["latency"] == "normal"

def test_convert_with_low_latency(self, tts_client, mock_client_wrapper):
"""latency="low", as a parameter or in the config, is sent as "low"."""
mock_response = Mock()
mock_response.iter_bytes.return_value = iter([b"audio"])
mock_client_wrapper.request.return_value = mock_response

tts_client.convert(text="Hello", latency="low")
payload = ormsgpack.unpackb(mock_client_wrapper.request.call_args[1]["content"])
assert payload["latency"] == "low"

mock_response.iter_bytes.return_value = iter([b"audio"])
tts_client.convert(text="Hello", config=TTSConfig(latency="low"))
payload = ormsgpack.unpackb(mock_client_wrapper.request.call_args[1]["content"])
assert payload["latency"] == "low"

def test_convert_with_speed_parameter(self, tts_client, mock_client_wrapper):
"""Test TTS with speed as direct parameter."""
mock_response = Mock()
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/test_types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
"""Tests for type definitions."""

import typing
from decimal import Decimal

import pytest
from pydantic import ValidationError

from fishaudio.types import (
ASRResponse,
ASRSegment,
Expand All @@ -15,6 +19,7 @@
TTSRequest,
Voice,
)
from fishaudio.types.shared import LatencyMode


class TestVoice:
Expand Down Expand Up @@ -190,6 +195,21 @@ def test_tts_config_custom_new_parameters(self):
assert config.condition_on_previous_chunks is False
assert config.early_stop_threshold == 0.8

def test_latency_mode_names_every_api_value(self):
"""LatencyMode offers the three values the API accepts, including low."""
assert set(typing.get_args(LatencyMode)) == {"low", "normal", "balanced"}

@pytest.mark.parametrize("latency", ["low", "normal", "balanced"])
def test_tts_config_and_request_accept_latency(self, latency):
"""TTSConfig and TTSRequest accept each latency value."""
assert TTSConfig(latency=latency).latency == latency
assert TTSRequest(text="Hi", latency=latency).latency == latency

def test_tts_config_rejects_unknown_latency(self):
"""A latency value the API does not know is rejected before sending."""
with pytest.raises(ValidationError):
TTSConfig(latency="fastest") # ty: ignore[invalid-argument-type]

def test_tts_request_defaults(self):
"""Test TTSRequest default values including new parameters."""
request = TTSRequest(text="Hello world")
Expand Down