def input_from_signal(fs: Frames, signal: Signal, chunk_size: Frames, input_queue: AudioChunkStream) -> Frames: print("starting input loop") cur_frame = 0 try: while True: metadata = ChunkMetadata(fs, cur_frame, cur_frame + chunk_size) chunk = StreamChunk( buf=signal.get_temporal(fs, metadata.start, metadata.end), metadata=metadata, ) input_queue.put(chunk) cur_frame += chunk_size except KeyboardInterrupt: print("shutting down input loop") input_queue.close() return cur_frame
def play_signal(signal: Signal, fs: int, start: Optional[Frames] = None, end: Optional[Frames] = None): start = to_frames(start if start is not None else 0) end = to_frames(end if end is not None else signal.get_range(fs)[1]) buffer = signal.get_temporal(fs, start, end) # Ensure that highest value is in 16-bit range audio = buffer / np.max(np.abs(buffer)) * (2 ** 15 - 1) # Convert to 16-bit data audio = audio.astype(np.int16) # Start playback play_obj = sa.play_buffer(audio, 1, 2, fs) # Wait for playback to finish before exiting play_obj.wait_done()
def analyze(fs: Hz, child: Signal, flt: BandPassSignal, start: Frames, end: Frames): fig, (ax) = plt.subplots(2, 3) t = np.arange(start, end, 1) xt = child.get_temporal(fs, start, end) time_plot = ax[0][0] time_plot.plot(t, xt) time_plot.title.set_text("x[t]") freq_plot = ax[1][0] xw = np.fft.fft(xt) draw_fft(xw, fs, freq_plot, "X[w]") yt = flt.get_temporal(fs, start, end) yt_plot = ax[0][2] yt_plot.plot(t, yt) yw = np.fft.fft(yt) yw_plot = ax[1][2] draw_fft(yw, fs, yw_plot, "Y[w]") ht = flt.ht_cache[fs] ht_plot = ax[0][1] ht_plot.plot(np.arange(0, len(ht), step=1) / fs, ht) ht_plot.title.set_text("h[t]") ht_plot.set_xlabel("t") om = np.linspace(-fs / 2.0, fs / 2.0, 512) hw_plot = ax[1][1] signal.freqz( b=ht, worN=om, plot=lambda w, h: hw_plot.plot(w, np.log10(np.abs(h)) * 20), fs=fs, ) hw_plot.title.set_text("H[w] (dB)") hw_plot.set_xlabel("Hz") plt.show()
def get_centered_sample(signal: Signal): lower, upper = signal.get_range() lower_frames = to_frames(lower) upper_frames = to_frames(upper) return np.roll(signal.get_temporal(lower_frames, upper_frames), lower_frames)