From 280b84524f0142349973e2cf0902e829167abed0 Mon Sep 17 00:00:00 2001 From: medusa Date: Sat, 23 Mar 2024 04:33:24 +0000 Subject: [PATCH] Add docs/tech_docs/python/tone_generate.py --- docs/tech_docs/python/tone_generate.py | 46 ++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 docs/tech_docs/python/tone_generate.py diff --git a/docs/tech_docs/python/tone_generate.py b/docs/tech_docs/python/tone_generate.py new file mode 100644 index 0000000..d1f6738 --- /dev/null +++ b/docs/tech_docs/python/tone_generate.py @@ -0,0 +1,46 @@ +from scipy.io.wavfile import write +import numpy as np + +# Parameters +sample_rate = 44100 # Sample rate in Hz +duration = 16 # Total duration of the countdown in seconds +base_frequency = 440 # Base frequency for the first beep in Hz +final_frequency = 880 # Final frequency for the beep before the end tone in Hz +end_tone_frequency = 1760 # Frequency for the final tone +volume = 0.5 # Volume, from 0 to 1 +beep_duration = 0.3 # Duration of each beep in seconds +beep_interval = 1 # Interval between beeps in seconds, decrease towards the end +end_tone_duration = 1.5 # Duration of the final tone in seconds + +# Time array +t = np.linspace(0, duration, int(sample_rate * duration), False) + +# Generate silent audio +audio = np.zeros_like(t) + +# Generate beeps +for i in range(int(duration / beep_interval)): + # Calculate beep frequency to increase over time + freq = np.linspace(base_frequency, final_frequency, int(duration / beep_interval))[i] + # Calculate start and end samples for the current beep + start_sample = int(i * beep_interval * sample_rate) + end_sample = int(start_sample + beep_duration * sample_rate) + # Generate beep + beep = np.sin(2 * np.pi * freq * t[start_sample:end_sample]) * volume + # Insert beep into the audio signal + audio[start_sample:end_sample] += beep + +# Generate the final tone +start_sample = int(duration * sample_rate - end_tone_duration * sample_rate) +end_sample = int(duration * sample_rate) +end_tone = np.sin(2 * np.pi * end_tone_frequency * t[start_sample:end_sample]) * volume +audio[start_sample:end_sample] += end_tone + +# Ensure audio is in 16-bit format +audio = np.int16(audio / np.max(np.abs(audio)) * 32767) + +# Save to file +wav_file = '/mnt/data/countdown_timer_tone.wav' +write(wav_file, sample_rate, audio) + +wav_file