create offiplayergui.py & update offitracker.py

Adds a GUI for easy usage
This commit is contained in:
Emanulator 2024-01-02 20:25:31 +00:00
parent 0af3446706
commit ba2c5a6e39
2 changed files with 122 additions and 67 deletions

45
offiplayergui.py Normal file
View File

@ -0,0 +1,45 @@
import PySimpleGUI as sg
import glob
import offitracker as offi
import sys
import csv
import numpy as np
import sounddevice as sd
import threading
names = glob.glob("./**/*.csv", recursive=True)
toplay = ""
layout = [[sg.Text('Search')],
[sg.Input(size=(200, 1), enable_events=True, key='-INPUT-')],
[sg.Listbox(names, size=(200, 10), enable_events=True, key='-LIST-')],
[sg.Button('Play'), sg.Button('Stop'), sg.Text("",key="file")]]
window = sg.Window('OffiPlayer', layout,size=(480, 320))
def playthread(window):
offi.stop_signal = False
offi.play_csv_file(toplay)
window.write_event_value(('-THREAD-', '** DONE **'), 'Done!')
while True:
event, values = window.read()
if event in (sg.WIN_CLOSED, 'Exit'):
break
if values['-INPUT-'] != '':
search = values['-INPUT-']
new_values = [x for x in names if search in x]
window['-LIST-'].update(new_values)
else:
window['-LIST-'].update(names)
if event == '-LIST-' and len(values['-LIST-']):
toplay = values['-LIST-'][0]
window['file'].update(toplay)
if event == 'Play':
window.start_thread(lambda: playthread(window), ('-THREAD-', '-THEAD ENDED-'))
if event == 'Stop':
offi.stop_signal = True
window.close()

View File

@ -1,67 +1,77 @@
import sys import sys
import csv import csv
import numpy as np import numpy as np
import sounddevice as sd import sounddevice as sd
import time
# OffiTracker, the tracker that no one asked for but I made it anyways :3 # OffiTracker, the tracker that no one asked for but I made it anyways :3
# Usage: Make a CSV table in Excel or LibreOffice with the following format: # Usage: Make a CSV table in Excel or LibreOffice with the following format:
# Frequency1 Effect1 Frequency2 Effect2 .... Noise Duration # Frequency1 Effect1 Frequency2 Effect2 .... Noise Duration
# You can make as many channels as you want. # You can make as many channels as you want.
# Effect = pulse width from 0 to 100 # Effect = pulse width from 0 to 100
# Frequency = tone in Hz. # Frequency = tone in Hz.
# Noise = noise amplitude from 0 to 10 # Noise = noise amplitude from 0 to 10
# Duration = tone duration in ms # Duration = tone duration in ms
# (c) 2024 mueller_minki, Feel free to modify or share. # (c) 2024 mueller_minki, Feel free to modify or share.
stop_signal = False
def play_square_waves(output_stream, frequencies, effects, duration, amplitude=1, noise_amplitude=0, sample_rate=44100):
num_waves = len(frequencies) def stop_playback():
t = np.linspace(0, duration / 1000, int(sample_rate * duration / 1000), endpoint=False) stop_signal = True
# Generate and sum square waves for each frequency with corresponding effects def play_square_waves(output_stream, frequencies, effects, duration, amplitude=1, noise_amplitude=0, sample_rate=44100):
waves = [amplitude * (effect / 100) * np.sign(np.sin(2 * np.pi * freq * t)) for freq, effect in zip(frequencies, effects)] if stop_signal == True:
output_stream.stop()
# Add optional noise channel pass
if noise_amplitude > 0: else:
noise = noise_amplitude * np.random.uniform(-1, 1, len(t)) num_waves = len(frequencies)
waves.append(noise) t = np.linspace(0, duration / 1000, int(sample_rate * duration / 1000), endpoint=False)
combined_wave = np.sum(waves, axis=0) # Generate and sum square waves for each frequency with corresponding effects
waves = [amplitude * (effect / 100) * np.sign(np.sin(2 * np.pi * freq * t)) for freq, effect in zip(frequencies, effects)]
combined_wave = combined_wave.astype(np.float32)
# Add optional noise channel
output_stream.write(combined_wave) if noise_amplitude > 0:
noise = noise_amplitude * np.random.uniform(-1, 1, len(t))
def play_csv_file(file_path): waves.append(noise)
with open(file_path, 'r') as csv_file:
csv_reader = csv.DictReader(csv_file) combined_wave = np.sum(waves, axis=0)
header = csv_reader.fieldnames
num_columns = len(header) combined_wave = combined_wave.astype(np.float32)
num_pairs = (num_columns - 1) // 2
output_stream.write(combined_wave)
with sd.OutputStream(channels=1) as output_stream:
for row in csv_reader: def play_csv_file(file_path):
frequencies = [float(row[f'Frequency{i}']) for i in range(1, num_pairs + 1)] stop_signal = False
effects = [float(row[f'Effect{i}']) for i in range(1, num_pairs + 1)]
duration = float(row['Duration']) with open(file_path, 'r') as csv_file:
csv_reader = csv.DictReader(csv_file)
# Check if 'Noise' column exists in the CSV file header = csv_reader.fieldnames
noise_amplitude = float(row.get('Noise', 0)) num_columns = len(header)
num_pairs = (num_columns - 1) // 2
play_square_waves(output_stream, frequencies, effects, duration, noise_amplitude=noise_amplitude)
with sd.OutputStream(channels=1) as output_stream:
if __name__ == "__main__": for row in csv_reader:
print(' ') frequencies = [float(row[f'Frequency{i}']) for i in range(1, num_pairs + 1)]
print(' Mueller\'s Software Domain proudly presents:') effects = [float(row[f'Effect{i}']) for i in range(1, num_pairs + 1)]
print('________ _____ _____._____________ __ ') duration = float(row['Duration'])
print('\_____ \_/ ____\/ ____\__\__ ___/___________ ____ | | __ ___________ ')
print(' / | \ __\\\\ __\| | | | \_ __ \__ \ _/ ___\| |/ // __ \_ __ \\') # Check if 'Noise' column exists in the CSV file
print('/ | \ | | | | | | | | | \// __ \\\\ \___| <\ ___/| | \/') noise_amplitude = float(row.get('Noise', 0))
print('\_______ /__| |__| |__| |____| |__| (____ /\___ >__|_ \\\\___ >__| ') if stop_signal == False:
print(' \/ \/ \/ \/ \/ ') play_square_waves(output_stream, frequencies, effects, duration, noise_amplitude=noise_amplitude)
print(' Version 1.1')
if len(sys.argv) > 1: if __name__ == "__main__":
csv_file_path = sys.argv[1] print(' ')
else: print(' Mueller\'s Software Domain proudly presents:')
csv_file_path = input("Choose a CSV file: ") print('________ _____ _____._____________ __ ')
play_csv_file(csv_file_path) print('\_____ \_/ ____\/ ____\__\__ ___/___________ ____ | | __ ___________ ')
print(' / | \ __\\\\ __\| | | | \_ __ \__ \ _/ ___\| |/ // __ \_ __ \\')
print('/ | \ | | | | | | | | | \// __ \\\\ \___| <\ ___/| | \/')
print('\_______ /__| |__| |__| |____| |__| (____ /\___ >__|_ \\\\___ >__| ')
print(' \/ \/ \/ \/ \/ ')
print(' Version 1.1')
if len(sys.argv) > 1:
csv_file_path = sys.argv[1]
else:
csv_file_path = input("Choose a CSV file: ")
play_csv_file(csv_file_path)