File size: 2,976 Bytes
9410704
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import asyncio

from elevenlabs import VoiceSettings, Voice
from elevenlabs.play import play
from elevenlabs.client import AsyncElevenLabs, ElevenLabs

from .utils import IN_GITHUB, DEFAULT_TEXT, DEFAULT_VOICE, DEFAULT_MODEL
import base64


def test_tts_convert() -> None:
    """Test basic text-to-speech generation."""
    client = ElevenLabs()
    audio_generator = client.text_to_speech.convert(text=DEFAULT_TEXT, voice_id=DEFAULT_VOICE, model_id=DEFAULT_MODEL)
    audio = b"".join(audio_generator)
    assert isinstance(audio, bytes), "TTS should return bytes"
    if not IN_GITHUB:
        play(audio)


def test_tts_convert_with_voice_settings() -> None:
    """Test TTS with custom voice settings."""
    client = ElevenLabs()
    audio_generator = client.text_to_speech.convert(
        text=DEFAULT_TEXT,
        voice_id=DEFAULT_VOICE,
        model_id=DEFAULT_MODEL,
        voice_settings=VoiceSettings(stability=0.71, similarity_boost=0.5, style=0.0, use_speaker_boost=True),
    )
    audio = b"".join(audio_generator)
    assert isinstance(audio, bytes), "TTS with voice settings should return bytes"
    if not IN_GITHUB:
        play(audio)


def test_tts_convert_as_stream():
    async def main():
        async_client = AsyncElevenLabs()
        results = async_client.text_to_speech.stream(
            text=DEFAULT_TEXT, voice_id=DEFAULT_VOICE, model_id=DEFAULT_MODEL
        )
        out = b""
        async for value in results:
            assert isinstance(value, bytes), "Stream chunks should be bytes"
            out += value
        if not IN_GITHUB:
            play(out)

    asyncio.run(main())


def test_tts_convert_with_timestamps() -> None:
    """Test TTS generation with timestamps."""
    client = ElevenLabs()
    result = client.text_to_speech.convert_with_timestamps(
        text=DEFAULT_TEXT, voice_id=DEFAULT_VOICE, model_id=DEFAULT_MODEL
    )

    # Check that the alignment data exists and has the expected structure
    assert hasattr(result, 'alignment')
    assert hasattr(result, 'audio_base_64')

    # Verify alignment contains timing data
    assert result.alignment is not None
    assert len(result.alignment.character_start_times_seconds) > 0
    assert len(result.alignment.character_end_times_seconds) > 0

    if not IN_GITHUB:
        audio_bytes = base64.b64decode(result.audio_base_64)
        play(audio_bytes)


def test_tts_stream_with_timestamps():
    async def main():
        async_client = AsyncElevenLabs()
        audio_data = b""
        async_stream = async_client.text_to_speech.stream_with_timestamps(
            voice_id=DEFAULT_VOICE,
            text=DEFAULT_TEXT,
            model_id=DEFAULT_MODEL,
        )
        async for chunk in async_stream:
            if hasattr(chunk, "audio_base_64"):
                audio_bytes = base64.b64decode(chunk.audio_base_64)
                audio_data += audio_bytes

        if not IN_GITHUB:
            play(audio_data)

    asyncio.run(main())