2013-12-14 321 views
0

我正在播放一個mp3文件,我收到一個請求後,谷歌翻譯。當我通過pygame的發揮它起着兩倍的速度,不管我在pygame.mixer.init()Pygame播放mp3速度太快(不管我定義了什麼頻率)

這裏設置什麼頻率的代碼:

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) 

很抱歉,如果裏面有許多東西的東西無關那裏很好,但我認爲它可能是有用的。

+0

你有一個MP3文件,它是你的機器上試過你的函數「speakSpeechFromText」?如果是這樣的話,你的文件是以那個頻率播放的?我已經在winamp中看到每個音頻文件如何改變它的頻率,甚至在48000Hz的頻率上有一個「聯合立體聲」的東西。那麼如何將勾號從10更改爲15或20? –

回答

0

您應該在pygame.init()之前撥打mixer.init(),因爲pygame.init()也會初始化調音臺。

如果您必須致電mixer.init()pygame.init()先致電mixer.quit()

這都是in the docs :)。

0

我懂了:

pygame.mixer.pre_init(16000, -16, 2, 2048) # setup mixer to avoid sound lag 
pygame.mixer.init() 
pygame.mixer.music.load("mymp3file.mp3") 
pygame.mixer.music.play() 
# I adjusted the 16000 - it was 44100, tryied 48000, 22000, 110000 and themn 16000