<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.3">Jekyll</generator><link href="https://salmonhumorous.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://salmonhumorous.github.io/" rel="alternate" type="text/html" /><updated>2023-03-13T11:49:00+00:00</updated><id>https://salmonhumorous.github.io/feed.xml</id><title type="html">A collection of generative art and music explorations.</title><subtitle>Hi, this website serves as a portfolio of my work in the generative art and music medium.</subtitle><author><name>Shubham</name></author><entry><title type="html">[ Tutorial ] How to design a sound generator that can create beats in a web browser procedurally</title><link href="https://salmonhumorous.github.io/2023/03/07/tutorial-first.html" rel="alternate" type="text/html" title="[ Tutorial ] How to design a sound generator that can create beats in a web browser procedurally" /><published>2023-03-07T00:00:00+00:00</published><updated>2023-03-07T00:00:00+00:00</updated><id>https://salmonhumorous.github.io/2023/03/07/tutorial-first</id><content type="html" xml:base="https://salmonhumorous.github.io/2023/03/07/tutorial-first.html">&lt;p&gt;Hello, in this tutorial we’ll design a sound generator that can create music in a web browser randomly. Follow along, if it sound fun to you. We will employ &lt;strong&gt;p5js&lt;/strong&gt; and &lt;strong&gt;tone.js&lt;/strong&gt;, two Javascript libraries, for this work. We are just going to scrape the surface of these libraries’ capabilities in this lesson, though it is worthwhile to read through their documentation ( &lt;a href=&quot;https://p5js.org/reference/&quot;&gt;p5js&lt;/a&gt;, &lt;a href=&quot;https://tonejs.github.io/docs/14.7.77/index.html&quot;&gt;tone.js&lt;/a&gt; ).
&lt;br /&gt;Also, I’ll start by assuming you are familiar with Javascript and how to use these libraries.&lt;/p&gt;

&lt;p&gt;My curiosity has recently been piqued by the practise of creating music, specifically music synthesis. I was looking for a way to synthesise drum sounds on the internet when I discovered ADSR, a clever method for modulating sounds, to create  a wide range of instruments.&lt;br /&gt;
Watch this &lt;a href=&quot;https://www.youtube.com/watch?v=wUSva_BnedA&quot;&gt;video&lt;/a&gt;, by CodingTrain for more information on ADSR in detail.  I will try to explain it briefly here.
&lt;br /&gt;ADSR is an amplitude or volume envelope used to modify the contour of sound.  An ADSR envelope features these four stages.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Attack: duration till peak volume is reached;&lt;/li&gt;
  &lt;li&gt;Decay: the period of time during which a volume decreases after peaking;&lt;/li&gt;
  &lt;li&gt;Sustain: The level at which the sound will be maintained following the decay phase;&lt;/li&gt;
  &lt;li&gt;Release: the duration it takes for sound to end completely;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We may produce a broad variety of sounds with different tonal qualities and dynamic characteristics by adjusting these 4 factors. Regarding drum sounds, I found out that the attack and decay times of percussion instruments are quite brief and have a low level of sustain. These characteristics will be used by us to shape our instruments.&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;In order to create a sound generator that can procedurally create random music that is not just noise, we will need to figure out some things,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;First and foremost, we will require a sound-producing instrument.  Also, in order to create a sound machine, we will need a collection of instruments.&lt;/li&gt;
  &lt;li&gt;Second, this sound generator should produce sound according to the BPM set by us.&lt;/li&gt;
  &lt;li&gt;Finally, the pattern or sound that is produced should be random.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;br /&gt;
