PyAudio Over Network 崩溃

发布于 2024-11-19 07:45:35 字数 4280 浏览 1 评论 0 原文

您好,我遇到了 PyAudio 的问题,无法解决。当我通过网络使用它时(一段时间后)它崩溃了。我得到的错误是

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 552, in __bootstrap_inner
    self.run()
  File "C:\Python27\lib\threading.py", line 505, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Users\maboroshi\Desktop\myChat\chat.py", line 71, in server
    frames_per_buffer = chunk)
  File "C:\Python27\lib\site-packages\pyaudio.py", line 714, in open
    stream = Stream(self, *args, **kwargs)
  File "C:\Python27\lib\site-packages\pyaudio.py", line 396, in __init__
    self._stream = pa.open(**arguments)
IOError: [Errno Device unavailable] -9985

我的代码如下(大部分是:-P

这是用于解密数据和管理连接

def decrypt_my_message(msg):
    iv = "1234567812345678"
    key = your_friends_key
    if len(key) not in (16, 24, 32):
        raise ValueError("Key must be 16, 24, or 32 bytes")
    if (len(msg) % 16) != 0:
        raise ValueError("Message must be a multiple of 16 bytes")
    if len(iv) != 16:
        raise ValueError("IV must be 16 bytes")
    cipher = AES.new(key, AES.MODE_CBC, iv)
    plaintext = cipher.decrypt(msg)
    return plaintext

### Server function ###
def server():
    HOST = ''
    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((HOST, PORT))
    server_socket.listen(5)
    ### Start recieve loop
    read_list = [server_socket]
    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(2024)
                if msg:                
                    cmd, msg = ord(msg[0]),msg[1:]
                    if cmd == CMD_MSG:
                        listb1.insert(END, decrypt_my_message(msg.strip()) + "\n")
                        listb1.yview(END)
                    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)
                        stream.write(decrypt_my_message(msg), chunk)    #Write the data back out to the speakers
                else:
                    s.close()
                    read_list.remove(s)

这是用于连接和发送加密音频

def encrypt_my_audio_message(msg):
    key = your_key
    iv = '1234567812345678'
    aes = AES.new(key, AES.MODE_CBC, iv)
    encoder = PKCS7Encoder()
    pad_text = encoder.encode(msg)
    msg = aes.encrypt(pad_text)
    return msg

def connectToServer():
    CLIENT_PORT = 9001
    CLIENT_HOST = str(entryhost.get())
    global s
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((CLIENT_HOST, CLIENT_PORT))
        print "Connected\n"
    except:
        print "Could not connect"

### Client Function ###        
def client(cmd, msg):
    try:
        s.send(cmd + msg)
    except:
        print "You are not connected"

def sendAudio():
    p = pyaudio.PyAudio()
    stream = p.open(format = FORMAT,
                    channels = CHANNELS,
                    rate = RATE,
                    input = True,
                    output = True,
                    frames_per_buffer = chunk)
    data = stream.read(chunk)
    return encrypt_my_audio_message(data)
##    stream.stop_stream()
##    stream.close()
##    p.terminate()

def keypress(event):
    if event.keysym == 'Escape':
        root.destroy()
    #x = event.char
    if event.keysym == 'Control_L':
        #print("Sending Data...")
        client(chr(CMD_AUDIO), sendAudio())
        #print("Data Sent!")

任何想法为什么会发生这种情况将是最有帮助的

Hello I am facing an issue with PyAudio I can not solve. When I use it over the network (after a while) it crashes. The error I get is

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 552, in __bootstrap_inner
    self.run()
  File "C:\Python27\lib\threading.py", line 505, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Users\maboroshi\Desktop\myChat\chat.py", line 71, in server
    frames_per_buffer = chunk)
  File "C:\Python27\lib\site-packages\pyaudio.py", line 714, in open
    stream = Stream(self, *args, **kwargs)
  File "C:\Python27\lib\site-packages\pyaudio.py", line 396, in __init__
    self._stream = pa.open(**arguments)
IOError: [Errno Device unavailable] -9985

My code is below (well most of it :-P

This is for decrypting the data and managing connections

def decrypt_my_message(msg):
    iv = "1234567812345678"
    key = your_friends_key
    if len(key) not in (16, 24, 32):
        raise ValueError("Key must be 16, 24, or 32 bytes")
    if (len(msg) % 16) != 0:
        raise ValueError("Message must be a multiple of 16 bytes")
    if len(iv) != 16:
        raise ValueError("IV must be 16 bytes")
    cipher = AES.new(key, AES.MODE_CBC, iv)
    plaintext = cipher.decrypt(msg)
    return plaintext

### Server function ###
def server():
    HOST = ''
    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((HOST, PORT))
    server_socket.listen(5)
    ### Start recieve loop
    read_list = [server_socket]
    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(2024)
                if msg:                
                    cmd, msg = ord(msg[0]),msg[1:]
                    if cmd == CMD_MSG:
                        listb1.insert(END, decrypt_my_message(msg.strip()) + "\n")
                        listb1.yview(END)
                    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)
                        stream.write(decrypt_my_message(msg), chunk)    #Write the data back out to the speakers
                else:
                    s.close()
                    read_list.remove(s)

And this is for connecting and sending encrypted audio

def encrypt_my_audio_message(msg):
    key = your_key
    iv = '1234567812345678'
    aes = AES.new(key, AES.MODE_CBC, iv)
    encoder = PKCS7Encoder()
    pad_text = encoder.encode(msg)
    msg = aes.encrypt(pad_text)
    return msg

def connectToServer():
    CLIENT_PORT = 9001
    CLIENT_HOST = str(entryhost.get())
    global s
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((CLIENT_HOST, CLIENT_PORT))
        print "Connected\n"
    except:
        print "Could not connect"

### Client Function ###        
def client(cmd, msg):
    try:
        s.send(cmd + msg)
    except:
        print "You are not connected"

def sendAudio():
    p = pyaudio.PyAudio()
    stream = p.open(format = FORMAT,
                    channels = CHANNELS,
                    rate = RATE,
                    input = True,
                    output = True,
                    frames_per_buffer = chunk)
    data = stream.read(chunk)
    return encrypt_my_audio_message(data)
##    stream.stop_stream()
##    stream.close()
##    p.terminate()

def keypress(event):
    if event.keysym == 'Escape':
        root.destroy()
    #x = event.char
    if event.keysym == 'Control_L':
        #print("Sending Data...")
        client(chr(CMD_AUDIO), sendAudio())
        #print("Data Sent!")

Any ideas why this happens would be most helpful

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情未る 2024-11-26 07:45:35

让我惊讶的是,您将 阻塞的线程 与select,循环看起来有点奇怪。你有很多事情在两个方向上进行——尝试并专注于使数据包的进出流像齿轮啮合在一起一样工作,或者确保有人在他们’时不会是空的或满的。我们预计两者都不会。

您可能会从以下内容中找到一些灵感:

The thing that jumps out at me is that you're mixing a thread that blocks with select, and the loop looks a bit weird. You've got a lot of stuff going on in both directions - try and focus on making the flow of packets in and out work like gears meshing together, or ensure that there's no way that somebody is going to be empty or full when they're expected to be neither.

You might find some inspiration from:

三月梨花 2024-11-26 07:45:35

我的猜测是您遇到了问题,因为您正在创建多个 PyAudio 对象并打开多个流而不关闭其中任何一个。

您应该创建一个 PyAudio 对象和一个流,并重新使用这些对象。

My guess is that you're running into problems because you are creating multiple PyAudio objects and opening multiple streams without closing any of them.

You should create one PyAudio object, and one stream, and re-use those objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文