{"id":2091,"date":"2019-04-20T11:07:44","date_gmt":"2019-04-20T03:07:44","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=2091"},"modified":"2019-04-20T11:30:19","modified_gmt":"2019-04-20T03:30:19","slug":"using-midi-in-processing-for-playback","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2019\/04\/20\/using-midi-in-processing-for-playback\/","title":{"rendered":"Using midi in Processing for playback"},"content":{"rendered":"\n<p>This is my first use of midi in Processing. I do not use the <a rel=\"noreferrer noopener\" aria-label=\"MidiBus (opens in a new tab)\" href=\"http:\/\/www.smallbutdigital.com\/projects\/themidibus\/\" target=\"_blank\">MidiBus<\/a> library for Processing. Instead, I try to use the standard <a rel=\"noreferrer noopener\" aria-label=\"midi package (opens in a new tab)\" href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/sound\/overview-MIDI.html\" target=\"_blank\">midi package<\/a> in Java. The SE8 standard Java package also contains the <a rel=\"noreferrer noopener\" aria-label=\"javadoc (opens in a new tab)\" href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/javax\/sound\/midi\/package-summary.html\" target=\"_blank\">javadoc<\/a> documentation.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"480\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2019\/04\/midi0512.png\" alt=\"\" class=\"wp-image-2094\" srcset=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2019\/04\/midi0512.png 640w, http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2019\/04\/midi0512-300x225.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><figcaption>Screenshot of the Processing sketch<\/figcaption><\/figure>\n\n\n\n<p>The Processing source code and sample midi files are in the <a rel=\"noreferrer noopener\" aria-label=\"Magicandlove GitHub repository (opens in a new tab)\" href=\"https:\/\/github.com\/chungbwc\/Magicandlove\/tree\/master\/ml20190419a\" target=\"_blank\">Magicandlove GitHub repository<\/a>. The midi example files are downloaded from the <a rel=\"noreferrer noopener\" aria-label=\"midiworld (opens in a new tab)\" href=\"http:\/\/midiworld.com\/\" target=\"_blank\">midiworld<\/a> website.<\/p>\n\n\n\n<p>The code basically needs a <a rel=\"noreferrer noopener\" aria-label=\"Synthesizer (opens in a new tab)\" href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/javax\/sound\/midi\/Synthesizer.html\" target=\"_blank\">Synthesizer<\/a> class to render midi instruments into audio and a <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/javax\/sound\/midi\/Sequencer.html\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Sequencer (opens in a new tab)\">Sequencer<\/a> class to playback the midi sequence.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Synthesizer synth = MidiSystem.getSynthesizer();<br>Sequencer player = MidiSystem.getSequencer();<br>synth.open();<br>player.open();<\/pre>\n\n\n\n<p>All the midi music files are in the <em>data<\/em> folder of the Processing sketch. To playback each piece of midi music, we need to convert each into a Java <a rel=\"noreferrer noopener\" aria-label=\"File (opens in a new tab)\" href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/io\/File.html\" target=\"_blank\">File<\/a> object and use the following code to playback it. The variable <em>f<\/em> is a <em>File<\/em> object instance containing the midi file in the <em>data<\/em> folder.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Sequence music = MidiSystem.getSequence(f);<br>player.setSequence(music);<br>player.start();<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is my first use of midi in Processing. I do not use the MidiBus library for Processing. Instead, I try to use the standard midi package in Java. The SE8 standard Java package also contains the javadoc documentation. The Processing source code and sample midi files are in the Magicandlove GitHub repository. The midi [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79,66],"tags":[173,62],"class_list":["post-2091","post","type-post","status-publish","format-standard","hentry","category-software-2","category-testing","tag-midi","tag-processing-org"],"_links":{"self":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/2091","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/comments?post=2091"}],"version-history":[{"count":6,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/2091\/revisions"}],"predecessor-version":[{"id":2098,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/2091\/revisions\/2098"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=2091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=2091"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=2091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}