voicemeeter-api-python/examples/gui/__main__.py

110 lines
3.2 KiB
Python
Raw Normal View History

2023-06-24 23:30:35 +01:00
import logging
2025-01-15 12:34:31 +00:00
import tkinter as tk
from tkinter import ttk
2023-06-24 23:30:35 +01:00
import voicemeeterlib
logging.basicConfig(level=logging.DEBUG)
class App(tk.Tk):
2023-06-28 13:57:45 +01:00
INDEX = 3
2023-06-24 23:30:35 +01:00
def __init__(self, vm):
super().__init__()
2025-01-16 14:51:20 +00:00
self._vm = vm
2025-01-15 12:40:31 +00:00
self.title(f'{vm} - version {vm.version}')
2025-01-16 14:51:20 +00:00
self._vm.observer.add(self.on_ldirty)
2023-06-24 23:30:35 +01:00
# create widget variables
2023-06-28 13:57:45 +01:00
self.button_var = tk.BooleanVar(value=vm.strip[self.INDEX].mute)
self.slider_var = tk.DoubleVar(value=vm.strip[self.INDEX].gain)
2023-06-24 23:30:35 +01:00
self.meter_var = tk.DoubleVar(value=self._get_level())
self.gainlabel_var = tk.StringVar(value=self.slider_var.get())
# initialize style table
self.style = ttk.Style()
2025-01-15 12:40:31 +00:00
self.style.theme_use('clam')
2023-06-24 23:30:35 +01:00
self.style.configure(
2025-01-15 12:40:31 +00:00
'Mute.TButton',
foreground='#cd5c5c' if vm.strip[self.INDEX].mute else '#5a5a5a',
2023-06-24 23:30:35 +01:00
)
# create labelframe and grid it onto the mainframe
2025-01-16 14:51:20 +00:00
self.labelframe = tk.LabelFrame(self, text=self._vm.strip[self.INDEX].label)
2023-06-24 23:30:35 +01:00
self.labelframe.grid(padx=1)
2023-06-28 13:57:45 +01:00
# create slider and grid it onto the labelframe
2023-06-24 23:30:35 +01:00
slider = ttk.Scale(
self.labelframe,
from_=12,
to_=-60,
2025-01-15 12:40:31 +00:00
orient='vertical',
2023-06-24 23:30:35 +01:00
variable=self.slider_var,
command=lambda arg: self.on_slider_move(arg),
)
slider.grid(
column=0,
row=0,
)
2025-01-15 12:40:31 +00:00
slider.bind('<Double-Button-1>', self.on_button_double_click)
2023-06-24 23:30:35 +01:00
# create level meter and grid it onto the labelframe
level_meter = ttk.Progressbar(
self.labelframe,
2025-01-15 12:40:31 +00:00
orient='vertical',
2023-06-24 23:30:35 +01:00
variable=self.meter_var,
maximum=72,
2025-01-15 12:40:31 +00:00
mode='determinate',
2023-06-24 23:30:35 +01:00
)
level_meter.grid(column=1, row=0)
# create gainlabel and grid it onto the labelframe
gainlabel = ttk.Label(self.labelframe, textvariable=self.gainlabel_var)
gainlabel.grid(column=0, row=1, columnspan=2)
# create button and grid it onto the labelframe
button = ttk.Button(
self.labelframe,
2025-01-15 12:40:31 +00:00
text='Mute',
style='Mute.TButton',
2023-06-24 23:30:35 +01:00
command=lambda: self.on_button_press(),
)
button.grid(column=0, row=2, columnspan=2, padx=1, pady=2)
# define callbacks
def on_slider_move(self, *args):
val = round(self.slider_var.get(), 1)
2025-01-16 14:51:20 +00:00
self._vm.strip[self.INDEX].gain = val
2023-06-24 23:30:35 +01:00
self.gainlabel_var.set(val)
def on_button_press(self):
self.button_var.set(not self.button_var.get())
2025-01-16 14:51:20 +00:00
self._vm.strip[self.INDEX].mute = self.button_var.get()
2023-06-24 23:30:35 +01:00
self.style.configure(
2025-01-15 12:40:31 +00:00
'Mute.TButton', foreground='#cd5c5c' if self.button_var.get() else '#5a5a5a'
2023-06-24 23:30:35 +01:00
)
2023-06-28 13:57:45 +01:00
def on_button_double_click(self, e):
self.slider_var.set(0)
self.gainlabel_var.set(0)
2025-01-16 14:51:20 +00:00
self._vm.strip[self.INDEX].gain = 0
2023-06-28 13:57:45 +01:00
2023-06-24 23:30:35 +01:00
def _get_level(self):
2025-01-16 14:51:20 +00:00
val = max(self._vm.strip[self.INDEX].levels.postfader)
2023-06-24 23:30:35 +01:00
return 0 if self.button_var.get() else 72 + val - 12
def on_ldirty(self):
self.meter_var.set(self._get_level())
def main():
2025-01-15 12:40:31 +00:00
with voicemeeterlib.api('banana', ldirty=True) as vm:
2023-06-24 23:30:35 +01:00
app = App(vm)
app.mainloop()
2025-01-15 12:40:31 +00:00
if __name__ == '__main__':
2023-06-24 23:30:35 +01:00
main()