#C4音对象 n = Note('C') #变为C5 n.set_note('C', 5) #音对象属性 n.name #十二音名 n.octave #第几八度 n.dynamics #其它属性 #音对象方法 int(n) #音对象的数值 c = Note() c.from_int(12) #使用数值创建音对象 c.octave_up() #升八度 c.octave_down() #降八度 c.change_octave(2) #升n八度 c.transpose('3', up=True) #向上升三度 c.augment() #升半音 c.diminish() #降半音 c.remove_redundant_accidentals() #清理多余升降号(只能成对清理,烂) #谱容器 from mingus.containers import NoteContainer #创建谱容器对象(继承列表,完全可以按列表操作,不用看下面的) n = NoteContainer(['A-3', 'C-5', 'B-4']) n.add_note('F-1') # 0位加音 n.remove_note('B', 4) #删音 n.empty() #清空 #乐器音色 from mingus.containers.instrument import Instrument, Piano, Guitar #创建乐器对象
def play_Music(filename): f = open(filename, 'rb') data = json.loads(f.read(), encoding='utf8') f.close() n = data['音符'] h = data['音高'] r = data['节拍'] l = data['组成'] k = data['调性'] t = Track() b = Bar('C', (4, 4)) b.place_rest(1) t.add_bar(b) name = 'CDEFGAB' symbol = '!@#$%^&' def tran(x): if x >= 'a': return ord(x) - 87 elif x == '0': return 0.5 else: return float(x) f = open(filename, 'rb') data = json.loads(f.read(), encoding='utf8') f.close() n = data['音符'] h = data['音高'] r = data['节拍'] l = data['组成'] k = data['调性'] t = Track() b = Bar('C', (4, 4)) b.place_rest(1) t.add_bar(b) name = 'CDEFGAB' symbol = '!@#$%^&' for i in range(len(l)): rn = list(map(tran, r[l[i]])) b = Bar('C', (4 * sum(rn)/8, 4)) for j in range(len(n[l[i]])): if n[l[i]][j] == '0': b.place_rest(8 / rn[j]) else: x = symbol.find(n[l[i]][j]) if x == -1: x = int(n[l[i]][j]) - 1 y = name[x] else: y = name[x] + '#' print(y) note = Note(y, int(h[l[i]][j])) note.transpose(k[i]) b.place_notes(note, 8 / rn[j]) t.add_bar(b) t2 = Track() b = Bar('C', (4, 4)) b.place_rest(1) t2.add_bar(b) for i in range(int(sum(map(sum, map(lambda x: map(tran, r[x]), l)))) // 8): b = Bar('C', (4, 4)) b.place_notes('C-3', 4) b.place_notes('C-7', 4) b.place_notes('C-5', 4) b.place_notes('C-7', 4) t2.add_bar(b) m = MidiFile() mt = MidiTrack(150) mt2 = MidiTrack(150) mt3 = MidiTrack(150) m.tracks = [mt,mt2,mt3] mt.set_instrument(1, 25) mt.play_Track(t) # for _, _, i in t2.get_notes(): # if i is not None: # i[0].set_channel(2) # mt2.set_instrument(2, 115) # mt2.play_Track(t2) # for _, _, i in t.get_notes(): # if i is not None: # i[0].set_channel(3) # mt3.set_instrument(3, 100) # mt3.track_data += mt3.controller_event(3, 7, 30) # mt3.play_Track(t) # m.write_file('D:/test.midi', False) # for i in range(len(l)): # rn = list(map(tran, r[l[i]])) # b = Bar('C', (4 * sum(rn) / 8, 4)) # for j in range(len(n[l[i]])): # # if i==0 and j==0: # # b.place_notes('D-4', 3) # # el # if n[l[i]][j] == '0': # b.place_rest(1 / rn[j]) # else: # x = symbol.find(n[l[i]][j]) # if x == -1: # x = int(n[l[i]][j]) - 1 # y = name[x] # else: # y = name[x] + '#' # print(y) # # note = Note(y, int(h[l[i]][j])) # note.transpose(k[i]) # #print(note) # print(rn[j]) # #print(b) # b.place_notes(note, 1 / rn[j]) # t.add_bar(b) # print(b) fluidsynth.init("D:\MyCode\MyPython\AiMusicCoach\GeneralUserSoftSynth\GeneralUserSoftSynth.sf2") fluidsynth.set_instrument(1, 1) #24=Nylon Guitar # 25=Steel Guitar # 26=Jazz Guitar # 27=Clean Guitar # 28=Muted Guitar # 29=Overdrive Guitar # 30=Distortion Guitar m.write_file('D:\MyCode\MyPython\AiMusicCoach\Backend\music_file\mysong.midi', False) os.system("d: && cd D:\\MyCode\\MyPython\\AiMusicCoach\\fluidsynth-x64\\bin && fluidsynth -F mysong.wav D:/MyCode/MyPython/AiMusicCoach/GeneralUserSoftSynth/GeneralUserSoftSynth.sf2 D:\MyCode\MyPython\AiMusicCoach\Backend\music_file\mysong.midi") fluidsynth.play_Track(t, channel=1, bpm=150)