|
| 1 | +from gtts import gTTS |
| 2 | +import os |
| 3 | + |
| 4 | + |
| 5 | +def text_to_speech( |
| 6 | + text, |
| 7 | + lang="en", |
| 8 | + tld="com", |
| 9 | + slow=False, |
| 10 | + lang_check=True, |
| 11 | + pre_processor_funcs=None, |
| 12 | + tokenizer_func=None, |
| 13 | + timeout=None, |
| 14 | + output_file="output.mp3", |
| 15 | +): |
| 16 | + """ |
| 17 | + Convert the provided text to speech and save it as an audio file. |
| 18 | +
|
| 19 | + Args: |
| 20 | + text (string): The text to be read. |
| 21 | + lang (string, optional): The language (IETF language tag) to read the text in. Default is 'en'. |
| 22 | + tld (string, optional): Top-level domain for Google Translate host (e.g., 'com', 'co.uk'). |
| 23 | + This affects accent localization. Default is 'com'. |
| 24 | + slow (bool, optional): If True, reads the text more slowly. Default is False. |
| 25 | + lang_check (bool, optional): If True, enforces valid language, raising a ValueError if unsupported. Default is True. |
| 26 | + pre_processor_funcs (list, optional): List of pre-processing functions to modify the text before tokenizing. |
| 27 | + Defaults to a list of built-in pre-processors. |
| 28 | + tokenizer_func (callable, optional): Function to tokenize the text. Defaults to a built-in tokenizer. |
| 29 | + timeout (float or tuple, optional): Seconds to wait for server response. Can be a float or a (connect, read) tuple. |
| 30 | + Default is None (wait indefinitely). |
| 31 | + output_file (string): Path for the output audio file (default: 'output.mp3'). |
| 32 | +
|
| 33 | + Raises: |
| 34 | + AssertionError: When text is None or empty. |
| 35 | + ValueError: When lang_check is True and lang is unsupported. |
| 36 | + """ |
| 37 | + |
| 38 | + # Use default pre-processor functions if not provided |
| 39 | + if pre_processor_funcs is None: |
| 40 | + pre_processor_funcs = [ |
| 41 | + # Example built-in functions from gTTS: |
| 42 | + # Converts tone marks, abbreviations, and deals with word substitutions |
| 43 | + lambda text: text.replace( |
| 44 | + ".", "" |
| 45 | + ), # You can define more or use built-ins from gTTS |
| 46 | + ] |
| 47 | + |
| 48 | + # Use default tokenizer if not provided |
| 49 | + if tokenizer_func is None: |
| 50 | + tokenizer_func = lambda text: text.split() # Basic tokenizer example |
| 51 | + |
| 52 | + try: |
| 53 | + # Create the gTTS object with the provided arguments |
| 54 | + tts = gTTS( |
| 55 | + text=text, |
| 56 | + lang=lang, |
| 57 | + tld=tld, |
| 58 | + slow=slow, |
| 59 | + lang_check=lang_check, |
| 60 | + pre_processor_funcs=pre_processor_funcs, |
| 61 | + tokenizer_func=tokenizer_func, |
| 62 | + timeout=timeout, |
| 63 | + ) |
| 64 | + |
| 65 | + # Save the audio file |
| 66 | + tts.save("Text To Speech/"+output_file) |
| 67 | + print(f"Audio saved at Text To Speech/{output_file}") |
| 68 | + |
| 69 | + # Optionally, play the audio file (Windows or Linux/MacOS) |
| 70 | + # if os.name == "nt": # Windows |
| 71 | + # os.system(f"start {output_file}") |
| 72 | + # else: # macOS/Linux |
| 73 | + # os.system(f"xdg-open {output_file}") |
| 74 | + |
| 75 | + except AssertionError as ae: |
| 76 | + print(f"Assertion Error: {ae}") |
| 77 | + except ValueError as ve: |
| 78 | + print(f"Value Error: {ve}") |
| 79 | + except RuntimeError as re: |
| 80 | + print(f"Runtime Error: {re}") |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + # Example usage of the text_to_speech function with various arguments |
| 85 | + |
| 86 | + # Basic example (English, default options) |
| 87 | + text = "Hello, welcome to the gTTS Python tutorial." |
| 88 | + text_to_speech(text) |
| 89 | + |
| 90 | + # # Custom example (Spanish, slow speech, and custom file name) |
| 91 | + # text_to_speech( |
| 92 | + # "Hola, bienvenido al tutorial de gTTS.", |
| 93 | + # lang="es", |
| 94 | + # slow=True, |
| 95 | + # output_file="spanish_slow.mp3", |
| 96 | + # ) |
| 97 | + |
| 98 | + # # Custom example with localized accent (UK English) |
| 99 | + # text_to_speech( |
| 100 | + # "Hello! How are you today?", |
| 101 | + # lang="en", |
| 102 | + # tld="co.uk", |
| 103 | + # output_file="british_accent.mp3", |
| 104 | + # ) |
| 105 | + |
| 106 | + # # You can pass custom pre-processor functions to modify the text before it’s tokenized. |
| 107 | + # text_to_speech( |
| 108 | + # "Dr. Smith is a great person.", |
| 109 | + # pre_processor_funcs=[lambda x: x.replace(".", "")], |
| 110 | + # output_file="custom_pre-processor.mp3", |
| 111 | + # ) |
| 112 | + |
| 113 | + # # You can set a timeout to limit how long the request to Google Translate waits. |
| 114 | + # text_to_speech( |
| 115 | + # "This will timeout after 5 seconds.", |
| 116 | + # output_file="timeout.mp3", |
| 117 | + # timeout=5.0 |
| 118 | + # ) |
0 commit comments