-
Notifications
You must be signed in to change notification settings - Fork 0
/
compose.py
57 lines (41 loc) · 1.88 KB
/
compose.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
import os
import re
from subprocess import call
from six.moves import cPickle
from model import Model
from config import Config
import tensorflow as tf
def compose(params, filename="outs/last-output.abc"):
with open(os.path.join(params.save_dir, 'config.pkl'), 'rb') as f:
params = cPickle.load(f)
with open(os.path.join(params.save_dir, 'chars_vocab.pkl'), 'rb') as f:
chars, vocab = cPickle.load(f)
model = Model(params, training=False)
with tf.Session() as sess:
saver = tf.train.Saver()
ckpt = tf.train.get_checkpoint_state(params.save_dir)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
with open(os.path.join(filename), 'wb') as fw:
sampled_abc = model.sample(sess, chars, vocab, params.n, params.prime).encode('utf-8')
sampled_abc = re.sub(r"\].*\[", "][", sampled_abc)
sampled_abc = re.sub(r"\\\s*\n[^\n]*$", "", sampled_abc)
fw.write("X: 1\n")
fw.write("T: Composer\n")
fw.write("M: 4/4\n")
fw.write("K: A\n")
fw.write(sampled_abc)
def listen(params, filename="outs/last-output.abc"):
with open(os.path.join(params.save_dir, 'config.pkl'), 'rb') as f:
params = cPickle.load(f)
output_mid = os.path.join(params.out_dir, "output.mid")
output_mp3 = os.path.join(params.out_dir, "output.mp3")
call("abc2midi {filename} -o {output_mid}".format(filename=filename, output_mid=output_mid), shell=True)
call("timidity -Or -o - {output_mid} | lame -r - {output_mp3}".format(output_mid=output_mid,
output_mp3=output_mp3), shell=True)
def main():
params = Config()
compose(params, "outs/last-output.abc")
listen(params, "outs/last-output.abc")
if __name__ == '__main__':
main()