-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplay.py
93 lines (75 loc) · 2.46 KB
/
play.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#Library to play the chords
#and save the music files
import subprocess
import decimal
import thread
#Usage
# play(notes - The string containing notes (eg: "A0 B3 E1 C4")
# delay_period [OPTIONAL] - Delay between notes' playback
# fade [OPTIONAL] - Fade time at end of playback
# )
# save(file_name - The file to save to. Do not include an extension
# notes - Array of a 2 element arrays (eg: [["A3 G2", 0.00], ["C3 D1", 0.07]])
# The strums play at the time specified in the second argument for each row.
# delay_period [OPTIONAL] - Delay between notes' playback (within a particular strum)
# fade [OPTIONAL] - Fade time at end of playback
# )
def _gen_string(notes, delay_period = 0.05, fade = [0, 4, 0.1]):
notes_array = notes.split(" ")
final = ""
for i in notes_array:
final += "pl " + i + " "
if not delay_period == 0:
two_places = decimal.Decimal ('10') ** -2
j = 0
final += "delay "
while j < len(notes_array):
final += str(decimal.Decimal(delay_period * j).quantize(two_places)) + " "
j += 1
j = 0
if not fade == [0, 0, 0]:
final += "remix - fade "
while j < len(fade):
final += str(fade[j]) + " "
j += 1
final += "norm -1"
return final
def _gen_times(base, size, delay_period = 0.05):
times = ""
j = 0
k = 0
two_places = decimal.Decimal ('10') ** -2
while j < size:
times += str(decimal.Decimal(base + delay_period * j).quantize(two_places)) + " "
j += 1
k = delay_period * (j-1) + base
return k, times
def _play(notes, delay_period = 0.05, fade = [0, 4, 0.1]):
subprocess.call("play -n synth " + _gen_string(notes, delay_period, fade), shell = True)
return
def _save(file_name, notes, delay_period = 0.05, fade = [0, 4, 0.1]):
final = "sox -n public/music/" + file_name + ".wav synth "
note_s = ""
for row in notes:
final += _gen_string(row[0], 0, [0, 0, 0])
j = 0
if not fade == [0, 0, 0]:
final += "delay "
base = 0
times = " "
for row in notes:
base, times = _gen_times(base + row[1], len(row[0].split(" ")), delay_period)
final += times
j = 0
if not fade == [0, 0, 0]:
final += "remix - fade "
while j < len(fade):
final += str(fade[j]) + " "
j += 1
final += "norm -1"
print final
subprocess.call(final, shell = True)
def play(notes, delay_period = 0.05, fade = [0, 4, 0.1]):
thread.start_new_thread(_play, (notes, delay_period, fade))
def save(file_name, notes, delay_period = 0.05, fade = [0, 4, 0.1]):
thread.start_new_thread(_save, (file_name, notes, delay_period, fade))