/
dreamstomusic.py
executable file
·53 lines (36 loc) · 1.31 KB
/
dreamstomusic.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
#!/usr/bin/python
"""
This script is the entry point for generating music on the server.
"""
import argparse
import datetime
import tempfile
import os
import shutil
from sleepmusicalization import controller
kunquat_export = os.path.expanduser("~/kunquat/bin/kunquat-export")
lame = "/usr/bin/lame"
def main():
parser = argparse.ArgumentParser(description="Read Beddit sleep data and compose music.")
parser.add_argument("-r", "--result", required=True)
parser.add_argument("-s", "--sleep", required=True)
parser.add_argument("output")
arguments = parser.parse_args()
tempdir = tempfile.mkdtemp()
print "Creating temporary directory", tempdir
os.chdir(tempdir)
print "Reading sleep data"
with open(arguments.sleep) as sleep_file:
sleep_data_json_string = sleep_file.read()
with open(arguments.result) as result_file:
result_data_json_string = result_file.read()
print "Generating music"
controller.composeSong(result_data_json_string, sleep_data_json_string)
print "Exporting"
os.system(kunquat_export + " -o sleep.wav kqtc00/")
print "Converting to mp3"
os.system(lame + " sleep.wav " + arguments.output)
print "Removing temporary directory"
shutil.rmtree(tempdir)
if __name__ == "__main__":
main()