Skip to content

Commit

Permalink
Update drum3x.py
Browse files Browse the repository at this point in the history
  • Loading branch information
RyoK3N authored Oct 18, 2024
1 parent df28673 commit afcc5cb
Showing 1 changed file with 16 additions and 31 deletions.
47 changes: 16 additions & 31 deletions drum3x/drum3x.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@

# Prevent Pygame from initializing the display module
os.environ["SDL_VIDEODRIVER"] = "dummy"

# Initialize Pygame mixer for audio handling
# Pygame mixer initialization
pygame.mixer.pre_init(frequency=44100, size=-16, channels=2, buffer=512)
pygame.mixer.init()

# Load the C library
# Loading the compiled C library
if sys.platform.startswith("win"):
lib = cdll.LoadLibrary("drum3x.dll")
else:
lib = cdll.LoadLibrary("./libdrum3x.so")

# Set up function prototypes
# Setting up the C function prototypes
lib.start_recording.restype = None
lib.stop_recording.restype = None
lib.record_beat.argtypes = [c_int]
Expand All @@ -44,7 +43,7 @@
root.title("Drum3x")
root.configure(bg="#0f0f0f") # Dark background

# Prepare the beats
# Preparing the beats
beat_filenames = [
"Bass_Drum_Comb.wav",
"Bass_Drum_Driven12.wav",
Expand All @@ -57,7 +56,7 @@
"SD_Snare_Drum_092.wav",
]

# Load beat sounds using Pygame
# Loading beats
beat_sounds = []
for filename in beat_filenames:
filepath = os.path.join("beats", filename)
Expand All @@ -70,18 +69,14 @@


def play_beat(beat_id):
# Play the beat sound
beat_sounds[beat_id].play()
# Record the beat
lib.record_beat(c_int(beat_id))
# Animate the button
animate_button(beat_id)


def animate_button(beat_id):
# Change the button style to indicate it's pressed
# Change the button style to indicate if it's pressed
buttons[beat_id].config(style="Neon.TButton")
# Schedule to revert the button style after a short delay
root.after(100, lambda: buttons[beat_id].config(style="Dark.TButton"))


Expand Down Expand Up @@ -115,19 +110,16 @@ def playback_function():
beat_id = lib.get_recorded_beat_id(c_int(i))
timestamp = (
lib.get_recorded_beat_timestamp(c_int(i)) / 1000.0
) # Convert to seconds
# Sleep until the time to play the beat
)
time_to_wait = (start_time + timestamp) - time.time()
if time_to_wait > 0:
time.sleep(time_to_wait)
# Play the beat
beat_sounds[beat_id].play()
# Animate the button
animate_button(beat_id)
status_label.config(text="Playback finished")


# Bind keys to beats
# Binding keys
key_bindings = ["q", "w", "e", "a", "s", "d", "z", "x", "c"]


Expand All @@ -139,11 +131,9 @@ def key_pressed(event):

root.bind("<KeyPress>", key_pressed)

# Create styles for the buttons
style = ttk.Style()
style.theme_use("clam") # Use a theme that allows style customization
style.theme_use("clam")

# Dark theme button style
style.configure(
"Dark.TButton",
background="#1f1f1f",
Expand All @@ -157,10 +147,9 @@ def key_pressed(event):
)
style.map("Dark.TButton", background=[("active", "#2f2f2f")])

# Neon highlight style
style.configure(
"Neon.TButton",
background="#00ff00", # Bright green
background="#00ff00", #Bright Green
foreground="black",
borderwidth=2,
focusthickness=3,
Expand All @@ -170,7 +159,7 @@ def key_pressed(event):
relief="flat",
)

# Create buttons
# Buttons
buttons = []
for i in range(9):
row = i // 3
Expand All @@ -182,7 +171,7 @@ def key_pressed(event):
button.grid(row=row, column=col, padx=5, pady=5, sticky="nsew")
buttons.append(button)

# Make grid cells expand equally
# Grid
for i in range(3):
root.grid_rowconfigure(i, weight=1)
root.grid_columnconfigure(i, weight=1)
Expand All @@ -206,16 +195,15 @@ def key_pressed(event):
)
play_button.grid(row=0, column=2, padx=5)

# Status label
status_label = ttk.Label(root, text="Ready", background="#0f0f0f", foreground="white")
status_label.grid(row=4, column=0, columnspan=3)

# Real-time computation monitoring window with CPU usage graph
# Real-time CPU usage
cpu_window = tk.Toplevel(root)
cpu_window.title("System Performance Monitor")
cpu_window.configure(bg="#0f0f0f")

# Create a figure for the CPU usage graph
# CPU usage
fig, ax = plt.subplots(facecolor="#0f0f0f")
fig.patch.set_facecolor("#0f0f0f")
ax.set_facecolor("#0f0f0f")
Expand All @@ -239,14 +227,13 @@ def update_cpu_usage(i):
cpu_percent = psutil.cpu_percent(interval=None)
cpu_usage_data.append(cpu_percent)
time_data.append(current_time)
# Limit data to last 60 seconds
if len(time_data) > 60:
time_data.pop(0)
cpu_usage_data.pop(0)
ax.clear()
ax.plot(
time_data, cpu_usage_data, color="#00ff00", linewidth=2
) # Bright green line
)
ax.set_ylim(0, 100)
ax.set_ylabel("CPU Usage (%)", color="white", fontweight="bold")
ax.set_xlabel("Time (s)", color="white", fontweight="bold")
Expand All @@ -261,8 +248,6 @@ def update_cpu_usage(i):
# Add grid lines
ax.grid(True, color="#2f2f2f")

# Add a gaming-themed background or overlay
# (Note: Adding images requires additional resources and may not be practical in this context)


canvas = FigureCanvasTkAgg(fig, master=cpu_window)
Expand Down

0 comments on commit afcc5cb

Please sign in to comment.