diff --git a/offitracker.py b/offitracker.py index 2e7ca87..ecd240f 100644 --- a/offitracker.py +++ b/offitracker.py @@ -1,3 +1,4 @@ +import sys import csv import numpy as np import sounddevice as sd @@ -12,7 +13,7 @@ import sounddevice as sd # Duration = tone duration in ms # (c) 2024 mueller_minki, Feel free to modify or share. -def play_square_waves(frequencies, effects, duration, amplitude=1, noise_amplitude=0, sample_rate=44100): +def play_square_waves(output_stream, frequencies, effects, duration, amplitude=1, noise_amplitude=0, sample_rate=44100): num_waves = len(frequencies) t = np.linspace(0, duration / 1000, int(sample_rate * duration / 1000), endpoint=False) @@ -26,7 +27,9 @@ def play_square_waves(frequencies, effects, duration, amplitude=1, noise_amplitu combined_wave = np.sum(waves, axis=0) - sd.play(combined_wave, sample_rate, blocking=True) + combined_wave = combined_wave.astype(np.float32) + + output_stream.write(combined_wave) def play_csv_file(file_path): with open(file_path, 'r') as csv_file: @@ -35,16 +38,16 @@ def play_csv_file(file_path): num_columns = len(header) num_pairs = (num_columns - 1) // 2 - for row in csv_reader: - frequencies = [float(row[f'Frequency{i}']) for i in range(1, num_pairs + 1)] - effects = [float(row[f'Effect{i}']) for i in range(1, num_pairs + 1)] - duration = float(row['Duration']) + with sd.OutputStream(channels=1) as output_stream: + for row in csv_reader: + frequencies = [float(row[f'Frequency{i}']) for i in range(1, num_pairs + 1)] + effects = [float(row[f'Effect{i}']) for i in range(1, num_pairs + 1)] + duration = float(row['Duration']) - # Check if 'Noise' column exists in the CSV file - noise_amplitude = float(row.get('Noise', 0)) - - play_square_waves(frequencies, effects, duration, noise_amplitude=noise_amplitude) + # Check if 'Noise' column exists in the CSV file + noise_amplitude = float(row.get('Noise', 0)) + play_square_waves(output_stream, frequencies, effects, duration, noise_amplitude=noise_amplitude) if __name__ == "__main__": print(' ') @@ -55,6 +58,10 @@ if __name__ == "__main__": print('/ | \ | | | | | | | | | \// __ \\\\ \___| <\ ___/| | \/') print('\_______ /__| |__| |__| |____| |__| (____ /\___ >__|_ \\\\___ >__| ') print(' \/ \/ \/ \/ \/ ') - csv_file_path = input("Choose a CSV file: ") + 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)