目录
这两天,我非常无聊,琢磨出了十二平均律,并利用mido库进行编写,实现了利用python演奏音乐。废话不多说了,直接上代码,供诸位大佬把玩。
1.代码
import os
import threading
import time
import mido
import pygame
mid = mido.MidiFile()
track = mido.MidiTrack()
mid.tracks.append(track)
def play_midi(file):
freq = 44100
bitsize = -16
channels = 2
buffer = 1024
pygame.mixer.init(freq, bitsize, channels, buffer)
pygame.mixer.music.set_volume(1)
clock = pygame.time.Clock()
try:
pygame.mixer.music.load(file)
except:
import traceback
print(traceback.format_exc())
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
clock.tick(30)
KEY = 1
neg_SPEED = 1.2 # 数值越小速度越快
# bpm = \frac{60000000}{tempo}
def music(note, base_num, base_time):
base_num, base_time = base_num + KEY, base_time * neg_SPEED
# mid = mido.MidiFile()
# track = mido.MidiTrack()
# mid.tracks.append(track)
# meta_time = 60 * 60 * 10 / bpm
major_notes = [0, 2, 2, 1, 2, 2, 2, 1]
base_note = 60
track.append(mido.Message('note_on', note=base_note + base_num * 12 + sum(major_notes[0:note]), velocity=96, time=0,
channel=1))
track.append(mido.Message('note_off', note=base_note + base_num * 12 + sum(major_notes[0:note]), velocity=96,
time=int(480 * base_time), channel=1))
# print('正在合成(',note, base_num, base_time,')音')
# track.append(mido.Message('note_on', note=base_note, velocity=96, time=0))
# track.append(mido.Message('note_off', note=base_note, velocity=96, time=480*base_time))
# mid.save('1.mid')
# play_midi('1.mid')
def lemon_music():
a = 0
b = 1
# 前奏
# music(1, a+b + 1, 2)
music(1, a + 1, 1 * 2)
music(6, a + 0, 0.5 * 2)
music(1, a + 1, 0.5 * 2)
music(3, a + 1, 1)
music(3, a + 1, 0.5)
music(2, a + 1, 0.5)
music(3, a + 1, 0.5)
music(2, a + 1, 0.5)
music(3, a + 1, 0.5)
music(2, a + 1, 0.5)
music(1, a + 1, 0.5)
music(7, a + 0, 0.5)
music(6, a + 0, 0.5)
music(5, a + 0, 0.5)
music(6, a + 0, 1)
music(7, a + 0, 0.5)
music(1, a + 1, 0.5)
music(2, a + 1, 1)
music(3, a + 1, 0.5)
music(4, a + 1, 0.5)
music(5, a + 1, 2)
music(3, a + 1, 0.5)
music(5, a + 1, 1)
music(2, a + 1, 0.5)
music(3, a + b + 1, 0.25)
music(1, a + b + 1, 0.25)
music(6, a + b + 0, 0.25)
music(3, a + b + 0, 0.25)
music(3, a + b + 1, 0.25)
music(1, a + b + 1, 0.25)
music(6, a + b + 0, 0.25)
music(3, a + b + 0, 0.25)
music(3, a + b + 1, 0.25)
music(1, a + b + 1, 0.25)
music(6, a + b + 0, 0.25)
music(3, a + b + 0, 0.25)
music(3, a + b + 1, 0.25)
music(1, a + b + 1, 0.25)
music(6, a + b + 0, 0.25)
music(3, a + b + 0, 0.25)
music(2, a + b + 1, 0.25)
music(7, a + b + 0, 0.25)
music(5, a + b + 0, 0.25)
music(2, a + b + 0, 0.25)
music(2, a + b + 1, 0.25)
music(7, a + b + 0, 0.25)
music(5, a + b + 0, 0.25)
music(2, a + b + 0, 0.25)
music(2, a + b + 1, 0.25)
music(7, a + b + 0, 0.25)
music(5, a + b + 0, 0.25)
music(2, a + b + 0, 0.25)
music(2, a + b + 1, 0.25)
music(7, a + b + 0, 0.25)
music(5, a + b + 0, 0.25)
music(2, a + b + 0, 0.25)
music(1, a + b + 1, 0.25)
music(6, a + b + 0, 0.25)
music(4, a + b + 0, 0.25)
music(1, a + b + 0, 0.25)
music(1, a + b + 1, 0.25)
music(6, a + b + 0, 0.25)
music(4, a + b + 0, 0.25)
music(1, a + b + 0, 0.25)
music(7, a + b + 0, 0.25)
music(5, a + b + 0, 0.25)
music(2, a + b + 0, 0.25)
music(7, a + b + 0, 0.25)
music(7, a + b + 0, 0.25)
music(5, a + b + 0, 0.25)
music(2, a + b + 0, 0.25)
music(7, a + b + 0, 0.25)
music(1, a + b + 1, 4)
# 主歌
track.append(mido.Message('note_on', note=0, velocity=0, time=0))
track.append(mido.Message('note_off', note=0, velocity=0, time=480 * 1))
music(5, a - 1, 1)
music(6, a - 1, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(3, a + 0, 1)
music(3, a + 0, 1)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(3, a + 0, 1)
music(3, a + 0, 1)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.75)
music(1, a + 0, 0.75)
music(6, a - 1, 0.5)
music(1, a + 0, 0.75)
music(2, a + 0, 0.75)
music(3, a + 0, 0.5)
music(3, a + 0, 1.5)
music(5, a - 1, 0.5)
music(6, a - 1, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 1)
music(5, a + 0, 1)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 1)
music(3, a + 0, 1)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.75)
music(2, a + 0, 0.75)
music(2, a + 0, 0.5)
music(2, a + 0, 0.75)
music(1, a + 0, 0.75)
music(6, a - 1, 0.5)
music(2, a + 0, 1.5)
music(5, a - 1, 0.5)
music(6, a - 1, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(3, a + 0, 1)
music(5, a + 0, 1)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(3, a + 0, 1)
music(6, a + 0, 1)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.75)
music(1, a + 0, 0.75)
music(6, a - 1, 0.5)
music(1, a + 0, 0.75)
music(2, a + 0, 0.75)
music(3, a + 0, 0.5)
music(5, a + 0, 1.5)
music(5, a - 1, 0.5)
music(6, a - 1, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 1)
music(6, a + 0, 1)
music(5, a + 0, 0.5)
music(3, a + 0, 0.5)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 1)
music(3, a + 0, 1)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.75)
music(2, a + 0, 0.75)
music(2, a + 0, 0.5)
music(2, a + 0, 0.75)
music(1, a + 0, 0.75)
music(6, a - 1, 0.5)
music(1, a + 0, 1.5)
# 副歌
music(1, a + 0, 0.5)
music(1, a + 1, 0.5)
music(7, a + 0, 0.5)
music(1, a + 1, 0.5)
music(6, a + 0, 0.5)
music(1, a + 1, 0.75)
music(6, a + 0, 0.75)
music(5, a + 0, 0.5)
music(6, a + 0, 0.5)
music(5, a + 0, 0.5)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(3, a + 0, 0.5)
music(5, a + 0, 0.5)
music(6, a + 0, 0.5)
music(5, a + 0, 1)
music(5, a + 0, 0.5)
music(6, a + 0, 0.5)
music(5, a + 0, 0.5)
music(6, a + 0, 0.75)
music(6, a + 0, 0.75)
music(6, a + 0, 0.5)
music(6, a + 0, 0.75)
music(5, a + 0, 0.75)
music(6, a + 0, 0.5)
music(5, a + 0, 0.5)
music(3, a + 0, 1.5)
track.append(mido.Message('note_on', note=0, velocity=0, time=0))
track.append(mido.Message('note_off', note=0, velocity=0, time=480 * 1))
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(3, a + 0, 0.5)
music(5, a + 0, 0.5)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 1)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(3, a + 0, 0.5)
music(6, a + 0, 0.5)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 1)
music(1, a + 0, 0.5)
music(1, a + 0, 0.5)
music(1, a + 1, 0.5)
music(1, a + 1, 0.75)
music(1, a + 1, 0.75)
music(1, a + 1, 0.5)
music(6, a + 0, 0.75)
music(1, a + 1, 0.5)
music(6, a + 0, 1.5)
music(5, a + 0, 1)
music(1, a + 0, 0.5)
music(1, a + 1, 0.5)
music(7, a + 0, 0.5)
music(1, a + 1, 0.5)
music(7, a + 0, 0.5)
music(6, a + 0, 0.5)
music(5, a + 0, 0.5)
music(6, a + 0, 0.5)
music(5, a + 0, 0.5)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(3, a + 0, 0.5)
music(5, a + 0, 0.5)
music(6, a + 0, 0.5)
music(5, a + 0, 1)
music(5, a + 0, 0.5)
music(6, a + 0, 0.5)
music(5, a + 0, 0.5)
music(6, a + 0, 0.75)
music(6, a + 0, 0.75)
music(6, a + 0, 0.5)
music(6, a + 0, 0.75)
music(5, a + 0, 0.75)
music(6, a + 0, 0.5)
music(5, a + 0, 0.5)
music(3, a + 0, 1.5)
track.append(mido.Message('note_on', note=0, velocity=0, time=0))
track.append(mido.Message('note_off', note=0, velocity=0, time=480 * 1))
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(3, a + 0, 0.5)
music(5, a + 0, 0.5)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 1)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(3, a + 0, 0.5)
music(6, a + 0, 0.5)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 0.5)
music(1, a + 1, 2)
track.append(mido.Message('note_on', note=0, velocity=0, time=0))
track.append(mido.Message('note_off', note=0, velocity=0, time=480 * 3))
music(1, a + 1, 0.5)
music(6, a + 0, 0.5)
music(1, a + 1, 0.5)
music(6, a + 0, 0.5)
music(5, a + 0, 1)
music(5, 0, 0.5)
music(6, a + 0, 0.5)
music(5, a + 0, 0.5)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(3, a + 0, 0.5)
music(2, a + 0, 1)
music(1, a + 0, 0.5)
music(6, a - 1, 0.5)
music(1, a + 0, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 4)
# 第二段
track.append(mido.Message('note_on', note=0, velocity=0, time=0))
track.append(mido.Message('note_off', note=0, velocity=0, time=480 * 1))
music(5, a - 1, 1)
music(6, a - 1, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(3, a + 0, 1)
music(3, a + 0, 1)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(3, a + 0, 1)
music(3, a + 0, 1)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.75)
music(1, a + 0, 0.75)
music(6, a - 1, 0.5)
music(1, a + 0, 0.75)
music(2, a + 0, 0.75)
music(3, a + 0, 0.5)
music(3, a + 0, 1.5)
music(5, a - 1, 0.5)
music(6, a - 1, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 1)
music(5, a + 0, 1)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 1)
music(3, a + 0, 1)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.75)
music(2, a + 0, 0.75)
music(2, a + 0, 0.5)
music(2, a + 0, 0.75)
music(1, a + 0, 0.75)
music(6, a - 1, 0.5)
music(2, a + 0, 1.5)
music(5, a - 1, 0.5)
music(6, a - 1, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(3, a + 0, 1)
music(5, a + 0, 1)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(3, a + 0, 1)
music(6, a + 0, 1)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.75)
music(1, a + 0, 0.75)
music(6, a - 1, 0.5)
music(1, a + 0, 0.75)
music(2, a + 0, 0.75)
music(3, a + 0, 0.5)
music(5, a + 0, 1.5)
music(5, a - 1, 0.5)
music(6, a - 1, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 1)
music(6, a + 0, 1)
music(5, a + 0, 0.5)
music(3, a + 0, 0.5)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 1)
music(3, a + 0, 1)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.75)
music(2, a + 0, 0.75)
music(2, a + 0, 0.5)
music(2, a + 0, 0.75)
music(1, a + 0, 0.75)
music(6, a - 1, 0.5)
music(1, a + 0, 1.5)
music(1, a + 0, 0.5)
music(1, a + 1, 0.5)
music(7, a + 0, 0.5)
music(1, a + 1, 0.5)
music(6, a + 0, 0.5)
music(1, a + 1, 0.75)
music(6, a + 0, 0.75)
music(5, a + 0, 0.5)
music(6, a + 0, 0.5)
music(5, a + 0, 0.5)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(3, a + 0, 0.5)
music(5, a + 0, 0.5)
music(6, a + 0, 0.5)
music(5, a + 0, 1)
music(5, a + 0, 0.5)
music(6, a + 0, 0.5)
music(5, a + 0, 0.5)
music(6, a + 0, 0.75)
music(6, a + 0, 0.75)
music(6, a + 0, 0.5)
music(6, a + 0, 0.75)
music(5, a + 0, 0.75)
music(6, a + 0, 0.5)
music(5, a + 0, 0.5)
music(3, a + 0, 1.5)
track.append(mido.Message('note_on', note=0, velocity=0, time=0))
track.append(mido.Message('note_off', note=0, velocity=0, time=480 * 1))
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(3, a + 0, 0.5)
music(5, a + 0, 0.5)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 1)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(3, a + 0, 0.5)
music(6, a + 0, 0.5)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 1)
music(1, a + 0, 0.5)
music(1, a + 0, 0.5)
music(1, a + 1, 0.5)
music(1, a + 1, 0.75)
music(1, a + 1, 0.75)
music(1, a + 1, 0.5)
music(6, a + 0, 0.75)
music(1, a + 1, 0.5)
music(6, a + 0, 1.5)
music(5, a + 0, 1)
music(1, a + 0, 0.5)
music(1, a + 1, 0.5)
music(7, a + 0, 0.5)
music(1, a + 1, 0.5)
music(7, a + 0, 0.5)
music(6, a + 0, 0.5)
music(5, a + 0, 0.5)
music(6, a + 0, 0.5)
music(5, a + 0, 0.5)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(3, a + 0, 0.5)
music(5, a + 0, 0.5)
music(6, a + 0, 0.5)
music(5, a + 0, 1)
music(5, a + 0, 0.5)
music(6, a + 0, 0.5)
music(5, a + 0, 0.5)
music(6, a + 0, 0.75)
music(6, a + 0, 0.75)
music(6, a + 0, 0.5)
music(6, a + 0, 0.75)
music(5, a + 0, 0.75)
music(6, a + 0, 0.5)
music(5, a + 0, 0.5)
music(3, a + 0, 1.5)
track.append(mido.Message('note_on', note=0, velocity=0, time=0))
track.append(mido.Message('note_off', note=0, velocity=0, time=480 * 1))
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(3, a + 0, 0.5)
music(5, a + 0, 0.5)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 1)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(3, a + 0, 0.5)
music(6, a + 0, 0.5)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 0.5)
music(1, a + 1, 2)
# 尾部
track.append(mido.Message('note_on', note=0, velocity=0, time=0))
track.append(mido.Message('note_off', note=0, velocity=0, time=480 * 3))
music(1, a + 1, 0.5)
music(6, a + 0, 0.5)
music(1, a + 1, 0.5)
music(6, a + 0, 0.5)
music(5, a + 0, 1)
music(5, 0, 0.5)
music(6, a + 0, 0.5)
music(5, a + 0, 0.5)
music(3, a + 0, 0.5)
music(2, a + 0, 0.5)
music(3, a + 0, 0.5)
music(2, a + 0, 1)
music(1, a + 0, 0.5)
music(6, a - 1, 0.5)
music(1, a + 0, 0.5)
music(1, a + 0, 0.5)
music(2, a + 0, 0.5)
music(1, a + 0, 2)
music(1, a + 0, 0.5)
music(1, a + 1, 0.5)
music(6, a + 0, 0.5)
music(1, a + 1, 0.5)
music(2, a + 1, 0.5)
music(1, a + 1, 1)
music(1, a + 1, 0.5)
music(2, a + 1, 0.5)
music(1, a + 1, 0.5)
music(6, a + 0, 0.5)
music(1, a + 1, 0.5)
music(3, a + 1, 0.5)
music(2, a + 1, 1)
music(1, a + 1, 0.5)
music(6, a + 0, 0.5)
music(1, a + 1, 0.5)
music(2, a + 1, 1)
music(1, a + 1, 4)
lemon_music()
mid.save('a2.mid')
def play():
play_midi('a2.mid')
def show():
for i in f.readlines():
print("\033[0;34;40m%s\033[0m" % i)
time.sleep(len(i) / 3)
os.system('cls')
if __name__ == '__main__':
f = open('lyrics.txt', 'r', encoding='utf-8')
t1 = threading.Thread(target=play)
t2 = threading.Thread(target=show)
t1.start()
t2.start()
os.remove('a2.mid')
2.文本
歌名:富士山下
演唱:陈奕迅
作词:林夕
作曲:泽日生
代码编写:***
联系方式:***********
拦路雨偏似雪花 饮泣的你冻吗
这风褛我给你磨到有襟花
连掉了渍也不怕 怎么始终牵挂
苦心选中今天想车你回家
原谅我不再送花 伤口应要结疤
花瓣铺满心里坟场才害怕
如若你非我不嫁 彼此终必火化
一生一世等一天需要代价
谁都只得那双手 靠拥抱亦难任你拥有
要拥有必先懂失去怎接受
曾沿着雪路浪游 为何为好事泪流
谁能凭爱意要富士山私有
何不把悲哀感觉 假设是来自你虚构
试管里找不到它染污眼眸
前尘硬化像石头 随缘地抛下便逃走
我绝不罕有 往街里绕过一周 我便化乌有
情人节不要说穿 只敢抚你发端
这种姿态可会令你更心酸
留在汽车里取暖 应该怎么规劝
怎么可以将手腕忍痛划损
人活到几岁算短 失恋只有更短
归家需要几里路谁能预算
忘掉我跟你恩怨 樱花开了几转
东京之旅一早比一世遥远
谁都只得那双手 靠拥抱亦难任你拥有
要拥有必先懂失去怎接受
曾沿着雪路浪游 为何为好事泪流
谁能凭爱意要富士山私有
何不把悲哀感觉 假设是来自你虚构
试管里找不到它染污眼眸
前尘硬化像石头 随缘地抛下便逃走
我绝不罕有 往街里绕过一周 我便化乌有
你还嫌不够 我把这陈年风褛 送赠你解咒
感谢收听!
3.效果图