开发者

Python Voice Communication

开发者 https://www.devze.com 2023-03-17 05:20 出处:网络
Hello I am trying to figure out some code which is suppose to send voic开发者_如何学Ce over the network. I am having problems with the audio it sends but its just a series of loud beeps and not the au

Hello I am trying to figure out some code which is suppose to send voic开发者_如何学Ce over the network. I am having problems with the audio it sends but its just a series of loud beeps and not the audio I input

After the beeps are finished I get an EOFError

I have spent the last 48 hours trying to figure this out any ideas are greatly appreciated

The relevant code

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号