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()
python类paInt16()的实例源码
googleSpeech_mic.py 文件源码
项目:googleSpeech_with_NaverTTS
作者: chandong83
项目源码
文件源码
阅读 23
收藏 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 _data_format(self, x):
"""The data types in numpy needs to be mapped to the equivalent type in
portaudio. This is an issue for 24 bit audio files since there isn't a
24 bit data type in numpy. This is currently not implemented. There are
some options on how to do this. We could for example use a 32 bit int and
store the 24 bits either so that bits 1 to 8 is set to zeroes or so that
bits 25 to 32 is set to zeros.
"""
retval = None
if x.samples.dtype == np.dtype(np.float32):
self._logger.debug("pyaudio.paFloat32")
retval = pyaudio.paFloat32
elif x.samples.dtype == np.dtype(np.int16):
self._logger.debug("pyaudio.paInt16")
retval = pyaudio.paInt16
elif x.samples.dtype == np.dtype(np.int32):
self._logger.debug("pyaudio.paInt32")
retval = pyaudio.paInt32
else:
raise NotImplementedError("Data type not understood: %s" %x.samples.dtype)
return retval
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 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 x_pya_input_rate(card, rate):
import pyaudio
rates = [ rate, 8000, 11025, 12000, 16000, 22050, 44100, 48000 ]
for r in rates:
if r >= rate:
ok = False
try:
ok = pya().is_format_supported(r,
input_device=card,
input_format=pyaudio.paInt16,
input_channels=1)
except:
pass
if ok:
return r
sys.stderr.write("weakaudio: no input rate >= %d\n" % (rate))
sys.exit(1)
# sub-process to avoid initializing pyaudio in main
# process, since that makes subsequent forks and
# multiprocessing not work.
def x_pya_output_rate(card, rate):
import pyaudio
rates = [ rate, 8000, 11025, 12000, 16000, 22050, 44100, 48000 ]
for r in rates:
if r >= rate:
ok = False
try:
ok = pya().is_format_supported(r,
output_device=card,
output_format=pyaudio.paInt16,
output_channels=1)
except:
pass
if ok:
return r
sys.stderr.write("weakaudio: no output rate >= %d\n" % (rate))
sys.exit(1)
def __init__(self, wave_prefix, total_seconds=-1, partial_seconds=None, incremental_mode = True, stop_callback=None, save_callback=None):
self.chunk = 1600
self.format = pyaudio.paInt16
self.channels = 1
self.rate = 16000
self.partial_seconds = partial_seconds
self.record_seconds = total_seconds
self.wave_prefix = wave_prefix
self.data = None
self.stream = None
self.p = pyaudio.PyAudio()
if partial_seconds != None and partial_seconds > 0:
self.multiple_waves_mode = True
else: self.multiple_waves_mode = False
self.incremental_mode = incremental_mode
self._stop_signal = True
self.stop_callback = stop_callback
self.save_callback = save_callback
def __init__(self, device_index = None, sample_rate = None, chunk_size = None):
assert device_index is None or isinstance(device_index, int), "Device index must be None or an integer"
if device_index is not None: # ensure device index is in range
audio = pyaudio.PyAudio(); count = audio.get_device_count(); audio.terminate() # obtain device count
assert 0 <= device_index < count, "Device index out of range"
assert sample_rate is None or isinstance(sample_rate, int) and sample_rate > 0, "Sample rate must be None or a positive integer"
assert chunk_size is None or isinstance(chunk_size, int) and chunk_size > 0, "Chunk size must be None or a positive integer"
if sample_rate is None: chunk_size = 16000
if chunk_size is None: chunk_size = 1024
self.device_index = device_index
self.format = pyaudio.paInt16 # 16-bit int sampling
self.SAMPLE_WIDTH = pyaudio.get_sample_size(self.format) # size of each sample
self.SAMPLE_RATE = sample_rate # sampling rate in Hertz
self.CHUNK = chunk_size # number of frames stored in each buffer
self.audio = None
self.stream = None
def find_input_devices(self):
# sample rate discovery based on https://stackoverflow.com/a/11837434
standard_sample_rates = [8000.0, 9600.0, 11025.0, 12000.0, 16000.0, 22050.0, 24000.0,
32000.0, 44100.0, 48000.0, 88200.0, 96000.0, 192000.0]
for i in range(self.pa.get_device_count()):
dev_info = self.pa.get_device_info_by_index(i)
if dev_info['maxInputChannels'] > 0:
supported_sample_rates = []
for f in standard_sample_rates:
try:
if self.pa.is_format_supported(
f,
input_device=dev_info['index'],
input_channels=dev_info['maxInputChannels'],
input_format=pyaudio.paInt16):
supported_sample_rates.append(f)
except ValueError:
pass
print("Device %d: %s, supported sample_rates: %s"
% (i, dev_info["name"], str(supported_sample_rates)))
def __init__(self):
self.miso = Queue()
self.mosi = Queue()
print "starting worker thread"
CHUNK = 1024
FORMAT = pyaudio.paInt16
#paUint16 #paInt8
CHANNELS = 1
RATE = 44100 #sample rate
self.paw = pyaudio.PyAudio()
self.stream = self.paw.open(format=FORMAT,
channels=CHANNELS,
#input_device_index = 4, # rocketfish
rate=RATE,
input=True,
stream_callback=self.callback,
frames_per_buffer=CHUNK) #buffer
#self.fort_proc = Process(target = fft_worker,
# args=(self.mosi, self.miso))
#self.fort_proc.start()
atexit.register(self.shutdown)
print "allegedly started worker"
def start(self):
"""Start recording."""
stream = self.p.open(
format=pyaudio.paInt16,
channels=self.channels,
rate=int(self.rate),
input=True,
frames_per_buffer=self.frames_per_element,
input_device_index=int(self.deviceindex),
as_loopback=True
)
def record():
"""Continuously read data and append to the ring buffer."""
while True:
audio_string = stream.read(self.frames_per_element)
self._ringbuffer.append(audio_string)
self.has_new_audio = True
thread = threading.Thread(target=record)
thread.start()
def __init__(self, rt):
super().__init__(rt)
config = rt.config['frontends']['speech']['recognizers']
self.listener_config = config
self.chunk_size = config['chunk_size']
self.format = pyaudio.paInt16
self.sample_width = pyaudio.get_sample_size(self.format)
self.sample_rate = config['sample_rate']
self.channels = config['channels']
self.p = pyaudio.PyAudio()
self.stream = self.p.open(format=self.format, channels=self.channels,
rate=self.sample_rate, input=True,
frames_per_buffer=self.chunk_size)
self.buffer_sec = config['wake_word_length']
self.talking_volume_ratio = config['talking_volume_ratio']
self.required_integral = config['required_noise_integral']
self.max_di_dt = config['max_di_dt']
self.noise_max_out_sec = config['noise_max_out_sec']
self.sec_between_ww_checks = config['sec_between_ww_checks']
self.recording_timeout = config['recording_timeout']
self.energy_weight = 1.0 - pow(1.0 - config['ambient_adjust_speed'],
self.chunk_size / self.sample_rate)
# For convenience
self.chunk_sec = self.chunk_size / self.sample_rate
self.av_energy = None
self.integral = 0
self.noise_level = 0
self._intercept = None
def __init__(self, config, pid):
audio_chunk = config['audio_chunk']
audio_format = pyaudio.paInt16 # paInt8
audio_channels = config['audio_channels']
audio_rate = config['audio_rate']
recording_time = config['recording_time']
wav_file_path = "/tmp/mama/output.wav"
p = pyaudio.PyAudio()
stream = p.open(format=audio_format,
channels=audio_channels,
rate=audio_rate,
input=True,
frames_per_buffer=audio_chunk) # buffer
# we play a sound to signal the start
parent_dir = os.path.dirname(os.path.abspath(__file__)).strip('librairy')
os.system('play ' + parent_dir + 'resources/sound.wav')
print("* recording")
os.system('touch /tmp/mama/mama_start_' + pid)
frames = []
for i in range(0, int(audio_rate / audio_chunk * recording_time)):
data = stream.read(audio_chunk)
frames.append(data) # 2 bytes(16 bits) per channel
print("* done recording")
os.system('touch /tmp/mama/mama_record_complete_' + pid)
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(wav_file_path, 'wb')
wf.setnchannels(audio_channels)
wf.setsampwidth(p.get_sample_size(audio_format))
wf.setframerate(audio_rate)
wf.writeframes(b''.join(frames))
wf.close()
def record(self,
record_seconds,
chunk=1024,
format=pyaudio.paInt16,
channels=2,
rate=16000,
output_path=None):
stream = self.__pyaudio.open(format=format, channels=channels,
rate=rate, input=True, frames_per_buffer=chunk)
print('Recording ...')
frames = []
for i in range(0, int(rate / chunk * record_seconds)):
data = stream.read(chunk)
frames.append(data)
'''??????'''
frames.append(stream.read(rate * record_seconds - int(rate / chunk * record_seconds) * chunk))
print('Done.')
stream.stop_stream()
stream.close()
self.__pyaudio.terminate()
'''????'''
print('Saving ...')
wav = wave.open(output_path, 'wb')
wav.setnchannels(channels)
wav.setsampwidth(self.__pyaudio.get_sample_size(format))
wav.setframerate(rate)
wav.writeframes(b''.join(frames))
wav.close()
print('Done.')
def __init__(self, chunk=1024, format_=pyaudio.paInt16, channels=1, rate=16000):
self.CHUNK = chunk
self.FORMAT = format_
self.CHANNELS = channels
self.RATE = rate
def record(self, duration):
# Use a stream with no callback function in blocking mode
self._stream = self._pa.open(format=pyaudio.paInt16,
channels=self.channels,
rate=self.rate,
input=True,
frames_per_buffer=self.frames_per_buffer)
for _ in range(int(self.rate / self.frames_per_buffer * duration)):
audio = self._stream.read(self.frames_per_buffer)
self.wavefile.writeframes(audio)
return None
def start_recording(self):
# Use a stream with a callback in non-blocking mode
self._stream = self._pa.open(format=pyaudio.paInt16,
channels=self.channels,
rate=self.rate,
input=True,
frames_per_buffer=self.frames_per_buffer,
stream_callback=self.get_callback())
self._stream.start_stream()
return self
def _prepare_file(self, fname, mode='wb'):
wavefile = wave.open(fname, mode)
wavefile.setnchannels(self.channels)
wavefile.setsampwidth(self._pa.get_sample_size(pyaudio.paInt16))
wavefile.setframerate(self.rate)
return wavefile
def __init__(self):
self.pa = pyaudio.PyAudio()
self.in_stream = self.pa.open(format=pyaudio.paInt16, channels=1,
rate=16000, input=True)
self.in_stream.start_stream()
self.out_stream = self.pa.open(format=pyaudio.paInt16, channels=1,
rate=16000, output=True)
self.out_stream.start_stream()
def __init__(self, rate=16000, channels=8, chunk_size=None):
self.pyaudio_instance = pyaudio.PyAudio()
self.queue = Queue.Queue()
self.quit_event = threading.Event()
self.channels = channels
self.sample_rate = rate
self.chunk_size = chunk_size if chunk_size else rate / 100
device_index = None
for i in range(self.pyaudio_instance.get_device_count()):
dev = self.pyaudio_instance.get_device_info_by_index(i)
name = dev['name'].encode('utf-8')
print(i, name, dev['maxInputChannels'], dev['maxOutputChannels'])
if dev['maxInputChannels'] == self.channels:
print('Use {}'.format(name))
device_index = i
break
if device_index is None:
raise Exception('can not find input device with {} channel(s)'.format(self.channels))
self.stream = self.pyaudio_instance.open(
input=True,
start=False,
format=pyaudio.paInt16,
channels=self.channels,
rate=int(self.sample_rate),
frames_per_buffer=int(self.chunk_size),
stream_callback=self._callback,
input_device_index=device_index,
)
def save_speech(data, p):
""" Saves mic data to temporary WAV file. Returns filename of saved
file """
filename = 'speech'
# writes data to WAV file
data = ''.join(data)
wf = wave.open(filename + '.wav', 'wb')
wf.setnchannels(1)
wf.setsampwidth(p.get_sample_size(pyaudio.paInt16))
wf.setframerate(16000) # TODO make this value a function parameter?
wf.writeframes(data)
wf.close()
return filename + '.wav'
def __init__(self, rate=16000, frames_size=None, channels=None, device_index=None):
self.sample_rate = rate
self.frames_size = frames_size if frames_size else rate / 100
self.channels = channels if channels else 1
self.pyaudio_instance = pyaudio.PyAudio()
if device_index is None:
if channels:
for i in range(self.pyaudio_instance.get_device_count()):
dev = self.pyaudio_instance.get_device_info_by_index(i)
name = dev['name'].encode('utf-8')
logger.info('{}:{} with {} input channels'.format(i, name, dev['maxInputChannels']))
if dev['maxInputChannels'] == channels:
logger.info('Use {}'.format(name))
device_index = i
break
else:
device_index = self.pyaudio_instance.get_default_input_device_info()['index']
if device_index is None:
raise Exception('Can not find an input device with {} channel(s)'.format(channels))
self.stream = self.pyaudio_instance.open(
start=False,
format=pyaudio.paInt16,
input_device_index=device_index,
channels=self.channels,
rate=int(self.sample_rate),
frames_per_buffer=int(self.frames_size),
stream_callback=self._callback,
input=True
)
self.sinks = []
def record_audio(rate, chunk):
"""Opens a recording stream in a context manager."""
audio_interface = pyaudio.PyAudio()
audio_stream = audio_interface.open(
format=pyaudio.paInt16,
# The API currently only supports 1-channel (mono) audio
# https://goo.gl/z757pE
channels=1, rate=rate,
input=True, frames_per_buffer=chunk,
)
# Create a thread-safe buffer of audio data
buff = queue.Queue()
# Spin up a separate thread to buffer audio data from the microphone
# This is necessary so that the input device's buffer doesn't overflow
# while the calling thread makes network requests, etc.
fill_buffer_thread = threading.Thread(
target=_fill_buffer, args=(audio_stream, buff, chunk))
fill_buffer_thread.start()
yield _audio_data_generator(buff)
audio_stream.stop_stream()
audio_stream.close()
fill_buffer_thread.join()
audio_interface.terminate()
# [END audio_stream]
def __input_loop(self, periodsize):
""" TODO """
p_in = pyaudio.PyAudio()
stream = p_in.open(input=True,
channels=1,
format=pyaudio.paInt16,
rate=pymumble.constants.PYMUMBLE_SAMPLERATE,
frames_per_buffer=periodsize)
while True:
data = stream.read(periodsize)
self.mumble.sound_output.add_sound(data)
stream.close()
return True
def __init__(self, pa):
self.pa = pa
self.event = threading.Event()
# self.stream = self.pa.open(format=pyaudio.paInt16,
# channels=1,
# rate=16000,
# output=True,
# start=False,
# # output_device_index=1,
# frames_per_buffer=CHUNK_SIZE,
# stream_callback=self.callback)
def record(self, duration):
# Use a stream with no callback function in blocking mode
self._stream = self._pa.open(format=pyaudio.paInt16,
channels=self.channels,
rate=self.rate,
input=True,
frames_per_buffer=self.frames_per_buffer)
for _ in range(int(self.rate / self.frames_per_buffer * duration)):
audio = self._stream.read(self.frames_per_buffer)
self.wavefile.writeframes(audio)
return None
def start_recording(self):
# Use a stream with a callback in non-blocking mode
self._stream = self._pa.open(format=pyaudio.paInt16,
channels=self.channels,
rate=self.rate,
input=True,
frames_per_buffer=self.frames_per_buffer,
stream_callback=self.get_callback())
self._stream.start_stream()
return self
def _prepare_file(self, fname, mode='wb'):
wavefile = wave.open(fname, mode)
wavefile.setnchannels(self.channels)
wavefile.setsampwidth(self._pa.get_sample_size(pyaudio.paInt16))
wavefile.setframerate(self.rate)
return wavefile