forked from IRL-CT/Interactive-Lab-Hub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_notes.py
More file actions
44 lines (40 loc) · 1.33 KB
/
Copy pathgenerate_notes.py
File metadata and controls
44 lines (40 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import numpy as np
from pydub import AudioSegment
from pydub.playback import play
def make_bell(freq, duration_ms=1000, volume=-6.0):
# Simple bell-ish envelope: fast attack, exponential decay + some overtones
sample_rate = 44100
t = np.linspace(0, duration_ms/1000, int(sample_rate * duration_ms/1000), False)
# base sine
wave = np.sin(2 * np.pi * freq * t)
# add a harmonic overtone
wave += 0.5 * np.sin(2 * np.pi * freq * 2 * t)
# apply an exponential decay envelope
envelope = np.exp(-3 * t) # adjust decay rate
wave = wave * envelope
# normalize to int16
audio = np.int16(wave / np.max(np.abs(wave)) * (2**15 - 1))
segment = AudioSegment(
audio.tobytes(),
frame_rate=sample_rate,
sample_width=2,
channels=1
)
segment = segment + volume # reduce gain
return segment
# Example: notes frequencies (A4 = 440 Hz)
note_freq = {
"A4": 440.0,
"B4": 493.88,
"C5": 523.25,
"D5": 587.33,
"E5": 659.25,
"F5": 698.46,
"G5": 783.99,
}
bells = {note: make_bell(freq, duration_ms=1000) for note, freq in note_freq.items()}
# To export to mp3/wav:
for note, bell in bells.items():
bell.export(f"notes/bell_{note}.mp3", format="mp3")
# bell.export(f"bell_{note}.wav", format="wav")
# bells["A4"].export("bell_A4.wav", format="wav")