pyaudio , grabar, reproducir y transmitir audio en #Python

Ya terminando una tarea de sistemas distribuidos donde por allí necesite aprender como grabar, reproducir y transmitir audio en python . A continuación coloco algunos programas a modo ejemplo:

 ojo deben tener instalado pyaudio

Grabar Audio

 """ Record a few seconds of audio and save to a WAVE file. """

import pyaudio
import wave
import sys

chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.ogg"

p = pyaudio.PyAudio()

stream = p.open(format = FORMAT,
                channels = CHANNELS,
                rate = RATE,
                input = True,
                frames_per_buffer = chunk)

print "* recording"
all = []
for i in range(0, RATE / chunk * RECORD_SECONDS):
    data = stream.read(chunk)
    all.append(data)
print "* done recording"

stream.close()
p.terminate()

# write data to WAVE file
data = ''.join(all)
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(data)
wf.close()

 

 

Grabar y Reproducir Audio

""" A wire between input and output. """

import pyaudio
import sys

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

p = pyaudio.PyAudio()

stream = p.open(format = FORMAT,
                channels = CHANNELS,
                rate = RATE,
                input = True,
                output = True,
                frames_per_buffer = chunk)

print "* recording"
for i in range(0, 44100 / chunk * RECORD_SECONDS):
    data = stream.read(chunk)
    #send data
    stream.write(data, chunk)
print "* done"

stream.stop_stream()
stream.close()
p.terminate()

Mandar Audio a un servidor y este reproduzca el sonido enviado , mediante UDP (El cliente captura el sonido y el servidor captura el dato enviado vía UDP y lo reproduce)

#
#    CLIENTE
#
import socket
import pyaudio
import sys


#UDP_IP="127.0.0.1"
UDP_IP= socket.gethostname()

UDP_PORT=5005
MESSAGE="Hello, World!"
 
print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT
print "message:", MESSAGE


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

p = pyaudio.PyAudio()

stream = p.open(format = FORMAT,
                channels = CHANNELS,
                rate = RATE,
                input = True,
                output = True,
                frames_per_buffer = chunk)

sock = socket.socket( socket.AF_INET, 
                      socket.SOCK_DGRAM ) # UDP
sock.sendto( MESSAGE, (UDP_IP, UDP_PORT) )

print "* recording"
#for i in range(0, 44100 / chunk * RECORD_SECONDS):
while True:
    MESSAGE = stream.read(chunk)
    sock = socket.socket( socket.AF_INET,socket.SOCK_DGRAM )#internet # UDP
    sock.sendto( MESSAGE, (UDP_IP, UDP_PORT) )
    sock.sendto( MESSAGE, (UDP_IP, UDP_PORT) )
    sock.sendto( MESSAGE, (UDP_IP, UDP_PORT) )


print "* done"

 

#
# SERVIDOR
#

import socket
import pyaudio
import sys
 
#UDP_IP="127.0.0.1"
UDP_IP = socket.gethostname()
UDP_PORT=5005

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

p = pyaudio.PyAudio()

stream = p.open(format = FORMAT,
                channels = CHANNELS,
                rate = RATE,
                input = True,
                output = True,
                frames_per_buffer = chunk)

sock = socket.socket( socket.AF_INET,
                      socket.SOCK_DGRAM ) # UDP
sock.
bind( (UDP_IP,UDP_PORT) )

data, addr = sock.recvfrom( 3072 )
print "received message:", data



print "* playing"
#for i in range(0, 44100 / chunk * RECORD_SECONDS):
#   print "get chunk %i" %i
while True:
   #data,addr = s.recvfrom(1024)
   data, addr = sock.recvfrom( 3072 )
   stream.write(data, chunk)
   #stream.write(data, chunk)
print "* done"

Este ultimo es algo grotesco, el sonido es horrible el de recepción a si que acepto mejoras a la propuesta de ejemplo, a medida que llegan las iré colocando

 

Saludos!