{"id":2298,"date":"2021-01-31T09:47:50","date_gmt":"2021-01-31T04:17:50","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=2298"},"modified":"2021-01-31T13:56:28","modified_gmt":"2021-01-31T08:26:28","slug":"using-l-system-to-generate-music","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2021\/01\/31\/using-l-system-to-generate-music\/","title":{"rendered":"Using L-System to Generate Music"},"content":{"rendered":"<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/L-system\" target=\"_blank\" rel=\"noopener\"><em><strong>L-system<\/strong><\/em><\/a>\u00a0is a powerful string rewriting system. <a href=\"https:\/\/opusmodus.com\" target=\"_blank\" rel=\"noopener\"><em><strong>Opusmodus<\/strong><\/em><\/a>\u00a0supports an implementation of this system (inspired by and based on <a href=\"http:\/\/www.vcn.bc.ca\/~griffink\/lisp_lsystems.pdf\" target=\"_blank\" rel=\"noopener\"><em><strong>this<\/strong><\/em><\/a>\u00a0implementation).<\/p>\n<p>It is possible to use the <em><strong>L-system<\/strong><\/em> to generate <em><strong>anything<\/strong><\/em>\u00a0that can be described in terms of an <em><strong>alphabet<\/strong><\/em> and a set of <em><strong>rewriting rules<\/strong><\/em>.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>Let us start with a simple example:<\/p>\n<figure id=\"attachment_2299\" aria-describedby=\"caption-attachment-2299\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/example1.png?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"2299\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/01\/31\/using-l-system-to-generate-music\/example1-22\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/example1.png\" data-orig-size=\"306,155\" 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=\"Defining an L-system in Opusmodus\" data-image-description=\"&lt;p&gt;Defining an L-system in Opusmodus&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Defining an L-system in Opusmodus&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/example1.png\" class=\"wp-image-2299\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/example1.png?resize=400%2C203&#038;ssl=1\" alt=\"Defining an L-system in Opusmodus\" width=\"400\" height=\"203\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/example1.png?resize=300%2C152&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/example1.png?w=306&amp;ssl=1 306w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/a><figcaption id=\"caption-attachment-2299\" class=\"wp-caption-text\"><strong>Defining an L-system in Opusmodus<\/strong><\/figcaption><\/figure>\n<p>As you can see, there are two steps. The first is to define a class derived from <em><strong>\u201cl-system\u201d<\/strong><\/em> with 2 fields, <em><strong>\u201caxiom\u201d<\/strong><\/em> and <em><strong>\u201cdepth\u201d<\/strong><\/em>, initialised appropriately.<\/p>\n<p>The second step is to define a set of rewriting rules using the <em><strong>\u201cl-productions\u201d<\/strong><\/em> method. As per the rules of this example, the symbol <em><strong>\u201ca\u201d<\/strong><\/em> expands to <em><strong>\u201ca\u201d<\/strong><\/em> followed by <em><strong>\u201cb\u201d<\/strong><\/em>. <em><strong>\u201cb\u201d<\/strong><\/em> expands to <em><strong>\u201ca\u201d<\/strong><\/em> followed by <em><strong>\u201cc\u201d<\/strong><\/em>. And <em><strong>\u201cc\u201d<\/strong><\/em> expands to <em><strong>\u201cb\u201d<\/strong><\/em>. The rewriting procedure starts with the <em><strong>\u201caxiom\u201d<\/strong><\/em>, in this case the string <em><strong>\u201ca b\u201d<\/strong><\/em>, and repeatedly expands it by applying the given rules.<\/p>\n<p>Let us see what we get:<\/p>\n<figure id=\"attachment_2300\" aria-describedby=\"caption-attachment-2300\" style=\"width: 449px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/example1-output.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2300\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/01\/31\/using-l-system-to-generate-music\/example1-output-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/example1-output.png\" data-orig-size=\"339,43\" 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=\"Generated Output\" data-image-description=\"&lt;p&gt;Generated Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Generated Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/example1-output.png\" class=\"wp-image-2300\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/example1-output.png?resize=449%2C57&#038;ssl=1\" alt=\"Generated Output\" width=\"449\" height=\"57\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/example1-output.png?resize=300%2C38&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/example1-output.png?w=339&amp;ssl=1 339w\" sizes=\"(max-width: 449px) 100vw, 449px\" \/><\/a><figcaption id=\"caption-attachment-2300\" class=\"wp-caption-text\"><strong>Generated Output<\/strong><\/figcaption><\/figure>\n<p>The <em><strong>\u201crewrite-lsystem\u201d<\/strong><\/em> method takes our class name and then generates the output starting with the given <em><strong>\u201caxiom\u201d<\/strong><\/em> and repeatedly applying the rules <em><strong>\u201cdepth\u201d<\/strong><\/em> times.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>Let us see the generated output starting from depth 0 to depth 3:<\/p>\n<figure id=\"attachment_2302\" aria-describedby=\"caption-attachment-2302\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/depth-check.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2302\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/01\/31\/using-l-system-to-generate-music\/depth-check\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/depth-check.png\" data-orig-size=\"427,142\" 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=\"Output at Different Depths\" data-image-description=\"&lt;p&gt;Output at Different Depths&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Output at Different Depths&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/depth-check.png\" class=\"wp-image-2302\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/depth-check.png?resize=550%2C183&#038;ssl=1\" alt=\"Output at Different Depths\" width=\"550\" height=\"183\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/depth-check.png?resize=300%2C100&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/depth-check.png?w=427&amp;ssl=1 427w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2302\" class=\"wp-caption-text\"><strong>Output at Different Depths<\/strong><\/figcaption><\/figure>\n<p>As the above shows, we can optionally specify the depth when invoking the <em><strong>\u201crewrite-lsystem\u201d<\/strong><\/em> method .<\/p>\n<p>Before proceeding with the next example, I want to simplify the task of defining the custom class and the corresponding <em><strong>\u201cl-productions\u201d<\/strong><\/em> method. Because this is <em><strong>Lisp<\/strong><\/em>, that is fairly straightforward:<\/p>\n<figure id=\"attachment_2303\" aria-describedby=\"caption-attachment-2303\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/def-lsystem.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2303\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/01\/31\/using-l-system-to-generate-music\/def-lsystem\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/def-lsystem.png\" data-orig-size=\"394,113\" 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=\"Simplifying L-system Definition\" data-image-description=\"&lt;p&gt;Simplifying L-system Definition&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Simplifying L-system Definition&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/def-lsystem.png\" class=\"wp-image-2303\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/def-lsystem.png?resize=500%2C143&#038;ssl=1\" alt=\"Simplifying L-system Definition\" width=\"500\" height=\"143\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/def-lsystem.png?resize=300%2C86&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/def-lsystem.png?w=394&amp;ssl=1 394w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2303\" class=\"wp-caption-text\"><strong>Simplifying L-system Definition<\/strong><\/figcaption><\/figure>\n<p>I have defined a <em><strong>\u201cmacro\u201d<\/strong><\/em> called <em><strong>\u201cdef-lsystem\u201d<\/strong><\/em> that combines the two steps into one.<\/p>\n<p>Let us use this method to define a grammar that generates simple <em><strong>\u201cnoun phrases\u201d<\/strong><\/em> in English.<\/p>\n<figure id=\"attachment_2304\" aria-describedby=\"caption-attachment-2304\" style=\"width: 399px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/example2.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2304\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/01\/31\/using-l-system-to-generate-music\/example2-18\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/example2.png\" data-orig-size=\"313,167\" 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=\"An L-system for Simple Noun Phrase\" data-image-description=\"&lt;p&gt;An L-system for Simple Noun Phrase&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;An L-system for Simple Noun Phrase&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/example2.png\" class=\"wp-image-2304\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/example2.png?resize=399%2C213&#038;ssl=1\" alt=\"An L-system for Simple Noun Phrase\" width=\"399\" height=\"213\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/example2.png?resize=300%2C160&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/example2.png?w=313&amp;ssl=1 313w\" sizes=\"(max-width: 399px) 100vw, 399px\" \/><\/a><figcaption id=\"caption-attachment-2304\" class=\"wp-caption-text\"><strong>An L-system for Simple Noun Phrase<\/strong><\/figcaption><\/figure>\n<p>The output is <em><strong>non-deterministic<\/strong><\/em> in the sense that each time it runs, it is <em><strong>\u201clikely\u201d<\/strong><\/em> to produce a different output. Here is what I got when I ran it 5 times:<\/p>\n<figure id=\"attachment_2305\" aria-describedby=\"caption-attachment-2305\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/example2-output.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2305\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/01\/31\/using-l-system-to-generate-music\/example2-output-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/example2-output.png\" data-orig-size=\"284,154\" 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=\"Generated Outputs\" data-image-description=\"&lt;p&gt;Generated Outputs&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Generated Outputs&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/example2-output.png\" class=\"wp-image-2305\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/example2-output.png?resize=400%2C217&#038;ssl=1\" alt=\"Generated Outputs\" width=\"400\" height=\"217\" \/><\/a><figcaption id=\"caption-attachment-2305\" class=\"wp-caption-text\"><strong>Generated Outputs<\/strong><\/figcaption><\/figure>\n<p>The above example also shows that we can use functions inside rules, and this is quite powerful indeed.<\/p>\n<p>OK, now that you have an idea of what an <em><strong>\u201cL-system\u201d<\/strong><\/em> can do, how is it connected to music? Since our system can generate <em><strong>\u201csequences\u201d<\/strong><\/em> from a given <em><strong>\u201cformal grammar\u201d<\/strong><\/em>, we can define a grammar for generating <em><strong>\u201cpitches\u201d<\/strong><\/em> if we so desire.<\/p>\n<p>Before I define such a grammar, it is interesting to know that the <em><strong>\u201crewrite-lsystem\u201d<\/strong><\/em> method optionally takes a <em><strong>\u201cmapping\u201d<\/strong><\/em> from one alphabet to another. Using this feature, we can turn the generated <em><strong>\u201cnoun phrase\u201d<\/strong><\/em> into a <em><strong>\u201cpitch phrase\u201d<\/strong><\/em>. See the following example:<\/p>\n<figure id=\"attachment_2306\" aria-describedby=\"caption-attachment-2306\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/pitch-mapping.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2306\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/01\/31\/using-l-system-to-generate-music\/pitch-mapping\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/pitch-mapping.png\" data-orig-size=\"548,184\" 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=\"L-system Output Mapping\" data-image-description=\"&lt;p&gt;L-system Output Mapping&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;L-system Output Mapping&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/pitch-mapping.png\" class=\"wp-image-2306\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/pitch-mapping.png?resize=550%2C185&#038;ssl=1\" alt=\"L-system Output Mapping\" width=\"550\" height=\"185\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/pitch-mapping.png?resize=300%2C101&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/pitch-mapping.png?w=548&amp;ssl=1 548w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2306\" class=\"wp-caption-text\"><strong>L-system Output Mapping<\/strong><\/figcaption><\/figure>\n<p>Of course, if the focus is on pitch generation, it is preferable to do it directly. Here is an attempt:<\/p>\n<figure id=\"attachment_2307\" aria-describedby=\"caption-attachment-2307\" style=\"width: 450px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/pitch-generation.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2307\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/01\/31\/using-l-system-to-generate-music\/pitch-generation\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/pitch-generation.png\" data-orig-size=\"375,208\" 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=\"L-system for Pitch Generation\" data-image-description=\"&lt;p&gt;L-system for Pitch Generation&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;L-system for Pitch Generation&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/pitch-generation.png\" class=\"wp-image-2307\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/pitch-generation.png?resize=450%2C250&#038;ssl=1\" alt=\"L-system for Pitch Generation\" width=\"450\" height=\"250\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/pitch-generation.png?resize=300%2C166&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/pitch-generation.png?w=375&amp;ssl=1 375w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><figcaption id=\"caption-attachment-2307\" class=\"wp-caption-text\"><strong>L-system for Pitch Generation<\/strong><\/figcaption><\/figure>\n<p>I have deliberately introduced randomness in the generated sequence, by using a function that returns a random pitch every time it is called.<\/p>\n<p>Here is the output:<\/p>\n<figure id=\"attachment_2308\" aria-describedby=\"caption-attachment-2308\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/pitch-gen-output.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2308\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/01\/31\/using-l-system-to-generate-music\/pitch-gen-output\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/pitch-gen-output.png\" data-orig-size=\"441,155\" 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=\"Generated Pitches\" data-image-description=\"&lt;p&gt;Generated Pitches&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Generated Pitches&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/pitch-gen-output.png\" class=\"wp-image-2308\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/pitch-gen-output.png?resize=550%2C193&#038;ssl=1\" alt=\"Generated Pitches\" width=\"550\" height=\"193\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/pitch-gen-output.png?resize=300%2C105&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/pitch-gen-output.png?w=441&amp;ssl=1 441w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2308\" class=\"wp-caption-text\"><strong>Generated Pitches<\/strong><\/figcaption><\/figure>\n<p>I hope the discussion so far has given you an idea of what one can do with <em><strong>L-systems<\/strong><\/em> in <em><strong>Opusmodus<\/strong><\/em>. In fact, there are some advanced features that I don\u2019t have time to discuss today. I am reserving that discussion for a future article. Stay in touch!<\/p>\n<p>You can download the sample code <a href=\"http:\/\/www.rangakrish.com\/downloads\/LSystem-Test.opmo\" target=\"_blank\" rel=\"noopener\"><em><strong>here<\/strong><\/em><\/a>.<\/p>\n<p>Have a great weekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>L-system\u00a0is a powerful string rewriting system. Opusmodus\u00a0supports an implementation of this system (inspired by and based on this\u00a0implementation). It is possible to use the L-system to generate anything\u00a0that can be described in terms of an alphabet and a set of rewriting rules.\u00a0 Let us start with a simple example: As you can see, there are [&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_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":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[18,5,17],"tags":[263,264,37],"class_list":["post-2298","post","type-post","status-publish","format-standard","hentry","category-lisp","category-music","category-programming","tag-l-system","tag-music-synthesis","tag-opusmodus"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-B4","jetpack-related-posts":[{"id":2174,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/10\/24\/using-random-walk-principle-to-generate-music\/","url_meta":{"origin":2298,"position":0},"title":"Using Random Walk Principle to Generate Music","author":"admin","date":"October 24, 2020","format":false,"excerpt":"In mathematics, the simplest example of Random Walk\u00a0is a random process along a one-dimensional plane of integers, starting at 0 and moving in the positive or negative direction in steps of +1 or -1, with equal probability. The Random Walk\u00a0theory has been applied in many domains including Physics and even\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Random Walk Example in Mathematica","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/10\/Mathematica-RandomWalk-300x135.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":2298,"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":2206,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/11\/22\/a-library-for-algorithmic-music-composition\/","url_meta":{"origin":2298,"position":2},"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":2242,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/12\/20\/jfugue-a-java-library-for-music-synthesis\/","url_meta":{"origin":2298,"position":3},"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":159,"url":"https:\/\/www.rangakrish.com\/index.php\/2016\/02\/28\/using-cellular-automata-to-generate-music\/","url_meta":{"origin":2298,"position":4},"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":169,"url":"https:\/\/www.rangakrish.com\/index.php\/2016\/03\/04\/a-gui-front-end-for-cellular-automata-generator\/","url_meta":{"origin":2298,"position":5},"title":"A GUI Front-end for Cellular Automata Generator","author":"admin","date":"March 4, 2016","format":false,"excerpt":"In my last post, I explained how we can use Cellular Automata (CA) in Opusmodus\u00a0to create music. For today\u2019s post, I wanted to build a simple UI to control the parameters used in the CA example. This posed a practical difficulty. Opusmodus does not yet provide a GUI library. Even\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Opusmodus GUI Driver","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/03\/Opusmodus-Driver.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/03\/Opusmodus-Driver.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/03\/Opusmodus-Driver.png?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2298","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=2298"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2298\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=2298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=2298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=2298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}