Я делаю небольшое приложение для визуализации звука, но управление не работает согласованно. Когда поля ввода включены (упакованы), тогда программа работает, как я и ожидал. Однако, если я закомментирую их, сюжет больше не будет отображаться, и я получаю только звук. Действительно озадачен этим ...
(работает python 3.7.4 в Ubuntu 18.04)
Следующие работы:
import threading
import numpy as np
import tkinter as tk
from tkinter import Label, Button, Entry, TOP, BOTH
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
import pyaudio
DEFAULT_FREQUENCY = 420 # frequency in Hz
DEFAULT_DURATION = 3.0 # length of sound stream in seconds
VOLUME = 0.1 # must < 1
INTERVAL = 100 # plot interval in millisecond
PACKAGE_LENGTH = 1024 # number of samples in sound package
FS = 2**12 # sampling frequency sound, normal is 44100
class SoundVisualiser:
def __init__(self, root):
self.root = root
self.root.title("Sound Visualiser")
label = tk.Label(self.root, text="Sound Visualiser")
label.pack()
self.fig, self.ax = plt.subplots(figsize=(5, 5))
self.canvas = FigureCanvasTkAgg(self.fig, master=self.root)
self.canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
self.ax.set_xlim(1000 * PACKAGE_LENGTH / FS, 0)
freq_label = tk.Label(self.root, text='Frequency')
self.frequency_entry = Entry(self.root)
self.frequency_entry.insert(0, DEFAULT_FREQUENCY)
durat_label = tk.Label(self.root, text='Duration')
self.duration_entry = Entry(self.root)
self.duration_entry.insert(0, DEFAULT_DURATION)
freq_label.pack(side='left')
self.frequency_entry.pack(side='left')
durat_label.pack(side='left')
self.duration_entry.pack(side='left')
self.quit_button = Button(
self.root, text='Quit', command=self.quit)
self.quit_button.pack(side='right')
self.control_button = Button(
self.root, text='Start')
self.control_button.bind('<Button-1>', self.start_visualisation)
self.control_button.pack(side='right')
self.duration = DEFAULT_DURATION
self.xdata = np.linspace(0, 1000 * PACKAGE_LENGTH / FS, PACKAGE_LENGTH)
self.audio = pyaudio.PyAudio()
def quit(self):
self.audio.terminate()
self.root.quit()
def generate_sound_stream(self):
self.sound_stream = (
(0.5 * np.sin(2 * np.pi * 325 / FS *
np.arange(FS * self.duration))) +
(0.1 * np.sin(2 * np.pi * 330 / FS *
np.arange(FS * self.duration))) +
(0.5 * np.sin(2 * np.pi * 340 / FS *
np.arange(FS * self.duration))) + 0
).astype(np.float32)
self.ax.set_ylim(1.1 * np.min(self.sound_stream), 1.1 * np.max(self.sound_stream))
def callback(self, in_data, frame_count, time_info, status):
out = self.sound_stream[:frame_count]
self.out_plot = out[:]
self.sound_stream = self.sound_stream[frame_count:]
return (out*VOLUME, pyaudio.paContinue)
def play_sound(self):
stream = self.audio.open(format=pyaudio.paFloat32,
channels=1,
rate=FS,
output=True,
stream_callback=self.callback)
stream.start_stream()
while stream.is_active():
pass
stream.stop_stream()
stream.close()
self.visualisation = None
self.control_button.config(text='Start')
def update_frame(self, frame):
samples = len(self.out_plot)
if samples == PACKAGE_LENGTH:
self.line.set_data(self.xdata, self.out_plot)
else:
xdata = np.linspace(0, 1000 * samples / FS, samples)
self.line.set_data(xdata, self.out_plot)
return self.line,
def start_visualisation(self, event):
self.duration = float(self.duration_entry.get())
self.generate_sound_stream()
self.line, = self.ax.plot([], [], lw=3)
self.control_button.config(text='run')
duration_range = np.arange(0, self.duration, INTERVAL / 1000)
self.visualisation = FuncAnimation(self.fig,
self.update_frame,
frames=duration_range,
interval=INTERVAL,
repeat=False)
# start audio in a seperate thread as otherwise audio and
# plot will not be at the same time
self.play_sound_thread = threading.Thread(target=self.play_sound)
self.play_sound_thread.start()
def main():
root = tk.Tk()
sound_visualiser = SoundVisualiser(root)
root.mainloop()
if __name__ == '__main__':
main()
комментируяПоля ввода перестают отображать сюжет
def __init__(self, root):
self.root = root
self.root.title("Sound Visualiser")
label = tk.Label(self.root, text="Sound Visualiser")
label.pack()
self.fig, self.ax = plt.subplots(figsize=(5, 5))
self.canvas = FigureCanvasTkAgg(self.fig, master=self.root)
self.canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
self.ax.set_xlim(1000 * PACKAGE_LENGTH / FS, 0)
freq_label = tk.Label(self.root, text='Frequency')
self.frequency_entry = Entry(self.root)
self.frequency_entry.insert(0, DEFAULT_FREQUENCY)
durat_label = tk.Label(self.root, text='Duration')
self.duration_entry = Entry(self.root)
self.duration_entry.insert(0, DEFAULT_DURATION)
# freq_label.pack(side='left')
# self.frequency_entry.pack(side='left')
# durat_label.pack(side='left')
# self.duration_entry.pack(side='left')
self.quit_button = Button(
self.root, text='Quit', command=self.quit)
self.quit_button.pack(side='right')
self.control_button = Button(
self.root, text='Start')
self.control_button.bind('<Button-1>', self.start_visualisation)
self.control_button.pack(side='right')
self.duration = DEFAULT_DURATION
self.xdata = np.linspace(0, 1000 * PACKAGE_LENGTH / FS, PACKAGE_LENGTH)
self.audio = pyaudio.PyAudio()
Нормальный вывод со звуком 