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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Per-PR attribution and contributor credits are published automatically on the co
## [Unreleased]
### Fixed
- `GAP_TRIM_MS=0` turns the base chunk trim off. It used to slice every chunk to nothing, so all audio came back silent
- Non-streamed WAV (`"stream": false`) has its real length in the header. It said 4 GB, so readers that trust the header, like Python's `wave`, reported a one-sentence file as about 25 hours long (#586)

## [v0.10.0] - 2026-11-08: Websockets/Realtime API, CPU Memory & API Keys
### Added
Expand Down
20 changes: 20 additions & 0 deletions api/src/services/streaming_audio_writer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Audio conversion service with proper streaming support"""

import struct
from io import BytesIO
from typing import Optional

Expand All @@ -8,6 +9,25 @@
from loguru import logger


def set_wav_sizes(data: bytes) -> bytes:
"""Replace the streaming size placeholders in a complete WAV with the real sizes."""
if data[:4] != b"RIFF" or data[8:12] != b"WAVE":
return data
Comment thread
remsky marked this conversation as resolved.
Resolved
if len(data) > 0xFFFFFFFF:
logger.warning("WAV over 4 GiB, using placeholder streaming size")
return data
out = bytearray(data)
struct.pack_into("<I", out, 4, len(out) - 8)
pos = 12
while pos + 8 <= len(out):
if out[pos : pos + 4] == b"data":
struct.pack_into("<I", out, pos + 4, len(out) - pos - 8)
break
(size,) = struct.unpack_from("<I", out, pos + 4)
pos += 8 + size + (size & 1)
return bytes(out)


class StreamingAudioWriter:
"""Handles streaming audio format conversions"""

Expand Down
9 changes: 5 additions & 4 deletions api/src/services/tts_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from ..inference.voice_manager import get_manager as get_voice_manager
from ..structures.schemas import NormalizationOptions, clamp_rate
from .audio import AudioNormalizer, AudioService
from .streaming_audio_writer import StreamingAudioWriter
from .streaming_audio_writer import StreamingAudioWriter, set_wav_sizes
from .text_processing.text_processor import (
check_pause_budget,
smart_split,
Expand Down Expand Up @@ -550,9 +550,10 @@ async def generate_audio(
if not output_parts:
raise ValueError("Input contains no speakable text")

return AudioChunk(
np.array([], dtype=np.int16), word_timestamps, b"".join(output_parts)
)
output = b"".join(output_parts)
if output_format == "wav":
output = set_wav_sizes(output)
return AudioChunk(np.array([], dtype=np.int16), word_timestamps, output)
except Exception as e:
logger.error(f"Error in audio generation: {str(e)}")
raise
Expand Down
37 changes: 36 additions & 1 deletion api/tests/test_tts_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import io
import struct
import wave
from contextlib import asynccontextmanager
from pathlib import Path
from unittest.mock import AsyncMock, MagicMock, patch
Expand All @@ -10,7 +12,7 @@

from api.src.inference.base import AudioChunk
from api.src.inference.kokoro_v1 import KokoroV1
from api.src.services.streaming_audio_writer import StreamingAudioWriter
from api.src.services.streaming_audio_writer import StreamingAudioWriter, set_wav_sizes
from api.src.services.tts_service import TTSService
from api.src.structures.schemas import WordTimestamp

Expand Down Expand Up @@ -613,6 +615,39 @@ async def fake_stream(*args, **kwargs):
assert [t.word for t in result.word_timestamps] == ["id3", "frame1", "frame2"]


@pytest.mark.asyncio
async def test_generate_audio_wav_header_carries_real_sizes():
"""Non-streamed WAV is complete, so its sizes must not keep the streaming placeholder (#586)."""
service = await _stubbed_service()
audio = (np.sin(np.linspace(0, 200, 4800)) * 20000).astype(np.int16)
writer = StreamingAudioWriter("wav", sample_rate=24000)

async def fake_stream(*args, **kwargs):
empty = np.array([], dtype=np.int16)
yield AudioChunk(empty, output=writer.write_chunk(audio))
yield AudioChunk(empty, output=writer.write_chunk(audio))
yield AudioChunk(empty, output=writer.write_chunk(finalize=True))

service.generate_audio_stream = fake_stream

result = await service.generate_audio("hi", "af_heart", writer, output_format="wav")

assert struct.unpack("<I", result.output[4:8])[0] == len(result.output) - 8
with wave.open(io.BytesIO(result.output)) as wav:
assert wav.getnframes() == 2 * len(audio)


def test_set_wav_sizes_keeps_placeholder_past_4_gib():
"""Sizes past 4 GiB do not fit the header, so the WAV is returned unchanged."""

class Huge(bytes):
def __len__(self):
return 0x1_0000_0000

data = Huge(b"RIFF\xff\xff\xff\xffWAVE")
assert set_wav_sizes(data) is data


@pytest.mark.asyncio
async def test_pause_budget_survives_voice_tag_segmentation():
"""Per-segment splitting must not reset the request's pause budget."""
Expand Down