There are many ways to create sound using Tone.js, but we’ll utilise &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Tone.Synth()&lt;/code&gt; to create an oscillator that can create various sound waves. When initiating we can also define parameters for a ADSR envelope to apply to the sound wave. Hence, we can utilise this method to make the instrument we want like this,&lt;/p&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;filter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Tone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;AutoFilter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;toMaster&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;instrument&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Tone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Synth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;oscillator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;sine&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt; 
    &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;envelope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
      &lt;span class=&quot;na&quot;&gt;attack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.001&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;decay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.005&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;sustain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;release&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.005&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;connect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;In the above code, we are connecting this synth to a low pass filter, which is then connected to the master output (ex. speakers). In tone.js, we can connect different filters and instruments together, sort of analog way using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.connect&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Now, we can use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;instrument.triggerAttackRelease(freq, duration);&lt;/code&gt; command to play sound using this instrument. We send in two arguments, frequency or the note we want to play and the period for which we want to play.
&lt;br /&gt;As of now, we have successfully produced an instrument that can play any note.
To build a machine, all that has to be done is to create other instruments with various envelope parameters using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Tone.synth()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As per our next criterion, we must put logic in place to have this machine play music at random and in accordance with a bpm.
We will make use of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Tone.Transport&lt;/code&gt; to accomplish this. Transport is a core feature of Tone.js that provide a timeline for scheduling events at precise time.  It enables us to control the tempo, switch between different time signatures, loop a piece, synchronise different parts of our composition, and many other things, which you can read in detail in the &lt;a href=&quot;https://tonejs.github.io/docs/14.7.77/Transport&quot;&gt;docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Each of our instruments will be programmed to play a note at predetermined intervals using the transport system. I won’t go into depth regarding the structure of our composition, but it will have a 4/4 beat, which means there will be 4 notes in each measure.
&lt;br /&gt;This sketch shows how instrument and transport system works,&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;img src=&quot;/assets/img/blog/tutorial-first-1.jpg&quot; alt=&quot;Rough sketch of instrument and transport system&quot; title=&quot;Rough sketch&quot; /&gt;
&lt;br /&gt;
&lt;br /&gt;But, we still need to include some &lt;em&gt;randomness&lt;/em&gt; in the mix; otherwise, all the instruments will play at the same time throughout each interval, creating the same loop.
&lt;br /&gt;Thus,  we will give each trigger event a probability so that, at each interval, each instrument will have a chance of being triggered or not. As time passes, this will lead to random patterns, as seen in the illustration below,
&lt;br /&gt;
&lt;br /&gt;&lt;img src=&quot;/assets/img/blog/tutorial-first-2.jpg&quot; alt=&quot;Sketch showing beat structure&quot; title=&quot;Sketch showing beat structure&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;In code, we can implement something like this,&lt;/p&gt;
&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;Tone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Transport&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;scheduleRepeat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;instrument&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;triggerAttackRelease&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;C4&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;8n&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;Tone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Transport&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Set the tempo&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;Tone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Transport&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bpm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;180&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We accomplished what we set out to do, but there is still much room for improvement. For instance, we may devise a function to provide randomization while still maintaining the composition’s overall structure. So that while the notes are random, pattern still repeats. 
&lt;br /&gt;We can also use logic to progressively alter the notes that different instruments play over time.&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;You can check my implementation and full code on &lt;a href=&quot;https://editor.p5js.org/salmonhumorous/sketches/7RbaJl2F2&quot;&gt;P5js Editor&lt;/a&gt;. If you create something cool with this, don’t forget to share it with me. You will find many more ways to experiment with sounds in the browser by looking at Examples in P5js and Tone.js , be sure to check that out as well.
&lt;br /&gt; Also, this tutorial is only a introduction to creating sounds and compositions in browser. Thus, for any meaningfull compostion you will have to further build upon it. Have fun!!&lt;/p&gt;</content><author><name>Shubham</name></author><summary type="html">Hello, in this tutorial we’ll design a sound generator that can create music in a web browser randomly. Follow along, if it sound fun to you. We will employ p5js and tone.js, two Javascript libraries, for this work. We are just going to scrape the surface of these libraries’ capabilities in this lesson, though it is worthwhile to read through their documentation ( p5js, tone.js ). Also, I’ll start by assuming you are familiar with Javascript and how to use these libraries.</summary></entry></feed>