def __init__(self, Fs=16000, TinSec=10): ''' Fs: 取樣頻率,預設值為 16000, TinSec: 保存語音長度,預設值為 10 sec ''' print('RyAudio use %s' % pa.get_portaudio_version_text()) self.Fs = Fs self.spBufferSize = 1024 self.fftWindowSize = self.spBufferSize self.aP = pa.PyAudio() self.iS = pa.Stream(PA_manager=self.aP, input=True, rate=self.Fs, channels=1, format=pa.paInt16) self.oS = pa.Stream(PA_manager=self.aP, output=True, rate=self.Fs, channels=1, format=pa.paInt16) self.iTh = None self.oTh = None #self.sound= None #self.soundTime= 0 self.gettingSound = True self.playingSound = True self.t = 0 self.b = None # byte string self.x = None # ndarray self.fft = None self.f0 = 0 #None self.en = 0 #None self.fm = 0 #None # frequency mean self.fv = 0 #None # frequency var self.fs = 0 #None # frequency std self.enP = 0 #None # AllPass self.enPL = 0 #None # LowPass self.enPH = 0 #None # HighPass self.entropy = 0 #None self.frameI = 0 #self.frameN= self.spBufferSize/4 #1024/4 = 256 self.TinSec = TinSec #10 # sec self.frameN = self.Fs * self.TinSec / self.spBufferSize #self.spBufferSize/4 #1024/4 = 256 self.frameN = int(self.frameN) self.specgram = pl.random([self.frameN, self.spBufferSize / 2]) self.xBuf = pl.random([self.frameN, self.spBufferSize])
def __init__(self, format=pa.paInt16, rate=44100): self.stream = pa.Stream(pa.PyAudio(), format=format, rate=rate, channels=2, input=True, output=True)
def __init__(self, sr=22050): self.sr = sr self.buff_size = 1024 self.buff = None self.specgram_h = self.buff_size // 2 + 1 self.specgram_w = 1000 self.specgram = np.zeros((self.specgram_h, self.specgram_w)) self.frame_counter = 0 self.pa = pa.PyAudio() self.input_stream = pa.Stream(PA_manager=self.pa, input=True, rate=self.sr, channels=1, format=pa.paInt16) self.output_stream = pa.Stream(PA_manager=self.pa, output=True, rate=self.sr, channels=1, format=pa.paFloat32) self.chirp = None self.get_data = True
# Get amplitude of input # in_amplitude = find_amplitude(in_data) subchunks = [] for osc in oscillators: osc.step_amp() subchunks.append(osc.get_samples(CHUNK_SIZE)) new_chunk = sum(subchunks) # Play sound return new_chunk.astype(numpy.float32).tostring(), pyaudio.paContinue pa_host = pyaudio.PyAudio() out_stream = pyaudio.Stream(pa_host, format=STREAM_FORMAT, channels=1, rate=SAMPLE_RATE, output=True, input=True, frames_per_buffer=CHUNK_SIZE, stream_callback=main_callback) out_stream.start_stream() # Initialize tkinter tk_host = tk.Tk() tk_host.wm_title('"the second hand somehow ..." drone program') tk_host.geometry("400x140+300+300") tk_host.resizable(0, 0) main_note_text = ('This is the electronic drone program for ' '"the second hand somehow different this time around." ' 'The program starts with the oscillator paused. '
import pyaudio import array import numpy import random import math # Initialize pyaudio CHANNELS = 1 RATE = 48000 MS_PER_UPDATE = 20 FORMAT = pyaudio.paFloat32 CHUNK = math.ceil((MS_PER_UPDATE / 1000) * RATE) pa_manager = pyaudio.PyAudio() stream = pyaudio.Stream(pa_manager, RATE, CHANNELS, FORMAT, input=True, frames_per_buffer=CHUNK) # Initialize variables sample_time_sec = 60 display_width = 100 average_samples = math.ceil((sample_time_sec * 1000) / 20) volume_list = array.array('f') picking = True out_arr = [] arr = [] while picking:
def open(self, **kwargs): new_pastream = pyaudio.Stream(self, **kwargs) self._streams.add(new_pastream) return new_pastream
The python-sounddevice and pyaudio libraries provide ways to record audio with Python. python-sounddevice records to NumPy arrays and pyaudio records to bytes objects. Both of these can be stored as WAV files using the scipy and wave libraries, respectively. python-sounddevice python-sounddevice allows you to record audio from your microphone and store it as a NumPy array. This is a handy datatype for sound processing that can be converted to WAV format for storage using the scipy.io.wavfile module. Make sure to install the scipy module for the following example (pip install scipy). This automatically installs NumPy as one of its dependencies: import sounddevice as sd from scipy.io.wavfile import write fs = 44100 # Sample rate seconds = 3 # Duration of recording myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2) sd.wait() # Wait until recording is finished write('output.wav', fs, myrecording) # Save as WAV file pyaudio Earlier in this article, you learned how to play sounds by reading a pyaudio.Stream(). Recording audio can be done by writing to this stream instead: import pyaudio import wave chunk = 1024 # Record in chunks of 1024 samples sample_format = pyaudio.paInt16 # 16 bits per sample channels = 2 fs = 44100 # Record at 44100 samples per second seconds = 3 filename = "output.wav" p = pyaudio.PyAudio() # Create an interface to PortAudio print('Recording')