您好,我在通過網絡發送音頻時遇到問題。在我的本地系統沒有距離沒有問題,但每當我在遠程系統上測試有音頻,但它不是語音輸入,我想它的波濤洶涌/ laggy等我相信它在我如何處理髮送的音頻,但我已經嘗試了4天,無法找到解決方案。通過網絡問題的Python音頻
,我將發佈所有相關的代碼,並試圖解釋盡我所能
這些都是常數/全局值
#initilaize Speex speex_enc = speex.Encoder() speex_enc.initialize(speex.SPEEX_MODEID_WB) speex_dec = speex.Decoder() speex_dec.initialize(speex.SPEEX_MODEID_WB) #some constant values chunk = 320 FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 44100
我發現調節採樣率值將允許更多的噪音
下面是pyAudio代碼初始化音頻設備,這也是全球
#initalize PyAudio p = pyaudio.PyAudio() stream = p.open(format = FORMAT, channels = CHANNELS, rate = RATE, input = True, output = True, frames_per_buffer = chunk)
這個下一個功能是從麥克風寫入數據並使用客戶端功能發送數據的按鍵功能。這是我相信我遇到問題的地方。
我相信我是如何處理這個問題的,因爲如果我按住它來獲取音頻,它會在每次迭代中循環發送。我不確定這裏要做什麼。 (想法!)
def keypress(event): #chunklist = [] #RECORD_SECONDS = 5 if event.keysym == 'Escape': root.destroy() #x = event.char if event.keysym == 'Control_L': #for i in range(0, 44100/chunk * RECORD_SECONDS): try: #get data from mic data = stream.read(chunk) except IOError as ex: if ex[1] != pyaudio.paInputOverflowed: raise data = '\x00' * chunk encdata = speex_enc.encode(data) #Encode the data. #chunklist.append(encdata) #send audio client(chr(CMD_AUDIO), encrypt_my_audio_message(encdata))
服務器代碼來處理音頻鍵盤的Tkinter的
root.bind_all('', keypress)
任何想法是極大的讚賞如何
### Server function ### def server(): PORT = 9001 ### Initialize socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_socket.bind((socket.gethostbyname(socket.gethostname()), PORT)) # socket.gethostbyname(socket.gethostname()) server_socket.listen(5) read_list = [server_socket] ### Start receive loop while True: readable, writable, errored = select.select(read_list, [], []) for s in readable: if s is server_socket: conn, addr = s.accept() read_list.append(conn) print "Connection from ", addr else: msg = conn.recv(2048) if msg: cmd, msg = ord(msg[0]),msg[1:] ## get a text message from GUI if cmd == CMD_MSG: listb1.insert(END, decrypt_my_message(msg).strip() + "\n") listb1.yview(END) ## get an audio message elif cmd == CMD_AUDIO: # make sure length is 16 --- HACK --- if len(msg) % 16 != 0: msg += '\x00' * (16 - len(msg) % 16) #decrypt audio data = decrypt_my_message(msg) decdata = speex_dec.decode(data) #Write the data back out to the speaker stream.write(decdata, chunk) else: s.close() read_list.remove(s)
,並完成綁定我可以根據需要使該按鍵方法正常工作,或者提出更好的方法,或者我可以完全做錯事
*歡呼
請注意,我已經測試過它沒有加密的方法也和同樣的事情:-)
你想做什麼?這是一個語音聊天應用程序嗎?應該要求用戶按住一個鍵才能發送音頻?爲什麼在編碼和發送之前錄製5秒的音頻? – jozzas
嘿Jozzas是這是一個語音聊天應用程序和五秒錄音只是爲了測試一些當前被註釋掉的東西:-)它應該是你按住左側的控制鍵併發送音頻,但就像我說的在我的本地系統它工作正常,但發送到遠程系統時,我聽到單擊單擊單擊:-)我試着玩取樣率有點,但我真的不確定需要做什麼*歡呼謝謝你的回覆 – ADE