|
| 1 | +# A exclusive CLI version can be made using inquirer library. |
| 2 | +from gtts import gTTS |
| 3 | +from io import BytesIO |
| 4 | + |
| 5 | +# only use when needed to avoid memory usage in program |
| 6 | +from pprint import pprint |
| 7 | + |
| 8 | +"""_summary_ |
| 9 | +def some_function(): |
| 10 | + # Pygame module is only imported when this function is called |
| 11 | + import pygame.mixer as mixer |
| 12 | + mixer.init() |
| 13 | +
|
| 14 | +# USE LAZY LOADING |
| 15 | +
|
| 16 | + Returns: |
| 17 | + _type_: _description_ |
| 18 | + """ |
| 19 | + |
| 20 | +""" |
| 21 | +# For example, if you are using pygame, you might do something like: |
| 22 | +# import pygame |
| 23 | +# audio_file.seek(0) # Reset the BytesIO object to the beginning |
| 24 | +# pygame.mixer.init() |
| 25 | +# pygame.mixer.music.load(audio_file) |
| 26 | +# pygame.mixer.music.play() |
| 27 | +
|
| 28 | +# Note: The actual loading and playing of the MP3 data in an audio library are not provided in the code snippet. |
| 29 | +# The last comments indicate that it depends on the specific audio library you choose. |
| 30 | +
|
| 31 | +""" |
| 32 | +# Should have |
| 33 | + |
| 34 | +# How to play a audio without saving it? |
| 35 | +# efficiently? |
| 36 | +# So I can also combine two languages? |
| 37 | +# Exception for network issues? |
| 38 | + |
| 39 | +# class userAudio: |
| 40 | + |
| 41 | +# print("\n") |
| 42 | +# print(dir(gTTS)) |
| 43 | + |
| 44 | +# file_naming can be added too. |
| 45 | + |
| 46 | + |
| 47 | +class userAudio: |
| 48 | + def __init__( |
| 49 | + self, |
| 50 | + text: str = None, |
| 51 | + language: str = "en", |
| 52 | + slow: bool = True, |
| 53 | + accent: str = "com", |
| 54 | + ): # Correct the syntax here. |
| 55 | + self.lang = language |
| 56 | + self.slow = slow |
| 57 | + self.accent = accent |
| 58 | + |
| 59 | + if text is None: |
| 60 | + self.user_input() |
| 61 | + else: |
| 62 | + self.text_to_audio = text |
| 63 | + |
| 64 | + self.gtts_object = gTTS( |
| 65 | + text=self.text_to_audio, lang=self.lang, slow=self.slow, tld=self.accent |
| 66 | + ) |
| 67 | + |
| 68 | + # ! Some error is here. |
| 69 | + def user_input(self): |
| 70 | + text = input("Enter the text you want to convert to audio: ") |
| 71 | + self.text_to_audio = text |
| 72 | + self.gtts_object = gTTS( |
| 73 | + text=self.text_to_audio, lang=self.lang, slow=self.slow, tld=self.accent |
| 74 | + ) # Just need to understand the class workings little better. |
| 75 | + # Isn't this declaring this again? |
| 76 | + |
| 77 | + def save_only(self, filename="default.mp3"): |
| 78 | + # The class will take care of the playing and saving. |
| 79 | + # The initialisation will take care of it. |
| 80 | + self.gtts_object.save(filename) |
| 81 | + |
| 82 | + def play_only(self): |
| 83 | + from pygame import mixer, time |
| 84 | + |
| 85 | + tts = self.gtts_object |
| 86 | + fp = BytesIO() |
| 87 | + tts.write_to_fp(fp) |
| 88 | + fp.seek(0) # Reset the BytesIO object to the beginning |
| 89 | + mixer.init() |
| 90 | + mixer.music.load(fp) |
| 91 | + mixer.music.play() |
| 92 | + while mixer.music.get_busy(): |
| 93 | + time.Clock().tick(10) |
| 94 | + # Consider using a different method for playing audio, Pygame might not be optimal |
| 95 | + |
| 96 | + # Object initialisation please. |
| 97 | + # def save_path(self): |
| 98 | + # from pathlib import Path |
| 99 | + |
| 100 | + # user_path = Path(input("Enter the path to save the audio: ")) |
| 101 | + |
| 102 | + # # .exists() is a method in Path class |
| 103 | + # if user_path.exists: |
| 104 | + # pprint(f"The provided path {user_path} exists.") |
| 105 | + # # full_path = user_path + "/" + input("Enter the file name: ") |
| 106 | + # full_path = user_path + "/" + "default.mp3" |
| 107 | + # self.save(user_path) |
| 108 | + # pprint("File saved successfully") |
| 109 | + # else: |
| 110 | + # # prompts the user again three times to do so. |
| 111 | + # # if not then choose the default one asking user to choose the default one. |
| 112 | + # # if he says no, then asks to input again. |
| 113 | + # # then ask three times. |
| 114 | + # # at max |
| 115 | + # """dir testing has to be done seprately""" |
| 116 | + |
| 117 | + # if user_path.is_dir: |
| 118 | + # gTTS.save(user_path) |
| 119 | + |
| 120 | + # def file_name(self): |
| 121 | + # while True: |
| 122 | + # file_path = input("Enter the file path: ") |
| 123 | + # if file_path.exists: |
| 124 | + # break |
| 125 | + # else: |
| 126 | + # # for wrong input type exceptions |
| 127 | + # while True: |
| 128 | + # continue_response = input("Are you sure you want to continue?(y/n):") |
| 129 | + # continue_response = continue_response.strip().lower() |
| 130 | + # if continue_response in ["y", "yes", "start"]: |
| 131 | + # break |
| 132 | + # elif continue_response in ["n", "no", "stop"]: |
| 133 | + # break |
| 134 | + # # file_path = user_path + "/" + input("Enter the file name: ") |
| 135 | + # # file_path = user_path + "/" + "default.mp3" |
| 136 | + # # Also work a best way to save good quality audio and what is best format to save it in. |
| 137 | + |
| 138 | + # def save_and_play(self): |
| 139 | + # self.save_only() |
| 140 | + # self.play_only() |
| 141 | + # self.save_path() |
| 142 | + # self.file_name() |
| 143 | + |
| 144 | + # def concatenate_audio(self): |
| 145 | + # # logic to concatenate audio? |
| 146 | + # # why, special feature about it? |
| 147 | + # # this is not a logic concatenation application. |
| 148 | + # pass |
| 149 | + |
| 150 | + |
| 151 | +# hello = userAudio("Hello, world!") |
| 152 | +# hello.play_only() |
| 153 | + |
| 154 | +with open("special_file.txt", "r") as f: |
| 155 | + retrieved_text = f.read() |
| 156 | +retrieved_text = retrieved_text.replace("\n", "") |
| 157 | + |
| 158 | +# hello = userAudio("Hello, user how are you?", slow=False) |
| 159 | +hello = userAudio |
| 160 | +hello.play_only() |
| 161 | + |
| 162 | + |
| 163 | +class fun_secret_generator_string: |
| 164 | + # Instructions on how to use it? |
| 165 | + def __init__(self, string): |
| 166 | + self.string = string |
| 167 | + |
| 168 | + # text = "Input your text here." |
| 169 | + # with open("special_file.txt", "w") as f: |
| 170 | + # for char in text: |
| 171 | + # f.write(char + "\n") |
| 172 | + # f.close() |
| 173 | + # print("File saved successfully") |
| 174 | + |
| 175 | + # Reading from the file |
| 176 | + with open("special_file.txt", "r") as f: |
| 177 | + retrieved_text = f.read() |
| 178 | + retrieved_text = retrieved_text.replace("\n", "") |
| 179 | + |
| 180 | + |
| 181 | +# Also have an option to play from a file, a text file. |
| 182 | +# Will later put other pdf and word2docx vectorisations. |
| 183 | +# from gtts import gTTS |
| 184 | +# import os |
| 185 | + |
| 186 | +# # Enter the name of your text file |
| 187 | +# mytextfile = "hello.txt" |
| 188 | + |
| 189 | +# # Specify the language in which you want your audio |
| 190 | +# language = "en" |
| 191 | + |
| 192 | +# # Get the contents of your file |
| 193 | +# with open(mytextfile, 'r') as f: |
| 194 | +# mytext = f.read() |
| 195 | +# f.close() |
| 196 | + |
| 197 | +# # Create an instance of gTTS class |
| 198 | +# myobj = gTTS(text=mytext, lang=language, slow=False) |
| 199 | + |
| 200 | +# # Method to create your audio file in mp3 format |
| 201 | +# myobj.save("hello.mp3") |
| 202 | +# print("Audio Saved") |
| 203 | + |
| 204 | +# # This will play your audio file |
| 205 | +# os.system("mpg321 hello.mp3") |
0 commit comments