Click to see powerpoint
Sin Frequencies Precal

Open notebook in Google Colab
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import Audio

SAMPLE_RATE = 44100

def make_note(frequency, duration, volume=1.0):
t = np.linspace(0, duration, int(SAMPLE_RATE * duration), endpoint=False)
wave = volume * np.sin(2 * np.pi * frequency * t)

fade_len = int(0.025 * SAMPLE_RATE) # 20ms fade
wave[:fade_len] *= np.linspace(0, 1, fade_len) # fade in
wave[-fade_len:] *= np.linspace(1, 0, fade_len) # fade out

return wave

notes = {

'C4': 261.63,
'D4': 293.66,
'E4': 329.63,
'F4': 349.23,
'G4': 392.00,
'A4': 440.00,
'B4': 493.88,
'C5': 523.25,
'D5': 587.33,
'REST': 0
}


melody = [
('E4', 0.8),
('G4', 0.4),
('D5', 1.2),
('C5', 0.8),
('G4', 0.4),
('F4', 1.2),
('E4', 0.8),
('E4', 0.4),
('REST', 0.2),
('E4', 0.4),
('F4', 0.4),
('G4', 1.2),
('F4', 1.2),
('E4', 0.4),
('E4', 0.4),
('F4', 0.8),
('G4', 1.2),
('C5', 0.8),
('C5', 0.4),
('G4', 0.8),
('E4', 0.4),
('D4', 0.4),
('E4', 0.4),
('F4', 1.2),
('G4', 0.4),
]

song = np.array([]) # start with an empty array

for note_name, duration in melody:
freq = notes[note_name]
if freq == 0:
# REST: just add silence
song = np.concatenate([song, np.zeros(int(SAMPLE_RATE * duration))])
else:
song = np.concatenate([song, make_note(freq, duration)])


song = song / np.max(np.abs(song))
Audio(song, rate=SAMPLE_RATE)