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 legacy voices #141

Merged
merged 2 commits into from
Jul 24, 2024
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
2 changes: 2 additions & 0 deletions custom_components/elevenlabs_tts/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@
DEFAULT_STYLE = 0.2
CONF_USE_SPEAKER_BOOST = "use_speaker_boost"
DEFAULT_USE_SPEAKER_BOOST = True

LEGACY_VOICE_SUFFIX = " (Legacy)"
11 changes: 9 additions & 2 deletions custom_components/elevenlabs_tts/elevenlabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
DEFAULT_STYLE,
DEFAULT_USE_SPEAKER_BOOST,
DEFAULT_VOICE,
LEGACY_VOICE_SUFFIX,
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -92,20 +93,26 @@ async def post(

async def get_voices(self) -> dict:
"""Get voices from the API."""
endpoint = "voices"
endpoint = "voices?show_legacy=true"
voices = await self.get(endpoint)
self._voices = voices.get("voices", [])

self.voices = []

for voice in self._voices:
new_voice = Voice(voice_id=voice["voice_id"], name=voice["name"])
if voice.get("is_legacy"):
name = voice["name"] + LEGACY_VOICE_SUFFIX
else:
name = voice["name"]
new_voice = Voice(voice_id=voice["voice_id"], name=name)
self.voices.append(new_voice)

return self._voices

async def get_voice_by_name_or_id(self, identifier: str) -> dict:
"""Get a voice by its name or ID."""
# Remove potential legacy suffix from identifier
identifier = identifier.replace(LEGACY_VOICE_SUFFIX, "")
_LOGGER.debug("Looking for voice with identifier %s", identifier)
for voice in self._voices:
if voice["name"] == identifier or voice["voice_id"] == identifier:
Expand Down
5 changes: 4 additions & 1 deletion tests/test_elevenlabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,10 @@ async def test_get_voices(client):
assert voices == mock_response["voices"]

# Assert that the request was made with the correct URL and headers
assert respx.calls[0].request.url == "https://api.elevenlabs.io/v1/voices"
assert (
respx.calls[0].request.url
== "https://api.elevenlabs.io/v1/voices?show_legacy=true"
)
assert respx.calls[0].request.headers["xi-api-key"] == client._api_key

# Assert that the client's _voices attribute is updated correctly
Expand Down