I´m playing an mp3 file I get after a request to google translate. When I play it through pygame it plays at twice the speed, regardless of what frequency I set in pygame.mixer.init()
Here's the code:
import tweepy, urllib, os, pygame, pycurl, difflib, time
pygame.init()
pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
consumer_key = 'xxxxxxxxx'
consumer_secret = 'xxxxxxxx'
access_token = 'xxxxxx'
access_token_secret = 'xxxxxx'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
# function to download a file from a url, used for testing
def downloadFile(url, fileName):
fp = open(fileName, "wb")
curl = pycurl.Curl()
curl.setopt(pycurl.URL, url)
curl.setopt(pycurl.WRITEDATA, fp)
curl.perform()
curl.close()
fp.close()
# returns the appropriate google speech url for a particular phrase
def getGoogleSpeechURL(phrase):
googleTranslateURL = "http://translate.google.com/translate_tts?tl=en&"
parameters = {'q': phrase}
data = urllib.urlencode(parameters)
googleTranslateURL = "%s%s" % (googleTranslateURL,data)
return googleTranslateURL
def speakSpeechFromText(phrase):
googleSpeechURL = getGoogleSpeechURL(phrase.encode('utf-8').strip())
downloadFile(googleSpeechURL,"tts.mp3")
pygame.mixer.music.load("tts.mp3")
pygame.mixer.music.play(0,0.0)
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
#os.system("mplayer tts.mp3 -af extrastereo=0 &")
def diffindex(string1, string2):
for i, (char1, char2) in enumerate(zip(string1, string2)):
if char1 != char2:
return 1
return 2
A = "a"
B = "b"
api = tweepy.API(auth)
while(true)
results = api.search(q="Hackney", count=1, result_type="recent")
for result in results:
A = result.text
if diffindex(A, B) != 2:
speakSpeechFromText(A)
B = A
else:
print ("jaha")
time.sleep(20)
Sorry if there´s a lot of irrelevant stuff there as well but I thought it might be of use.