0

So I'm trying to get my code to get the user to define the frequency and a duration of time. After that my program takes the frequency and plays it with pyaudio, and I wanted an animation to show up. I managed to get that done, but my problem is when I put in a Tkinter scale to make a delay similar to the doppler effect. I tried setting the DoubleVar into a string represented by the letter d, but my program now just quits after emitting the sound.

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)

There's no error messages.

pppery
  • 3,731
  • 22
  • 33
  • 46
  • have you imported the correct modules? – SF12 Study Sep 05 '19 at 05:47
  • @SF12 Study yes, I've use matplotlib, numpy and tkinter, but i didn't add it onto the original post – YeetorbeYeeted Sep 05 '19 at 14:39
  • Just in case I'll add it here: import numpy as np, from matplotlib import pyplot as plt, import pyaudio, import tkinter as tk from numpy import arange, sin, pi from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from matplotlib.figure import Figure from tkinter import ttk import numpy as np import matplotlib.animation as animation' – YeetorbeYeeted Sep 05 '19 at 20:36
  • I don't know if it is needed in ```Class```, but it looks like you have not added ```Example.mainloop()``` – SF12 Study Sep 12 '19 at 14:39

0 Answers0