Advertisement
j0h

noteScanGUI

j0h
Jul 7th, 2025
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import simpleaudio as sa
  3. import numpy as np
  4. import tkinter as tk
  5. from tkinter import ttk
  6. # sudo apt-get install -y python3-dev libasound2-dev
  7. notes = {
  8.     "C": 261.63, "C#": 277.18, "D": 293.66, "D#": 311.13,
  9.     "E": 329.63, "F": 349.23, "F#": 369.99, "G": 392.00,
  10.     "G#": 415.30, "A": 440.00, "A#": 466.16, "B": 493.88
  11. }
  12.  
  13. buffer = ""
  14.  
  15. def play_note(freq, duration=0.3):
  16.     fs = 44100
  17.     t = np.linspace(0, duration, int(fs * duration), False)
  18.     pitch = pitch_slider.get()
  19.     vol = vol_slider.get() / 100.0
  20.     ch = int(ch_slider.get())
  21.     bps = int(bp_slider.get())
  22.  
  23.     freq *= pitch
  24.     wave = 0.5 * np.sin(2 * np.pi * freq * t)
  25.     wave = wave * (vol * 32767)
  26.     audio = wave.astype(np.int16)
  27.  
  28.     if ch == 2:
  29.         audio = np.column_stack((audio, audio)).flatten()
  30.  
  31.     sa.play_buffer(audio, ch, bps, fs).wait_done()
  32.  
  33. def on_key(event):
  34.     global buffer
  35.     char = event.char.upper()
  36.  
  37.     if event.keysym == "Return":
  38.         note = buffer.strip()
  39.         if note in notes:
  40.             play_note(notes[note])
  41.             status.set(f"Played: {note}")
  42.         else:
  43.             status.set(f"Unknown: {note}")
  44.         buffer = ""  # Clear for next scan
  45.     elif char.isalnum() or char in "#":
  46.         buffer += char
  47.  
  48. # === GUI ===
  49. root = tk.Tk()
  50. root.title("Note Player (Barcode Ready)")
  51.  
  52. main = ttk.Frame(root, padding=10)
  53. main.grid()
  54.  
  55. # Volume
  56. ttk.Label(main, text="Volume (%)").grid(column=0, row=0, sticky="w")
  57. vol_slider = ttk.Scale(main, from_=0, to=100, orient="horizontal")
  58. vol_slider.set(50)
  59. vol_slider.grid(column=1, row=0, columnspan=2, sticky="ew")
  60.  
  61. # Channels
  62. ttk.Label(main, text="Channels (1=Mono, 2=Stereo)").grid(column=0, row=1, sticky="w")
  63. ch_slider = tk.Scale(main, from_=1, to=2, resolution=1, orient="horizontal")
  64. ch_slider.set(1)
  65. ch_slider.grid(column=1, row=1, columnspan=2, sticky="ew")
  66.  
  67. # Bits per sample
  68. ttk.Label(main, text="Bits per Sample (1–4)").grid(column=0, row=2, sticky="w")
  69. bp_slider = tk.Scale(main, from_=1, to=4, resolution=1, orient="horizontal")
  70. bp_slider.set(2)
  71. bp_slider.grid(column=1, row=2, columnspan=2, sticky="ew")
  72.  
  73. # Pitch bend
  74. ttk.Label(main, text="Pitch Bend (0.5–2.0)").grid(column=0, row=3, sticky="w")
  75. pitch_slider = tk.Scale(main, from_=0.5, to=2.0, resolution=0.01, digits=4,
  76.                         orient="horizontal", length=200)
  77. pitch_slider.set(1.0)
  78. pitch_slider.grid(column=1, row=3, columnspan=2, sticky="ew")
  79.  
  80. # Status
  81. status = tk.StringVar()
  82. ttk.Label(main, textvariable=status, foreground="blue").grid(column=0, row=4, columnspan=3, sticky="w")
  83.  
  84. main.columnconfigure(1, weight=1)
  85.  
  86. # Bind keys globally
  87. root.bind_all("<Key>", on_key)
  88.  
  89. root.mainloop()
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement