Python语音通信

时间:2011-07-01 18:11:24

标签: python voip speex pyaudio

您好我正在尝试找出一些代码,这些代码可以通过网络发送语音。我发送的音频有问题,但它只是一连串响亮的哔声,而不是我输入的音频

哔哔声结束后,我得到EOFError

我花了最后48小时试图弄清楚任何想法都非常感激

相关代码

import pyaudio
import speex
import sys

chunk = 320
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 5

### Server function ###
def server():
    ### Initialize socket 
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT))
    s.listen(5)
    ### Start recieve loop
    while True:
...
        elif cmd == CMD_AUDIO:
            d = speex.Decoder()
            d.initialize(speex.SPEEX_MODEID_WB)
            p = pyaudio.PyAudio()
            stream = p.open(format = FORMAT,
                            channels = CHANNELS,
                            rate = RATE,
                            input = True,
                            output = True,
                            frames_per_buffer = chunk)

            #voice = cPickle.loads(decrypt_my_message(msg))
            voice = cPickle.loads(msg)
            print voice
            for i in range(len(voice)):
                decdata = d.decode(voice[i])#DECODE my data. (YaY)#DECODE my data. (YaY)
                stream.write(str(voice), chunk)    #Write the data back out to the speakers
            stream.stop_stream()
            stream.close()
            p.terminate()
            d.destroy()
        if not msg: break

    conn.close()


### READ DATA FROM THE MIC ###

def sendAudio():
    chunklist = []
    init_my_audio = speex.Encoder()
    init_my_audio.initialize(speex.SPEEX_MODEID_WB)
    p = pyaudio.PyAudio()
    stream = p.open(format = FORMAT,
                    channels = CHANNELS,
                    rate = RATE,
                    input = True,
                    output = True,
                    frames_per_buffer = chunk)

    for i in range(0, 44100 / chunk * RECORD_SECONDS):
        try:
            data = stream.read(chunk)
        except IOError:
            pass
        encdata = init_my_audio.encode(data)
        chunklist.append(encdata)
        client(chr(CMD_AUDIO), cPickle.dumps((chunklist), 1))
    stream.stop_stream()
    stream.close()
    p.terminate()
    init_my_audio.destroy()

0 个答案:

没有答案