{"id":227,"date":"2016-05-10T15:22:21","date_gmt":"2016-05-10T15:22:21","guid":{"rendered":"http:\/\/www.rangakrish.com\/?p=227"},"modified":"2016-05-10T15:25:42","modified_gmt":"2016-05-10T15:25:42","slug":"generating-melody-to-fit-harmony","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2016\/05\/10\/generating-melody-to-fit-harmony\/","title":{"rendered":"Generating Melody to Fit Harmony"},"content":{"rendered":"<p>There are two common approaches to composing music. One involves synthesizing a suitable chord progression first and then layering melody over it. The other is to start with melody and then harmonize it by applying chords. Of course, you can mix both approaches &#8211; use one technique for a portion of the song and then another for other portions. Remember, this is art, and hence nothing is cast in stone.<\/p>\n<p>I have two objectives in this post:<\/p>\n<p style=\"padding-left: 30px;\">1) Describe a simple technique for deriving melody based on the chord progression. We will use both Chord Tones and Non Cord Tones (but in the same scale &#8211; diatonic).<\/p>\n<p style=\"padding-left: 30px;\">2) Play the generated music through <a href=\"https:\/\/www.ableton.com\/en\/\" target=\"_blank\">Ableton Live<\/a> using its rich instrument collection.<\/p>\n<p>In many cases, we might want the melody to be at a higher octave than the chord progression, so this octave shift is passed as an argument to our algorithm. We also assume that the chords are in a specific scale, and hence diatonic.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Here are the steps:<\/strong><\/span><\/p>\n<p style=\"padding-left: 30px;\">1) Given a chord progression, for each bar: find the pitches of the chord, remove duplicates and sort the pitches in ascending order.<\/p>\n<p style=\"padding-left: 30px;\">2) Since the melody must be from the same scale (but shifted by some octave), expand the scale to determine its pitches and derive a sequence of pitches {-1, 0, +1} octaves apart.<\/p>\n<p style=\"padding-left: 30px;\">3) Taking the pitches obtained in (1), determine a sub-sequence from (2) that covers both chord and non-chord tones.<\/p>\n<p style=\"padding-left: 30px;\">4) Take as many consecutive elements as possible from (3) based on the melodic rhythm for that bar.<\/p>\n<p style=\"padding-left: 30px;\">5) Apply a pitch-shaper function randomly on the sequence obtained in (4)<\/p>\n<p>Let us take a simple example and work through the steps. Suppose the first bar has the chord <strong>c3e3g3<\/strong>\u00a0(i.e., Tonic in c3 major scale). We will assume that we need the melody in 4th octave, not 3rd, but in the same scale.<\/p>\n<p style=\"padding-left: 30px;\">1) We get (c4 e4 g4) from the first chord<\/p>\n<p style=\"padding-left: 30px;\">2) The allowed pitches for this scale after extended transpositions will be:<\/p>\n<p style=\"padding-left: 30px;\">(c3 d3 e3\u2026c4 d4 e4 f4 g4 a4 b4 c5 d5 e5 \u2026)<\/p>\n<p style=\"padding-left: 30px;\">3) We get (c4 d4 e4 f4 g4) as the largest sub-sequence in (2)<\/p>\n<p style=\"padding-left: 30px;\">4) If our melodic rhythm for this bar is \u2018(q -e e q q), we need 4 pitches, so we select a random subset of (3), say, (d4 e4 f4 g4)<\/p>\n<p style=\"padding-left: 30px;\">5) Applying a random pitch shaper function, we might get the sequence (d4 e4 g4 f4) as the final melody for this bar<\/p>\n<p>We repeat the above steps for all the bars.<\/p>\n<p>As you can see, the core logic is straightforward. The code I have uploaded has many functions (distributed over 3 source files), including some utility functions from my own library, so that you will be able to test the entire implementation without other dependencies.<\/p>\n<figure id=\"attachment_229\" aria-describedby=\"caption-attachment-229\" style=\"width: 700px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Melody-Over-Harmony.png\" rel=\"attachment wp-att-229\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"229\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2016\/05\/10\/generating-melody-to-fit-harmony\/melody-over-harmony\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Melody-Over-Harmony.png\" data-orig-size=\"958,1105\" 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=\"Melody Over Harmony\" data-image-description=\"&lt;p&gt;Melody Over Harmony&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Melody Over Harmony&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Melody-Over-Harmony-888x1024.png\" class=\"wp-image-229\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Melody-Over-Harmony-888x1024.png?resize=700%2C807\" alt=\"Melody Over Harmony\" width=\"700\" height=\"807\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Melody-Over-Harmony.png?resize=888%2C1024&amp;ssl=1 888w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Melody-Over-Harmony.png?resize=260%2C300&amp;ssl=1 260w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Melody-Over-Harmony.png?resize=768%2C886&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Melody-Over-Harmony.png?w=958&amp;ssl=1 958w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><figcaption id=\"caption-attachment-229\" class=\"wp-caption-text\">Melody Over Harmony<\/figcaption><\/figure>\n<p>Let us now come to the second part of this post: How to connect Opusmodus to Ableton Live? By the way, the only reason I wanted to connect to Live was the rich set of instruments available in Live compared to the GM. In an earlier <a href=\"http:\/\/www.rangakrish.com\/index.php\/2016\/01\/08\/how-to-use-reaktor-with-opusmodus\/\" target=\"_blank\">article<\/a>, I showed\u00a0how to connect OM to Reaktor, so connecting to Live itself is simple.<\/p>\n<p>I have included the screenshot of Live instrument setup, so that you can see what instruments I have used in different tracks. Make sure you configure the input Bus correctly for each of the tracks.<\/p>\n<figure id=\"attachment_228\" aria-describedby=\"caption-attachment-228\" style=\"width: 700px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Ableton-Live-9-Setup.png\" rel=\"attachment wp-att-228\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"228\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2016\/05\/10\/generating-melody-to-fit-harmony\/ableton-live-9-setup\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Ableton-Live-9-Setup.png\" data-orig-size=\"1033,882\" 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=\"Ableton Live 9 Setup\" data-image-description=\"&lt;p&gt;Ableton Live 9 Setup&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Ableton Live 9 Setup&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Ableton-Live-9-Setup-1024x874.png\" class=\"wp-image-228\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Ableton-Live-9-Setup-1024x874.png?resize=700%2C598\" alt=\"Ableton Live 9 Setup\" width=\"700\" height=\"598\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Ableton-Live-9-Setup.png?resize=1024%2C874&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Ableton-Live-9-Setup.png?resize=300%2C256&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Ableton-Live-9-Setup.png?resize=768%2C656&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Ableton-Live-9-Setup.png?w=1033&amp;ssl=1 1033w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><figcaption id=\"caption-attachment-228\" class=\"wp-caption-text\">Ableton Live 9 Setup<\/figcaption><\/figure>\n<p>I also recorded the playing (by arming the tracks) and exported the song as a WAV file. You can <a href=\"http:\/\/www.rangakrish.com\/downloads\/Melody Over Harmony.wav\">download<\/a>\u00a0it and listen to the generated song. One advantage of using an external environment such as Ableton Live is that when you playback, you can add other effects to soup up the song. In my case, that was not the intention, so I let go at the basic step.<\/p>\n<p><a href=\"http:\/\/www.rangakrish.com\/downloads\/MOH-source.zip\">Here<\/a>\u00a0is the source code (zipped).<\/p>\n<p>If you want to use the GM set in stead of Live, remove the ports from the <em><strong>def-score<\/strong><\/em>\u00a0and include the conventional parameters instead.<\/p>\n<p>I would love to hear about your own techniques, if you have tried any, for layering melody on top of harmony.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are two common approaches to composing music. One involves synthesizing a suitable chord progression first and then layering melody over it. The other is to start with melody and then harmonize it by applying chords. Of course, you can mix both approaches &#8211; use one technique for a portion of the song and then [&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":[57,56,55],"class_list":["post-227","post","type-post","status-publish","format-standard","hentry","category-lisp","category-music","category-programming","tag-ableton-live","tag-harmony","tag-melody"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-3F","jetpack-related-posts":[{"id":2224,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/12\/06\/using-falcon-2-with-opusmodus\/","url_meta":{"origin":227,"position":0},"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":245,"url":"https:\/\/www.rangakrish.com\/index.php\/2016\/05\/26\/constraint-propagation-in-picat\/","url_meta":{"origin":227,"position":1},"title":"Constraint Programming in Picat","author":"admin","date":"May 26, 2016","format":false,"excerpt":"In my last post\u00a0I briefly described how we can use the Screamer Lisp library for constraint programming in music. Another language I have been hearing a lot about, in the context of constraint programming, is Picat, a Prolog-derived language. Although I am familiar with Prolog and have been a user\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Picat Program","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Picat-Program.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":2242,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/12\/20\/jfugue-a-java-library-for-music-synthesis\/","url_meta":{"origin":227,"position":2},"title":"JFugue: A Java Library for Music Synthesis","author":"admin","date":"December 20, 2020","format":false,"excerpt":"JFugue\u00a0is an open-source Java library that can be used for algorithmic music composition. Developed by David Koelle, currently it is in its fifth major version. You can get the library from here. I had downloaded the library some time ago, but could not spend much time since my primary focus\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Simple Java Program","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/12\/java-2-300x142.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/12\/java-2-300x142.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/12\/java-2-300x142.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2206,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/11\/22\/a-library-for-algorithmic-music-composition\/","url_meta":{"origin":227,"position":3},"title":"A Library for Algorithmic Music Composition","author":"admin","date":"November 22, 2020","format":false,"excerpt":"I have recently started implementing a library for algorithmic music composition. It provides higher-level algorithms and abstractions for composing multi-track music, and works with Opusmodus\u00a0software. Opusmodus is a great platform for music composition and has excellent support for MIDI-based music. My library is an attempt to enrich the Opusmodus platform\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"App for Controlling Opusmodus","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/11\/Screen1-300x139.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":179,"url":"https:\/\/www.rangakrish.com\/index.php\/2016\/03\/12\/converting-text-to-music\/","url_meta":{"origin":227,"position":4},"title":"Converting Text to Music","author":"admin","date":"March 12, 2016","format":false,"excerpt":"Converting text to music can be fun. Opusmodus\u00a0has a few built-in functions to work with text. Look up the description of text-to-pitch, for example. As described in its documentation, (text-to-pitch '(\"To be, or not to be, that is the question\")) => (((g5 d5) (cs4 e4) (d5 f5) (cs5 d5 g5)\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Text to Music","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/03\/Text-to-Music-204x300.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/03\/Text-to-Music-204x300.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/03\/Text-to-Music-204x300.png?resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/03\/Text-to-Music-204x300.png?resize=700%2C400 2x"},"classes":[]},{"id":237,"url":"https:\/\/www.rangakrish.com\/index.php\/2016\/05\/17\/constraint-programming-using-screamer\/","url_meta":{"origin":227,"position":5},"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":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/227","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=227"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/227\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}