-
Notifications
You must be signed in to change notification settings - Fork 3
/
synth.py
49 lines (43 loc) · 1.45 KB
/
synth.py
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
'''Synthesisation of primitive audio building blocks'''
import cfg
import math
import numpy as np
import random
def sine_wave_note(frequency, duration):
'''
Creates audio buffer representing a sine-wave
frequency: Hz
duration: seconds
'''
elements = math.ceil(duration * cfg.sample_rate)
timesteps = np.linspace(start=0, stop=duration, num=elements, endpoint=False)
return np.sin(frequency * timesteps * 2 * np.pi)
def sawtooth_wave_note(frequency, duration):
'''
Creates audio buffer representing a sine-wave
frequency: Hz
duration: seconds
'''
elements = math.ceil(duration * cfg.sample_rate)
timesteps = np.linspace(start=0, stop=duration, num=elements, endpoint=False)
print(timesteps)
timesteps = timesteps.tolist()
timesteps = [1-((x * frequency * 2 * np.pi)%1) for x in timesteps]
timesteps = np.array(timesteps)
return frequency * timesteps * 2 * np.pi
def random_wave_note(frequency, duration):
'''
Creates audio buffer representing a sine-wave
frequency: Hz
duration: seconds
'''
elements = math.ceil(duration * cfg.sample_rate)
timesteps = np.linspace(start=0, stop=duration, num=elements, endpoint=False)
return np.array([float(x%1)-1 for x in range(len(timesteps))])
def silence(duration):
'''
Creates audio buffer representing silence
duration: seconds
'''
elements = math.ceil(duration * cfg.sample_rate)
return np.zeros(elements)