Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import serial
- import numpy as np
- import time
- import threading
- import random
- import socket
- import re
- import math
- from pynput import mouse
- from pynput import keyboard
- import matplotlib.pyplot as plt
- import configparser
- config = configparser.ConfigParser()
- # Serial settings
- serial_port = "COM4" # Replace with the correct COM port
- baud_rate = 576000
- # Monitor settings
- monitor = {
- "left": 1400,
- "top": 1130,
- "width": 1060, # Adjust to your screen resolution
- "height": 10, # Thin strip for screen capture
- }
- # customizable
- beat_color = (200, 200, 200) #RGB format
- scratch_color = (255,127,80) #why the fuck is this blue
- red_tap = (255, 0, 255) #RGB
- blue_tap = (0, 255, 130) #RGB
- left_spin_color = (0, 255, 0) #RGB format
- right_spin_color = (253, 0, 228) #RGB format
- beat_background = False #Legacy
- beat_boost = 20 #for beat_background only. tells how much to increase brightness on beat by. Also ignores maxbrightness
- saturation_boost = 2 #does this work?
- strip_length = 600 #how many LEDs are on the strip
- spin_animation_speed = 1 #how many pixels each frame jumps
- max_brightness = 200 #max brightness for everything on the strip
- dampening_factor = 0.9 #mm damp. this might not work
- tap_size = 200 #size of tap in pixels
- #non-customizable
- overlay_lock = threading.Lock()
- brightfactor = max_brightness / 255 #
- left_spin_color = (left_spin_color[0]*brightfactor, left_spin_color[1]*brightfactor, left_spin_color[2]*brightfactor) #equally dampens rgb of spin effect to ensure it is under the maxbrightness
- right_spin_color = (right_spin_color[0]*brightfactor, right_spin_color[1]*brightfactor, right_spin_color[2]*brightfactor)
- tap_size = int(tap_size / 2) # halfing because logic will double tap size
- #initializing values
- previous_colors = [(0, 0, 0)] * strip_length
- overlay_colors = [(0, 0, 0)] * strip_length
- livetime = time.time
- start_time = 1
- active_effects = []
- current_time = 1
- sync_frame = bytearray([255, 0, 0, 255]) # for syncing with ESP32
- spinning_l = False
- spinning_r = False
- colors = [[0,0,0] for _ in range(strip_length)]
- # Track the last time we updated the smoothed colors
- last_update_time = time.time()
- last_smooth_colors = [] # Stores the last smoothed colors
- m = 0
- t = 0
- osc_damp = 1 #dampen rate for parabola
- osc_freq = 20 #oscillation frequency for parabola
- data = ""
- data_prev = "a"
- notes = []
- temp_spin_color = [[0,0,0] for _ in range(strip_length)]
- temp_hold_blue_color = [[0,0,0] for _ in range(strip_length)]
- temp_hold_red_color = [[0,0,0] for _ in range(strip_length)]
- match_index = (strip_length / 9)
- s_down = True #for scratches. makes effect bounce
- s = 1
- v = time.time()
- n = 0#for counting time AFTER blue hold has ended (fade out effect)
- u = 0#for counting time AFTER red hold has ended (fade out effect)
- spinright = time.time()
- spinleft = time.time()
- red_match_timer = 100000
- blue_match_timer = 100000
- strip_length
- spinstart = time.time()
- beathold = False
- frequency = 1
- holdstart = False
- tempb = 0.2 #for holds to store decreasing value on fade out effect
- tempr = 0.2
- #calcing random scratch colors
- scratch_min = (min(255, int((scratch_color[0] / 10) * 6)),
- min(255, int((scratch_color[1] / 10) * 6)),
- min(255, int((scratch_color[2] / 10) * 6)))
- scratch_max = (min(255, int((scratch_color[0] / 10) * 12)),
- min(255, int((scratch_color[1] / 10) * 12)),
- min(255, int((scratch_color[2] / 10) * 12)))
- def update_colors():
- global red_tap, blue_tap, left_spin_color, right_spin_color, scratch_color, beat_color, beat_boost, spin_animation_speed, max_brightness, tap_size
- config.read('config.ini')
- red_tap = (int(config.get('Red Tap', 'B', fallback=100)), int(config.get('Red Tap', 'G', fallback=100)), int(config.get('Red Tap', 'R', fallback=100)))
- blue_tap = (int(config.get('Blue Tap', 'B', fallback=100)), int(config.get('Blue Tap', 'G', fallback=100)), int(config.get('Blue Tap', 'R', fallback=100)))
- left_spin_color = (int(config.get('Left Spin', 'B', fallback=100)), int(config.get('Left Spin', 'G', fallback=100)), int(config.get('Left Spin', 'R', fallback=100)))
- right_spin_color = (int(config.get('Right Spin', 'B', fallback=100)), int(config.get('Right Spin', 'G', fallback=100)), int(config.get('Right Spin', 'R', fallback=100)))
- scratch_color = (int(config.get('Scratch', 'B', fallback=100)), int(config.get('Scratch', 'G', fallback=100)), int(config.get('Scratch', 'R', fallback=100)))
- beat_color = (int(config.get('Beat', 'B', fallback=100)), int(config.get('Beat', 'G', fallback=100)), int(config.get('Beat', 'R', fallback=100)))
- beat_boost = int(config.get('Other Effects', 'Beat Boost', fallback = 20))
- spin_animation_speed = int(config.get('Other Effects', 'Spin Animation Speed', fallback = 20)) / 20
- max_brightness = int(config.get('Other Effects', 'Max Brightness' , fallback = 200))
- tap_size = int(config.get('Other Effects', 'Tap Size' , fallback = 200))
- def on_press(key):
- global blue_match_timer, red_match_timer, spinning_l, spinning_r, scratch, start_spintime, current_value, loop, start_time, livetime
- try:
- if key.char == '1': # Check if '1' is pressed
- var = 1
- for effect in active_effects:
- if effect["start_index"] == 300 and effect["effect_id"] == 5 or effect["start_index"] == 300 and effect["effect_id"] == 6:
- var = effect["effect_var"] + 1
- print(var)
- active_effects.remove(effect)
- active_effects.append({
- "start_index": 300,
- "start_time": time.time(),
- "effect_id": 5,
- "effect_var": var
- })
- blue_match_timer = time.time()
- print("match hit")
- if key.char == '2': # Check if '1' is pressed
- var = 1
- for effect in active_effects:
- if effect["start_index"] == 300 and effect["effect_id"] == 5 or effect["start_index"] == 300 and effect["effect_id"] == 6:
- var = effect["effect_var"] + 1
- active_effects.remove(effect)
- active_effects.append({
- "start_index": 300,
- "start_time": time.time(),
- "effect_id": 6,
- "effect_var": var
- })
- red_match_timer = time.time()
- print("match hit")
- if key.char == '3': # Check if '1' is pressed
- active_effects.append({
- "start_index": 300,
- "start_time": time.time(),
- "effect_id": 3,
- "effect_var": 20 #osc_freq
- })
- print("tap hit")
- if key.char == '4': # Check if '1' is pressed
- active_effects.append({
- "start_index": 300,
- "start_time": time.time(),
- "effect_id": 4,
- "effect_var": 20 #osc_freq
- })
- print("tap hit")
- if key.char == '5': # Check if '1' is pressed
- active_effects.append({
- "start_index": strip_length,
- "start_time": time.time(),
- "effect_id": 1,
- "effect_var": ease_out(0, strip_length, t)
- })
- print("spin hit")
- #print("Starting left spin")
- remove_note("SpinLeftStart")
- scratch = False
- loop = False #for ease out
- if key.char == '6': # Check if '1' is pressed
- active_effects.append({
- "start_index": strip_length,
- "start_time": time.time(),
- "effect_id": 2,
- "effect_var": ease_out(0, strip_length, t)
- })
- print("spin hit")
- #print("Starting right spin")
- remove_note("SpinRightStart")
- scratch = False
- loop = False #for ease out
- if key.char == '7': # Check if '1' is pressed
- active_effects.append({
- "start_index": 300,
- "start_time": time.time(),
- "effect_id": 8,
- "effect_var": tap_size
- })
- print("hold hit")
- #print("Starting right spin")
- remove_note("SpinRightStart")
- scratch = False
- loop = False #for ease out
- if key.char == '8':
- start_time = livetime() + 0.5
- int(start_time)
- except AttributeError:
- print(f"Special key {key} pressed")
- listener = keyboard.Listener(
- on_press=on_press)
- listener.start()
- def port_connect():
- global data
- host = '127.0.0.1' # Same as C# server
- port = 8008 # Same port as C# server
- # Create a socket and connect to the server
- #print("attempgin connection")
- client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- #print("attempgin connection 2")
- client_socket.connect((host, port))
- #print("Connected to C# server.")
- #try:
- while True:
- # Receive data
- data = client_socket.recv(1024).decode('utf-8')
- print(f"Received: {data}")
- str(data)
- add_note(data)
- data = ""
- #except KeyboardInterrupt:
- #print("Disconnected.")
- def add_note(note):
- global notes
- notes.append(note)
- def remove_note(name):
- global notes
- notes = [note for note in notes if not note.startswith(name)]
- def apply_overlay(main):
- """Combine the main colors with the overlay buffer."""
- with overlay_lock:
- # Merge overlay effects with main colors
- combined_colors = [
- overlay if overlay != (0, 0, 0) else smoothed_colors
- for overlay, smoothed_colors in zip(overlay_colors, main)
- ]
- return combined_colors
- def on_beat():
- global start_time, beathold
- """Handle key press events."""
- if any("DrumStart" in note for note in notes):
- for note in notes:
- if "DrumStart" and "Beatholdtrue" in note:
- beathold = True
- print("bveathold!")
- start_time = livetime() + 0.5
- int(start_time)
- ##print("Beat! Brightening all LEDs.")
- #print("beat triggered")
- remove_note("DrumStart")
- def beat_end():
- global start_time, beathold
- if any("DrumEnd" in note for note in notes):
- start_time = livetime() + 0.5
- int(start_time)
- beathold = False
- print("beat ended!")
- remove_note("DrumEnd")
- def on_hold():
- global holdstart
- if any("Blue HoldStart" in note for note in notes):
- for note in notes:
- if "Blue HoldStart" and "Holdtrue" in note:
- holdstart = True
- match = re.search(r"Blue HoldStart \((-?\d+)\)", note)
- try:
- index = int((int(match.group(1)) + 4) * match_index)
- except:
- index = 0
- active_effects.append({
- "start_index": index,
- "start_time": time.time(),
- "effect_id": 8,
- "effect_var": 0,
- "xvalue": 0,
- "yvalue": 0,
- "end" : False
- })
- remove_note("Blue HoldStart")
- elif "Blue HoldStart" in note:
- match = re.search(r"Blue HoldStart \((-?\d+)\)", note)
- try:
- index = int((int(match.group(1)) + 4) * match_index)
- except:
- index = 0
- active_effects.append({
- "start_index": index,
- "start_time": time.time(),
- "effect_id": 8,
- "effect_var": 0,
- "xvalue": 0,
- "yvalue": 0,
- "end" : False
- })
- remove_note("Blue HoldStart")
- if any("Red HoldStart" in note for note in notes):
- for note in notes:
- if "Red HoldStart" and "Holdtrue" in note:
- holdstart = True
- match = re.search(r"Red HoldStart \((-?\d+)\)", note)
- try:
- index = int((int(match.group(1)) + 4) * match_index)
- except:
- index = 0
- active_effects.append({
- "start_index": index,
- "start_time": time.time(),
- "effect_id": 9,
- "effect_var": 0,
- "xvalue": 0,
- "yvalue": 0,
- "end" : False
- })
- remove_note("Red HoldStart")
- elif "Red HoldStart" in note:
- match = re.search(r"Red HoldStart \((-?\d+)\)", note)
- try:
- index = int((int(match.group(1)) + 4) * match_index)
- except:
- index = 0
- active_effects.append({
- "start_index": index,
- "start_time": time.time(),
- "effect_id": 9,
- "effect_var": 0,
- "xvalue": 0,
- "yvalue": 0,
- "end" : False
- })
- remove_note("Red HoldStart")
- def on_sectioncontinuationorend():
- global holdstart
- if any("SectionContinuationOrEnd" in note for note in notes):
- for note in notes:
- if "SectionContinuationOrEnd" and "Holdtrue" in note:
- return
- else:
- print("ending hold")
- holdstart = False
- def on_release():
- global start_time
- if "IsDrum" in notes:
- start_time = time.time()
- #print("Beat release")
- def on_scratch():
- global scratch
- if any("ScratchStart" in note for note in notes):
- #print("scratch triggered")
- active_effects.append({
- "start_index": 0,
- "start_time": 0,
- "effect_id": 7
- })
- scratch = True
- remove_note("ScratchStart")
- def on_continue():
- if any("SectionContinuationOrEnd" in note for note in notes):
- for note in notes:
- if "SectionContinuationOrEnd" in note:
- remove_note("SectionContinuationOrEnd")
- if any("Blue HoldStart" in note for note in notes):
- for note in notes:
- if "Blue HoldStart" in note:
- remove_note("Blue HoldStart")
- if any("Red HoldStart" in note for note in notes):
- for note in notes:
- if "Red HoldStart" in note:
- remove_note("Red HoldStart")
- if any("Red Match" in note for note in notes):
- for note in notes:
- if "Red Match" in note:
- remove_note("Red Match")
- if any("Blue Match" in note for note in notes):
- for note in notes:
- if "Blue Match" in note:
- remove_note("Blue Match")
- def on_match():
- global scratch, blue_match_timer, red_match_timer, active_effects
- if any("Blue Match" in note for note in notes):
- var = 1
- for note in notes:
- if "Blue Match" in note:
- match = re.search(r"Blue Match \((-?\d+)\)", note)
- #print("blue match triggered")
- try:
- index = (int(match.group(1)) + 4) * match_index
- for effect in active_effects:
- if effect["start_index"] == index and effect["effect_id"] == 5 or effect["start_index"] == index and effect["effect_id"] == 6:
- var = effect["effect_var"] + 1
- active_effects.remove(effect)
- except:
- index = 0
- active_effects.append({
- "start_index": index,
- "start_time": time.time(),
- "effect_id": 5,
- "effect_var": var
- })
- remove_note("Blue Match")
- scratch = False
- blue_match_timer = time.time()
- #print(f"blue match activated{note}")
- if any("Red Match" in note for note in notes):
- for note in notes:
- if "Red Match" in note:
- var = 1
- match = re.search(r"Red Match \((-?\d+)\)", note)
- #print("red match triggered")
- try:
- index = (int(match.group(1)) + 4) * match_index
- for effect in active_effects:
- if effect["start_index"] == index and effect["effect_id"] == 5 or effect["start_index"] == index and effect["effect_id"] == 6:
- var = effect["effect_var"] + 1
- active_effects.remove(effect)
- except:
- index = 0
- active_effects.append({
- "start_index": index,
- "start_time": time.time(),
- "effect_id": 6,
- "effect_var": var
- })
- remove_note("Red Match")
- scratch = False
- red_match_timer = time.time()
- #print(f"blue match activated{note}")
- def spin_start():
- global spinning_l, spinning_r, effects_to_remove, scratch, current_value, start_spintime, loop, active_effects
- if any("SpinLeftStart" in note for note in notes):
- active_effects.append({
- "start_index": strip_length,
- "start_time": time.time(),
- "effect_id": 1,
- "effect_var": ease_out(spinstart, strip_length, t)
- })
- spinning_l = True
- spinning_r = False
- #print("Starting left spin")
- remove_note("SpinLeftStart")
- current_value = ease_out(spinstart, strip_length, t)
- start_spintime = time.time() #for ease out
- scratch = False
- loop = False #for ease out
- #setting matches to 0
- for effect in active_effects:
- if effect["effect_id"] == 3 or effect["effect_id"] == 4:
- effects_to_remove.append(effect)
- #print(effect)
- if any("SpinRightStart" in note for note in notes):
- active_effects.append({
- "start_index": strip_length,
- "start_time": time.time(),
- "effect_id": 2,
- "effect_var": ease_out(spinstart, strip_length, t)
- })
- #print("Starting right spin")
- remove_note("SpinRightStart")
- current_value = ease_out(spinstart, strip_length, t)
- start_spintime = time.time() #for ease out
- scratch = False
- loop = True #for ease out
- #setting matches to 0
- for effect in active_effects:
- if effect["effect_id"] == 3 or effect["effect_id"] == 4:
- effects_to_remove.append(effect)
- #print(effect)
- return # Exit the function as we've already triggered the action
- def tap_start():
- global active_effects, strip_length, scratch, osc_freq
- if any("Blue Tap" in note for note in notes):
- for note in notes:
- if "Blue Tap" in note:
- # Add the new effect with its start index and start time
- match = re.search(r"Blue Tap \((-?\d+)\)", note)
- remove_note("Blue Tap")
- try:
- index = int((int(match.group(1)) + 4) * match_index)
- index = int(index)
- except:
- index = 0
- index = random.randint(index - 20, index + 20)
- active_effects.append({
- "start_index": index,
- "start_time": time.time(),
- "effect_id": 3,
- "effect_var": 20 #osc_freq
- })
- #print(note)
- scratch = False
- ##print(active_effects)
- if any("Red Tap" in note for note in notes):
- for note in notes:
- if "Red Tap" in note:
- # Calculate a unique start index for this effect
- # Add the new effect with its start index and start time
- match = re.search(r"Red Tap \((-?\d+)\)", note)
- remove_note("Red Tap")
- try:
- index = int((int(match.group(1)) + 4) * match_index)
- index = int(index)
- except:
- index = 0
- index = random.randint(index - 20, index + 20)
- active_effects.append({
- "start_index": index,
- "start_time": time.time(),
- "effect_id": 4,
- "effect_var": 20 #osc_freq
- })
- #print(note)
- scratch = False
- def sine_wave(x, amplitude, frequency, phase=0):
- return amplitude * np.sin(2 * np.pi * frequency * x + phase)
- def oscillating_parabola(x, osc_damp, osc_freq):
- normalized_x = x / tap_size # Normalize x to the range [-50, 50]. this determines the range of the tap
- unscaled = (1 - normalized_x**2) * np.exp(-osc_damp * np.abs(normalized_x)) * np.cos(osc_freq * normalized_x)
- return 127 * unscaled # Scale to make max value 255
- def get_y_coords_for_index(i):
- # Ensure i is within the valid range
- if 0 <= i < len(coordinates):
- #print(i)
- x, y = coordinates[i] # Get the (x, y) pair at index i
- #print(x)
- return y
- else:
- raise ValueError(f"i is out of range{i}")
- def beats():
- global colors, start_time
- live = livetime()
- int(live)
- time_diff = max(start_time - live, 0)
- if beat_background == True:
- for i in range(strip_length):
- index = i
- r, g, b = colors[i][2], colors[i][1], colors[i][0]
- r = min(max(int(r, (time_diff*beat_boost), 0)))
- g = min(max(int(g, (time_diff*beat_boost), 0)))
- b = min(max(int(b, (time_diff*beat_boost), 0)))
- r = min(max(int(r), 0), 255)
- g = min(max(int(g), 0), 255)
- b = min(max(int(b), 0), 255)
- colors[index] = (r, g, b)
- elif beathold == True:
- for i in range(strip_length):
- index = i
- r, g, b = colors[i][2], colors[i][1], colors[i][0]
- increase_r = beat_boost - r
- increase_g = beat_boost - g
- increase_b = beat_boost - b
- max_increase = max(increase_r, increase_g, increase_b)
- # Apply the same increase to all color components
- r = min(max(int(r + max_increase), 0), 255)
- g = min(max(int(g + max_increase), 0), 255)
- b = min(max(int(b + max_increase), 0), 255)
- colors[index] = (r, g, b)
- else:
- for i in range(strip_length):
- index = i
- r, g, b = colors[i][2], colors[i][1], colors[i][0]
- # Calculate the increase for each color component
- increase_r = time_diff * beat_boost - r
- increase_g = time_diff * beat_boost - g
- increase_b = time_diff * beat_boost - b
- # Determine the maximum increase
- max_increase = max(increase_r, increase_g, increase_b)
- max_increase = max(max_increase, 0)
- # Apply the same increase to all color components
- r = min(max(int(r + max_increase), 0), 255)
- g = min(max(int(g + max_increase), 0), 255)
- b = min(max(int(b + max_increase), 0), 255)
- colors[index] = (r, g, b)
- return colors
- def ease_out(spinstart, strip_length, t):
- return strip_length - (strip_length - spinstart) * math.exp(-spin_animation_speed * t) #first value controls speed, 2nd controls difference in speed of start of animation compared to the end
- def send_colors(colors, ser):
- #try:
- with overlay_lock: # Ensure exclusive access to the serial port
- # Send the sync frame first
- ser.write(sync_frame)
- max_color = max(colors, key=lambda c: sum(c)) # Find the brightest color based on sum of RGB
- print("Brightest color:", max_color)
- # Send the LED data
- data = bytearray(np.clip(np.array(colors), 0, 255).astype(np.uint8).flatten())
- ser.write(data)
- ##print(f"Sync frame sent: {len(sync_frame)} bytes, LED data sent: {len(data)} bytes")
- #except Exception as e:
- #print(f"Error sending data: {e}")
- def update_effects():
- global active_effects, effects_to_remove, overlay_colors, overlay_lock, tempb, tempr, strip_length,temp_hold_blue_color,temp_hold_red_color, blue_match, red_match, spinning_l, spinning_r, loop, m , q, u, n, t, s, d, e, f, v, osc_freq, effects_to_remove, colors, spinright, spinleft, previous_colors, s_down, scratch, blue_match_timer, red_match_timer, current_value, x_values, y_values, coordinates, holdstart
- active_effects.sort(key=lambda effect: effect["effect_id"])
- current_time = time.time()
- blue_match = False
- red_match = False
- with overlay_lock:
- # Reset overlay colors
- overlay_colors = [(0, 0, 0)] * strip_length
- for effect in active_effects:
- elapsed_time = current_time - effect["start_time"]
- start_index = int(effect["start_index"])
- if effect["effect_id"] == 1: # LEFT SPIN
- current_spintime = time.time()
- elapsed_spintime = current_spintime - effect["start_time"]
- t = elapsed_spintime
- effect["effect_var"] = math.ceil(ease_out(0, strip_length * 2, t))
- if effect["effect_var"] < strip_length:
- for i in range(0 , int(effect["effect_var"])):
- ##print(current_value)
- index = i
- #print(i)
- mod = effect["effect_var"] - i
- mod = (mod / effect["effect_var"])
- #mod = (i / strip_length)
- mod = max(0, (255 * mod) - i)
- r, g, b = left_spin_color
- r = max(0, r - mod)
- g = max(0, g - mod)
- b = max(0, b - mod)
- #print(r,g,b)
- #time.sleep(0.1)
- temp_spin_color[index] = (r,g,b)
- #compare old colors and keep highest value
- colors[index] = (r, g, b)
- elif effect["effect_var"] >= strip_length:
- #print(effect["effect_var"])
- effect["start_index"] = effect["start_index"] + spin_animation_speed * 5
- #print(effect["start_index"])
- for i in range(0 , strip_length):
- ##print(current_value)
- #r, g, b = temp_spin_color[index][0], temp_spin_color[index][1], temp_spin_color[index][2]
- index = int(effect["start_index"] - strip_length + i)
- if 0 <= index < strip_length:
- r, g, b = temp_spin_color[i][0], temp_spin_color[i][1], temp_spin_color[i][2]
- colors[index] = (r, g, b)
- #print(index)
- #temp_spin_color[index] = (r,g,b)
- if index >= strip_length: # Only clear LEDs that move off the strip666666666666667
- colors[min(strip_length - 1, index - strip_length)] = (0, 0, 0)
- #previous_colors[i] = (r,g,b)
- #colors[i] = (0, 0, 0)
- if effect["start_index"] > strip_length * 2:
- print("true")
- effects_to_remove.append(effect)
- #print("spin ended")
- break
- if effect["effect_id"] == 2: #RIGHT SPIN
- current_spintime = time.time()
- elapsed_spintime = current_spintime - effect["start_time"]
- t = elapsed_spintime
- effect["effect_var"] = math.ceil(ease_out(0, strip_length * 2, t))
- if effect["effect_var"] < strip_length:
- for i in range(0 , int(effect["effect_var"])):
- ##print(current_value)
- index = i
- #print(i)
- mod = effect["effect_var"] - i
- mod = (mod / effect["effect_var"])
- #mod = (i / strip_length)
- mod = max(0, (255 * mod) - i)
- r, g, b = right_spin_color
- r = max(0, r - mod)
- g = max(0, g - mod)
- b = max(0, b - mod)
- #print(r,g,b)
- #time.sleep(0.1)
- temp_spin_color[index] = (r,g,b)
- #compare old colors and keep highest value
- colors[index] = (r, g, b)
- elif effect["effect_var"] >= strip_length:
- #print(effect["effect_var"])
- effect["start_index"] = effect["start_index"] + spin_animation_speed * 5
- #print(effect["start_index"])
- for i in range(0 , strip_length):
- ##print(current_value)
- #r, g, b = temp_spin_color[index][0], temp_spin_color[index][1], temp_spin_color[index][2]
- index = int(effect["start_index"] - strip_length + i)
- if 0 <= index < strip_length:
- r, g, b = temp_spin_color[i][0], temp_spin_color[i][1], temp_spin_color[i][2]
- colors[index] = (r, g, b)
- #print(index)
- #temp_spin_color[index] = (r,g,b)
- if index >= strip_length: # Only clear LEDs that move off the strip666666666666667
- colors[min(strip_length - 1, index - strip_length)] = (0, 0, 0)
- #previous_colors[i] = (r,g,b)
- #colors[i] = (0, 0, 0)
- if effect["start_index"] > strip_length * 2:
- print("true")
- effects_to_remove.append(effect)
- #print("spin ended")
- break
- if effect["effect_id"] == 5 and (blue_match_timer + 1) <= current_time: #blue match
- effects_to_remove.append(effect)
- if effect["effect_id"] == 5 and blue_match == False:
- for i in range(max(0, start_index - effect["effect_var"] * 5), min(strip_length, start_index + effect["effect_var"] * 5)):
- r, g, b = colors[i][0], colors[i][1], colors[i][0]
- r = abs(r - 255)
- g = abs(g - 255)
- b = abs(b - 255)
- colors[i] = (r, g, b)
- blue_match = True
- if effect["effect_id"] == 6 and (red_match_timer + 1) <= current_time: #red match
- effects_to_remove.append(effect)
- if effect["effect_id"] == 6:
- for i in range(max(0, start_index - effect["effect_var"] * 5), min(strip_length, start_index + effect["effect_var"] * 5)):
- r, g, b = colors[i][0], colors[i][1], colors[i][0]
- r = abs(r - 255)
- g = abs(g - 255)
- b = abs(b - 255)
- colors[i] = (r, g, b)
- red_match = True
- if effect["effect_id"] == 3: # BLUE TAP
- inverse = 1 - elapsed_time # timer
- #logic for sine wave
- osc_freq = effect["effect_var"]
- effect["effect_var"] = effect["effect_var"] / 1.5
- x_values = np.arange(-tap_size, tap_size, 1) #size of tap
- y_values = [oscillating_parabola(x, osc_damp, osc_freq) for x in x_values]
- coordinates = [(int(x), int(oscillating_parabola(x, osc_damp, osc_freq))+ 127) for x in x_values]
- for i in range(max(0, start_index - tap_size), min(strip_length, start_index + tap_size)):
- #getting old colors
- x, y, z = colors[i][0], colors[i][1], colors [i][2]
- #logic for the main tap brightness
- iterate = i - start_index + tap_size
- brightness = get_y_coords_for_index(iterate)
- r, g, b = blue_tap
- r = (r / 255) * brightness
- g = (g / 255) * brightness
- b = (b / 255) * brightness
- #logic for fadeout effect
- r, g, b = [int(c * brightness / 255) for c in blue_tap]
- r = max(0, r - abs((i - start_index) * 2)) * inverse #the number controls the size of the tap. lower == bigger
- g = max(0, g - abs((i - start_index) * 2)) * inverse
- b = max(0, b - abs((i - start_index) * 2)) * inverse
- #compare old colors against new colors
- if x >= r:
- r = x
- if y >= g:
- g = y
- if z >= b:
- b = z
- #send colors to array
- colors[i] = (r, g, b)
- osc_freq = 20 # resets freq for next tap
- if current_time - effect["start_time"] > 1:
- effects_to_remove.append(effect)
- if effect["effect_id"] == 4: # Red TAP
- inverse = 1 - elapsed_time # timer
- #logic for sine wave
- osc_freq = effect["effect_var"]
- effect["effect_var"] = effect["effect_var"] / 1.5
- x_values = np.arange(-tap_size, tap_size, 1) #size of tap
- y_values = [oscillating_parabola(x, osc_damp, osc_freq) for x in x_values]
- coordinates = [(int(x), int(oscillating_parabola(x, osc_damp, osc_freq))+ 127) for x in x_values]
- for i in range(max(0, start_index - tap_size), min(strip_length, start_index + tap_size)):
- #getting old colors
- x, y, z = colors[i][0], colors[i][1], colors [i][2]
- #logic for the main tap brightness
- iterate = i - start_index + tap_size
- brightness = get_y_coords_for_index(iterate)
- r, g, b = red_tap
- r = (r / 255) * brightness
- g = (g / 255) * brightness
- b = (b / 255) * brightness
- #logic for fadeout effect
- r, g, b = [int(c * brightness / 255) for c in red_tap]
- r = max(0, r - abs((i - start_index) * 2)) * inverse #the number controls the size of the tap. lower == bigger
- g = max(0, g - abs((i - start_index) * 2)) * inverse
- b = max(0, b - abs((i - start_index) * 2)) * inverse
- #compare old colors against new colors
- if x >= r:
- r = x
- if y >= g:
- g = y
- if z >= b:
- b = z
- #send colors to array
- colors[i] = (r, g, b)
- osc_freq = 20 # resets freq for next tap
- if current_time - effect["start_time"] > 1:
- effects_to_remove.append(effect)
- if effect["effect_id"] == 8: # Blue Hold
- inverse = 1 - (n - current_time)
- frequency = 10
- x_values = np.arange(effect["effect_var"], (effect["effect_var"] + tap_size), 1)
- #print(x_values)
- effect["xvalue"] = np.concatenate([x_values, x_values[::-1]])
- effect["effect_var"] = effect["effect_var"] + 5
- amplitude = 127 # Example amplitude
- frequency = 0.04 # Example frequency
- effect["yvalue"] = sine_wave(effect["xvalue"], amplitude, frequency)
- iterate = 0
- if holdstart == True and effect["end"] == False:
- tempb = min(tempb * 2, 1)
- for i in range(max(0, start_index - tap_size), min(strip_length, start_index + tap_size)):
- #capture old colors
- x, y, z = colors[i][0], colors[i][1], colors [i][2]
- #logic for the main tap brightness
- brightness = effect["yvalue"][iterate]
- brightness = (brightness + 127) * tempb
- iterate = iterate + 1
- print(brightness)
- r, g, b = blue_tap
- r = (r / 255) * brightness
- g = (g / 255) * brightness
- b = (b / 255) * brightness
- r, g, b = [int(c * brightness / 255) for c in blue_tap]
- r = max(0, r - abs((i - start_index) * 3)) #the number controls the size of the tap. lower == bigger
- g = max(0, g - abs((i - start_index) * 3))
- b = max(0, b - abs((i - start_index) * 3))
- #send colors to array
- #compare old colors against new colors
- if x >= r:
- r = x
- if y >= g:
- g = y
- if z >= b:
- b = z
- temp_hold_blue_color[i] = (r, g, b)
- colors[i] = (r, g, b)
- effect["start_time"] = time.time() #for counting time AFTER note has ended (fade out effect)
- if holdstart == False or effect["end"] == True:
- tempb = tempb * 1.5
- for i in range(max(0, start_index - tap_size), min(strip_length, start_index + tap_size)):
- r, g, b = temp_hold_blue_color[i][0], temp_hold_blue_color[i][1], temp_hold_blue_color[i][2]
- index = i
- x, y, z = colors[i][0], colors[i][1], colors [i][2]
- #logic for the main tap brightness
- brightness = effect["yvalue"][iterate]
- brightness = (brightness + 127)
- iterate = iterate + 1
- r, g, b = blue_tap
- r = (r / 255) * brightness
- g = (g / 255) * brightness
- b = (b / 255) * brightness
- r, g, b = [int(c * brightness / 255) for c in blue_tap]
- r = max(0, r - abs((i - start_index) * 3)) #the number controls the size of the tap. lower == bigger
- g = max(0, g - abs((i - start_index) * 3))
- b = max(0, b - abs((i - start_index) * 3))
- #send colors to array
- r = r / tempb
- g = g / tempb
- b = b / tempb
- #compare old colors against new colors
- if x >= r:
- r = x
- if y >= g:
- g = y
- if z >= b:
- b = z
- temp_hold_blue_color[i] = (r, g, b)
- colors[i] = (r, g, b)
- effect["end"] = True
- if current_time - effect["start_time"] >= 1:
- tempb = 0.2
- effects_to_remove.append(effect)
- temp_hold_blue_color = [[0,0,0] for _ in range(strip_length)]
- if effect["effect_id"] == 9: # Red Hold
- inverse = 1 - (n - current_time)
- frequency = 10
- x_values = np.arange(effect["effect_var"], (effect["effect_var"] + tap_size), 1)
- #print(x_values)
- effect["xvalue"] = np.concatenate([x_values, x_values[::-1]])
- effect["effect_var"] = effect["effect_var"] + 5
- amplitude = 127 # Example amplitude
- frequency = 0.04 # Example frequency
- effect["yvalue"] = sine_wave(effect["xvalue"], amplitude, frequency)
- iterate = 0
- if holdstart == True and effect["end"] == False:
- tempr = min(tempr * 2, 1)
- for i in range(max(0, start_index - tap_size), min(strip_length, start_index + tap_size)):
- #capture old colors
- x, y, z = colors[i][0], colors[i][1], colors [i][2]
- #logic for the main tap brightness
- brightness = effect["yvalue"][iterate]
- brightness = (brightness + 127) * tempr
- iterate = iterate + 1
- r, g, b = red_tap
- r = (r / 255) * brightness
- g = (g / 255) * brightness
- b = (b / 255) * brightness
- r, g, b = [int(c * brightness / 255) for c in red_tap]
- r = max(0, r - abs((i - start_index) * 3)) #the number controls the size of the tap. lower == bigger
- g = max(0, g - abs((i - start_index) * 3))
- b = max(0, b - abs((i - start_index) * 3))
- #send colors to array
- #compare old colors against new colors
- if x >= r:
- r = x
- if y >= g:
- g = y
- if z >= b:
- b = z
- temp_hold_red_color[i] = (r, g, b)
- colors[i] = (r, g, b)
- effect["start_time"] = time.time() #for counting time AFTER note has ended (fade out effect)
- if holdstart == False or effect["end"] == True:
- tempr = tempr * 1.5
- for i in range(max(0, start_index - tap_size), min(strip_length, start_index + tap_size)):
- r, g, b = temp_hold_red_color[i][0], temp_hold_red_color[i][1], temp_hold_red_color[i][2]
- index = i
- x, y, z = colors[i][0], colors[i][1], colors [i][2]
- #logic for the main tap brightness
- brightness = effect["yvalue"][iterate]
- brightness = (brightness + 127)
- iterate = iterate + 1
- r, g, b = blue_tap
- r = (r / 255) * brightness
- g = (g / 255) * brightness
- b = (b / 255) * brightness
- r, g, b = [int(c * brightness / 255) for c in red_tap]
- r = max(0, r - abs((i - start_index) * 3)) #the number controls the size of the tap. lower == bigger
- g = max(0, g - abs((i - start_index) * 3))
- b = max(0, b - abs((i - start_index) * 3))
- #send colors to array
- r = r / tempr
- g = g / tempr
- b = b / tempr
- #compare old colors against new colors
- if x >= r:
- r = x
- if y >= g:
- g = y
- if z >= b:
- b = z
- temp_hold_red_color[i] = (r, g, b)
- colors[i] = (r, g, b)
- effect["end"] = True
- if current_time - effect["start_time"] >= 1:
- effects_to_remove.append(effect)
- temp_hold_red_color = [[0,0,0] for _ in range(strip_length)]
- tempr = 0.2
- if any(any(string in note for string in ["Blue Tap", "Red Tap", "Blue Match", "Red Match", "SectionContinuationOrEnd", "SpinLeftStart", "SpinRightStart"]) for note in notes):
- for effect in active_effects:
- if effect["effect_id"] == 7:
- effects_to_remove.append(effect)
- #print("removing effect 7")
- break
- if effect["effect_id"] == 7 and scratch == True: #SCRATCH
- for i in range(start_index, strip_length):
- r, g, b = colors[i][0], colors[i][1], colors[i][2]
- d, e, f = scratch_min[0], scratch_min[1], scratch_min[2]
- j, k, l = scratch_max[0], scratch_max[1], scratch_max[2]
- x, y, z = (random.randint(d, j), random.randint(e, k), random.randint(f, l))
- r = r + x
- g = g + y
- b = b + z
- colors[i] = r, g, b
- colors = colors[-s:] + colors[:-s]
- if s_down == True:
- s = s - 6
- if s <= -24:
- s_down = False
- break
- else:
- s = s + 6
- if s >= 24:
- s_down = True
- break
- if effect["effect_id"] == 7 and scratch == False:
- effects_to_remove.append(effect)
- for effect in effects_to_remove:
- try:
- ##print(f"effect being removed{effect}")
- active_effects.remove(effect)
- except:
- print("effect not found")
- return colors
- def mouse_light_effect(start_index, effect_id):
- """Start a light effect at a given LED index with a unique effect ID."""
- global active_effects
- effect_id = 1
- # Add a new effect with its start time, index, and ID
- active_effects.append({
- "start_time": livetime(),
- "start_index": start_index,
- "effect_id": effect_id
- })
- # Open the serial connection
- with serial.Serial(serial_port, baud_rate, timeout=1) as ser:
- last_sent_time = time.time()
- t1 = threading.Thread(None, port_connect)
- t1.start()
- while True:
- colors = [[0,0,0] for _ in range(strip_length)]
- # Measure start time
- #start_time = time.time()
- #if data != data_prev:
- #for tap effect
- x_values = np.arange(-tap_size, tap_size, 1) #size of tap
- y_values = [oscillating_parabola(x, osc_damp, osc_freq) for x in x_values]
- coordinates = [(int(x), int(oscillating_parabola(x, osc_damp, osc_freq))+ 127) for x in x_values]
- effects_to_remove = [] # reset effects to remove
- #on_match()
- spin_start()
- tap_start()
- on_scratch()
- on_beat()
- on_hold()
- beat_end()
- on_release()
- on_sectioncontinuationorend()
- on_continue()
- previous_colors = colors
- update_effects()
- beats()
- send_colors(colors, ser)
- update_colors()
- # print(f"notes{notes}")
- # print(f"active effects{active_effects}")
- # print(f"removed effects{effects_to_remove}")
- # time.sleep(1)
- ##print(smoothed_colors[:2])
- # Measure execution time
- # execution_time = time.time() - start_time
- # #print(f"Loop executed in {execution_time:.6f} seconds")
- # execution_time = timeit.timeit("get_screen_colors()", globals=globals(), number=100)
- # #print(f"on_spin executed in {execution_time / 100:.6f} seconds per run (average)")
Add Comment
Please, Sign In to add comment