{"id":218,"date":"2016-04-27T13:42:26","date_gmt":"2016-04-27T13:42:26","guid":{"rendered":"http:\/\/www.rangakrish.com\/?p=218"},"modified":"2016-04-27T13:51:08","modified_gmt":"2016-04-27T13:51:08","slug":"reading-and-transforming-a-midi-file","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2016\/04\/27\/reading-and-transforming-a-midi-file\/","title":{"rendered":"Reading and Transforming a MIDI File"},"content":{"rendered":"<p>Sometimes it can be interesting to take a MIDI file and transform the score it represents. For example, you can change the tempo, alter the instruments, or even modify the pitches and rhythm. To do something like this, it is helpful to build a convenient framework. In today\u2019s post, I am outlining one approach that you can start with and enhance as needed\u00a0to suit your requirements.<\/p>\n<p>This discussion applies to the <a href=\"https:\/\/opusmodus.com\" target=\"_blank\">Opusmodus<\/a>\u00a0software environment.<\/p>\n<p>First we need to convert a MIDI file to a <em><strong>def-score<\/strong><\/em> object (actually, a list with name, title section and multiple instrument sections). The function <em><strong>convert-midifile-to-score<\/strong><\/em> does just that. To make it easy to traverse the different sections and apply an operation on each one, we define a function called <em><strong>transform-score<\/strong><\/em>. It takes a score and a user-written handler function and applies the handler on each section of the score.<\/p>\n<p>The handler function provided by the user takes two arguments: the actual section it should operate on and an ID that denotes that section. In the current implementation, there are three types of sections, namely, the score name, title section (with fields such as <strong>:composer<\/strong>, <strong>:time-signature<\/strong>, etc.) and one or more instrument sections. If the handler chooses not to modify a particular section, it can return <em><strong>t<\/strong><\/em>. Returning <em><strong>nil<\/strong><\/em> will cause that section to be skipped in the output.<\/p>\n<p>I have also defined a utility function called <em><strong>replace-key-values<\/strong><\/em> that is handy when you want to change any key-value pair in a section, for example in the title section.<\/p>\n<figure id=\"attachment_223\" aria-describedby=\"caption-attachment-223\" style=\"width: 670px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/04\/Transform-Score-1.png\" rel=\"attachment wp-att-223\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"223\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2016\/04\/27\/reading-and-transforming-a-midi-file\/transform-score-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2016\/04\/Transform-Score-1.png\" data-orig-size=\"768,973\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Transform Score\" data-image-description=\"&lt;p&gt;Transform Score&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Transform Score&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2016\/04\/Transform-Score-1.png\" class=\"wp-image-223\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/04\/Transform-Score-1.png?resize=670%2C849\" alt=\"Transform Score\" width=\"670\" height=\"849\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/04\/Transform-Score-1.png?w=768&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/04\/Transform-Score-1.png?resize=237%2C300&amp;ssl=1 237w\" sizes=\"(max-width: 670px) 100vw, 670px\" \/><\/a><figcaption id=\"caption-attachment-223\" class=\"wp-caption-text\">Transform Score<\/figcaption><\/figure>\n<p>What I have outlined is just a basic idea. You can extend this further by introducing sophisticated pattern matching techniques to manipulate the score object.<\/p>\n<p>You can download the code <a href=\"http:\/\/www.rangakrish.com\/downloads\/Transform Score.opmo\" target=\"_blank\">here<\/a>. The sample MIDI file that I have used is available <a href=\"http:\/\/www.rangakrish.com\/downloads\/raga-bhanumanjari.mid\" target=\"_blank\">here<\/a>. Make sure that you change the file path appropriately if you are testing this code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes it can be interesting to take a MIDI file and transform the score it represents. For example, you can change the tempo, alter the instruments, or even modify the pitches and rhythm. To do something like this, it is helpful to build a convenient framework. In today\u2019s post, I am outlining one approach that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18,5,17],"tags":[38,37],"class_list":["post-218","post","type-post","status-publish","format-standard","hentry","category-lisp","category-music","category-programming","tag-midi","tag-opusmodus"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-3w","jetpack-related-posts":[{"id":268,"url":"https:\/\/www.rangakrish.com\/index.php\/2016\/06\/29\/pattern-matching-with-optima-lisp-library-part-3\/","url_meta":{"origin":218,"position":0},"title":"Pattern Matching with Optima Lisp Library &#8211; Part 3","author":"admin","date":"June 29, 2016","format":false,"excerpt":"In the previous two posts on this topic, I explained some of the basic pattern matching facilities of Optima\u00a0library. There are many\u00a0more advanced features in the library and I will try to discuss them in future posts. In today\u2019s post, I will outline a straightforward application of the library for\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Optima Example","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/06\/Optima-Example-1024x622.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/06\/Optima-Example-1024x622.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/06\/Optima-Example-1024x622.png?resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/06\/Optima-Example-1024x622.png?resize=700%2C400 2x"},"classes":[]},{"id":159,"url":"https:\/\/www.rangakrish.com\/index.php\/2016\/02\/28\/using-cellular-automata-to-generate-music\/","url_meta":{"origin":218,"position":1},"title":"Using Cellular Automata to Generate Music","author":"admin","date":"February 28, 2016","format":false,"excerpt":"Cellular automata are a powerful computational formalism. Stephen Wolfram\u2019s book A New Kind of Science\u00a0provides a thorough study of these devices. Not surprisingly,\u00a0Wolfram Mathematica has great support for working with them. For example, the following figure shows the evolution of Rule 30 over 50 steps, plotted using ArrayPlot function (in\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Cellular Automata","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/02\/Cellular-Automata.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":124,"url":"https:\/\/www.rangakrish.com\/index.php\/2016\/01\/08\/how-to-use-reaktor-with-opusmodus\/","url_meta":{"origin":218,"position":2},"title":"How to Use Reaktor with Opusmodus","author":"admin","date":"January 8, 2016","format":false,"excerpt":"For those of you who do not know about Opusmodus (OM), it is a very powerful and flexible algorithmic music composition environment built in Lisp. It is available for the Mac. Today I wanted to understand how to make Opusmodus communicate with Reaktor 6 to play some tunes. The built-in\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"IAC Driver","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/01\/IAC-Driver-283x300.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":2224,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/12\/06\/using-falcon-2-with-opusmodus\/","url_meta":{"origin":218,"position":3},"title":"Using Falcon 2 with Opusmodus","author":"admin","date":"December 6, 2020","format":false,"excerpt":"The recently concluded Black Friday sale by UVI\u00a0was an irresistible temptation for me and I ended up investing in Falcon 2 and a few other instruments. Although I have the latest versions of Ableton Live, Propellerheads Reason, Renoise, and Reaper, I felt Falcon 2 would be a good addition to\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Sample Code in Opusmodus","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/12\/code-248x300.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":237,"url":"https:\/\/www.rangakrish.com\/index.php\/2016\/05\/17\/constraint-programming-using-screamer\/","url_meta":{"origin":218,"position":4},"title":"Constraint Programming Using Screamer","author":"admin","date":"May 17, 2016","format":false,"excerpt":"A few years I ago I had briefly experimented with the\u00a0Screamer\u00a0library in the context of automatic test case generation from specification. I felt it was a useful utility for doing simple constraint programming tasks. Recently while going through some of the past articles in the\u00a0Opusmodus\u00a0forum, I was pleasantly surprised to\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Constraint Programming in Screamer","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Constraint-Programming-in-Screamer.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Constraint-Programming-in-Screamer.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Constraint-Programming-in-Screamer.png?resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Constraint-Programming-in-Screamer.png?resize=700%2C400 2x"},"classes":[]},{"id":3525,"url":"https:\/\/www.rangakrish.com\/index.php\/2024\/10\/13\/using-chatgpt-for-algorithmic-music-composition\/","url_meta":{"origin":218,"position":5},"title":"Using ChatGPT for Algorithmic Music Composition","author":"admin","date":"October 13, 2024","format":false,"excerpt":"Algorithmic music composition is one of my favourite areas. And in terms of tools, as several of my past articles\u00a0show, OpusModus software is the main choice.\u00a0 Recently I have been spending some time checking if ChatGPT could be cajoled into writing some good OpusModus code. But the experience has not\u2026","rel":"","context":"In &quot;Music&quot;","block_context":{"text":"Music","link":"https:\/\/www.rangakrish.com\/index.php\/category\/music\/"},"img":{"alt_text":"SonicPi Code for Twinkle Twinkle Lite Star","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/10\/twinkle-300x280.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/10\/twinkle-300x280.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/10\/twinkle-300x280.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=218"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/218\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}