-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathspeech.py
66 lines (57 loc) · 2.2 KB
/
speech.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# coding: utf-8
from gtts import gTTS
from google.cloud import texttospeech
import os
import pydub
from pydub import AudioSegment
import librosa
import stringUtils as s
def changePitch(filename, steps):
sound = AudioSegment.from_mp3(filename)
sound.export("temp.wav", format="wav")
y, sr = librosa.load("temp.wav", sr=16000) # y is a numpy array of the wav file, sr = sample rate
y_shifted = librosa.effects.pitch_shift(y, sr, n_steps=steps)
librosa.output.write_wav('temp.wav', y_shifted, sr)
sound = AudioSegment.from_file("temp.wav")
sound.export(filename, format="mp3")
os.remove("temp.wav")
def saveMP3OfTextGoogle(speechText, mp3File):
'''
client = texttospeech.TextToSpeechClient()
synthesis_input = texttospeech.types.SynthesisInput(text=speechText)
voice = texttospeech.types.VoiceSelectionParams(
language_code='pt-br',
ssml_gender=texttospeech.enums.SsmlVoiceGender.NEUTRAL)
print("ok")
input()
'''
tts = gTTS(text=speechText, lang='pt-br', slow=False)
tts.save(mp3File)
def saveMP3OfTextMicrosoft(speechText, mp3File):
f = open("gpt2.vbs","w+")
speechText = s.cleanSentence(speechText)
speechText = speechText.replace("\"", "")
speechText = speechText.replace("\'", "")
f.writelines(["Const SAFT48kHz16BitStereo = 39\n",
"Const SSFMCreateForWrite = 3\n",
"Dim oFileStream, oVoice\n",
"Set oFileStream = CreateObject(\"SAPI.SpFileStream\")\n",
"oFileStream.Format.Type = SAFT48kHz16BitStereo\n",
"oFileStream.Open \"" + os.getcwd() + "\\temp.wav\", SSFMCreateForWrite\n",
"Set oVoice = CreateObject(\"SAPI.SpVoice\")\n",
"Set oVoice.AudioOutputStream = oFileStream\n",
"oVoice.Speak \"" + speechText + "\"\n",
"oFileStream.Close"])
f.close()
os.system("gpt2.vbs")
sound = AudioSegment.from_file("temp.wav")
sound.export(mp3File, format="mp3")
#os.remove("temp.wav")
#os.remove("gpt2.vbs")
def speechNews(speech, filename, whoIsTalking):
speech += "......"
if whoIsTalking == "iasmim":
saveMP3OfTextGoogle(speech, filename)
#changePitch(filename, 2)
else:
saveMP3OfTextMicrosoft(speech, filename)