def __init__(self, byte_data, sample_rate, bit_width, channels, dtype = None): """ byte_data: A byte string containing the raw data. BIT_WIDTH: bit width in bytes. """ assert isinstance(bit_width, (int, long)) and bit_width > 0, \ "`bit_width` must be positive integer." bit_width = pyaudio.get_sample_size(pyaudio.get_format_from_width(bit_width)) assert isinstance(channels, int) and channels in [1, 2], \ "`channels` can be either 1(mono) or 2(stereo)." assert channels in (1, 2), \ "`channels` can be either 1(mono) or 2(stereo) only." assert sample_rate > 0, "`sample_rate` must be positive." self.__bit_width = bit_width self.__channels = channels self.__sample_rate = sample_rate self.__byte_data = byte_data # a byte string if dtype is None: dtype = self._get_dtype_by_bit_width() if not self._validate_dtype(dtype): raise ValueError("`dtype` is not compatible with the `bit_width`.") self.__dtype = dtype self.format = pyaudio.get_format_from_width(self.BIT_WIDTH)
def init_pyaudio_stream(PyAudio=PA, width=WIDTH, channels=CHANNELS, rate=RATE, frames_per_buffer=CHUNK, input=False, output=True, blocking=AUDIO_BLOCKING): if blocking: return PyAudio.open(format=pyaudio.get_format_from_width(width), channels=channels, rate=rate, frames_per_buffer=frames_per_buffer, input=input, output_device_index=0, output=output) else: return PyAudio.open(format=pyaudio.get_format_from_width(width), channels=channels, rate=rate, frames_per_buffer=frames_per_buffer, input=input, output_device_index=0, output=output, stream_callback=audify_data_callback)
def calibrate(): print ('Calibrating Microphones ...') # Play stream_1 = calib_play.open(format = pa.get_format_from_width(sweep.getsampwidth()), channels = channel_count, rate = sweep.getframerate(), input = False, output = True, input_device_index = get_Mics()[0], output_device_index = get_Spkrs()[0]) # Record stream_2 = calib_recd.open(format = pa.get_format_from_width(sweep.getsampwidth()), channels = channel_count, rate = sweep.getframerate(), input = True, output = False, input_device_index = get_Mics()[1], output_device_index = get_Spkrs()[0], frames_per_buffer = samples_chunk) frames = [] in_rms = [] out_data = sweep.readframes(int(sweep.getframerate()/10)) for i in range(0, int(sweep.getframerate() / (samples_chunk * 5))): stream_1.write(out_data) out_data = sweep.readframes(int(sweep.getframerate()/10)) in_data = stream_2.read(samples_chunk) frames.append(in_data) rms = np.fromstring(in_data, dtype=np.int16) rms = np.sqrt(np.mean(rms ** 2.0)) in_rms.append(rms) global sweep_in_rms sweep_in_rms = np.mean(np.array(in_rms)) / 100.0 stream_1.stop_stream() stream_2.stop_stream() stream_1.close() stream_2.close() sweep.close() calib_play.terminate() calib_recd.terminate() global sweep_file_rms #sweep_file_rms = sweep_file_rms/50.0 #sweep_in_rms = sweep_in_rms/50.0 print('Sine Sweep RMS from File = ' + str(sweep_file_rms)) print('Sine Sweep RMS from Mics = ' + str(sweep_in_rms)) if sweep_file_rms > sweep_in_rms: gain_ratio = sweep_in_rms/sweep_file_rms else: gain_ratio = sweep_file_rms/sweep_in_rms print ('Gain = ' + str(gain_ratio)) print ('Microphones Calibrated !') time.sleep(5) return gain_ratio
def __init__(self, device_index, sample_rate, bit_width, chunk_size = 8092, channels = 1): audio = pyaudio.PyAudio() ## Checking the device_index is valid or not. assert isinstance(device_index, (int, long)), "Device index must be an integer." device_count = audio.get_device_count() assert 0 <= device_index < device_count, "`device_index` out of range: {} out of {}".format(device_index, count) audio.terminate() self.__device_index = device_index if not self.device_info["maxInputChannels"] > 0: raise DeviceTypeError("Can not source from a non-input device.") self.__format = pyaudio.get_format_from_width(bit_width) self.__bit_width = pyaudio.get_sample_size(self.FORMAT) assert isinstance(sample_rate, (int, long)), "`sample_rate` must be integer." max_sample_rate = self.device_info["defaultSampleRate"] assert 0 < sample_rate <= max_sample_rate, "`sample_rate` out of range: {} out of {}".format(sample_rate, max_sample_rate) self.__sample_rate = sample_rate assert isinstance(chunk_size, (int, long)), "`chunk_size` must be integer." self.__chunk_size = chunk_size assert channels in [1, 2], '`channels` can be either 1 or 2. 1 for mono audio, 2 for stereo.' self.__channels = channels # audio resource and streams. self.__audio = None self.__input_stream = None
def openStream(self, file, p): return p.open(format=pyaudio.get_format_from_width( file.getsampwidth()), channels=file.getnchannels(), rate=file.getframerate(), output=True, output_device_index=4)
def __init__(self, audioformat, queue=None): """Constructor. Creates a pyaudio sound renderer. Parameters ---------- audioformat : dict A dictionary containing the properties of the audiostream queue : Queue.queue A queue object which serves as a buffer on which the individual audio frames are placed by the decoder. """ global pyaudio import pyaudio if not queue is None: self.queue = queue self.pa = pyaudio.PyAudio() self.stream = self.pa.open(channels=audioformat["nchannels"], rate=audioformat["fps"], frames_per_buffer=audioformat['buffersize'], format=pyaudio.get_format_from_width( audioformat["nbytes"]), output=True, stream_callback=self.get_frame) self.keep_listening = True
def on_command(self, data): """ Perform actions after voice input was recorded. :param np.array data: The voice input data """ self.recording_state = False # Simulate action time.sleep(5) if self.raspi_mode: self.light.processing() # TODO: For now, save the file filename = 'testapp' + datetime.now().strftime( '%Y-%m-%d_%H-%M-%S') + '.wav' wf = wave.open(filename, 'wb') wf.setnchannels(self.channels) wf.setsampwidth( pyaudio.get_sample_size(pyaudio.get_format_from_width(self.width))) wf.setframerate(self.sample_rate) wf.writeframes(b''.join(data)) wf.close() self.recording = [] self.data = np.zeros(self.feed_samples, dtype=self.format) self.queue.empty() self.recording_state = False if self.raspi_mode: self.light.off()
def playWave(self): #define stream chunk chunk = 1024 #open wav file wFile = wave.open('C:\Users\Liz\workspace\SineLanguage\message.wav', 'rb') #instantiate pyaudio p = pyaudio.PyAudio() #open stream stream = p.open(format = pyaudio.get_format_from_width(wFile.getsampwidth()), channels = wFile.getnchannels(), rate = wFile.getframerate(), output = True) #read data data = wFile.readframes(chunk) #play stream while data != '': stream.write(data) data = wFile.readframes(chunk) #stop stream stream.stop_stream() stream.close() #close PyAudio p.terminate() wFile.close()
def __configure_input_file(self, **kwargs): in_file = kwargs[KEY_INPUT_FILE_NAME] self.file_source = wave.open(in_file, 'rb') self.format = pyaudio.get_format_from_width( self.file_source.getsampwidth()) self.channels = self.file_source.getnchannels() self.rate = self.file_source.getframerate()
def create_ui(self): self.setWindowTitle('AVCapture') self.resize(960, 540) self.m_pyaudio = pyaudio.PyAudio() self.m_pyaudio_stream = self.m_pyaudio.open( format=pyaudio.get_format_from_width(2), channels=2, rate=48000, output=True) self.m_pyaudio_stream.start_stream() # self.m_video_render = CRenderWid(self) self.m_video_render = QRenderWidget(self) self.m_video_render.move(0, 0) self.m_video_render.resize(self.width(), self.height()) self.m_menubar = self.menuBar() self.m_menu_file = QMenu('File', self.m_menubar) self.m_menu_start_act = QAction("Start Record", self) self.m_menu_file.addAction(self.m_menu_start_act) self.m_menu_start_act.setEnabled(True) self.m_menu_stop_act = QAction("Stop Record", self) self.m_menu_file.addAction(self.m_menu_stop_act) self.m_menu_stop_act.setEnabled(False) self.m_menu_save_act = QAction("Save Image", self) self.m_menu_file.addAction(self.m_menu_save_act) self.m_menu_save_act.setEnabled(True) self.m_menu_save_act.triggered.connect(self.slot_button_save) self.m_menubar.addMenu(self.m_menu_file) self.m_menu_file.triggered.connect(self.slot_file_selected) self.m_menu_device = QMenu('Device', self.m_menubar) self.m_menu_device_act = [] device_name_list = self.m_capture.list_device() index = 0 for device_name in device_name_list: if device_name.find("Pro Capture") < 0 and device_name.find( "Eco Capture") < 0: continue action = QAction(device_name, self) self.m_menu_device_act.append(action) self.m_menu_device.addAction(action) action.setCheckable(True) self.m_menubar.addMenu(self.m_menu_device) self.m_menu_device.triggered.connect(self.slot_device_selected) ctrlWidget = MWControlWidget(self.m_capture) vbox = QtWidgets.QVBoxLayout() vbox.addWidget(self.m_video_render) vbox.addWidget(ctrlWidget) qpb_save = QPushButton("Save Image") qpb_save.clicked.connect(self.slot_button_save) vbox.addWidget(qpb_save) widget = QtWidgets.QWidget() widget.setLayout(vbox) self.setCentralWidget(widget)
def _record(self): # Start recording audio on the current thread until stop() is # called. p = pyaudio.PyAudio() channels, rate = self.config.CHANNELS, self.config.RATE frames_per_buffer = self.config.FRAMES_PER_BUFFER pa_format = pyaudio.get_format_from_width(self.config.SAMPLE_WIDTH) stream = p.open(input=True, format=pa_format, channels=channels, rate=rate, frames_per_buffer=frames_per_buffer) # Start recognising in a loop stream.start_stream() while self._recording: with self._condition: self._buffers.append(stream.read(frames_per_buffer)) # Notify waiting threads (if any). self._condition.notifyAll() # This improves the performance; we don't need to process as # much audio as the device can read. time.sleep(self.read_interval) stream.close() p.terminate()
def run(self): self._running = True self._stream = self._audio.open( format=pyaudio.get_format_from_width(self.params.nbytes), channels=self.params.channels, rate=self.params.sample_rate, input=True, frames_per_buffer=self.params.frame_per_buffer) while self._stream.is_active() and self._running: if self._paused: self._stream.stop_stream() with self._condition: self._condition.wait() self._stream.start_stream() data = self._stream.read(self._chunk_size, exception_on_overflow=False) if self._consumer is not None: self._consumer.input(data) if self._stream.is_active(): self._stream.stop_stream() if self._running: self.on_error("Stream has unexpectedly stopped") self._stream.close() self._audio.terminate()
def play_audio(file_name): # sets up file and PyAudio file = wave.open(file_name, "rb") py_audio = pyaudio.PyAudio() # opens the audio stream stream = py_audio.open(format=pyaudio.get_format_from_width( file.getsampwidth()), channels=file.getnchannels(), rate=file.getframerate(), output=True) # read in bytes from file data = file.readframes(1024) # plays the audio stream while data: stream.write(data) data = file.readframes(1024) # closes the stream and terminates stream.stop_stream() stream.close() py_audio.terminate()
def __init__(self, audioformat, queue=None): """Constructor. Creates a pyaudio sound renderer. Parameters ---------- audioformat : dict A dictionary containing the properties of the audiostream queue : Queue.queue A queue object which serves as a buffer on which the individual audio frames are placed by the decoder. """ if pyaudio is None: raise RuntimeError("Pyaudio sound renderer is not available") super(SoundrendererPyAudio, self).__init__() if not queue is None: self.queue = queue self.pa = pyaudio.PyAudio() self.stream = self.pa.open( channels = audioformat["nchannels"], rate = audioformat["fps"], # frames_per_buffer = audioformat['buffersize'], format = pyaudio.get_format_from_width(audioformat["nbytes"]), output = True, )
def generate_speech(self): self.wf.rewind() self.input_stream = self.pa.open(format=pyaudio.get_format_from_width(self.wf.getsampwidth()), channels=1, rate = self.template_rate, input=True, stream_callback = self.read_callback) if (self.muck_up): self.wf.close() self.wf = wave.open('/home/justin/auditory-soa/sample-completely-different.wav', 'rb') self.output_stream = self.pa.open(format=pyaudio.get_format_from_width(self.wf.getsampwidth()), channels=1, rate = self.template_rate, output=True, stream_callback = self.write_callback) self.template_idx = 0 self.signal = []
def from_file(cls, original_file): """Initialise recording from file""" data_sequence, sample_width, num_channels, frame_rate = read_wav( original_file) return cls(frames=data_sequence, rate=frame_rate, num_channels=num_channels, audio_format=pyaudio.get_format_from_width(sample_width))
def __init__(self, audioformat): fps = audioformat["fps"] nchannels = audioformat["nchannels"] nbytes = audioformat["nbytes"] p = pyaudio.PyAudio() self.stream = p.open(channels=nchannels, rate=fps, format=pyaudio.get_format_from_width(nbytes), output=True)
def play(self): """plays audio""" audio = self.get_audio() pyaudio_format = pyaudio.get_format_from_width(audio['sample_width']) stream = self.pyaudio_ins.open(rate=audio['sample_rate'], channels=audio['channels'], format=pyaudio_format, output=True) stream.write(audio['recording_values']) stream.close()
def load_from_file(cls, wav_file): wav = wave.open(wav_file, 'rb') audio_data = wav.readframes(wav.getnframes()) wave_data = WaveData(wav.getnchannels(), wav.getframerate(), pyaudio.get_format_from_width(wav.getsampwidth())) sample_size = wav.getsampwidth() sample_duration = wav.getnframes() * wav.getframerate() sound_wave = generate_sound_wave_from_audio_data(audio_data) volume = sound_wave[1].max() return cls(wave_data, volume, sample_duration, sample_size, sound_wave)
def say(text, notification="Open Assistant"): p = pyaudio.PyAudio() # open stream (2) stream = p.open(format=pyaudio.get_format_from_width(2), channels=1, rate=20000, output=True) data = synthesizer.synthesize(text) stream.write(data) stream.stop_stream() stream.close()
def __init__(self, audioformat): fps = audioformat["fps"] nchannels = audioformat["nchannels"] nbytes = audioformat["nbytes"] p = pyaudio.PyAudio() self.stream = p.open( channels = nchannels, rate = fps, format = pyaudio.get_format_from_width(nbytes), output = True )
def play(data): try: print() print("========== »нициализаци¤ аудиоподсистемы ==========") audio = pyaudio.PyAudio() print("============ »нициализаци¤ завершена ==============") print("") with wave.open(io.BytesIO(data), 'rb') as wav: # Measure number of frames: nFrames = int( len(data) / wav.getsampwidth() / wav.getnchannels() + 65) # Read ALL frames in memory: frames = wav.readframes(nFrames) # and calculate actual number of frames read... nFrames = int( len(frames) / wav.getsampwidth() / wav.getnchannels()) # Calculate wav length in seconds waveLen = nFrames / wav.getframerate() + 0.3 audioStream = audio.open( format=pyaudio.get_format_from_width(wav.getsampwidth()), channels=wav.getnchannels(), rate=wav.getframerate(), output=True, output_device_index=config.audioOutputDevice, frames_per_buffer=nFrames - 16 #!!! Dirty hack to workaround RPi cracking noise ) audioStream.start_stream() startTime = time.time() audioStream.write(frames) # Wait until played while time.time() < startTime + waveLen: time.sleep(0.2) except Exception as e: print(f'Exception playing audio: {e}') finally: try: audioStream.stop_stream() except: pass try: audioStream.close() except: pass try: audio.terminate() except: pass
def init(): global pyaudio_obj, stream pyaudio_obj = pa.PyAudio() stream = pyaudio_obj.open(format=pa.get_format_from_width(SAMPLE_WIDTH), channels=NUM_CHANNELS, rate=FRAME_RATE, input=True, output=True, frames_per_buffer=BLOCK_SIZE, stream_callback=callback) stream.start_stream()
def generate_speech(self): self.wf.rewind() self.input_stream = self.pa.open(format=pyaudio.get_format_from_width( self.wf.getsampwidth()), channels=1, rate=self.template_rate, input=True, stream_callback=self.read_callback) if (self.muck_up): self.wf.close() self.wf = wave.open( '/home/justin/auditory-soa/sample-completely-different.wav', 'rb') self.output_stream = self.pa.open(format=pyaudio.get_format_from_width( self.wf.getsampwidth()), channels=1, rate=self.template_rate, output=True, stream_callback=self.write_callback) self.template_idx = 0 self.signal = []
def start_pre_recorded_audio_stream(self): "This function opens the audio stream using a pre-recorded file containing ECG (left channel) and PCG (right channel) signals " #open the audio stream here self.currentdir = os.getcwd() self.recfilename = 'stethoscope_and_ecg_rec_example.wav' self.wf = wave.open( self.currentdir + '/recordings/' + self.recfilename, 'rb') self.audio_instance = audio(channels=self.wf.getnchannels(), rate=self.wf.getframerate(), frames_per_buffer=self.frame_size, format=pyaudio.get_format_from_width( self.wf.getsampwidth())) self.audio_instance.start_pyaudio_pre_recorded(self.wf) self.audio_instance.start_stream()
def run_decoder(): decoder = wake_decoders.get(profile.name) if decoder is None: logging.info('Loading wake decoder with hmm=%s, dict=%s' % (hmm_path, dict_path)) decoder_config = pocketsphinx.Decoder.default_config() decoder_config.set_string('-hmm', hmm_path) decoder_config.set_string('-dict', dict_path) decoder_config.set_string('-keyphrase', keyphrase) decoder_config.set_float('-kws_threshold', kws_threshold) decoder = pocketsphinx.Decoder(decoder_config) wake_decoders[profile.name] = decoder decoder.start_utt() finished_event = threading.Event() def stream_callback(data, frame_count, time_info, status): decoder.process_raw(data, False, False) hyp = decoder.hyp() if hyp: decoder.end_utt() logging.debug('Keyphrase detected') finished_event.set() return (data, pyaudio.paComplete) return (data, pyaudio.paContinue) audio = pyaudio.PyAudio() data_format = pyaudio.get_format_from_width(2) mic = audio.open(format=data_format, channels=1, rate=16000, input=True, input_device_index=device_index, stream_callback=stream_callback) # Block until wake word is detected mic.start_stream() finished_event.wait() # Shut down audio input mic.stop_stream() mic.close() audio.terminate() # Pass to next stage wake_word_detected()
def stream_file_to_dev(pyaud: pyaudio.PyAudio, dev: dict): input_file = wave.open(os.getenv("WAVE_INPUT_FILENAME", "input.wav"), "rb") output_stream = pyaud.open( format=pyaudio.get_format_from_width(input_file.getsampwidth()), channels=input_file.getnchannels(), output_device_index=dev.get("index"), rate=int(dev.get("defaultSampleRate")), output=True, ) input_file.close() file = open(os.getenv("WAVE_INPUT_FILENAME", "input.wav"), "rb") semaphore.release() output_stream.write(file.read()) file.close()
def get_stream(self): """ Open pyaudio stream :returns pyaudio: Stream object """ stream = self.pyaudio.open(format=pyaudio.get_format_from_width( self.width), channels=self.channels, rate=self.sample_rate, input=True, frames_per_buffer=self.sample_chunks, input_device_index=self.index, stream_callback=self.callback) return stream
def __init__(self, byte_data, sample_rate, bit_width, channels, dtype=None): """ byte_data: A byte string containing the raw data. BIT_WIDTH: bit width in bytes. """ assert isinstance(bit_width, (int, long)) and bit_width > 0, \ "`bit_width` must be positive integer." bit_width = pyaudio.get_sample_size( pyaudio.get_format_from_width(bit_width)) assert isinstance(channels, int) and channels in [1, 2], \ "`channels` can be either 1(mono) or 2(stereo)." assert channels in (1, 2), \ "`channels` can be either 1(mono) or 2(stereo) only." assert sample_rate > 0, "`sample_rate` must be positive." self.__bit_width = bit_width self.__channels = channels self.__sample_rate = sample_rate self.__byte_data = byte_data # a byte string if dtype is None: dtype = self._get_dtype_by_bit_width() if not self._validate_dtype(dtype): raise ValueError("`dtype` is not compatible with the `bit_width`.") self.__dtype = dtype self.format = pyaudio.get_format_from_width(self.BIT_WIDTH)
def run(filename, chunk, led): wf = wave.open(filename, 'rb') pyaudio_format = pyaudio.get_format_from_width(wf.getsampwidth()) if pyaudio_format == pyaudio.paInt16: width = np.int16 else: sys.exit("add a mapping from this pyaudio format to width") with audio_stream(wf) as stream: data = wf.readframes(chunk) while data != '': stream.write(data) light_io(led, np.fromstring(data, dtype=width)) data = wf.readframes(chunk)
def __init__(self, filename, chunk=1024): self.filename = filename self.wavfile = wave.open(self.filename, 'rb') audio_format = pyaudio.get_format_from_width( self.wavfile.getsampwidth()) sample_rate = self.wavfile.getframerate() channels = self.wavfile.getnchannels() super(AudioStreamFile, self).__init__(audio_format, sample_rate, channels, chunk, audio_input=False, audio_output=True)
def echo(data, channels = 1, rate = 8000, sampwidth = 4): """Echo speech signal from wave data. :param data: the wave data :param channel: wave channels(single or stereo) :param rate: sample rate :returns: None""" pa = pyaudio.PyAudio() stream = pa.open(format = pyaudio.get_format_from_width(sampwidth), channels = channels, rate = rate, output = True) stream.write(data, len(data)) stream.stop_stream() stream.close() pa.terminate()
def stream_audio(self): pya = pyaudio.PyAudio() audio_stream = pya.open( format=pyaudio.get_format_from_width(self._sample_width), channels=self._number_of_channels, rate=self._sample_rate, output=True, stream_callback=self._get_next_buffer, frames_per_buffer=BUFFER_SIZE, ) audio_stream.start_stream() try: yield audio_stream finally: audio_stream.stop_stream() audio_stream.close() pya.terminate()
def __init__(self, wave_samplewidth, wave_channels, wave_rate, initial_buffer_size, processed_data_handler): self._stream = audio.open( format=pyaudio.get_format_from_width(wave_samplewidth), channels=wave_channels, rate=wave_rate, frames_per_buffer=CHUNK_SIZE, output=True, stream_callback=self.callback) self._buffer_size = initial_buffer_size self._processed_data_handler = processed_data_handler self._play_lock = threading.Lock() self._add_lock = threading.Lock() self._play_lock.acquire() self._buffer = [] self._is_playing = False self._finish = False
def record_sound(s): audio = pyaudio.PyAudio() stream = audio.open(input = True, output = True, frames_per_buffer = CHUNK_SIZE, channels = CHANNELS, rate = RATE, format = pyaudio.get_format_from_width(BIT_WIDTH)) frames = [] for _ in range(s): data_buffer = stream.read(CHUNK_SIZE) stream.write(data_buffer) frames.append(data_buffer) data = b''.join(frames) stream.stop_stream() stream.close() return data
def __init__(self, device_index, sample_rate, bit_width, chunk_size=8092, channels=1): audio = pyaudio.PyAudio() ## Checking the device_index is valid or not. assert isinstance(device_index, (int, long)), "Device index must be an integer." device_count = audio.get_device_count() assert 0 <= device_index < device_count, "`device_index` out of range: {} out of {}".format( device_index, count) audio.terminate() self.__device_index = device_index if not self.device_info["maxInputChannels"] > 0: raise DeviceTypeError("Can not source from a non-input device.") self.__format = pyaudio.get_format_from_width(bit_width) self.__bit_width = pyaudio.get_sample_size(self.FORMAT) assert isinstance(sample_rate, (int, long)), "`sample_rate` must be integer." max_sample_rate = self.device_info["defaultSampleRate"] assert 0 < sample_rate <= max_sample_rate, "`sample_rate` out of range: {} out of {}".format( sample_rate, max_sample_rate) self.__sample_rate = sample_rate assert isinstance(chunk_size, (int, long)), "`chunk_size` must be integer." self.__chunk_size = chunk_size assert channels in [ 1, 2 ], '`channels` can be either 1 or 2. 1 for mono audio, 2 for stereo.' self.__channels = channels # audio resource and streams. self.__audio = None self.__input_stream = None
def send(self, messageType, message): freqArr = self.getFreqs(message) # Create new .wav file wFile = wave.open('message.wav', 'w') # Set it up wFile.setparams((1, 2, self.sampleRate, 0, 'NONE', 'not compressed')) wData = "" # Add data for note in freqArr: for sample in range(0, int(self.sampleRate*(self.notelength))): wData += struct.pack('h', self.volume*(math.sin(2*sample*note*math.pi/(self.sampleRate)))) # Close and reopen in read mode wFile.writeframes(wData) wFile.close() wFile = wave.open('message.wav','rb') # Define stream chunk chunk = 1024 p = pyaudio.PyAudio() # Open stream stream = p.open(format = pyaudio.get_format_from_width(wFile.getsampwidth()), channels = wFile.getnchannels(), rate = wFile.getframerate(), output = True) data = wFile.readframes(chunk) #play stream while data != '': stream.write(data) data = wFile.readframes(chunk) #stop stream stream.stop_stream() stream.close() #close PyAudio and clean up p.terminate() wFile.close() os.remove('message.wav')
def play_audio(audio, fs): ''' plays single channel audio data ''' p = pyaudio.PyAudio() datatype = audio.dtype #print datatype if datatype == np.int8: width = 1 elif datatype == np.int16: width = 2 elif datatype == np.int32: width = 4 elif datatype == np.float32: width = 4 output_format = pyaudio.get_format_from_width(width, False) stream = p.open(format=output_format, channels=1, rate=fs, output=True) stream.write(audio.tostring())
def play(self, data, nLoop=inf): self.stop() data = reshape(data, [-1, self.nChannels], order='F') framesPerBuffer = round(self.OUTPUT_BUFFER_LENGTH * self.sampleRate) nDataFrames = shape(data)[0] data = self.encodeData(data) data = repeat(data, ceil(framesPerBuffer / nDataFrames)) callback = self.createPlayCallback(data, nLoop * nDataFrames) self.outputStream = self.session.open( format=pyaudio.get_format_from_width(self.sampleSize, unsigned=False), channels=self.nChannels, rate=self.sampleRate, output=True, output_device_index=self.deviceIdx, stream_callback=callback, frames_per_buffer=framesPerBuffer) self.outputStream.start_stream()
def main(text): # Instantiates a client client = texttospeech.TextToSpeechClient() # Set the text input to be synthesized synthesis_input = texttospeech.SynthesisInput(text=text) # Build the voice request, select the language code ("en-US") and the ssml # voice gender ("neutral") voice = texttospeech.VoiceSelectionParams( language_code="ko-KR", ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL) # Select the type of audio file you want returned audio_config = texttospeech.AudioConfig( audio_encoding=texttospeech.AudioEncoding.LINEAR16) # Perform the text-to-speech request on the text input with the selected # voice parameters and audio file type response = client.synthesize_speech(input=synthesis_input, voice=voice, audio_config=audio_config) # response.audio_content is a byte string with wave.open(io.BytesIO(response.audio_content), 'rb') as f: width = f.getsampwidth() channels = f.getnchannels() rate = f.getframerate() pa = pyaudio.PyAudio() pa_stream = pa.open(format=pyaudio.get_format_from_width(width), channels=channels, rate=rate, output=True) pa_stream.write(response.audio_content) # if __name__ == "__main__": # main()
def record(self, t): framesPerBuffer = round(self.INPUT_BUFFER_LENGTH * self.sampleRate) nDataFrames = round(t * self.sampleRate) nWarmupFrames = framesPerBuffer nTotalFrames = nDataFrames + nWarmupFrames inputStream = self.session.open( format=pyaudio.get_format_from_width(self.sampleSize, unsigned=False), channels=self.nChannels, rate=self.sampleRate, input=True, input_device_index=self.deviceIdx, frames_per_buffer=framesPerBuffer) data = [] iFrame = 0 while iFrame < nTotalFrames: dataTmp = inputStream.read(framesPerBuffer) dataTmp = fromstring(dataTmp, dtype=uint8) data.append(dataTmp) iFrame += framesPerBuffer data = array(data, dtype='uint8') inputStream.stop_stream() inputStream.close() data = self.decodeData(data) data = data[nWarmupFrames:,:] data = data[:nDataFrames,:] data = data.flatten(order='F').tolist() return data
def __init__(self, filename: str, metre: float, q: Manager().Queue, *, loop: bool=False): self.filename = filename self.q = q self.wf = open_wav(self.filename, "rb") self.formatting = get_format_from_width(self.wf.getsampwidth()) self.channels = self.wf.getnchannels() self.no_frames = self.wf.getnframes() self.rate = self.wf.getframerate() self.data = self.wf.readframes(self.no_frames) self.duration = self.no_frames/self.rate self.wf.close() self.qs = [] self.processes_pointers = [] if not loop: self.no_p = int(self.duration*metre*10)+2 self.qs = [Queue() for _ in range(self.no_p)] self.ps = [Process( target=self.throw_process, args=(q,)) for q in self.qs] self.setup() self.i = 0
import pyaudio WIDTH=2 p = pyaudio.PyAudio() format_pyaudio_int = pyaudio.paInt16 NUM_CHANNEL_INT = 1 FS = 16000 LENGTH_BUFFER_INT = 2 stream = p.open(format=format_pyaudio_int, channels=NUM_CHANNEL_INT, rate=FS, input=False, output=True, frames_per_buffer=LENGTH_BUFFER_INT) # print pyaudio.paInt16 print pyaudio.paInt8 print pyaudio.get_format_from_width(1, unsigned=False) # print pyaudio.get_format_from_width(WIDTH)
args = parser.parse_args() output_file = args.out n = args.n CHUNK_SIZE = 8092 RATE = 44100 BIT_WIDTH = 2 CHANNELS = 1 audio = pyaudio.PyAudio() input_stream = audio.open(input = True, frames_per_buffer = CHUNK_SIZE, channels = CHANNELS, rate = RATE, format = pyaudio.get_format_from_width(BIT_WIDTH)) frames = [] for _ in range(n): data_buffer = input_stream.read(CHUNK_SIZE) frames.append(data_buffer) input_stream.stop_stream() input_stream.close() data = b''.join(frames) wav_writer = wave.open(output_file, "wb") wav_writer.setframerate(RATE) wav_writer.setsampwidth(BIT_WIDTH) wav_writer.setnchannels(CHANNELS) wav_writer.writeframes(data) wav_writer.close() audio.terminate()