import pygame
import pygame.midi

import sys
from pygame.locals import QUIT

class Miku:
    LETTERS={"ア":0,"イ":0x01,"ウ":2,"エ":3,"オ":4,\
            "カ":5,"キ":6,"ク":7,"ケ":8,"コ":9,"ガ":10,"ギ":11,"グ":12,"ゲ":13,"ゴ":14,\
            "キャ":15,"キュ":16,"キョ":17,"ギャ":18,"ギュ":19,"ギョ":20,\
            "サ":21,"スィ":22,"ス":23,"セ":24,"ソ":25,"ザ":26,"ズィ":27,"ズ":28,"ゼ":29,"ゾ":30,\
            "シャ":31,"シ":32,"シュ":33,"シェ":34,"ショ":35,"ジャ":36,"ジ":37,"ジュ":38,"ジェ":39,"ジョ":40,\
            "タ":41,"ティ":42,"トゥ":43,"テ":44,"ト":45,"ダ":46,"ディ":47,"ドゥ":48,"デ":49,"ド":50,\
            "テュ":51,"デュ":52,"チャ":53,"チ":54,"チュ":55,"チェ":56,"チョ":57,"ツァ":58,"ツィ":59,"ツ":60,"ツェ":61,"ツォ":62,\
            "ナ":63,"ニ":64,"ヌ":65,"ネ":66,"ノ":67,"ニャ":68,"ニュ":69,"ニョ":70,\
            "ハ":71,"ヒ":72,"フ":73,"ヘ":74,"ホ":75,"バ":76,"ビ":77,"ブ":78,"ベ":79,"ボ":80,\
            "パ":81,"ピ":82,"プ":83,"ペ":84,"ポ":85,"ヒャ":86,"ヒュ":87,"ヒョ":88,"ビャ":89,"ビュ":90,"ビョ":91,\
            "ピャ":92,"ピュ":93,"ピョ":94,"ファ":95,"フィ":96,"フュ":97,"フェ":98,"フォ":99,\
            "マ":100,"ミ":101,"ム":102,"メ":103,"モ":104,"ミャ":105,"ミュ":106,"ミョ":107,\
            "ヤ":108,"ユ":109,"ヨ":110,"ラ":111,"リ":112,"ル":113,"レ":114,"ロ":115,\
            "リャ":116,"リュ":117,"リョ":118,"ワ":119,"ウィ":120,"ウェ":121,"ウォ":122,"ヲ":122,"ン":123,\
            "ヅァ":26,"ヅィ":27,"ヅ":28,"ヅェ":29,"ヅォ":30}


    def __init__(self):
        pygame.init()
        pygame.midi.init()
        port = pygame.midi.get_default_output_id() #usb
        self.midi_out=pygame.midi.Output(port)
        self.midi_out.set_instrument(0)

    def note_onoff(self,pitch=60,velocity=80,onoff="on"):
        midi_out=self.midi_out
        if onoff=="on":
            midi_out.note_on(pitch, velocity)
        else:
            midi_out.note_off(pitch, velocity)

    def set_letter_list(self,letter_list):
        midi_out=self.midi_out
        output_list=[0xF0, 0x43, 0X79, 0x09, 0x11, 0x0A, 0x00]

        output_list.extend(letter_list)
        output_list.extend([0xF7])
        
        midi_out.write_sys_ex(pygame.midi.time(),output_list)

    def exit(self):
        QUIT()
