# -*- coding: utf-8 -*- """ Created on Tue Nov 22 15:03:48 2022 @author: 86155 """ import pygame import time music=input("请输入歌曲名") Name=music musicName=open(file=music,mode='r',encoding='utf-8') music=musicName.read() musicName.close() print('播放{}'.format(Name)) rhythm=eval(input("请输入节拍(正常速度是0.5)")) beat=rhythm print('节拍已确定') tone=input('请确定基调') #创建键盘#确定基调 if tone == 'C' : pianokey={'1':'c1','2':'d1','3':'e1','4':'f1','5':'g1','6':'a1','7':'b1', 'q':'c','w':'d','e':'e','r':'f','t':'g','y':'a','u':'b', 'a':'c2','s':'d2','d':'e2','f':'f2','g':'g2','h':'a2','j':'b2', '0':'0' } elif tone =='D': pianokey={'1':'d1','2':'e1','3':'f1#','4':'g1','5':'a1','6':'b1','7':'c2#', 'q':'d','w':'e','e':'f#','r':'g','t':'a','y':'b','u':'c1#', 'a':'d2','s':'e2','d':'f2#','f':'g2','g':'a2','h':'b2','j':'c3#' } elif tone =='E': pianokey={'1':'e1','2':'f1#','3':'g1#','4':'a1','5':'b1','6':'c2#','7':'d2#', 'q':'e','w':'f#','e':'g#','r':'a','t':'b','y':'c1#','u':'d1#', 'a':'e2','s':'f2#','d':'g2#','f':'a2','g':'b2','h':'c#','j':'d#', '0':'0' } elif tone =='F': pianokey={'1':'f1','2':'g1','3':'a1','4':'a1#','5':'c2','6':'d2','7':'e2', 'q':'f','w':'g','e':'a','r':'a#','t':'c1','y':'d1','u':'e1', 'a':'f2','s':'g2','d':'a2','f':'a2#','g':'c3','h':'d3','j':'e3', '0':'0' } elif tone =='G': pianokey={'1':'g1','2':'a1','3':'b1','4':'c2','5':'d2','6':'e2','7':'f2#', 'q':'g','w':'a','e':'b','r':'c1','t':'d1','y':'e1','u':'f1#', 'a':'g2','s':'a2','d':'b2','f':'c3','g':'d3','h':'e3','j':'f3#', '0':'0' } elif tone =='A': pianokey={'1':'a1','2':'b1','3':'c2#','4':'d2','5':'e2','6':'g2#','7':'h2#', 'q':'a','w':'b','e':'c1#','r':'d1','t':'e1','y':'g1#','u':'h1#', 'a':'a2','s':'b2','d':'c3#','f':'d3','g':'e3','h':'g3#','j':'h3#', '0':'0'} elif tone =='B': pianokey={'1':'b1','2':'c2#','3':'d2#','4':'e2','5':'f2#','6':'g2#','7':'a2#', 'q':'b','w':'c1#','e':'d1#','r':'e1','t':'f1#','y':'g1#','u':'a1#', 'a':'b2','s':'c3#','d':'d3#','f':'e3','g':'f3#','h':'g3#','j':'g3#', '0':'0' } print('基调已确定') pygame.init()#初始化? pygame.mixer.init()#初始化混音器模块 def playpiano(music): music=music.split(' ') print(music) for n in music: if n=='|': continue elif '_·'in n: pygame.mixer.Sound("audios/"+pianokey[n[0]]+ ".wav").play(maxtime = 1500) time.sleep(beat*(3/4)) elif '__'in n: pygame.mixer.Sound("audios/"+pianokey[n[0]]+ ".wav").play(maxtime = 1500) time.sleep(beat/4) elif '_'in n: pygame.mixer.Sound("audios/"+pianokey[n[0]]+ ".wav").play(maxtime = 1500) time.sleep(beat/2) elif '·'in n: pygame.mixer.Sound("audios/"+pianokey[n[0]]+ ".wav").play(maxtime = 1500) time.sleep(beat*(1.5)) elif '-'in n: k=n.count('-') pygame.mixer.Sound("audios/"+pianokey[n[0]]+ ".wav").play(maxtime = 1500) time.sleep(beat*(k+1)) else: pygame.mixer.Sound("audios/"+pianokey[n[0]]+ ".wav").play(maxtime = 1500) time.sleep(beat) playpiano(music)