Created
July 24, 2021 17:23
-
-
Save tatmos/526cb47e3aa46c3d312ad886528a9b1a to your computer and use it in GitHub Desktop.
MIDIからノートオンの情報のみで■を表示する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
from pygame.locals import * | |
import sys | |
import datetime | |
import asyncio | |
import mido | |
from mido import MidiFile | |
MIDI_FILE = "Musicbox.mid" | |
mid = MidiFile(MIDI_FILE) | |
outText = "" | |
prevTime = 0 | |
duration = 20 | |
notes = [] | |
for i, track in enumerate(mid.tracks): | |
print('Track {}: {}'.format(i, track.name)) | |
absolute_time = 0 | |
for msg in track: | |
if msg.type == 'set_tempo': | |
tempo = msg.tempo | |
print("temo is {0}".format(tempo)) | |
if msg.time > 0: | |
absolute_time = absolute_time + mido.tick2second(msg.time,480,tempo) * 1000 | |
#note onのみ | |
if i == 0: | |
if msg.type == 'note_on': | |
if msg.velocity > 0: | |
print(absolute_time) | |
curNote = (msg.note - 24) + 36 | |
notes.append((absolute_time,curNote - 72-6)) | |
print(str(notes[-1])) | |
prevTime = absolute_time + duration | |
args = sys.argv | |
pygame.init() | |
SURFACE = pygame.display.set_mode((400, 300)) # サイズを指定して画面を作成 | |
pygame.display.set_caption("Musicbox_Visualize") # タイトル文字を指定 | |
font = pygame.font.Font(None, 60) # フォントの設定(60px) | |
noteIndex = 0 | |
async def go(note): | |
global SURFACE | |
sz = 10 | |
dx = 8 | |
y = 300 | |
print(note) | |
pygame.draw.line(SURFACE, (102, 205, 170), | |
(note[1] * sz, y - 300), | |
(note[1] * sz, y + sz), sz) | |
pygame.display.update() | |
await asyncio.sleep(0.1) | |
pygame.draw.line(SURFACE, (0, 0, 0), | |
(note[1] * sz, y - 300), | |
(note[1] * sz, y + sz), sz) | |
pygame.display.update() | |
# メイン関数 | |
def main(): | |
global noteIndex | |
# 表示更新ループ | |
while True: | |
SURFACE.fill((0,0,0)) # 背景(壁)を緑色で塗りつぶす | |
curTime = pygame.time.get_ticks()-1000 | |
if notes[noteIndex][0] < curTime: | |
asyncio.run(go(notes[noteIndex])) | |
noteIndex+=1 | |
pygame.display.update() # 画面更新 | |
# イベントを処理 | |
for event in pygame.event.get(): | |
if event.type == QUIT: # 閉じるボタンが押されたら終了 | |
pygame.quit() # Pygameの終了(画面を閉じる) | |
sys.exit() # プログラムの終了 | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment