-
Notifications
You must be signed in to change notification settings - Fork 0
/
mootifs.py
executable file
·98 lines (87 loc) · 2.68 KB
/
mootifs.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
93
94
95
96
97
98
#!/usr/bin/env python
import mta
import mashup
import music
import numpy
import sys
import wav
def usage():
print "usage: mootifs [command]"
print "commands:"
print " bpm [wav] - detect bpm of wav"
print " instrumental [in] [out] - extract instrumentals"
print " transpose [in] [out] [shift] - change pitch by shift factor"
print " mash [file] [file] ... [out] - generate mashup from files"
print " wav [file] - find motifs from wav file"
print " csv [file] - find motifs from csv file"
def command_bpm(*files):
w = wav.Wav(files[0])
bpm = music.get_bpm(w)
print "{}bpm".format(bpm)
def command_instrumental(*args):
if len(args) != 2:
usage()
return
w = wav.Wav(args[0])
out = music.extract_instrumentals(w.time_series)
wav.write(args[1], out, w.sample_rate)
def command_transpose(*args):
if len(args) != 3:
usage()
return
w = wav.Wav(args[0])
out = music.transpose_key(float(args[2]), w)
wav.write(args[1], out, w.sample_rate)
def command_mash(*args):
wavs = [wav.Wav(w) for w in args[:-1]]
out = mashup.generate(wavs)
wav.write(args[-1], out, 44100)
def command_wav(*files):
SAMPLES = 1000
w = wav.Wav(files[0])
arr = wav.mono(w.resample(SAMPLES))
motifs = mta.get_motifs(arr)
print
print "discovered motifs"
print "-----------------"
print "data length:", arr.shape[0]
print
for tracker in motifs:
print tracker.word
for loc in tracker.loc:
start_sec = (loc['start'] / float(SAMPLES)) * w.duration
len_sec = (loc['len'] / float(SAMPLES)) * w.duration
print "\tstart {}s, length {}s".format(start_sec, len_sec)
def command_csv(*files):
arr = numpy.genfromtxt(files[0], delimiter=',')
if arr.ndim > 1:
raise Error('csv has more than one column')
motifs = mta.get_motifs(arr)
print
print "discovered motifs"
print "-----------------"
print "data length:", arr.shape[0]
print
for tracker in motifs:
print tracker.word
for loc in tracker.loc:
print "\tstart {}, length {}".format(loc['start'], loc['len'])
if len(sys.argv) > 2:
command = sys.argv[1]
args = sys.argv[2:]
if command == 'bpm':
command_bpm(*args)
elif command == 'instrumental':
command_instrumental(*args)
elif command == 'transpose':
command_transpose(*args)
elif command == 'mash':
command_mash(*args)
elif command == 'wav':
command_wav(*args)
elif command == 'csv':
command_csv(*args)
else:
usage()
else:
usage()