0

所以我试图让我的代码让用户定义频率和持续时间。之后我的程序获取频率并用 pyaudio 播放它,我想要一个动画来显示。我设法做到了,但我的问题是当我放入 Tkinter 量表以产生类似于多普勒效应的延迟时。我尝试将 DoubleVar 设置为由字母 d 表示的字符串,但我的程序现在在发出声音后就退出了。

p=pyaudio.PyAudio()
fs=44100
volume=.7
f=int(input('Enter your frequency:'))
t = float(input('Enter duration of signal (seconds):'))
samples=(np.sin(f*2*np.pi*np.arange(fs*t)/(fs)).astype(np.float32))
stream=p.open(format=pyaudio.paFloat32,
            channels=1,
            rate=fs,
            output=True)

stream.write(volume*samples)
stream.stop_stream()
stream.close()
p.terminate()

class Example(tk.Tk):
    def __init__(self):
        super().__init__()
        self.dou_var=tk.DoubleVar()
        self.scale = tk.Scale(self, from_=1, to=100, resolution=0.01,
                              variable=self.dou_var)
        self.scale.pack()
        tk.Button(self, text="Check Scale", command=self.check_scale).pack()

    def check_scale(self):
            d=str(self.dou_var.get())

def main():
    if __name__ == "__main__":
        Example().after()

fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line = ax.plot([], [], lw=2)

def init():
    line.set_data([], [])
    return line,
def animate(i):
    x = np.linspace(0, 2, 1000)
    y = np.sin(np.sin((f/d)*2*np.pi*np.arange(fs*t)/(fs)).astype(np.float32)*i)
    line.set_data(x, y)
    return line,
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=100, interval=20, blit=True)

没有错误消息。

4

0 回答 0