# -*- coding: utf-8 -*-
# Spec_Recoeder Ver.b
# License = MIT
# Author = Yuta Minakawa
import subprocess
import datetime
import time
import os

import ConfigParser

import Image
import ImageDraw
import ImageFont

high_color = 0
conf = ConfigParser.SafeConfigParser()
conf.read("./sr.conf")
if conf.get("sox","high_color") == "on":
	high_color = 1
	print "high_color is available"
else:
	print "high_color is unavailable"

def write_cara(x,y,string,filename):
	font = ImageFont.truetype("/usr/share/fonts/truetype/takao-gothic/TakaoGothic.ttf", 15, encoding="utf-8")
	img = Image.open(filename)
	draw = ImageDraw.Draw(img)
	draw.text((x,y), string, font=font, fill=255)
	img.save(filename)

def rec(filename):
	#It is not used.
	output = subprocess.call()
	return(output)
	
def how_long(filename):
	output = subprocess.check_output(["soxi", "-d", filename])
	print output
	minute = output[4]
	ten_second = output[6]
	second = output[7]
	file_info = (minute , ten_second, second)
	return(file_info)
	
def spec(filename):
	#output = subprocess.call(['sox', filename + ".mp3", "-n", 'rate', '3k',"spectrogram" , "-X", "8", "-Y", "300", "-o", filename + ".png" ])
	if high_color == 1:
		output = subprocess.call(['sox', filename + ".mp3", "-n", 'rate', '3k',"spectrogram" , "-X", "8", "-Y", "300", "-z", "100", "-q", "30", "-h", "-o", filename + ".png" ])
	else:
		output = subprocess.call(['sox', filename + ".mp3", "-n", 'rate', '3k',"spectrogram" , "-X", "8", "-Y", "300", "-z", "100", "-q", "30", "-o", filename + ".png" ])
	print filename +".png" + " was made"
	if high_color == 1:
		subprocess.call(['sox', filename + ".mp3", "-n", 'rate', '3k',"spectrogram" , "-X", "8", "-Y", "300", "-z", "100", "-q", "30", "-h"])
	else:
		subprocess.call(['sox', filename + ".mp3", "-n", 'rate', '3k',"spectrogram" , "-X", "8", "-Y", "300", "-z", "100", "-q", "30"])
	return(output)

print "Spec_Recorder Ver.b"

now_minute = int(datetime.datetime.now().strftime("%M"))
now_hour = int(datetime.datetime.now().strftime("%H"))
now_month = int(datetime.datetime.now().strftime("%m"))
now_day = int(datetime.datetime.now().strftime("%d"))
now_year = int(datetime.datetime.now().strftime("%Y"))

while 1==1:
	if int(datetime.datetime.now().strftime("%M")) % 5 == 0:
		start_year = str(datetime.datetime.now().strftime("%Y"))[2:]
		start_month = str(datetime.datetime.now().strftime("%m"))[:]
		if start_month < 10:
			start_month = "0" + str(datetime.datetime.now().strftime("%m"))[:]
		start_day = str(datetime.datetime.now().strftime("%d"))[:]
		if start_day < 10:
			start_day = "0" + str(datetime.datetime.now().strftime("%d"))[:]
		start_hour = str(datetime.datetime.now().strftime("%H"))[:]
		if start_hour < 10:
			start_hour = "0" + str(datetime.datetime.now().strftime("%H"))[:]
		start_minute = str(datetime.datetime.now().strftime("%M"))[:]
		if datetime.datetime.now().strftime("%M") < 10:
			start_minute = "0" + str(datetime.datetime.now().strftime("%M"))[:]
		filename = str(start_year)[:] + str(start_month)[:] + str(start_day)[:] + str(start_hour)[:] + str(start_minute)[:]
		print "Audio fileneme is " + filename + ".mp3"
		print "Waiting 5 minutes"
		time.sleep(300)
		for cnt in range(100):
			if os.path.exists(filename + ".mp3") == 1:
				if how_long(filename + ".mp3")[0] == "5":
					break
			print "Waiting for " + filename +".mp3."
			time.sleep(0.1)
		spec(filename)
		for cnt in range(100):
			if os.path.exists(filename + ".png") == 1:
				print filename + ".png is exist"
				break
			print "Waiting for " + filename +".png"
			time.sleep(0.1)
		write_string = filename[:]
		write_cara(90,320,write_string,filename + ".png")
		#img = Image.open(filename + ".png")
		#im = ImageDraw.ImageDraw(img)
		#im.rectangle((100, 100, 300, 300))
		#img.show()
	time.sleep(0.001)