{"id":1169949,"date":"2025-01-15T16:16:41","date_gmt":"2025-01-15T08:16:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1169949.html"},"modified":"2025-01-15T16:16:44","modified_gmt":"2025-01-15T08:16:44","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%bc%96%e5%86%99%e6%ad%8c%e6%9b%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1169949.html","title":{"rendered":"\u5982\u4f55\u7528python\u7f16\u5199\u6b4c\u66f2"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26070804\/fc36090d-51d3-4100-ace0-444111c25f44.webp\" alt=\"\u5982\u4f55\u7528python\u7f16\u5199\u6b4c\u66f2\" \/><\/p>\n<p><p> \u7528Python\u7f16\u5199\u6b4c\u66f2\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4e3b\u8981\u5305\u62ec<strong>\u4f7f\u7528\u97f3\u9891\u5904\u7406\u5e93\u3001\u751f\u6210MIDI\u6587\u4ef6\u3001\u5408\u6210\u97f3\u9891\u4fe1\u53f7<\/strong>\u7b49\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528MIDI\u6587\u4ef6\u751f\u6210\u5de5\u5177\u2014\u2014<code>mido<\/code>\u5e93\u8fdb\u884c\u6b4c\u66f2\u7f16\u5199\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5f15\u8a00<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7f16\u5199\u6b4c\u66f2\u53ef\u4ee5\u5b9e\u73b0\u5404\u79cd\u97f3\u4e50\u521b\u4f5c\u548c\u5904\u7406\u4efb\u52a1\u3002<strong>\u901a\u8fc7\u97f3\u9891\u5904\u7406\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u52a0\u8f7d\u548c\u7f16\u8f91\u73b0\u6709\u97f3\u9891\u6587\u4ef6<\/strong>\uff0c<strong>\u751f\u6210MIDI\u6587\u4ef6\u5219\u53ef\u4ee5\u521b\u5efa\u65b0\u7684\u97f3\u4e50<\/strong>\uff0c<strong>\u5408\u6210\u97f3\u9891\u4fe1\u53f7\u5219\u53ef\u4ee5\u4ece\u57fa\u7840\u9891\u7387\u4fe1\u53f7\u5f00\u59cb\u521b\u5efa\u58f0\u97f3<\/strong>\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u91cd\u70b9\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>mido<\/code>\u5e93\u6765\u751f\u6210MIDI\u6587\u4ef6\uff0c\u4ee5\u5b9e\u73b0\u6b4c\u66f2\u7f16\u5199\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b89\u88c5\u548c\u914d\u7f6e\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7f16\u5199\u6b4c\u66f2\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5\u4e00\u4e9b\u5fc5\u8981\u7684\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5<code>mido<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install mido<\/p>\n<p>pip install python-rtmidi<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>mido<\/code>\u662f\u4e00\u4e2a\u65b9\u4fbf\u7684MIDI\u5e93\uff0c<code>python-rtmidi<\/code>\u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684\u5b9e\u65f6MIDI\u8f93\u5165\u8f93\u51fa\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001MIDI\u57fa\u7840\u77e5\u8bc6<\/h3>\n<\/p>\n<p><h4>1\u3001\u4ec0\u4e48\u662fMIDI\uff1f<\/h4>\n<\/p>\n<p><p>MIDI\uff08Musical Instrument Digital Interface\uff09\u662f\u4e00\u79cd\u901a\u4fe1\u534f\u8bae\uff0c\u5141\u8bb8\u7535\u5b50\u4e50\u5668\u3001\u8ba1\u7b97\u673a\u548c\u5176\u4ed6\u8bbe\u5907\u4e92\u76f8\u4f20\u9012\u97f3\u4e50\u4fe1\u606f\u3002MIDI\u6587\u4ef6\u4e0d\u5305\u542b\u5b9e\u9645\u7684\u97f3\u9891\u6570\u636e\uff0c\u800c\u662f\u5305\u542b\u4e00\u7cfb\u5217\u6307\u4ee4\uff0c\u4f8b\u5982\u54ea\u4e00\u4e2a\u97f3\u7b26\u88ab\u6f14\u594f\u3001\u6f14\u594f\u7684\u65f6\u957f\u3001\u97f3\u91cf\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2\u3001MIDI\u6d88\u606f<\/h4>\n<\/p>\n<p><p>MIDI\u6d88\u606f\u662fMIDI\u534f\u8bae\u4e2d\u7684\u57fa\u672c\u5355\u4f4d\u3002\u5e38\u89c1\u7684MIDI\u6d88\u606f\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>Note On<\/strong>\uff1a\u6307\u793a\u67d0\u4e2a\u97f3\u7b26\u5f00\u59cb\u6f14\u594f<\/li>\n<li><strong>Note Off<\/strong>\uff1a\u6307\u793a\u67d0\u4e2a\u97f3\u7b26\u505c\u6b62\u6f14\u594f<\/li>\n<li><strong>Control Change<\/strong>\uff1a\u6539\u53d8\u67d0\u4e2a\u63a7\u5236\u5668\u7684\u72b6\u6001\uff0c\u4f8b\u5982\u97f3\u91cf\u3001\u8e0f\u677f\u7b49<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u4f7f\u7528<code>mido<\/code>\u5e93\u751f\u6210MIDI\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u4e00\u4e2aMIDI\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>mido<\/code>\u5e93\u521b\u5efa\u4e00\u4e2aMIDI\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0Note On\u548cNote Off\u6d88\u606f\u6765\u751f\u6210\u97f3\u4e50\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mido<\/p>\n<p>from mido import MidiFile, MidiTrack, Message<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684MIDI\u6587\u4ef6<\/strong><\/h2>\n<p>midi = MidiFile()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8f68\u9053<\/strong><\/h2>\n<p>track = MidiTrack()<\/p>\n<p>midi.tracks.append(track)<\/p>\n<h2><strong>\u6dfb\u52a0Note On\u548cNote Off\u6d88\u606f<\/strong><\/h2>\n<p>track.append(Message(&#39;note_on&#39;, note=64, velocity=64, time=0))<\/p>\n<p>track.append(Message(&#39;note_off&#39;, note=64, velocity=64, time=480))<\/p>\n<h2><strong>\u4fdd\u5b58MIDI\u6587\u4ef6<\/strong><\/h2>\n<p>midi.save(&#39;example.mid&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684MIDI\u6587\u4ef6\u548c\u8f68\u9053\uff0c\u5e76\u5728\u8f68\u9053\u4e2d\u6dfb\u52a0\u4e86Note On\u548cNote Off\u6d88\u606f\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c06MIDI\u6587\u4ef6\u4fdd\u5b58\u4e3a<code>example.mid<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6dfb\u52a0\u591a\u4e2a\u97f3\u7b26<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u591a\u4e2aNote On\u548cNote Off\u6d88\u606f\u6765\u521b\u5efa\u66f4\u590d\u6742\u7684\u97f3\u4e50\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u97f3\u9636\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mido<\/p>\n<p>from mido import MidiFile, MidiTrack, Message<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684MIDI\u6587\u4ef6<\/strong><\/h2>\n<p>midi = MidiFile()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8f68\u9053<\/strong><\/h2>\n<p>track = MidiTrack()<\/p>\n<p>midi.tracks.append(track)<\/p>\n<h2><strong>\u6dfb\u52a0\u97f3\u9636<\/strong><\/h2>\n<p>notes = [60, 62, 64, 65, 67, 69, 71, 72]<\/p>\n<p>for note in notes:<\/p>\n<p>    track.append(Message(&#39;note_on&#39;, note=note, velocity=64, time=0))<\/p>\n<p>    track.append(Message(&#39;note_off&#39;, note=note, velocity=64, time=480))<\/p>\n<h2><strong>\u4fdd\u5b58MIDI\u6587\u4ef6<\/strong><\/h2>\n<p>midi.save(&#39;scale.mid&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6dfb\u52a0\u66f4\u591a\u97f3\u4e50\u5143\u7d20<\/h3>\n<\/p>\n<p><h4>1\u3001\u8bbe\u7f6e\u8282\u62cd\u548c\u901f\u5ea6<\/h4>\n<\/p>\n<p><p>\u5728MIDI\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u8282\u62cd\uff08Time Signature\uff09\u548c\u901f\u5ea6\uff08Tempo\uff09\u6765\u63a7\u5236\u97f3\u4e50\u7684\u8282\u594f\u548c\u901f\u5ea6\u3002\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u8bbe\u7f6e4\/4\u62cd\u548c\u6bcf\u5206\u949f120\u62cd\u7684\u901f\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mido<\/p>\n<p>from mido import MidiFile, MidiTrack, Message, MetaMessage<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684MIDI\u6587\u4ef6<\/strong><\/h2>\n<p>midi = MidiFile()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8f68\u9053<\/strong><\/h2>\n<p>track = MidiTrack()<\/p>\n<p>midi.tracks.append(track)<\/p>\n<h2><strong>\u8bbe\u7f6e\u8282\u62cd\u548c\u901f\u5ea6<\/strong><\/h2>\n<p>track.append(MetaMessage(&#39;time_signature&#39;, numerator=4, denominator=4))<\/p>\n<p>track.append(MetaMessage(&#39;set_tempo&#39;, tempo=mido.bpm2tempo(120)))<\/p>\n<h2><strong>\u6dfb\u52a0\u97f3\u7b26<\/strong><\/h2>\n<p>notes = [60, 62, 64, 65, 67, 69, 71, 72]<\/p>\n<p>for note in notes:<\/p>\n<p>    track.append(Message(&#39;note_on&#39;, note=note, velocity=64, time=0))<\/p>\n<p>    track.append(Message(&#39;note_off&#39;, note=note, velocity=64, time=480))<\/p>\n<h2><strong>\u4fdd\u5b58MIDI\u6587\u4ef6<\/strong><\/h2>\n<p>midi.save(&#39;tempo_time_signature.mid&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u591a\u4e2a\u8f68\u9053<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5728\u4e00\u4e2aMIDI\u6587\u4ef6\u4e2d\u4f7f\u7528\u591a\u4e2a\u8f68\u9053\u6765\u521b\u5efa\u66f4\u590d\u6742\u7684\u97f3\u4e50\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u4e86\u4e24\u4e2a\u8f68\u9053\uff0c\u6bcf\u4e2a\u8f68\u9053\u6f14\u594f\u4e0d\u540c\u7684\u97f3\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mido<\/p>\n<p>from mido import MidiFile, MidiTrack, Message<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684MIDI\u6587\u4ef6<\/strong><\/h2>\n<p>midi = MidiFile()<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e00\u4e2a\u8f68\u9053<\/strong><\/h2>\n<p>track1 = MidiTrack()<\/p>\n<p>midi.tracks.append(track1)<\/p>\n<h2><strong>\u6dfb\u52a0\u97f3\u7b26\u5230\u7b2c\u4e00\u4e2a\u8f68\u9053<\/strong><\/h2>\n<p>notes1 = [60, 62, 64, 65, 67, 69, 71, 72]<\/p>\n<p>for note in notes1:<\/p>\n<p>    track1.append(Message(&#39;note_on&#39;, note=note, velocity=64, time=0))<\/p>\n<p>    track1.append(Message(&#39;note_off&#39;, note=note, velocity=64, time=480))<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u4e2a\u8f68\u9053<\/strong><\/h2>\n<p>track2 = MidiTrack()<\/p>\n<p>midi.tracks.append(track2)<\/p>\n<h2><strong>\u6dfb\u52a0\u97f3\u7b26\u5230\u7b2c\u4e8c\u4e2a\u8f68\u9053<\/strong><\/h2>\n<p>notes2 = [72, 71, 69, 67, 65, 64, 62, 60]<\/p>\n<p>for note in notes2:<\/p>\n<p>    track2.append(Message(&#39;note_on&#39;, note=note, velocity=64, time=0))<\/p>\n<p>    track2.append(Message(&#39;note_off&#39;, note=note, velocity=64, time=480))<\/p>\n<h2><strong>\u4fdd\u5b58MIDI\u6587\u4ef6<\/strong><\/h2>\n<p>midi.save(&#39;multiple_tracks.mid&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u97f3\u8272\u548c\u63a7\u5236\u5668<\/h3>\n<\/p>\n<p><h4>1\u3001\u9009\u62e9\u97f3\u8272<\/h4>\n<\/p>\n<p><p>\u5728MIDI\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7Program Change\u6d88\u606f\u6765\u9009\u62e9\u4e0d\u540c\u7684\u97f3\u8272\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u9009\u62e9\u4e86\u94a2\u7434\u97f3\u8272\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mido<\/p>\n<p>from mido import MidiFile, MidiTrack, Message<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684MIDI\u6587\u4ef6<\/strong><\/h2>\n<p>midi = MidiFile()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8f68\u9053<\/strong><\/h2>\n<p>track = MidiTrack()<\/p>\n<p>midi.tracks.append(track)<\/p>\n<h2><strong>\u9009\u62e9\u94a2\u7434\u97f3\u8272<\/strong><\/h2>\n<p>track.append(Message(&#39;program_change&#39;, program=0))<\/p>\n<h2><strong>\u6dfb\u52a0\u97f3\u7b26<\/strong><\/h2>\n<p>notes = [60, 62, 64, 65, 67, 69, 71, 72]<\/p>\n<p>for note in notes:<\/p>\n<p>    track.append(Message(&#39;note_on&#39;, note=note, velocity=64, time=0))<\/p>\n<p>    track.append(Message(&#39;note_off&#39;, note=note, velocity=64, time=480))<\/p>\n<h2><strong>\u4fdd\u5b58MIDI\u6587\u4ef6<\/strong><\/h2>\n<p>midi.save(&#39;piano_program_change.mid&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u53d1\u9001<code>program_change<\/code>\u6d88\u606f\u6765\u9009\u62e9\u97f3\u8272\uff0c\u7a0b\u5e8f\u53f7<code>0<\/code>\u8868\u793a\u94a2\u7434\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u63a7\u5236\u5668<\/h4>\n<\/p>\n<p><p>MIDI\u63a7\u5236\u5668\u6d88\u606f\u53ef\u4ee5\u7528\u6765\u8c03\u6574\u5404\u79cd\u53c2\u6570\uff0c\u4f8b\u5982\u97f3\u91cf\u3001\u8e0f\u677f\u7b49\u3002\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u63a7\u5236\u5668\u6d88\u606f\u6765\u8c03\u6574\u97f3\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mido<\/p>\n<p>from mido import MidiFile, MidiTrack, Message<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684MIDI\u6587\u4ef6<\/strong><\/h2>\n<p>midi = MidiFile()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8f68\u9053<\/strong><\/h2>\n<p>track = MidiTrack()<\/p>\n<p>midi.tracks.append(track)<\/p>\n<h2><strong>\u8bbe\u7f6e\u97f3\u91cf\u4e3a\u6700\u5927<\/strong><\/h2>\n<p>track.append(Message(&#39;control_change&#39;, control=7, value=127))<\/p>\n<h2><strong>\u6dfb\u52a0\u97f3\u7b26<\/strong><\/h2>\n<p>notes = [60, 62, 64, 65, 67, 69, 71, 72]<\/p>\n<p>for note in notes:<\/p>\n<p>    track.append(Message(&#39;note_on&#39;, note=note, velocity=64, time=0))<\/p>\n<p>    track.append(Message(&#39;note_off&#39;, note=note, velocity=64, time=480))<\/p>\n<h2><strong>\u4fdd\u5b58MIDI\u6587\u4ef6<\/strong><\/h2>\n<p>midi.save(&#39;volume_control_change.mid&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u53d1\u9001<code>control_change<\/code>\u6d88\u606f\u6765\u8bbe\u7f6e\u97f3\u91cf\uff0c\u63a7\u5236\u53f7<code>7<\/code>\u8868\u793a\u97f3\u91cf\uff0c\u503c<code>127<\/code>\u8868\u793a\u6700\u5927\u97f3\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u7efc\u5408\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u7ed3\u5408\u4ee5\u4e0a\u6240\u6709\u77e5\u8bc6\u70b9\u6765\u751f\u6210\u4e00\u4e2a\u66f4\u590d\u6742\u7684MIDI\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mido<\/p>\n<p>from mido import MidiFile, MidiTrack, Message, MetaMessage<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684MIDI\u6587\u4ef6<\/strong><\/h2>\n<p>midi = MidiFile()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8f68\u9053<\/strong><\/h2>\n<p>track = MidiTrack()<\/p>\n<p>midi.tracks.append(track)<\/p>\n<h2><strong>\u8bbe\u7f6e\u8282\u62cd\u548c\u901f\u5ea6<\/strong><\/h2>\n<p>track.append(MetaMessage(&#39;time_signature&#39;, numerator=4, denominator=4))<\/p>\n<p>track.append(MetaMessage(&#39;set_tempo&#39;, tempo=mido.bpm2tempo(120)))<\/p>\n<h2><strong>\u9009\u62e9\u94a2\u7434\u97f3\u8272<\/strong><\/h2>\n<p>track.append(Message(&#39;program_change&#39;, program=0))<\/p>\n<h2><strong>\u8bbe\u7f6e\u97f3\u91cf\u4e3a\u6700\u5927<\/strong><\/h2>\n<p>track.append(Message(&#39;control_change&#39;, control=7, value=127))<\/p>\n<h2><strong>\u6dfb\u52a0\u97f3\u7b26<\/strong><\/h2>\n<p>notes = [60, 62, 64, 65, 67, 69, 71, 72]<\/p>\n<p>for note in notes:<\/p>\n<p>    track.append(Message(&#39;note_on&#39;, note=note, velocity=64, time=0))<\/p>\n<p>    track.append(Message(&#39;note_off&#39;, note=note, velocity=64, time=480))<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u4e2a\u8f68\u9053<\/strong><\/h2>\n<p>track2 = MidiTrack()<\/p>\n<p>midi.tracks.append(track2)<\/p>\n<h2><strong>\u9009\u62e9\u5f26\u4e50\u97f3\u8272<\/strong><\/h2>\n<p>track2.append(Message(&#39;program_change&#39;, program=40))<\/p>\n<h2><strong>\u8bbe\u7f6e\u97f3\u91cf\u4e3a\u4e2d\u7b49<\/strong><\/h2>\n<p>track2.append(Message(&#39;control_change&#39;, control=7, value=64))<\/p>\n<h2><strong>\u6dfb\u52a0\u97f3\u7b26<\/strong><\/h2>\n<p>notes2 = [72, 71, 69, 67, 65, 64, 62, 60]<\/p>\n<p>for note in notes2:<\/p>\n<p>    track2.append(Message(&#39;note_on&#39;, note=note, velocity=64, time=0))<\/p>\n<p>    track2.append(Message(&#39;note_off&#39;, note=note, velocity=64, time=480))<\/p>\n<h2><strong>\u4fdd\u5b58MIDI\u6587\u4ef6<\/strong><\/h2>\n<p>midi.save(&#39;complex_example.mid&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Python\u7f16\u5199\u6b4c\u66f2\uff0c\u901a\u8fc7\u4f7f\u7528<code>mido<\/code>\u5e93\u751f\u6210MIDI\u6587\u4ef6\u3002\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u4e86MIDI\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u5982\u4f55\u521b\u5efaMIDI\u6587\u4ef6\uff0c\u6dfb\u52a0\u97f3\u7b26\uff0c\u8bbe\u7f6e\u8282\u62cd\u548c\u901f\u5ea6\uff0c\u4f7f\u7528\u591a\u4e2a\u8f68\u9053\uff0c\u9009\u62e9\u97f3\u8272\u548c\u4f7f\u7528\u63a7\u5236\u5668\u3002\u901a\u8fc7\u7efc\u5408\u793a\u4f8b\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u7ed3\u5408\u6240\u6709\u8fd9\u4e9b\u5143\u7d20\u6765\u751f\u6210\u66f4\u590d\u6742\u7684MIDI\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>Python\u7f16\u5199\u6b4c\u66f2\u7684\u8fc7\u7a0b\u4e0d\u4ec5\u6709\u8da3\uff0c\u800c\u4e14\u53ef\u4ee5\u5e94\u7528\u4e8e\u5404\u79cd\u97f3\u4e50\u521b\u4f5c\u548c\u5904\u7406\u4efb\u52a1\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u4e3a\u4f60\u7684\u97f3\u4e50\u521b\u4f5c\u63d0\u4f9b\u4e00\u4e9b\u5e2e\u52a9\u548c\u542f\u53d1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u751f\u6210\u97f3\u4e50\u6216\u6b4c\u66f2\uff1f<\/strong><br \/>\u4f7f\u7528Python\u7f16\u5199\u6b4c\u66f2\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u5e93\u6765\u5b9e\u73b0\uff0c\u6bd4\u5982<code>Mido<\/code>\u3001<code>Music21<\/code>\u6216<code>PyDub<\/code>\u7b49\u3002\u8fd9\u4e9b\u5e93\u5141\u8bb8\u4f60\u521b\u5efaMIDI\u6587\u4ef6\u3001\u5904\u7406\u97f3\u9891\u6216\u751f\u6210\u4e50\u8c31\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u4ee3\u7801\u6765\u5b9a\u4e49\u97f3\u7b26\u3001\u8282\u62cd\u548c\u548c\u5f26\uff0c\u4ece\u800c\u7ec4\u5408\u6210\u4e00\u9996\u5b8c\u6574\u7684\u6b4c\u66f2\u3002\u53ef\u4ee5\u53c2\u8003\u8fd9\u4e9b\u5e93\u7684\u6587\u6863\uff0c\u9010\u6b65\u5b66\u4e60\u5982\u4f55\u4f7f\u7528\u5b83\u4eec\u6765\u5b9e\u73b0\u97f3\u4e50\u521b\u4f5c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u521b\u5efa\u97f3\u4e50\u9700\u8981\u54ea\u4e9b\u5de5\u5177\u548c\u5e93\uff1f<\/strong><br \/>\u4e3a\u4e86\u5728Python\u4e2d\u7f16\u5199\u6b4c\u66f2\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u51e0\u4e2a\u6d41\u884c\u7684\u5e93\u3002<code>Mido<\/code>\u7528\u4e8e\u5904\u7406MIDI\u6587\u4ef6\uff0c<code>Music21<\/code>\u9002\u5408\u97f3\u4e50\u7406\u8bba\u548c\u4e50\u8c31\u751f\u6210\uff0c<code>PyDub<\/code>\u5219\u53ef\u4ee5\u5904\u7406\u97f3\u9891\u6587\u4ef6\u7684\u7f16\u8f91\u548c\u751f\u6210\u3002\u6b64\u5916\uff0c<code>Pygame<\/code>\u4e5f\u53ef\u4ee5\u7528\u4e8e\u97f3\u9891\u64ad\u653e\u3002\u9009\u62e9\u9002\u5408\u4f60\u9700\u6c42\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u9ad8\u6548\u5730\u8fdb\u884c\u97f3\u4e50\u521b\u4f5c\u3002<\/p>\n<p><strong>Python\u7f16\u5199\u6b4c\u66f2\u7684\u5b66\u4e60\u66f2\u7ebf\u662f\u600e\u6837\u7684\uff1f<\/strong><br \/>\u5b66\u4e60\u5982\u4f55\u7528Python\u7f16\u5199\u6b4c\u66f2\u7684\u66f2\u7ebf\u56e0\u4eba\u800c\u5f02\u3002\u5bf9\u4e8e\u5df2\u7ecf\u6709\u7f16\u7a0b\u57fa\u7840\u7684\u4eba\uff0c\u638c\u63e1\u97f3\u4e50\u76f8\u5173\u5e93\u7684\u57fa\u672c\u7528\u6cd5\u53ef\u80fd\u76f8\u5bf9\u8f83\u5feb\u3002\u521d\u5b66\u8005\u5219\u9700\u8981\u82b1\u65f6\u95f4\u7406\u89e3\u97f3\u4e50\u7406\u8bba\u548c\u7f16\u7a0b\u6982\u5ff5\u3002\u63a8\u8350\u4ece\u7b80\u5355\u7684\u9879\u76ee\u5f00\u59cb\uff0c\u4f8b\u5982\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u65cb\u5f8b\uff0c\u7136\u540e\u9010\u6e10\u589e\u52a0\u590d\u6742\u6027\uff0c\u6bd4\u5982\u6dfb\u52a0\u548c\u58f0\u6216\u8282\u594f\u5143\u7d20\u3002\u901a\u8fc7\u5b9e\u8df5\u548c\u4e0d\u65ad\u5c1d\u8bd5\uff0c\u4f60\u4f1a\u9010\u6e10\u638c\u63e1\u8fd9\u9879\u6280\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u7f16\u5199\u6b4c\u66f2\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u97f3\u9891\u5904\u7406\u5e93\u3001\u751f\u6210MIDI\u6587\u4ef6\u3001\u5408\u6210\u97f3\u9891\u4fe1\u53f7\u7b49\u3002\u4ee5\u4e0b\u5c06\u8be6 [&hellip;]","protected":false},"author":3,"featured_media":1169956,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169949"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1169949"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169949\/revisions"}],"predecessor-version":[{"id":1169959,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169949\/revisions\/1169959"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1169956"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1169949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1169949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1169949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}