OSDN Git Service

first commit
[spec-recorder/spec-recorder.git] / main.py.bak
1 # -*- coding: utf-8 -*-
2 # Spec_Recoeder Ver.1.02
3 # License = MIT
4 # Author = Yuta Minakawa
5 import subprocess
6 import datetime
7 import time
8 import os
9 import ConfigParser
10
11 import Image
12 import ImageDraw
13 import ImageFont
14
15 high_color = 0
16 mono_color = 0
17 del_ogg = 0
18 write_filename = 0
19 write_csv = 0
20 oggfilename = ""
21 pngfilename = ""
22 analyze = 0
23 conf = ConfigParser.SafeConfigParser()
24 conf.read("./sr.conf")
25 if conf.get("sox","high_color") == "on":
26         high_color = 1
27         print "high_color is available"
28 else:
29         print "high_color is unavailable"
30         
31 if conf.get("sox","mono_color") == "on":
32         mono_color = 1
33         print "mono_color is available"
34 else:
35         print "mono_color is unavailable"
36         
37 if conf.get("server", "server") == "on":
38         subprocess.Popen(["python", "server.py"])
39         print "HTTP server is working"
40 else:
41         print "HTTP server is unavailable"
42         
43 if conf.get("file", "del_ogg") == "on":
44         del_ogg = 1
45         print "del_ogg is available"
46 else:
47         print "del_ogg is unavailable"
48         
49 if conf.get("file", "write_filename") == "on":
50         write_filename = 1
51         print "filename will be written"
52 else:
53         print "filename will not be written"
54
55 if conf.get("file", "write_csv") == "on":
56         write_csv = 1
57         print "CSV file will be saved"
58         
59 if conf.get("analyze", "analyze") == "on":
60         analyze = 1
61         print "data will be analyzed"
62         
63 if os.name == 'posix':
64         print "OS = Linux,etc This system is supported"
65 if os.name == 'nt':
66         print "OS = Windows This system is NOT supported"
67         
68 def freq_exec(rev):
69         if rev == 1: 
70                 subprocess.Popen(['rec', '-c', '1', "test.wav", 'trim', '0', '00:10'])
71                 time.sleep(9)
72                 wait_time = 0
73                 while os.path.exists("test.wav") == 1:
74                         time.sleep(0.01)
75                         wait_time = wait_time + 1
76                         print 3
77                 if wait_time >= 100:
78                         subprocess.Popen(["python", "./freq_exec.py", str(wait_time)])
79                 else:
80                         subprocess.Popen(["python", "./freq_exec.py", "0"])
81                         print 12
82         print "check:done"
83         return(wait_time)
84
85 def write_cara(x,y,string,filename):
86         font = ImageFont.truetype("/usr/share/fonts/truetype/takao-gothic/TakaoGothic.ttf", 15, encoding="utf-8")
87         img = Image.open(filename)
88         draw = ImageDraw.Draw(img)
89         draw.text((x,y), string, font=font, fill=255)
90         img.save(filename)
91
92 def rec(filename):
93         #It is not used.
94         output = subprocess.call()
95         return(output)
96         
97 def how_long(filename):
98         output = subprocess.check_output(["soxi", "-d", filename])
99         print output
100         minute = output[4]
101         ten_second = output[6]
102         second = output[7]
103         file_info = (minute , ten_second, second)
104         return(file_info)
105         
106 def write2freq_exec(content):
107         f = open('text.txt', 'w')
108         f.write(content)
109         f.close()
110         
111 def spec(filename):
112         #output = subprocess.call(['sox', filename + ".ogg", "-n", 'rate', '3k',"spectrogram" , "-X", "8", "-Y", "300", "-o", filename + ".png" ])
113         oggfilename = "./ogg/" + filename + ".ogg"
114         pngfilename = "./PNG/" + filename + ".png"
115         if high_color == 1:
116                 if mono_color == 1:
117                         output = subprocess.Popen(['sox', oggfilename[:], "-n", 'rate', '3k',"spectrogram" , "-X", "8", "-Y", "300", "-z", "100", "-q", "30", "-h", "-m", "-o", pngfilename[:] ])
118                 else:
119                         output = subprocess.Popen(['sox', oggfilename[:], "-n", 'rate', '3k',"spectrogram" , "-X", "8", "-Y", "300", "-z", "100", "-q", "30", "-h", "-o", pngfilename[:] ])
120         else:
121                 if mono_color == 1:
122                         output = subprocess.Popen(['sox', oggfilename[:], "-n", 'rate', '3k',"spectrogram" , "-X", "8", "-Y", "300", "-z", "100", "-q", "30", "-m", "-o", pngfilename[:] ])
123                 else:
124                         output = subprocess.Popen(['sox', oggfilename[:], "-n", 'rate', '3k',"spectrogram" , "-X", "8", "-Y", "300", "-z", "100", "-q", "30", "-o", pngfilename[:] ])
125         print filename +".png" + " was made"
126         if high_color == 1:
127                 if mono_color == 1:
128                         subprocess.Popen(['sox', "./ogg/" + filename + ".ogg", "-n", 'rate', '3k',"spectrogram" , "-X", "8", "-Y", "300", "-z", "100", "-q", "30", "-h", "-m"])
129                 else:
130                         subprocess.Popen(['sox', "./ogg/" + filename + ".ogg", "-n", 'rate', '3k',"spectrogram" , "-X", "8", "-Y", "300", "-z", "100", "-q", "30", "-h"])
131         else:
132                 if mono_color == 1:
133                         subprocess.Popen(['sox', "./ogg/" + filename + ".ogg", "-n", 'rate', '3k',"spectrogram" , "-X", "8", "-Y", "300", "-z", "100", "-q", "30", "-m"])
134                 else:
135                         subprocess.Popen(['sox', "./ogg/" + filename + ".ogg", "-n", 'rate', '3k',"spectrogram" , "-X", "8", "-Y", "300", "-z", "100", "-q", "30"])
136         return(output)
137
138 print "Spec_Recorder Ver.b"
139
140 now_minute = int(datetime.datetime.now().strftime("%M"))
141 now_hour = int(datetime.datetime.now().strftime("%H"))
142 now_month = int(datetime.datetime.now().strftime("%m"))
143 now_day = int(datetime.datetime.now().strftime("%d"))
144 now_year = int(datetime.datetime.now().strftime("%Y"))
145
146 if int(datetime.datetime.now().strftime("%M")) % 5 == 0:
147         time.sleep(60)
148 subprocess.Popen(["python", "./freq_exec.py", "0"])
149 print "Executed freq_exec.py"
150
151
152 while 1==1:
153         if int(datetime.datetime.now().strftime("%M")) % 5 == 0:
154                 start_year = str(datetime.datetime.now().strftime("%Y"))[2:]
155                 start_month = str(datetime.datetime.now().strftime("%m"))[:]
156                 if start_month < 10:
157                         start_month = "0" + str(datetime.datetime.now().strftime("%m"))[:]
158                 start_day = str(datetime.datetime.now().strftime("%d"))[:]
159                 if start_day < 10:
160                         start_day = "0" + str(datetime.datetime.now().strftime("%d"))[:]
161                 start_hour = str(datetime.datetime.now().strftime("%H"))[:]
162                 if start_hour < 10:
163                         start_hour = "0" + str(datetime.datetime.now().strftime("%H"))[:]
164                 start_minute = str(datetime.datetime.now().strftime("%M"))[:]
165                 if datetime.datetime.now().strftime("%M") < 10:
166                         start_minute = "0" + str(datetime.datetime.now().strftime("%M"))[:]
167                 filename = str(start_year)[:] + str(start_month)[:] + str(start_day)[:] + str(start_hour)[:] + str(start_minute)[:]
168                 print "Audio fileneme is " + filename + ".ogg"
169                 print "Waiting 5 minutes"
170                 time.sleep(290)
171                 for cnt in range(1000):
172                         if os.path.exists("./ogg/" + filename + ".ogg") == 1:
173                                 if how_long("./ogg/"+ filename + ".ogg")[0] == "5":
174                                         break
175                         #print "Waiting for " + filename +".ogg."
176                         time.sleep(0.1)
177                 spec(filename)
178                 if del_ogg == 1:
179                         subprocess.call(["rm", "./ogg/" + filename + ".ogg"])
180                 for cnt in range(20):
181                         if os.path.exists("./PNG/" + filename + ".png") == 1:
182                                 print filename + ".png is exist"
183                                 break
184                         print "Waiting for " + filename +".png"
185                         time.sleep(0.1)
186                 if write_filename == 1:
187                         if os.path.exists("./PNG/" + filename + ".png") == 1:
188                                 write_string = filename[:]
189                                 write_cara(90,320,write_string,"./PNG/" + filename + ".png")
190                                 write_cara(90,320,write_string,"spectrogram" + ".png")
191                 else:
192                         print "Error! " + filename + ".png is not found"
193                 if write_csv == 1:
194                         if analyze != 1:
195                                 print "make csv"
196                                 subprocess.Popen(["python", "./make_csv.py", "-c", filename])
197                 if analyze == 1:
198                         print "make csv"
199                         subprocess.Popen(["python", "./make_csv.py", "-ac", filename])
200                 #img = Image.open(filename + ".png")
201                 #im = ImageDraw.ImageDraw(img)
202                 #im.rectangle((100, 100, 300, 300))
203                 #img.show()
204         time.sleep(0.001)
205