-
Notifications
You must be signed in to change notification settings - Fork 0
/
note_generator.py
68 lines (46 loc) · 1.65 KB
/
note_generator.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import librosa
import numpy as np
cow = librosa.load('/home/sonja/Dropbox/RC/passive_ear_trainer/Songs/Kuh muht-TdheW61w4Co.m4a')
def extend_short_rec(short_rec, desired_num_samples, desired_vol):
bounds = [1113687, 1148687] #FIXME
current_vol = max(short_rec[0])
sample = last_not_0_before_par(short_rec, bounds[1])
for _ in range(50):
sample = sign_change(short_rec, sample-1)
end_copy = sample
for _ in range(1000):
sample = sign_change(short_rec, sample-1)
beginning_copy = sample
extension = short_rec[0][bounds[0]:end_copy]
while len(extension) < desired_num_samples:
extension = np.concatenate((extension, short_rec[0][beginning_copy:end_copy]))
extension = extension/current_vol*desired_vol
return extension[:desired_num_samples]
def cow_generator(amp, rate):
if rate != cow[1]:
return False
def f(num_samples):
return extend_short_rec(cow, num_samples, amp)
return f
def last_not_0_before_par(rec, par):
ind = par
while rec[0][ind] == 0:
ind -= 1
return ind
def first_0_before_par(rec, par):
ind = par
while rec[0][ind] != 0:
ind -= 1
return ind
def sign_change(rec, par):
ind = par
while rec[0][ind] * rec[0][par] > 0:
ind -= 1
return ind
def note_by_num_samples(freq, num_samples, amp, rate):
data = np.array([np.sin(2*np.pi*freq*librosa.samples_to_time(sample, rate))*amp for sample in range(num_samples)], dtype=np.float32)
return data
def synth_generator(freq, amp, rate):
def f(num_samples):
return note_by_num_samples(freq, num_samples, amp, rate)
return f