{"id":854,"date":"2019-09-20T23:08:58","date_gmt":"2019-09-20T23:08:58","guid":{"rendered":"https:\/\/thinkcolorful.org\/?p=854"},"modified":"2024-12-13T19:41:36","modified_gmt":"2024-12-13T19:41:36","slug":"fractal-melodies-using-l-systems","status":"publish","type":"post","link":"https:\/\/thinkcolorful.org\/?p=854","title":{"rendered":"Fractal Melodies using L-Systems"},"content":{"rendered":"\n<p>I thought it would be a fun challenge to write a program which generates a melody using an L-System, since they are not used too often in music creation. I couldn&#8217;t find any other instances of using fractals and music in this way so I explored l-system code that is used to generate drawings and applied that to music. I wrote <a href=\"https:\/\/github.com\/ephemeralwaves\/Fractal-Music-Generator\/blob\/master\/FractalGenerator.qml\">this program<\/a> as a plugin for <a href=\"https:\/\/musescore.org\/en\">Musescore<\/a> which had the added benefit practicing Javascript. I created it as a compositional tool, and the rule set can be changed to generate different melodies. The rule set I used is one that is usually used in visual art to generate Koch curves (via <a href=\"https:\/\/en.wikipedia.org\/wiki\/L-system#Example_4:_Koch_curve\">wikipedia<\/a>):<\/p>\n\n\n\n<!--more-->\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>A variant of the&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Koch_snowflake\">Koch curve<\/a>&nbsp;which uses only right angles.<\/p>\n\n\n\n<p><strong>variables<\/strong>&nbsp;: F<\/p>\n\n\n\n<p><strong>constants<\/strong>&nbsp;: + \u2212<\/p>\n\n\n\n<p><strong>start<\/strong>&nbsp;: F<\/p>\n\n\n\n<p><strong>rules<\/strong>&nbsp;: (F \u2192 F+F\u2212F\u2212F+F)<\/p>\n\n\n\n<p>Here, F means &#8220;draw forward&#8221;, + means &#8220;turn left 90\u00b0&#8221;, and \u2212 means &#8220;turn right 90\u00b0&#8221; (see&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Turtle_graphics\">turtle graphics<\/a>).<em>n<\/em>&nbsp;= 0:F<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/en.wikipedia.org\/wiki\/File:Square_koch.svg\"><img decoding=\"async\" src=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/thumb\/a\/ab\/Square_koch.svg\/20px-Square_koch.svg.png\" alt=\"Koch Square - 0 iterations\"\/><\/a><\/figure>\n\n\n\n<p><em>n<\/em>&nbsp;= 1:F+F\u2212F\u2212F+F<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/en.wikipedia.org\/wiki\/File:Square_koch_1.svg\"><img decoding=\"async\" src=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/thumb\/1\/18\/Square_koch_1.svg\/40px-Square_koch_1.svg.png\" alt=\"Koch Square - 1 iterations\"\/><\/a><\/figure>\n\n\n\n<p><em>n<\/em>&nbsp;= 2:F+F\u2212F\u2212F+F+F+F\u2212F\u2212F+F\u2212F+F\u2212F\u2212F+F\u2212F+F\u2212F\u2212F+F+F+F\u2212F\u2212F+F<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/en.wikipedia.org\/wiki\/File:Square_koch_2.svg\"><img decoding=\"async\" src=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/thumb\/e\/ec\/Square_koch_2.svg\/100px-Square_koch_2.svg.png\" alt=\"Koch Square - 2 iterations\"\/><\/a><\/figure>\n\n\n\n<p><em>n<\/em>&nbsp;= 3:F+F\u2212F\u2212F+F+F+F\u2212F\u2212F+F\u2212F+F\u2212F\u2212F+F\u2212F+F\u2212F\u2212F+F+F+F\u2212F\u2212F+F+F+F\u2212F\u2212F+F+F+F\u2212F\u2212F+F\u2212F+F\u2212F\u2212F+F\u2212F+F\u2212F\u2212F+F+F+F\u2212F\u2212F+F\u2212F+F\u2212F\u2212F+F+F+F\u2212F\u2212F+F\u2212F+F\u2212F\u2212F+F\u2212F+F\u2212F\u2212F+F+F+F\u2212F\u2212F+F\u2212F+F\u2212F\u2212F+F+F+F\u2212F\u2212F+F\u2212F+F\u2212F\u2212F+F\u2212F+F\u2212F\u2212F+F+F+F\u2212F\u2212F+F+F+F\u2212F\u2212F+F+F+F\u2212F\u2212F+F\u2212F+F\u2212F\u2212F+F\u2212F+F\u2212F\u2212F+F+F+F\u2212F\u2212F+F<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/en.wikipedia.org\/wiki\/File:Square_koch_3.svg\"><img decoding=\"async\" src=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/thumb\/d\/d6\/Square_koch_3.svg\/280px-Square_koch_3.svg.png\" alt=\"Koch Square - 3 iterations\"\/><\/a><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>The melody generator interprets each &#8220;F&#8221; as a note and each &#8220;+&#8221; or &#8220;-&#8221; as going up or down a pitch. I set my start note to &#8220;C4&#8221;, confined the melody to only move up and down the C pentatonic scale and created  the score below using the Koch curve ruleset as seen above.  It can technically go on forever, so I limited the score to 51 measures.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"510\" height=\"721\" src=\"https:\/\/thinkcolorful.org\/wp-content\/uploads\/2019\/09\/Fractal-1-510x721.png\" alt=\"Fractal Score\" class=\"wp-image-860\" srcset=\"https:\/\/thinkcolorful.org\/wp-content\/uploads\/2019\/09\/Fractal-1-510x721.png 510w, https:\/\/thinkcolorful.org\/wp-content\/uploads\/2019\/09\/Fractal-1-212x300.png 212w, https:\/\/thinkcolorful.org\/wp-content\/uploads\/2019\/09\/Fractal-1-768x1086.png 768w, https:\/\/thinkcolorful.org\/wp-content\/uploads\/2019\/09\/Fractal-1-370x523.png 370w, https:\/\/thinkcolorful.org\/wp-content\/uploads\/2019\/09\/Fractal-1-1040x1470.png 1040w\" sizes=\"auto, (max-width: 510px) 100vw, 510px\" \/><\/figure><\/div>\n\n\n\n<figure class=\"wp-block-audio\"><audio controls src=\"https:\/\/thinkcolorful.org\/wp-content\/uploads\/2019\/09\/koch.mp3\"><\/audio><\/figure>\n\n\n\n<p><strong>Update: This is available as <\/strong><a href=\"https:\/\/www.npmjs.com\/package\/l-system-melody\"><strong>an npm package<\/strong><\/a><strong> too!<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I thought it would be a fun challenge to write a program which generates a melody using an L-System,&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":867,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,214,13,10],"tags":[],"class_list":["post-854","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-art","category-javascript","category-music","category-programming"],"_links":{"self":[{"href":"https:\/\/thinkcolorful.org\/index.php?rest_route=\/wp\/v2\/posts\/854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thinkcolorful.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thinkcolorful.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thinkcolorful.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thinkcolorful.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=854"}],"version-history":[{"count":18,"href":"https:\/\/thinkcolorful.org\/index.php?rest_route=\/wp\/v2\/posts\/854\/revisions"}],"predecessor-version":[{"id":1120,"href":"https:\/\/thinkcolorful.org\/index.php?rest_route=\/wp\/v2\/posts\/854\/revisions\/1120"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thinkcolorful.org\/index.php?rest_route=\/wp\/v2\/media\/867"}],"wp:attachment":[{"href":"https:\/\/thinkcolorful.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thinkcolorful.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thinkcolorful.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}