Created
March 12, 2024 16:56
-
-
Save willwade/b0427b83db51d529d6b9d3aa099f8c4e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import eng_to_ipa as ipa | |
# The word you want to convert | |
word = "dog" | |
# Convert the word to its IPA representation | |
word_ipa = ipa.convert(word) | |
# Format the IPA representation for SSML | |
# Note: SSML tags are used here for illustrative purposes; actual usage depends on the SSML interpreter's capabilities | |
ssml_output = f"<phoneme alphabet='ipa' ph='{word_ipa}'>{word}</phoneme>" | |
print(ssml_output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
# Azure subscription key and service region | |
subscription_key = 'YourAzureSubscriptionKey' | |
service_region = 'YourServiceRegion' | |
# Set up the TTS endpoint | |
tts_endpoint = f'https://{service_region}.tts.speech.microsoft.com/cognitiveservices/v1' | |
# Set up the headers for the HTTP request | |
headers = { | |
'Ocp-Apim-Subscription-Key': subscription_key, | |
'Content-Type': 'application/ssml+xml', | |
'X-Microsoft-OutputFormat': 'audio-16khz-32kbitrate-mono-mp3' | |
} | |
# The SSML document, including IPA notation for the word "dog" | |
ssml = """ | |
<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='en-US'> | |
<voice name='en-US-AriaNeural'> | |
<phoneme alphabet='ipa' ph='dɔg'>dog</phoneme> | |
</voice> | |
</speak> | |
""" | |
# Make the HTTP request to the Azure TTS service | |
response = requests.post(tts_endpoint, headers=headers, data=ssml) | |
# Check if the request was successful | |
if response.status_code == 200: | |
# Save the audio to a file | |
with open('output.mp3', 'wb') as audio_file: | |
audio_file.write(response.content) | |
print("Audio saved to output.mp3") | |
else: | |
print(f"Error: {response.status_code}") | |
print(response.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment