{"id":144,"date":"2024-05-31T09:34:03","date_gmt":"2024-05-31T01:34:03","guid":{"rendered":"http:\/\/blog.huanmie.fun\/?p=144"},"modified":"2024-06-04T10:47:43","modified_gmt":"2024-06-04T02:47:43","slug":"%e4%bd%bf%e7%94%a8python%e5%b0%86midi%e9%9f%b3%e9%a2%91%e8%bd%ac%e6%8d%a2%e4%b8%barom%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/blog.huanmie.fun\/index.php\/2024\/05\/31\/%e4%bd%bf%e7%94%a8python%e5%b0%86midi%e9%9f%b3%e9%a2%91%e8%bd%ac%e6%8d%a2%e4%b8%barom%e6%96%87%e4%bb%b6\/","title":{"rendered":"\u4f7f\u7528Python\u5c06MIDI\u97f3\u9891\u8f6c\u6362\u4e3aROM\u6587\u4ef6"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u529f\u80fd\u4ecb\u7ecd\uff1a<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/strong>\u8be5\u4ee3\u7801\u53ef\u5c06\u5355\u8f68\u9053\u7684midi\u6587\u4ef6\u8f6c\u6362\u4e3a\u4ee5\u6700\u5c0f\u97f3\u7b26\u957f\u5ea6\u4e3a\u5355\u4f4d\u7684ROM\u6587\u4ef6\uff08\u683c\u5f0f\u4e3aQuartus\u7684mif\u6587\u4ef6\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5177\u4f53\u8f6c\u6362\u65b9\u5f0f\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/strong>\u4f8b\u5982\u4e00\u6bb5\u7b80\u8c31\u4e3a\uff1a1\u00a022\u00a033\u00a04\uff0c\u5219\u8f93\u51fa\u4e3a\uff1a11\u00a022\u00a033\u00a044\uff0c\u5373\uff1a\u97f3\u7b26\u4e2a\u6570=\u97f3\u7b26\u957f\u5ea6\/\u6700\u5c0f\u97f3\u7b26\u957f\u5ea6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\uff1aMIDI\u6587\u4ef6\u9700\u4e3a\u5355\u8f68\u4e14\u4e3a\u5355\u97f3\u3002<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f7f\u7528\u8bf4\u660e\uff1a<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/strong>\u5c06MIDI\u6587\u4ef6\u653e\u7f6e\u5728.py\u6587\u4ef6\u76f8\u540c\u76ee\u5f55\u4e0b\uff0c\u8fd0\u884cpython\u6587\u4ef6\u5373\u53ef\u751f\u6210\uff1atest1.txt\u4e0etest2.txt\uff08midi\u97f3\u7b26\u4fe1\u606f\uff09\u3001test3.txt\uff08\u5c06\u97f3\u7b26\u957f\u5ea6\u8f6c\u6362\u4e3a\u6700\u5c0f\u97f3\u7b26\u957f\u5ea6\uff09\u3001ROM.txt\uff08\u9002\u7528\u4e0eQuartus\u7684ROM\u5e8f\u5217\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5177\u4f53\u6587\u4ef6\u5185\u5bb9\u5982\u56fe\u6240\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"952\" height=\"768\" src=\"http:\/\/blog.huanmie.fun\/wp-content\/uploads\/2024\/05\/image-17.png\" alt=\"\" class=\"wp-image-147\" srcset=\"https:\/\/blog.huanmie.fun\/wp-content\/uploads\/2024\/05\/image-17.png 952w, https:\/\/blog.huanmie.fun\/wp-content\/uploads\/2024\/05\/image-17-300x242.png 300w, https:\/\/blog.huanmie.fun\/wp-content\/uploads\/2024\/05\/image-17-768x620.png 768w\" sizes=\"auto, (max-width: 952px) 100vw, 952px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import mido\nimport os\n \n# MIDI\u97f3\u7b26\u503c\u5230\u97f3\u4e50\u97f3\u7b26\u7684\u6620\u5c04\nnotes = &#91;'C', 'Cs', 'D', 'Ds', 'E', 'F', 'Fs', 'G', 'Gs', 'A', 'As', 'B']\n# \u5220\u9664\u6587\u4ef6\nif os.path.exists(\"test1.txt\"):\n    os.remove(\"test1.txt\")\nif os.path.exists(\"test2.txt\"):\n    os.remove(\"test2.txt\")\nif os.path.exists(\"test3.txt\"):\n    os.remove(\"test3.txt\")\nif os.path.exists(\"test4.txt\"):\n    os.remove(\"test4.txt\")\n \n# \u8bfb\u53d6MIDI\u6587\u4ef6\nmid = mido.MidiFile('untitled.mid')\nmin = 12  # \u97f3\u7b26\u6700\u5c0f\u957f\u5ea6\nf = open(\"test1.txt\", \"a\")  # \u539f\u6587\u672c\u5f62\u5f0f\n# \u904d\u5386\u6bcf\u4e2a\u97f3\u8f68\nfor i, track in enumerate(mid.tracks):\n    f.write('\\n----------------------\\n')\n    last_note_on_time = {}\n    absolute_time = 0\n    last_event_time = 0\n    for msg in track:\n        # \u7d2f\u52a0\u65f6\u95f4\u6233\n        absolute_time += msg.time\n        # \u68c0\u67e5\u662f\u5426\u662f\u97f3\u7b26\u4e8b\u4ef6\n        if msg.type in &#91;'note_on', 'note_off']:\n            # \u5c06MIDI\u97f3\u7b26\u503c\u8f6c\u6362\u4e3a\u97f3\u4e50\u97f3\u7b26\n            note = notes&#91;msg.note % 12] + str((msg.note \/\/ 12)) if msg.note != 0 else '0'  # \u97f3\u7b26\n            if msg.type == 'note_on':\n                # \u5982\u679c\u8fd9\u4e2anote_on\u4e8b\u4ef6\u548c\u4e0a\u4e00\u4e2a\u4e8b\u4ef6\u4e4b\u95f4\u7684\u65f6\u95f4\u95f4\u9694\u4e0d\u4e3a0\uff0c\u90a3\u4e48\u8fd9\u4e2a\u65f6\u95f4\u95f4\u9694\u5c31\u8868\u793a\u4e86\u4e00\u4e2a\u4f11\u6b62\u7b26\n                if absolute_time - last_event_time > 0:\n                    print('\u97f3\u9ad8: 0, \u901f\u5ea6: 64, \u957f\u5ea6: {}'.format(absolute_time - last_event_time))\n                    f.write('\u97f3\u9ad8: 0, \u901f\u5ea6: 64, \u957f\u5ea6: {}\\n'.format(absolute_time - last_event_time))\n                # \u8bb0\u5f55note_on\u4e8b\u4ef6\u7684\u7edd\u5bf9\u65f6\u95f4\u6233\n                last_note_on_time&#91;note] = absolute_time\n            elif msg.type == 'note_off' and note in last_note_on_time:\n                # \u8ba1\u7b97\u97f3\u7b26\u7684\u957f\u5ea6\n                note_length = absolute_time - last_note_on_time&#91;note]\n                # \u6253\u5370\u97f3\u7b26\u7684\u97f3\u9ad8\u3001\u901f\u5ea6\u548c\u957f\u5ea6\n                print('\u97f3\u9ad8: {}, \u901f\u5ea6: {}, \u957f\u5ea6: {}'.format(note, msg.velocity, note_length))\n                # \u5c06\u97f3\u7b26\u7684\u97f3\u9ad8\u3001\u901f\u5ea6\u548c\u957f\u5ea6\u5199\u5165\u6587\u4ef6\n                f.write('\u97f3\u9ad8: {}, \u901f\u5ea6: {}, \u957f\u5ea6: {}\\n'.format(note, msg.velocity, note_length))\n                # if note_length &lt; min:\n                #    min = min\n            # \u8bb0\u5f55\u8fd9\u4e2a\u4e8b\u4ef6\u7684\u65f6\u95f4\u6233\n            last_event_time = absolute_time\nprint(\"\u6700\u5c0f\u97f3\u7b26\u957f\u5ea6\uff1a\", min)\n \nf2 = open(\"test2.txt\", \"a\")  # \u539f\u6587\u672c\u5f62\u5f0f\n# \u8bfb\u53d6\u97f3\u7b26\u6587\u4ef6\nwith open('test1.txt', 'r') as f:\n    notes = f.readlines()\n# \u904d\u5386\u6587\u4ef6\nfor i in range(0, len(notes)):\n    # \u83b7\u53d6\u97f3\u7b26\u957f\u5ea6\n    note_parts = notes&#91;i].split(' ')\n    if note_parts&#91;-1].strip().isdigit():\n        note_length = int(note_parts&#91;-1])\n        # \u5224\u65ad\u97f3\u7b26\u957f\u5ea6\u5982\u679c\u5927\u4e8e\u6700\u5c0f\u97f3\u7b26x\u500d\uff0c\u5219\u8f93\u51fax\u4e2a\u8be5\u97f3\u7b26\n        num = note_length \/ min\n        for j in range(int(num)):  # \u8f93\u51fax\u4e2a\u8be5\u97f3\u7b26\n            f2.write(notes&#91;i])  # \u5199\u5165\u6587\u4ef6\n            print(notes&#91;i])\n    else:\n        print(f\"Skipping line {i}: not a valid note\")\nf.close()\nf2.close()\n \n# \u904d\u5386\u6587\u4ef6\uff0c\u8bfb\u53d6\u97f3\u9ad8\nf2 = open(\"test2.txt\", \"r\")\nnotes = f2.readlines()\nf2.close()\nf3 = open(\"test3.txt\", \"a\")  # \u97f3\u9ad8\nfor i in range(0, len(notes)):\n    note_parts = notes&#91;i].split(' ')\n    note = note_parts&#91;1]\n    f3.write(note + '\\n')\n    print(note)\nf3.close()\n \n# \u97f3\u9ad8\u5230rom\u7684\u6620\u5c04\n# C2 -> 1, Cs2 -> 2, D2 -> 3, Ds2 -> 4, E2 -> 5, F2 -> 6, Fs2 -> 7, G2 -> 8, Gs2 -> 9, A2 -> 10, As2 -> 11, B2 -> 12,\n# C3 -> 13, Cs3 -> 14, D3 -> 15, Ds3 -> 16, E3 -> 17, F3 -> 18, Fs3 -> 19, G3 -> 20, Gs3 -> 21, A3 -> 22, As3 -> 23, B3 -> 24,\n# C4 -> 25, Cs4 -> 26, D4 -> 27, Ds4 -> 28, E4 -> 29, F4 -> 30, Fs4 -> 31, G4 -> 32, Gs4 -> 33, A4 -> 34, As4 -> 35, B4 -> 36,\n# C5 -> 37, Cs5 -> 38, D5 -> 39, Ds5 -> 40, E5 -> 41, F5 -> 42, Fs5 -> 43, G5 -> 44, Gs5 -> 45, A5 -> 46, As5 -> 47, B5 -> 48,\n# C6 -> 49, Cs6 -> 50, D6 -> 51, Ds6 -> 52, E6 -> 53, F6 -> 54, Fs6 -> 55, G6 -> 56, Gs6 -> 57, A6 -> 58, As6 -> 59, B6 -> 60,\n# LL -> 0\n \nrom_notes = &#91;'0,', 'C4,', 'Cs4,', 'D4,', 'Ds4,', 'E4,', 'F4,', 'Fs4,', 'G4,', 'Gs4,', 'A4,', 'As4,', 'B4,',\n             'C5,', 'Cs5,', 'D5,', 'Ds5,', 'E5,', 'F5,', 'Fs5,', 'G5,', 'Gs5,', 'A5,', 'As5,', 'B5,',\n            ]\n# \u904d\u5386\u6587\u4ef6\uff0c\u8bfb\u53d6\u97f3\u9ad8\nf3 = open(\"test3.txt\", \"r\")\nnotes = f3.readlines()\n# \u5c06\u97f3\u9ad8\u8f6c\u6362\u4e3arom\nf4 = open(\"ROM.txt\", \"a\")  # rom\nfor i in range(0, len(notes)):\n    note = notes&#91;i].strip()\n    if note == '0':\n        rom = 'LL'\n    else:\n        rom = rom_notes.index(note) + 1\n    f4.write(str(i) + \" : \" + str(rom) + ';\\n')\n    print(rom)\nprint(\"\u6700\u5c0f\u97f3\u7b26\u957f\u5ea6\uff1a\", min)\nf3.close()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u529f\u80fd\u4ecb\u7ecd\uff1a \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u8be5\u4ee3\u7801\u53ef\u5c06\u5355\u8f68\u9053\u7684midi\u6587\u4ef6\u8f6c\u6362\u4e3a\u4ee5\u6700\u5c0f\u97f3\u7b26\u957f\u5ea6\u4e3a\u5355\u4f4d\u7684ROM\u6587\u4ef6\uff08\u683c\u5f0f\u4e3aQuartus\u7684mif\u6587\u4ef6 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":147,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[6],"tags":[],"class_list":["post-144","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/blog.huanmie.fun\/index.php\/wp-json\/wp\/v2\/posts\/144","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.huanmie.fun\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.huanmie.fun\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.huanmie.fun\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.huanmie.fun\/index.php\/wp-json\/wp\/v2\/comments?post=144"}],"version-history":[{"count":3,"href":"https:\/\/blog.huanmie.fun\/index.php\/wp-json\/wp\/v2\/posts\/144\/revisions"}],"predecessor-version":[{"id":148,"href":"https:\/\/blog.huanmie.fun\/index.php\/wp-json\/wp\/v2\/posts\/144\/revisions\/148"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.huanmie.fun\/index.php\/wp-json\/wp\/v2\/media\/147"}],"wp:attachment":[{"href":"https:\/\/blog.huanmie.fun\/index.php\/wp-json\/wp\/v2\/media?parent=144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.huanmie.fun\/index.php\/wp-json\/wp\/v2\/categories?post=144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.huanmie.fun\/index.php\/wp-json\/wp\/v2\/tags?post=144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}