def play_file(fname):
# create an audio object
wf = wave.open(fname, 'rb')
p = pyaudio.PyAudio()
chunk = 1024
# open stream based on the wave object which has been input.
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
# read data (based on the chunk size)
data = wf.readframes(chunk)
# play stream (looping from beginning of file to the end)
while data != '':
# writing to the stream is what *actually* plays the sound.
stream.write(data)
data = wf.readframes(chunk)
# cleanup stuff.
stream.close()
p.terminate()
python类PyAudio()的实例源码
def audio_interfaces():
"""
Extracts audio interfaces data
:return list[AudioInterface]: Audio interfaces data
"""
p = pyaudio.PyAudio()
interfaces = []
for i in range(p.get_device_count()):
data = p.get_device_info_by_index(i)
if 'hw' not in data['name']:
interfaces.append(AudioInterface(data))
p.terminate()
return interfaces
def audio_int(num_samples=50):
""" Gets average audio intensity of your mic sound. You can use it to get
average intensities while you're talking and/or silent. The average
is the avg of the 20% largest intensities recorded.
"""
print "Getting intensity values from mic."
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
values = [math.sqrt(abs(audioop.avg(stream.read(CHUNK), 4)))
for x in range(num_samples)]
values = sorted(values, reverse=True)
r = sum(values[:int(num_samples * 0.2)]) / int(num_samples * 0.2)
print " Finished "
print " Average audio intensity is ", r
stream.close()
p.terminate()
return r
def play_all():
# create an audio object
pya = pyaudio.PyAudio()
# open stream based on the wave object which has been input.
stream = pya.open(
format = pyaudio.paInt16,
channels = 1,
rate = np.int16(np.round(orig_rate)),
output = True)
# read data (based on the chunk size)
audata = orig_au.astype(np.int16).tostring()
stream.write(audata)
# cleanup stuff.
stream.close()
pya.terminate()
googleSpeech_mic.py 文件源码
项目:googleSpeech_with_NaverTTS
作者: chandong83
项目源码
文件源码
阅读 18
收藏 0
点赞 0
评论 0
def __enter__(self):
self._audio_interface = pyaudio.PyAudio()
self._audio_stream = self._audio_interface.open(
format=pyaudio.paInt16,
# The API currently only supports 1-channel (mono) audio
# https://goo.gl/z757pE
channels=1, rate=self._rate,
input=True, frames_per_buffer=self._chunk,
# Run the audio stream asynchronously to fill the buffer object.
# This is necessary so that the input device's buffer doesn't
# overflow while the calling thread makes network requests, etc.
stream_callback=self._fill_buffer,
)
self.closed = False
return self
def record_to_file(filename,FORMAT = pyaudio.paInt16, CHANNELS = 1, RATE = 8000,
CHUNK = 1024, RECORD_SECONDS=1):
audio = pyaudio.PyAudio()
# start Recording
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()
waveFile = wave.open(filename, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
def start(self):
try:
p = pyaudio.PyAudio()
self.decoder = Decoder(CHUNK_SIZE*20)
self.stream = p.open(format=p.get_format_from_width(2),
channels=2,
rate=44100,
output=True)
yield self.stream
finally:
self.stream.stop_stream()
self.stream.close()
p.terminate()
def start(self):
try:
p = pyaudio.PyAudio()
self.decoder = Decoder(CHUNK_SIZE*20)
self.stream = p.open(format=p.get_format_from_width(2),
channels=2,
rate=44100,
output=True)
yield self.stream
finally:
self.stream.stop_stream()
self.stream.close()
p.terminate()
def record_iter(total=10, chunk=5):
try:
audio = pyaudio.PyAudio()
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
print "recording..."
for k in range(0, int(int(total)/int(chunk))):
data=b""
for j in range(0, int(chunk)):
for i in range(0, int(RATE / CHUNK * 1)):
data += stream.read(CHUNK)
yield (audio.get_sample_size(FORMAT), CHANNELS, RATE, data)
print "finished recording"
finally:
stream.stop_stream()
stream.close()
audio.terminate()
def speak(self, text, is_phonetic=False):
temp = 'temp.wav'
self.save_wav(text, temp, is_phonetic)
w = wave.open(temp)
p = pyaudio.PyAudio()
stream = p.open(
format=p.get_format_from_width(w.getsampwidth()),
channels=w.getnchannels(),
rate=w.getframerate(),
output=True)
chunk = 1024
data = w.readframes(chunk)
while data:
stream.write(data)
data = w.readframes(chunk)
stream.close()
p.terminate()
def _play_audio(sound, delay):
try:
time.sleep(delay)
wf = wave.open("sounds/"+sound+".wav", 'rb')
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
data = wf.readframes(TextToSpeech.CHUNK)
while data:
stream.write(data)
data = wf.readframes(TextToSpeech.CHUNK)
stream.stop_stream()
stream.close()
p.terminate()
return
except:
pass
def play_audio_file(fname=DETECT_DONG):
"""Simple callback function to play a wave file. By default it plays
a Ding sound.
:param str fname: wave file name
:return: None
"""
ding_wav = wave.open(fname, 'rb')
ding_data = ding_wav.readframes(ding_wav.getnframes())
audio = pyaudio.PyAudio()
stream_out = audio.open(
format=audio.get_format_from_width(ding_wav.getsampwidth()),
channels=ding_wav.getnchannels(),
rate=ding_wav.getframerate(), input=False, output=True)
stream_out.start_stream()
stream_out.write(ding_data)
time.sleep(0.2)
stream_out.stop_stream()
stream_out.close()
audio.terminate()
def _play_with_pyaudio(seg):
import pyaudio
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(seg.sample_width),
channels=seg.channels,
rate=seg.frame_rate,
output=True)
# break audio into half-second chunks (to allows keyboard interrupts)
for chunk in make_chunks(seg, 500):
stream.write(chunk._data)
stream.stop_stream()
stream.close()
p.terminate()
def play_audio_file(fname=DETECT_DING):
"""Simple callback function to play a wave file. By default it plays
a Ding sound.
:param str fname: wave file name
:return: None
"""
ding_wav = wave.open(fname, 'rb')
ding_data = ding_wav.readframes(ding_wav.getnframes())
audio = pyaudio.PyAudio()
stream_out = audio.open(
format=audio.get_format_from_width(ding_wav.getsampwidth()),
channels=ding_wav.getnchannels(),
rate=ding_wav.getframerate(), input=False, output=True)
stream_out.start_stream()
stream_out.write(ding_data)
time.sleep(0.2)
stream_out.stop_stream()
stream_out.close()
audio.terminate()
def play_audio_file(fname=DETECT_DING):
"""Simple callback function to play a wave file. By default it plays
a Ding sound.
:param str fname: wave file name
:return: None
"""
ding_wav = wave.open(fname, 'rb')
ding_data = ding_wav.readframes(ding_wav.getnframes())
audio = pyaudio.PyAudio()
stream_out = audio.open(
format=audio.get_format_from_width(ding_wav.getsampwidth()),
channels=ding_wav.getnchannels(),
rate=ding_wav.getframerate(), input=False, output=True)
stream_out.start_stream()
stream_out.write(ding_data)
time.sleep(0.2)
stream_out.stop_stream()
stream_out.close()
audio.terminate()
def play_audio_file(fname=DETECT_DING):
"""Simple callback function to play a wave file. By default it plays
a Ding sound.
:param str fname: wave file name
:return: None
"""
ding_wav = wave.open(fname, 'rb')
ding_data = ding_wav.readframes(ding_wav.getnframes())
audio = pyaudio.PyAudio()
stream_out = audio.open(
format=audio.get_format_from_width(ding_wav.getsampwidth()),
channels=ding_wav.getnchannels(),
rate=ding_wav.getframerate(), input=False, output=True)
stream_out.start_stream()
stream_out.write(ding_data)
time.sleep(0.2)
stream_out.stop_stream()
stream_out.close()
audio.terminate()
def __init__(self, speaker, passive_stt_engine, active_stt_engine):
"""
Initiates the pocketsphinx instance.
Arguments:
speaker -- handles platform-independent audio output
passive_stt_engine -- performs STT while Jasper is in passive listen
mode
acive_stt_engine -- performs STT while Jasper is in active listen mode
"""
self._logger = logging.getLogger(__name__)
self.speaker = speaker
self.passive_stt_engine = passive_stt_engine
self.active_stt_engine = active_stt_engine
self._logger.info("Initializing PyAudio. ALSA/Jack error messages " +
"that pop up during this process are normal and " +
"can usually be safely ignored.")
self._audio = pyaudio.PyAudio()
self._logger.info("Initialization of PyAudio completed.")
def __init__(self):
self.rospack = RosPack()
with open(join(self.rospack.get_path('apex_playground'), 'config', 'environment.json')) as f:
self.params = json.load(f)
with open(join(self.rospack.get_path('apex_playground'), 'config', 'bounds.json')) as f:
self.bounds = json.load(f)["sensory"]["sound"][0]
self.p = pyaudio.PyAudio()
self.fs = 44100 # sampling rate, Hz, must be integer
self.duration = 1./self.params['rate']
# for paFloat32 sample values must be in range [-1.0, 1.0]
self.stream = self.p.open(format=pyaudio.paFloat32,
channels=1,
rate=self.fs,
output=True)
def open(self):
"""Open a PyAudio instance. This needs to be called before play(),
play_rec() or rec() is called. This can be done in two ways:
snd = PA()
snd.open()
try:
snd.play(x)
finally:
snd.close()
or use the 'with' statement:
with PA() as snd:
snd.play(x)
"""
self._logger.debug("creating pyaudio instance")
self.pa = pyaudio.PyAudio()
def my_record():
pa=PyAudio()
stream=pa.open(format=paInt16,channels=1,
rate=framerate,input=True,
frames_per_buffer=NUM_SAMPLES)
my_buf=[]
count=0
print "* start recoding *"
while count<TIME*5:
string_audio_data=stream.read(NUM_SAMPLES)
my_buf.append(string_audio_data)
count+=1
print '.'
#filename=datetime.now().strftime("%Y-%m-%d_%H_%M_%S")+".wav"
filename="01.wav"
save_wave_file(filename, my_buf)
stream.close()
print "* "+filename, "is saved! *"
def play_audio_file(fname=DETECT_DING):
"""Simple callback function to play a wave file. By default it plays
a Ding sound.
:param str fname: wave file name
:return: None
"""
ding_wav = wave.open(fname, 'rb')
ding_data = ding_wav.readframes(ding_wav.getnframes())
audio = pyaudio.PyAudio()
stream_out = audio.open(
format=audio.get_format_from_width(ding_wav.getsampwidth()),
channels=ding_wav.getnchannels(),
rate=ding_wav.getframerate(), input=False, output=True)
stream_out.start_stream()
stream_out.write(ding_data)
time.sleep(0.2)
stream_out.stop_stream()
stream_out.close()
audio.terminate()
def kayurecord(woodname, duration):
""" Record audio and save to wav file
"""
filename = time_now() + "_" + woodname + ".wav"
container = pyaudio.PyAudio()
stream = container.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("* start recording...")
data = []
frames = []
for i in range(0, int(RATE / CHUNK * duration)):
data = stream.read(CHUNK)
frames.append(data)
stream.stop_stream()
stream.close()
container.terminate()
print("* done recording!")
kayurecord_save(filename, frames, container)
return filename
def play(self):
"""Play audio from source.
This method will block until the source runs out of audio.
"""
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(self._width),
channels=self._channels,
rate=self._freq,
output=True)
async with self._source.listen():
async for block in self._source:
async for chunk in block:
stream.write(chunk.audio)
stream.stop_stream()
stream.close()
p.terminate()
def run(self):
pya = PyAudio()
self._stream = pya.open(
format=paInt16,
channels=1,
rate=SAMPLE_RATE,
input=True,
frames_per_buffer=WINDOW_SIZE,
stream_callback=self._process_frame,
)
self._stream.start_stream()
while self._stream.is_active() and not raw_input():
time.sleep(0.1)
self._stream.stop_stream()
self._stream.close()
pya.terminate()
def __init__(self, sonic=None, block_size=None, **kwargs):
if not sonic:
sonic = Sonic()
self.sonic = sonic
self.channels = sonic.channels #???
self.sample_width = sonic.sample_width #????(byte)
self.sample_frequency = sonic.sample_frequency #????
if block_size:
self.block_size = block_size #wave????????
else:
self.block_size = sonic.sample_length #wave????????
self.wave_buffer = list()#?????
self.record_cache = queue.Queue()
self.player = pyaudio.PyAudio()
self.stream = self.player.open(
format = self.player.get_format_from_width(self.sample_width),
channels = self.channels,
rate = self.sample_frequency,
frames_per_buffer = self.block_size,
input = True,
**kwargs
)
def play(file):
chunk = 1024
f = wave.open(file,"rb")
p = pyaudio.PyAudio()
stream = p.open(format = p.get_format_from_width(f.getsampwidth()),
channels = f.getnchannels(),
rate = f.getframerate(),
output = True)
data = f.readframes(chunk)
while data:
stream.write(data)
data = f.readframes(chunk)
stream.stop_stream()
stream.close()
#p.terminate()
def play(file):
chunk = 1024
f = wave.open(file,"rb")
p = pyaudio.PyAudio()
stream = p.open(format = p.get_format_from_width(f.getsampwidth()),
channels = f.getnchannels(),
rate = f.getframerate(),
output = True)
data = f.readframes(chunk)
while data:
stream.write(data)
data = f.readframes(chunk)
stream.stop_stream()
stream.close()
p.terminate()
def play_audio_file(fname=DETECT_DING):
"""Simple callback function to play a wave file. By default it plays
a Ding sound.
:param str fname: wave file name
:return: None
"""
ding_wav = wave.open(fname, 'rb')
ding_data = ding_wav.readframes(ding_wav.getnframes())
audio = pyaudio.PyAudio()
stream_out = audio.open(
format=audio.get_format_from_width(ding_wav.getsampwidth()),
channels=ding_wav.getnchannels(),
rate=ding_wav.getframerate(), input=False, output=True)
stream_out.start_stream()
stream_out.write(ding_data)
time.sleep(0.2)
stream_out.stop_stream()
stream_out.close()
audio.terminate()
def __init__(self, filename):
"""
Creates the high quality recorder
:param filename: Filename to record into
"""
self._config = HQCConfig.get_instance('conn.conf')
audio_config = self._config.get_section('HQRecordingSettings')
self._p = pyaudio.PyAudio()
self._stream = self._p.open(format=self._p.get_format_from_width(int(audio_config['width'])),
channels=int(audio_config['channels']),
rate=int(audio_config['rate']),
input=True,
stream_callback=self._callback)
self._output = wave.open(filename, 'wb')
self._output.setnchannels(int(audio_config['channels']))
self._output.setsampwidth(int(audio_config['width']))
self._output.setframerate(int(audio_config['rate']))
self._audio_writer = Thread(target=self._write_queue_to_file)
self._audio_writer.daemon = True
self._audio_writer.start()
def valid_input_devices(self):
"""
See which devices can be opened for microphone input.
call this when no PyAudio object is loaded.
"""
mics=[]
for device in range(self.p.get_device_count()):
if self.valid_test(device):
mics.append(device)
if len(mics)==0:
print("no microphone devices found!")
else:
print("found %d microphone devices: %s"%(len(mics),mics))
return mics
### SETUP AND SHUTDOWN