Reputation: 1405
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)
How can i do that? what modules should i use for it?
Upvotes: 0
Views: 2800
Reputation: 2978
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/):
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. 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.
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.
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.
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.
Upvotes: 2