def audio_generator(dict_dir, text, output_dest):
with open(dict_dir + "/myDict.py") as f:
myDict = ast.literal_eval(f.read())
textList = text.split(" ")
mainList = []
for i in textList:
if i in myDict.keys():
mainList.append(AudioSegment.from_wav(dict_dir + "/" + myDict[i]))
# Check to see if at least one word was generated
if mainList == []:
raise Exception('\033[91m' + "None of the words you entered was" +
" spoken by your figure." + '\033[0m')
# If a file with the default name exits, create a new name with a
# new suffix
res = 0
while(os.path.exists(output_dest + "/output" + str(res) + ".wav")):
res += 1
mainAudio = mainList[0]
# Concatenate selected audio words
for i in range(1, len(mainList)):
mainAudio += mainList[i]
# Export the joined audio
mainAudio.export(output_dest + '/output' + str(res) + '.wav', format="wav")
if os.path.exists(output_dest + "/output" + str(res) + ".wav"):
print ('\033[94m' + "Speech-Hacker: " +
"Your audio was generated at: " + output_dest + "/output" +
str(res) + ".wav" + '\033[0m')
else:
print ("Speech-Hacker: " '\033[91m' +
"Failed to generate your requested audio." + '\033[0m')
评论列表
文章目录