我如何使此流读取与输入相对应的计算机声音输出

时间:2019-05-20 09:59:48

标签: python numpy matplotlib fft pyaudio

基本上,我为一个使用pyaudio读取我的麦克风输入的声音的项目制作了这个频谱,但是我希望对频谱而言它是可选的,以便将计算机的输出声音用于其数据,我会得到什么改变

import pyaudio
import numpy as np
import time
import matplotlib.animation as animation
import matplotlib.pyplot as plt
from matplotlib import style

pa = pyaudio.PyAudio()
audio_data = 0

def callback(in_data, frame_count, time_info, flag):
    global audio_data
    audio_data = np.fromstring(in_data, dtype=np.float32)
    return None,pyaudio.paContinue


stream = pa.open(format=pyaudio.paFloat32,
                 channels=1,
                 rate=44100,
                 output=False,
                 input=True,
                 stream_callback=callback)

stream.start_stream()

plt.ion() # Stop matplotlib windows from blocking

# Setup figure, axis and initiate plot
fig, ax = plt.subplots()
ln, = ax.plot([], [], 'b,:')

x = 0

while stream.is_active():

    fft_data = np.fft.fft(audio_data)
    fft_freq = np.fft.fftfreq(len(fft_data),d=1/44100)

    ln.set_xdata(np.abs(fft_freq))
    ln.set_ydata(np.abs(fft_data))


    fig.canvas.draw()
    fig.canvas.flush_events()

    if x == 0:
        ax.relim()
        ax.autoscale_view()
        ax.set_xlim([-500,10000])
        ax.set_ylim([-1,10])
        x=1

stream.close()
pa.terminate()

0 个答案:

没有答案