1
0
mirror of https://github.com/osmarks/random-stuff synced 2024-11-09 22:09:55 +00:00
random-stuff/beep.py

83 lines
2.5 KiB
Python
Raw Normal View History

2020-08-12 18:16:20 +00:00
#!/usr/bin/python
# based on : www.daniweb.com/code/snippet263775.html
# from https://stackoverflow.com/questions/33879523/python-how-can-i-generate-a-wav-file-with-beeps
import math
import wave
import struct
# Audio will contain a long list of samples (i.e. floating point numbers describing the
# waveform). If you were working with a very long sound you'd want to stream this to
# disk instead of buffering it all in memory list this. But most sounds will fit in
# memory.
audio = []
2022-02-05 14:45:04 +00:00
sample_rate = 48000.0
phase = 0
2020-08-12 18:16:20 +00:00
def append_silence(duration_milliseconds=500):
num_samples = duration_milliseconds * (sample_rate / 1000.0)
for x in range(int(num_samples)):
audio.append(0.0)
return
def append_sinewave(
freq=440.0,
duration_milliseconds=500,
volume=1.0):
2022-02-05 14:45:04 +00:00
global phase
2020-08-12 18:16:20 +00:00
num_samples = duration_milliseconds * (sample_rate / 1000.0)
2022-02-05 14:45:04 +00:00
pc = math.tau * freq / sample_rate
2020-08-12 18:16:20 +00:00
for x in range(int(num_samples)):
2022-02-05 14:45:04 +00:00
phase += pc
audio.append(volume * math.sin(phase))
2020-08-12 18:16:20 +00:00
def append_squarewave(
freq=440.0,
duration_milliseconds=500,
volume=1.0):
global audio
num_samples = duration_milliseconds * (sample_rate / 1000.0)
samples_per_toggle = math.floor(sample_rate / freq)
for x in range(int(num_samples)):
on = math.floor(x / samples_per_toggle) % 2
audio.append(volume * on)
def save_wav(file_name):
wav_file=wave.open(file_name,"w")
nchannels = 1
sampwidth = 2
nframes = len(audio)
comptype = "NONE"
compname = "not compressed"
wav_file.setparams((nchannels, sampwidth, sample_rate, nframes, comptype, compname))
for sample in audio:
wav_file.writeframes(struct.pack('h', int( sample * 32767.0 )))
wav_file.close()
return
2023-06-19 13:09:54 +00:00
BAD_INTERVALS = [6, 8, 10, 11, 13]
NOTE_FACTOR = pow(2, 1/12)
LOG_NOTE_FACTOR = math.log(NOTE_FACTOR)
BASE = 440
MIN = math.ceil(math.log(200/440) / LOG_NOTE_FACTOR)
MAX = math.floor(math.log(2000/440) / LOG_NOTE_FACTOR)
2022-02-05 14:45:04 +00:00
import random
2023-06-19 13:09:54 +00:00
for i in range(10):
"""
for i in range(3):
append_sinewave(800, 100)
append_silence(100)
append_sinewave(600, 500)
append_silence()
"""
note = random.randint(MIN, MAX)
while MIN <= note <= MAX:
interval = random.choice(BAD_INTERVALS)
time = random.randint(50, 1000)
append_sinewave(BASE * pow(NOTE_FACTOR, note), time)
note += interval if random.choice((True, False)) else -interval
2022-02-05 14:45:04 +00:00
save_wav("output.wav")