异想天开

What's the true meaning of light, Could you tell me why

调式打印-python

日期:2015-08-03 10:29:14
  
最后更新日期:2015-08-03 10:33:07
打印输出自然大调,自然小调,和弦小调,旋律小调。
依赖:
xlwt 写excel文件的python库

[code lang="cpp"]
#!/usr/bin/python
# encoding: utf-8
import MySQLdb
import sys
import traceback
import threading
import Queue
import time
import xlwt
import math

remote_movie_host=''
remote_movie_port=3306
remote_movie_passwd=''
remote_movie_dbname=''
remote_movie_user=''

#remote_db=MySQLdb.connect(host=remote_movie_host,user=remote_movie_user,passwd=remote_movie_passwd,db=remote_movie_dbname,port=remote_movie_port)

def test():
print 'hello world'

panio_keys = ['C','#C','D','#D','E','F','#F','G','#G','A','#A','B']
panio_nums = [1,1,2,2,3,4,4,5,5,6,6,7]

panio_keys_black = [1,0,1,0,1,1,0,1,0,1,0,1]
wb = xlwt.Workbook(encoding='utf8')
style0 = xlwt.easyxf('font: name Times New Roman, color-index blue, bold on',num_format_str='#,##0.00')
style1 = xlwt.easyxf(num_format_str='D-MMM-YY')
g_line = 0
ws = wb.add_sheet('调式')

#oldeight , neweight 默认为0
#比如B,若看成C,则认为是bC,而不是前面n个升号的C
def treat_as_tone(oldtone,newtone):
_map = {'C':1,'#C':2,'D':3,'#D':4,'E':5,'F':6,'#F':7,'G':8,'#G':9,'A':10,'#A':11,'B':12}
_map_int = {1:'C',2:'D',3:'E',4:'F',5:'G',6:'A',7:'B'}
if type(newtone) == int:
newtone = _map_int[newtone]
if type(oldtone) != str :
raise Exception,"oldtone not str type"
inc1 = (_map[oldtone] + 12) - _map[newtone]
inc2 = _map[oldtone] - (_map[newtone] + 12)
inc = _map[oldtone] - _map[newtone]
if math.fabs(inc) > math.fabs(inc2):
inc = inc2
if math.fabs(inc) > math.fabs(inc1):
inc = inc1
return what_tone(newtone,inc)

# what_tone('C',2)
# return bbC
def what_tone(base,inc):
_map = {1:'C',2:'D',3:'E',4:'F',5:'G',6:'A',7:'B'}
key = base
if type(base) == int:
if 0 == base%8 :
base = 1
else:
base = base%8;
key = _map[base]
if type(inc) != int:
raise Exception,"inc should be int"
if inc > 0 :
prefix = '#'*inc
elif inc < 0:
prefix = 'b'*(-inc)
else:
prefix = ""
return prefix+key


def impl_print(name,tone_degree):
global panio_keys,panio_nums
global panio_keys_black
global g_line
global ws

line = g_line
print name
ws.col(0).width = 5000
ws.write(line,0,name)

line = line + 1
keys = panio_keys
for i in range(len(keys)):
major_tone = ""
col = 3
for j in range(len(tone_degree)):
incr = sum(tone_degree[k] for k in range(j+1))
#第一个音阶
if 0 == j :
base_tone = panio_nums[i]

k = (i+incr)%12
if 0 == j:
key = panio_keys[i]
else:
key = treat_as_tone(panio_keys[k],base_tone)

major_tone = major_tone + key + " "
base_tone = (base_tone + 1)%8
if 0 == base_tone:
base_tone = 1

if panio_keys_black[(i+incr)%12] == 0:
ws.write(line,col,key,style0)
else:
ws.write(line,col,key)
col = col + 1
print major_tone
line = line + 1
g_line = line

def print_tone():
global wb
global panio_nums
print len(panio_nums)
nature_major_tone_degree = [0,2,2,1,2,2,2,1]
nature_minor_tone_degree = [0,2,1,2,2,1,2,2]
harmonic_minor_tone_degree = [0,2,1,2,2,1,3,1]
melodic_minor_tone_degree = [0,2,1,2,2,2,2,1]

impl_print("major_tone/自然大调",nature_major_tone_degree)
impl_print("harmonic_minor_tone/和声小调",harmonic_minor_tone_degree)
impl_print("minor_tone/自然小调",nature_minor_tone_degree)
impl_print("melodic_minor_tone/旋律小调",melodic_minor_tone_degree)
wb.save('大小调式.xls')

def test_treate_as_tone():
print treat_as_tone('#C','C')
print treat_as_tone('C','C')
#print treat_as_tone('bC','C')
print treat_as_tone('C','D')
print treat_as_tone('E','F')
print treat_as_tone('F','E')
print treat_as_tone('C','B')
print treat_as_tone('B','C')
print treat_as_tone('C','B')



if __name__=='__main__':
print_tone()
#test_treate_as_tone()
[/code]