Tidal Harmony Python GUI

👤 Sharing: AI
import tkinter as tk
from tkinter import ttk
import random
import time

class TidalHarmonyApp:
    def __init__(self, root):
        self.root = root
        self.root.title("Tidal Harmony")

        self.note_sequence = []
        self.tempo = 120  # Default tempo
        self.is_playing = False

        # --- GUI Elements ---
        self.create_widgets()

    def create_widgets(self):
        # Note Entry
        self.note_label = ttk.Label(self.root, text="Enter Note (A-G, #, b, octave):")
        self.note_label.grid(row=0, column=0, padx=5, pady=5, sticky="w")
        self.note_entry = ttk.Entry(self.root)
        self.note_entry.grid(row=0, column=1, padx=5, pady=5, sticky="ew")

        # Add Note Button
        self.add_note_button = ttk.Button(self.root, text="Add Note", command=self.add_note)
        self.add_note_button.grid(row=0, column=2, padx=5, pady=5)

        # Tempo Adjustment
        self.tempo_label = ttk.Label(self.root, text="Tempo (BPM):")
        self.tempo_label.grid(row=1, column=0, padx=5, pady=5, sticky="w")
        self.tempo_scale = tk.Scale(self.root, from_=30, to=240, orient=tk.HORIZONTAL, command=self.update_tempo)
        self.tempo_scale.set(self.tempo)
        self.tempo_scale.grid(row=1, column=1, padx=5, pady=5, sticky="ew")

        # Play/Stop Button
        self.play_button = ttk.Button(self.root, text="Play", command=self.toggle_play)
        self.play_button.grid(row=2, column=0, padx=5, pady=5)
        self.stop_button = ttk.Button(self.root, text="Stop", command=self.stop_playback)
        self.stop_button.grid(row=2, column=1, padx=5, pady=5)

        # Sequence Display
        self.sequence_label = ttk.Label(self.root, text="Note Sequence:")
        self.sequence_label.grid(row=3, column=0, padx=5, pady=5, sticky="w")
        self.sequence_text = tk.Text(self.root, height=5, width=40, state=tk.DISABLED)
        self.sequence_text.grid(row=4, column=0, columnspan=3, padx=5, pady=5, sticky="ew")

        # Details Button
        self.details_button = ttk.Button(self.root, text="Details", command=self.show_details)
        self.details_button.grid(row=5, column=0, padx=5, pady=5)

        # Configure grid weights to make it responsive
        for i in range(3):  # Columns
            self.root.columnconfigure(i, weight=1)
        for i in range(6):  # Rows
            self.root.rowconfigure(i, weight=0)

    def add_note(self):
        note = self.note_entry.get().strip()
        # Basic validation (can be extended)
        if note:
            self.note_sequence.append(note)
            self.update_sequence_display()
            self.note_entry.delete(0, tk.END)

    def update_tempo(self, tempo):
        self.tempo = int(tempo)

    def toggle_play(self):
        if not self.is_playing:
            self.is_playing = True
            self.play_sequence()
        else:
            self.stop_playback()

    def stop_playback(self):
        self.is_playing = False

    def play_sequence(self):
        if self.is_playing and self.note_sequence:
            for note in self.note_sequence:
                if not self.is_playing: # Check for stop during playback
                    break
                self.play_note(note)
                time.sleep(60 / self.tempo)  # Simulate note duration based on tempo

    def play_note(self, note):
        # Placeholder for actual sound generation
        print(f"Playing note: {note}")
        # To actually play the note, you'd need a library like PyAudio, PySynth, or similar
        # and code to convert the note string into a frequency and play it.


    def update_sequence_display(self):
        self.sequence_text.config(state=tk.NORMAL)
        self.sequence_text.delete("1.0", tk.END)
        self.sequence_text.insert(tk.END, ", ".join(self.note_sequence))
        self.sequence_text.config(state=tk.DISABLED)

    def show_details(self):
        details_window = tk.Toplevel(self.root)
        details_window.title("Tidal Harmony - Details")
        details_text = tk.Text(details_window, height=15, width=60)
        details_text.pack(padx=10, pady=10)
        details_text.insert(tk.END, """
Tidal Harmony is a versatile musical sequencer that allows users to create and play musical sequences by entering notes. It provides control over tempo and allows for adding, removing, and playing notes in real-time.

Key Features:

*   Note Input: Enter musical notes using standard notation (e.g., A4, C#5, Bb3).
*   Tempo Control: Adjust the tempo (beats per minute) to control the speed of the sequence.
*   Play/Stop: Start and stop playback of the sequence.
*   Sequence Display: View the current sequence of notes.
*   Scalable GUI: Uses Tkinter Grid layout with weights for resposiveness.

Further Development:

*   Integration with a sound library (e.g., PyAudio, PySynth) to generate actual audio.
*   More sophisticated note validation.
*   Saving and loading sequences.
*   Advanced sequencing features (e.g., looping, transposing).
*   Different instrument sounds.
""")
        details_text.config(state=tk.DISABLED)

if __name__ == "__main__":
    root = tk.Tk()
    app = TidalHarmonyApp(root)
    root.mainloop()
👁️ Viewed: 5

Comments