Files
the_information_nexus/tech_docs/python/tone_generate.py
2024-05-01 12:28:44 -06:00

47 lines
1.7 KiB
Python

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