我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用pyaudio.paInt16()。
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()
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 recode(self): pa=PyAudio() stream=pa.open(format=paInt16,channels=1,rate=self.SAMPLES_RATE,input=True,frames_per_buffer=self.NUM_SAMPLES) save_count=0 save_buffer=[] time_count=self.TIME_COUNT print '\n\n\n??????????????' while True: time_count-=1 string_audio_data=stream.read(self.NUM_SAMPLES) audio_data=numpy.fromstring(string_audio_data,dtype=numpy.short) large_sample_count=numpy.sum(audio_data>self.LEVEL) print(numpy.max(audio_data)) if large_sample_count>self.COUNT_NUM: save_count=self.SAVE_LENGTH else: save_count-=1 if save_count<0: save_count=0 if save_count>0: save_buffer.append(string_audio_data) else: if len(save_buffer): self.Voice_String=save_buffer save_buffer=[] print '????????' return True if not time_count: if len(save_buffer): self.Voice_String=save_buffer save_buffer=[] print '????????' return True else: return False
def save_audio(data, params): """ Saves mic data to wav file.""" filename = gettime() data = ''.join(data) wf = wave.open(filename + '.wav', 'wb') wf.setnchannels(1) wf.setsampwidth(params.get_sample_size(pyaudio.paInt16)) wf.setframerate(RATE) wf.writeframes(data) wf.close()
def record_wave(): #open the input of wave pa = PyAudio() stream = pa.open(format = paInt16, channels = 1, rate = framerate, input = True, frames_per_buffer = NUM_SAMPLES) save_buffer = [] count = 0 while count < TIME*4: #read NUM_SAMPLES sampling data string_audio_data = stream.read(NUM_SAMPLES) save_buffer.append(string_audio_data) count += 1 print '.'
def fetch_threshold(self): # TODO: Consolidate variables from the next three functions THRESHOLD_MULTIPLIER = 1.8 RATE = 16000 CHUNK = 1024 # number of seconds to allow to establish threshold THRESHOLD_TIME = 1 # prepare recording stream stream = self._audio.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNK) # stores the audio data frames = [] # stores the lastN score values lastN = [i for i in range(20)] # calculate the long run average, and thereby the proper threshold for i in range(0, RATE / CHUNK * THRESHOLD_TIME): data = stream.read(CHUNK) frames.append(data) # save this data point as a score lastN.pop(0) lastN.append(self.get_score(data)) average = sum(lastN) / len(lastN) stream.stop_stream() stream.close() # this will be the benchmark to cause a disturbance over! THRESHOLD = average * THRESHOLD_MULTIPLIER return THRESHOLD
def start_recording(self): audio = pyaudio.PyAudio() # start Recording stream = audio.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024) logger.info("recording...") self._audio = audio self._stream = stream self._stopped = False self._event = threading.Event()
def save_speech(data, p): """ Saves mic data to temporary WAV file. Returns filename of saved file """ filename = 'resources/output_'+str(int(time.time())) # 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, pa): self.pa = pa self.event = threading.Event() self.wav = wave.open(os.path.join(PIANO_PATH, 'c1.wav'), 'rb') self.stream = self.pa.open(format=pyaudio.paInt16, channels=self.wav.getnchannels(), rate=self.wav.getframerate(), output=True, # start=False, # output_device_index=1, frames_per_buffer=CHUNK_SIZE, stream_callback=self._callback)
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, output=False, input=True, frames_per_buffer=chunk, #input_device_index = 0, ) # 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 record_audio(channels, rate, chunk): """Opens a recording stream in a context manager.""" audio_interface = pyaudio.PyAudio() audio_stream = audio_interface.open( format=pyaudio.paInt16, channels=channels, rate=rate, input=True, frames_per_buffer=chunk, input_device_index=1, ) yield audio_stream audio_stream.stop_stream() audio_stream.close() audio_interface.terminate() # [END audio_stream]
def __init__(self, audio_format=None, channels=1, rate=16000, device_ndx=0): super(Microphone, self).__init__() audio_format = audio_format or pyaudio.paInt16 self._format = audio_format self._channels = channels self._rate = rate self._device_ndx = device_ndx self._pyaudio = None self._stream = None self._stream_queue = None
def record_audio(channels, rate, chunk): """Opens a recording stream in a context manager.""" audio_interface = pyaudio.PyAudio() audio_stream = audio_interface.open( format=pyaudio.paInt16, channels=channels, rate=rate, input=True, frames_per_buffer=chunk, ) yield audio_stream audio_stream.stop_stream() audio_stream.close() audio_interface.terminate() # [END audio_stream]
def start(): CHUNK = 1024 FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 16000 RECORD_SECONDS = 3 WAVE_OUTPUT_FILENAME = path+'recording.wav' p = pyaudio.PyAudio() stream = p.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) print("Done!") stream.stop_stream() stream.close() p.terminate() 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() alexa()
def __init__(self): # Microphone stream config. self.CHUNK = 1024 # CHUNKS of bytes to read each time from mic self.FORMAT = pyaudio.paInt16 self.CHANNELS = 1 self.RATE = 16000 self.SILENCE_LIMIT = 1 # Silence limit in seconds. The max ammount of seconds where # only silence is recorded. When this time passes the # recording finishes and the file is decoded self.PREV_AUDIO = 0.5 # Previous audio (in seconds) to prepend. When noise # is detected, how much of previously recorded audio is # prepended. This helps to prevent chopping the beginning # of the phrase. self.THRESHOLD = 4500 self.num_phrases = -1 # These will need to be modified according to where the pocketsphinx folder is MODELDIR = "../../tools/pocketsphinx/model" DATADIR = "../../tools/pocketsphinx/test/data" # Create a decoder with certain model #config = Decoder.default_config() #config.set_string('-hmm', os.path.join(MODELDIR, 'en-us/en-us')) #config.set_string('-lm', os.path.join(MODELDIR, 'en-us/en-us.lm.bin')) #config.set_string('-dict', os.path.join(MODELDIR, 'en-us/cmudict-en-us.dict')) LMD = "4842.lm" DICTD = "4842.dic" # Creaders decoder object for streaming data. #self.decoder = Decoder(config) self.decoder = Decoder(lm=LMD, dict=DICTD)
def save_speech(self, data, p): """ Saves mic data to temporary WAV file. Returns filename of saved file """ filename = 'output_'+str(int(time.time())) # 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 read_chunks(self, size): 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 not device_index: # print('can not find input device with {} channel(s)'.format(self.channels)) # return stream = self.pyaudio_instance.open( input=True, format=pyaudio.paInt16, channels=self.channels, rate=self.sample_rate, frames_per_buffer=size, stream_callback=self._callback, input_device_index = device_index, ) while not self.quit_event.is_set(): frames = self.queue.get() if not frames: break yield frames stream.close()
def start(self, quit_event=None, show=None): stream = self.pyaudio_instance.open( rate=RATE, frames_per_buffer=FRAMES, format=pyaudio.paInt16, channels=2, input=True, # output_device_index=1, stream_callback=self._callback) self.event.clear() if not quit_event: quit_event = threading.Event() phat = [0] * (2 * direction_n + 1) while not (quit_event.is_set() or self.event.is_set()): try: data = self.queue.get() buf = np.fromstring(data, dtype='int16') tau, cc = gcc_phat(buf[0::2] * window, buf[1::2] * window, fs=RATE, max_tau=max_tau, interp=1) theta = math.asin(tau / max_tau) * 180 / math.pi print('\ntheta: {}'.format(int(theta))) for i, v in enumerate(cc): phat[i] = int(v * 512) if show: show(phat) # print [l for l in level] except KeyboardInterrupt: break stream.close()
def valid_test(self,device,rate=44100): """given a device ID and a rate, return TRUE/False if it's valid.""" try: self.info=self.p.get_device_info_by_index(device) if not self.info["maxInputChannels"]>0: return False stream=self.p.open(format=pyaudio.paInt16,channels=1, input_device_index=device,frames_per_buffer=self.chunk, rate=int(self.info["defaultSampleRate"]),input=True) stream.close() return True except: return False
def stream_start(self): """adds data to self.data until termination signal""" self.initiate() print(" -- starting stream") self.keepRecording=True # set this to False later to terminate stream self.data=None # will fill up with threaded recording data self.fft=None self.dataFiltered=None #same self.stream=self.p.open(format=pyaudio.paInt16,channels=1, rate=self.rate,input=True,frames_per_buffer=self.chunk) self.stream_thread_new()
def record_audio(rate, chunk): """Opens a recording stream in a context manager.""" # Create a thread-safe buffer of audio data buff = queue.Queue() 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, # 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=functools.partial(_fill_buffer, buff), ) yield _audio_data_generator(buff) audio_stream.stop_stream() audio_stream.close() # Signal the _audio_data_generator to finish buff.put(None) audio_interface.terminate() # [END audio_stream]