# SUMMARY
This code is a script that converts text to speech using the edge-tts tool
and plays the resulting audio using the pygame module. The script takes a
single argument called data, which is the text to be converted to speech. It
first runs the edge-tts tool with the specified voice and text and writes
the resulting audio to a file called "data.mp3". It then initializes the
pygame module, loads the "data.mp3" file into the pygame mixer, and plays
the audio file. The script has exception handling to handle any errors that
might occur, and it cleans up by stopping the audio file and shutting down
the pygame mixer when it is finished.
# Here is an explanation of each line of code in the provided script:
1 . Pip Install :
- pip install edge-tts
- pip install pygame
- pip install os
2 . Imports :
- import os
- import pygame
- import os: This line imports the Python os module, which provides functions for interacting with the operating system.
- import pygame: This line imports the Python pygame module, which provides functions for creating games and other interactive applications.
#Example: edge-tts command
- edge-tts --text "Hello, world!" --write-media hello.mp3
- f'edge-tts --pitch="{pitch}" --rate="{rate}" --volume="{volume}" --voice "{voice}" --text "{text}" --write-media "{filename}"'
3. Explanation
- voice = "en-GB-SoniaNeural"
This line creates a string variable called voice and sets its value to
"en-GB-SoniaNeural", which is the voice to be used for
text-to-speech.
- def speak(data):
This line defines a function called speak that takes a single argument
called data. The lines of code indented underneath this line are part of
the function.
- command1 = f'edge-tts --voice "{voice}" --text "{data}" --write-media "data.mp3"'
This line creates a string called command1 that includes a command to run
the edge-tts tool with the specified voice and text, and writes the
resulting audio to a file called "data.mp3". The f before the string
indicates that this is an f-string, which allows you to embed expressions
in a string by enclosing them in curly braces.
- os.system(command1)
This line runs the command1 string as a command in the operating system.
This will run the edge-tts tool and create the "data.mp3" file.
- pygame.init()
This line initializes the pygame module.
- pygame.mixer.init()
This line initializes the pygame mixer module, which is used for playing
sounds.
- pygame.mixer.music.load("data.mp3")
This line loads the "data.mp3" file created in the previous step
into the pygame mixer module.
- pygame.mixer.music.play()
This line plays the audio file that was loaded into the pygame
mixer.
- while pygame.mixer.music.get_busy():
This line creates a loop that will continue until the audio file has
finished playing.
- pygame.time.Clock().tick(10)
This line tells the pygame to wait for 10 milliseconds before checking if
the audio file has finished playing.
- except Exception as e:
This line is the start of a block of code that will handle any
exceptions that occur during the execution of the speak function. An
exception is an error that occurs during the execution of a program.
- print(e)
This line prints the exception object (e) to the console.
- finally:
This line is the start of a block of code that will always be
executed, regardless of whether an exception occurred or not.
- pygame.mixer.music.stop()
This line stops the audio file that is currently playing.
- pygame.mixer.quit()
This line shuts down the pygame mixer module.
3 . Full code Speak() Function V1:
- import os
- import pygame
- voice2 = 'en-GB-SoniaNeural'
- def speak(data):
- voice = 'en-US-SteffanNeural'
- command = f'edge-tts --voice "{voice}" --text "{data}" --write-media "data.mp3"'
- os.system(command)
- pygame.init()
- pygame.mixer.init()
- pygame.mixer.music.load("data.mp3")
- try:
- pygame.mixer.music.play()
- while pygame.mixer.music.get_busy():
- pygame.time.Clock().tick(10)
- except Exception as e:
- print(e)
- finally:
- pygame.mixer.music.stop()
- pygame.mixer.quit()
Many people copy and paste the code without watching the video.
Remember, true learning comes from understanding and implementing, not just copying and pasting. By actively working through the code, you'll gain a deeper understanding of the material and be better prepared to apply it to your own projects.
[ In a few days, I will update this page with the code! ]
# 300+ Natural Voices :
Replace Voice as the short name from the list of voices