简体   繁体   English

录制和播放从麦克风录制的音频流

[英]Recording and play audio stream recorded from microphone

I want to record a sound / conversation with python (pyaudio) and after that use the stream that was recorded and play it on another computer using sockets (without files, just python) 我想用python(pyaudio)录制声音/对话,之后使用录制的流并使用套接字在没有文件的情况下在另一台计算机上播放(只有python)

How can i do that? 我怎样才能做到这一点? what modules should i use for it? 我应该使用哪些模块?

Just send the raw frames over a socket instead of writing to a file. 只需通过套接字发送原始帧而不是写入文件。 Use the play example found on the port audio webpage. 使用端口音频网页上的播放示例。 Simply replace this code in the pyaudio examples for recording ( http://people.csail.mit.edu/hubert/pyaudio/ ): 只需在pyaudio示例中替换此代码进行录制( http://people.csail.mit.edu/hubert/pyaudio/ ):

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()

With sending the ((b''.join(frames)), FORMAT, CHANNELS, RATE) over a socket. 通过套接字发送((b''。join(frames)),FORMAT,CHANNELS,RATE)。 An easy way to encode this information is probably as a post request, where the parameters are the FORMAT, CHANNELS, and RATE, and the body is the audio bytestring. 编码此信息的简单方法可能是作为post请求,其中参数是FORMAT,CHANNELS和RATE,而body是音频字节串。

import urrllib2
url = '192.168.1.10'
data = urllib.urlencode({'FORMAT' : format,
                         'CHANNELS'  : channels,
                         'RATE' : rate})
response = urllib2.urlopen(url=url, data= b''.join(frames)).read()
assert(response == "Successfully Played")

Just modify your ip address in this example, then set up a simple web server on the receiving end (such as with flask) to handle the request. 只需在此示例中修改您的IP地址,然后在接收端(例如使用flask)设置一个简单的Web服务器来处理请求。

Then the user can decode this information on the socket and use the play example by creating a new stream with the correct parameters ala the Play example. 然后,用户可以在套接字上解码此信息,并通过在Play示例中创建具有正确参数的新流来使用播放示例。

If you need realtime streaming of audio, initiate a single request with the format channels and rate, and then just open a raw tcp socket to send the raw frames. 如果您需要实时流式传输音频,请使用格式通道和速率发出单个请求,然后只需打开原始tcp套接字即可发送原始帧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM