<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Harsh Mehta</title>
    <description>The latest articles on DEV Community by Harsh Mehta (@obitodarky).</description>
    <link>https://dev.to/obitodarky</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F189729%2F78dac429-5753-4a43-ba42-be5d5cdbb06a.png</url>
      <title>DEV Community: Harsh Mehta</title>
      <link>https://dev.to/obitodarky</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/obitodarky"/>
    <language>en</language>
    <item>
      <title>Create Music with Code: 2</title>
      <dc:creator>Harsh Mehta</dc:creator>
      <pubDate>Thu, 02 Jul 2020 03:30:14 +0000</pubDate>
      <link>https://dev.to/obitodarky/create-music-with-code-2-57je</link>
      <guid>https://dev.to/obitodarky/create-music-with-code-2-57je</guid>
      <description>&lt;p&gt;Well, it's been a long time since I wrote &lt;a href="https://dev.to/obitodarky/create-music-with-code-4d8o"&gt;Create Music With Code&lt;/a&gt; and a lot of people seemed interested in a second part. Unfortunately, I didn't get the time, up until now! &lt;br&gt;
So here goes! &lt;/p&gt;
&lt;h2&gt;
  
  
  Where We Left Off
&lt;/h2&gt;

&lt;p&gt;Last time we left off with &lt;code&gt;loops&lt;/code&gt;. We learned how to loop basically anything that you want to play for like an infinite amount of time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="kp"&gt;loop&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;sample&lt;/span&gt; &lt;span class="ss"&gt;:bd_haus&lt;/span&gt;
  &lt;span class="nb"&gt;sleep&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code snippet will play the &lt;code&gt;bd_haus&lt;/code&gt; sample every second over and over again. This makes sense from a programming point of view, but since we cannot exit from this loop, it makes no sense to just play one loop and ignore the rest of the code. Good for us that Sonic Pi has &lt;code&gt;live_loop&lt;/code&gt; for that!&lt;/p&gt;

&lt;h2&gt;
  
  
  Live Loops
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;live_loop&lt;/code&gt; is a way to loop your music and also play other code while you're at it. Moreover, you can play multiple live loops at the same time. A better alternative to &lt;code&gt;loop&lt;/code&gt; don't you think? Yes, in fact, I've never used &lt;code&gt;loop&lt;/code&gt; at all :p! &lt;/p&gt;

&lt;p&gt;So why learn &lt;code&gt;loop&lt;/code&gt; in the first place? In my opinion, &lt;code&gt;loop&lt;/code&gt; is a good way to teach the basics of programming to kids, or maybe someone who is new to programming itself, how loops work in other programming languages, which I think is one of the bases of any programming language out there. &lt;/p&gt;

&lt;p&gt;Enough talk, let's cut to the chase. &lt;br&gt;
A &lt;code&gt;live_loop&lt;/code&gt; looks very similar to &lt;code&gt;loop&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;live_loop&lt;/span&gt; &lt;span class="ss"&gt;:beat&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;sample&lt;/span&gt; &lt;span class="ss"&gt;:bd_haus&lt;/span&gt;
  &lt;span class="nb"&gt;sleep&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="nb"&gt;sleep&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="n"&gt;play&lt;/span&gt; &lt;span class="ss"&gt;:c4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will play the loop and after 3 seconds, it will play the &lt;code&gt;play :c4&lt;/code&gt; command. You'll notice how the &lt;code&gt;live_loop&lt;/code&gt; still keeps on playing and that's the best part about it.&lt;br&gt;
You can notice that it's a bit different than the &lt;code&gt;loop&lt;/code&gt;. I've added &lt;code&gt;:beat&lt;/code&gt; after its syntax. In order to declare a &lt;code&gt;live_loop&lt;/code&gt; you need to name it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;live_loop&lt;/span&gt; &lt;span class="p"&gt;:[&lt;/span&gt;&lt;span class="n"&gt;any_name&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
   &lt;span class="c1"&gt;#your music&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's actually a very good reason why we name a &lt;code&gt;live_loop&lt;/code&gt;, but I'm not covering it in this article. What's even more interesting is that &lt;code&gt;live_loop&lt;/code&gt; gives you the power to change it while it's running!&lt;br&gt;
In the previous code, try changing anything in the &lt;code&gt;live_loop&lt;/code&gt; while it's still running. for example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;live_loop&lt;/span&gt; &lt;span class="ss"&gt;:beat&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;sample&lt;/span&gt; &lt;span class="ss"&gt;:bd_sone&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This probably won't make much sense if you're reading this but when you try changing anything in a &lt;code&gt;live_loop&lt;/code&gt; all the changes will reflect even when you are still running the code. Which is amazing. &lt;/p&gt;

&lt;h2&gt;
  
  
  Using Effects
&lt;/h2&gt;

&lt;p&gt;In Sonic Pi, you can even add Effects or FX to almost anything that you play. You can add FX by using the following syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;with_fx&lt;/span&gt; &lt;span class="p"&gt;:[&lt;/span&gt;&lt;span class="n"&gt;selected_fx&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="c1"&gt;#code that you want to add the FX to&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll get a drop-down list as soon as you write &lt;code&gt;with_fx&lt;/code&gt; which will give a list of available effects you can use.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fir5qzz3rvzjwk71fpesd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fir5qzz3rvzjwk71fpesd.png" alt="List" width="525" height="275"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's try out the &lt;code&gt;echo&lt;/code&gt; FX in a &lt;code&gt;live_loop&lt;/code&gt; and see how it sounds.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;live_loop&lt;/span&gt; &lt;span class="ss"&gt;:tune&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;with_fx&lt;/span&gt; &lt;span class="ss"&gt;:echo&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;play&lt;/span&gt; &lt;span class="ss"&gt;:c3&lt;/span&gt;
    &lt;span class="nb"&gt;sleep&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll notice that the command &lt;code&gt;play :c3&lt;/code&gt; echoes. You can use anything and give it an FX. For example, you can even use samples, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;live_loop&lt;/span&gt; &lt;span class="ss"&gt;:beat&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;with_fx&lt;/span&gt; &lt;span class="ss"&gt;:reverb&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;sample&lt;/span&gt; &lt;span class="ss"&gt;:bd_haus&lt;/span&gt;
    &lt;span class="nb"&gt;sleep&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;reverb&lt;/code&gt; FX adds a bit of "room effect" to your music. &lt;/p&gt;

&lt;h2&gt;
  
  
  Randomization
&lt;/h2&gt;

&lt;p&gt;Sonic Pi has some built-in functions that can add randomness to your music. One such function is the &lt;code&gt;rrand&lt;/code&gt; function which will give a random value between two numbers. The syntax goes something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;rrand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Providing &lt;code&gt;min&lt;/code&gt; and &lt;code&gt;max&lt;/code&gt; basically defines the range between two numbers. So let's check it out&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;play&lt;/span&gt; &lt;span class="n"&gt;rrand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;95&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run it and you'll notice it'll play &lt;code&gt;86.251&lt;/code&gt; as a random value. But hold on, how come that every time you press &lt;code&gt;Run&lt;/code&gt;, it plays the same note over and over again? &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The answer is that it is not truly random, it’s pseudo-random. Sonic Pi will give you random-like numbers in a repeatable manner. This is very useful for ensuring that the music you create on your machine sounds identical to everybody else’s machine - even if you use some randomness in your composition.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The above reasoning is taken from Sonic Pi's official documentation and I don't think that I can explain any better than this.&lt;/p&gt;

&lt;p&gt;What's interesting is that if you play it twice it will generate a different note the second time. For example, check this out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;play&lt;/span&gt; &lt;span class="n"&gt;rrand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;95&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;sleep&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="n"&gt;play&lt;/span&gt; &lt;span class="n"&gt;rrand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;95&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run it and you'll notice that it plays a new note the second time. So basically, a new pseudo-random number is generated if you use it again. &lt;br&gt;
You can take a &lt;code&gt;live_loop&lt;/code&gt; to generate random notes every time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;live_loop&lt;/span&gt; &lt;span class="ss"&gt;:melody&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;play&lt;/span&gt; &lt;span class="n"&gt;rrand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nb"&gt;sleep&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will play a different note every time it enters the loop.&lt;/p&gt;

&lt;h3&gt;
  
  
  Done and Dusted?
&lt;/h3&gt;

&lt;p&gt;That's about it for now! Play around with live loops, FX, random numbers, try using multiple live loops, adding random numbers in different sample parameters, and get creative! Remember, it's all fun and games. There is no such thing as a right and a wrong approach in art! It all depends on your creativity.&lt;/p&gt;

&lt;h3&gt;
  
  
  Missed Part 1?
&lt;/h3&gt;

&lt;p&gt;I have covered the extreme basics of Sonic Pi in my Part 1, in case you missed it, &lt;a href="https://dev.to/obitodarky/create-music-with-code-4d8o"&gt;do check it out&lt;/a&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  Contact Me
&lt;/h3&gt;

&lt;p&gt;You can contact me if you have any doubts and you can also let me know on social media if you'd like a part 3. :D &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/obitodarky" rel="noopener noreferrer"&gt;Find me on Twitter&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/obitodarky" rel="noopener noreferrer"&gt;Find me on GitHub&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>music</category>
    </item>
    <item>
      <title>Create Music with Code</title>
      <dc:creator>Harsh Mehta</dc:creator>
      <pubDate>Wed, 01 Apr 2020 09:01:39 +0000</pubDate>
      <link>https://dev.to/obitodarky/create-music-with-code-4d8o</link>
      <guid>https://dev.to/obitodarky/create-music-with-code-4d8o</guid>
      <description>&lt;p&gt;Hello 👋🏼&lt;/p&gt;

&lt;p&gt;I was always interested in music but somehow I just couldn't get myself to explore those costly tools with complex UI. Until a few weeks ago, I found something really interesting. I found out about a tool that compiles code into Music! I tested it out and was instantly hooked. It's really fun and even if you're not that much into making music, you can kill some great time with Sonic Pi. So, here's a blog on how to get started! &lt;/p&gt;

&lt;h2&gt;
  
  
  What's Sonic Pi?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Sonic Pi is a code-based music creation and performance tool.&lt;/code&gt;&lt;br&gt;
This basically means that you can create, mix music by code. Sonic Pi is built on Ruby and is &lt;a href="//github.com/samaaron/sonic-pi/"&gt;Open Source&lt;/a&gt;.&lt;br&gt;
You can download Sonic Pi from &lt;a href="//sonic-pi.net/"&gt;their website&lt;/a&gt;. You can download it for Windows, macOS and Raspberry Pi as well. &lt;/p&gt;
&lt;h2&gt;
  
  
  Basic Music Theory
&lt;/h2&gt;

&lt;p&gt;Before we start, I'll be covering some basic Music theory which is very easy to understand so don't worry.&lt;/p&gt;
&lt;h3&gt;
  
  
  Notes
&lt;/h3&gt;

&lt;p&gt;The piano diagram below shows the various piano notes that piano keys usually correspond to. The ones with &lt;code&gt;#&lt;/code&gt; are the sharp notes and the ones with the symbol that looks like a &lt;code&gt;b&lt;/code&gt; are the flat notes. Keys, wherever multiple notes are written, can be referenced as those notes. Meaning that &lt;code&gt;C# and Db&lt;/code&gt; are the same key.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.piano-keyboard-guide.com%2Fwp-content%2Fuploads%2F2015%2F05%2Fpiano_notes.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.piano-keyboard-guide.com%2Fwp-content%2Fuploads%2F2015%2F05%2Fpiano_notes.jpg" width="690" height="252"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can test what these notes usually sound on piano on &lt;a href="https://www.onlinepianist.com/virtual-piano" rel="noopener noreferrer"&gt;this virtual piano&lt;/a&gt;. Make sure you enable the "Letter Notes" so that you know which note you are playing by pressing the key. &lt;/p&gt;
&lt;h3&gt;
  
  
  Numbers, Notes and Octaves.
&lt;/h3&gt;

&lt;p&gt;Every note along with its octave represents a number, depending on the number of keys on a keyboard. An Octave is an interval between musical pitch and another with double its frequency. So, numbers or notes belonging to one octave will have the same frequency and those with different octave numbers will have different frequencies. Here is a cheat sheet I've taken from MeHackit's Sonic Pi tutorial. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fsonic-pi.mehackit.org%2Fassets%2Fimg%2Fmidi_notes.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fsonic-pi.mehackit.org%2Fassets%2Fimg%2Fmidi_notes.png" width="800" height="233"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Coding on Sonic Pi
&lt;/h2&gt;

&lt;p&gt;Once you open Sonic Pi you'll probably be able to understand most of the interface but Sonic Pi's documentation has a UI guide that I'm attaching below for your reference.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsonic-pi.net%2Fmedia%2Fimages%2Ftutorial%2FGUI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsonic-pi.net%2Fmedia%2Fimages%2Ftutorial%2FGUI.png" width="800" height="478"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Playing notes
&lt;/h3&gt;

&lt;p&gt;We'll start by playing a note and this is how you play a note. In your editor, just type &lt;code&gt;play :c&lt;/code&gt; and hit &lt;code&gt;Run&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Congratulations! You've created and played your first music :p! &lt;/p&gt;

&lt;p&gt;You can also change the octaves in the notes like so&lt;br&gt;
&lt;code&gt;play :c2&lt;/code&gt; which will play the C note at the 2nd octave. You can experiment with different notes and octaves yourself. You can also refer to the cheat sheet provided above.&lt;/p&gt;

&lt;p&gt;You can also play a note by its number. If you look for the note C at 4th octave you'll find the number 60. So, playing &lt;code&gt;:c4&lt;/code&gt; is the same as playing the 60th note number. To play a note number on Sonic Pi, just type &lt;code&gt;play&lt;/code&gt; and the corresponding note:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;play&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how we did not include the colon as we did in the &lt;code&gt;play :c&lt;/code&gt; command. When we play a number, we don't have to use any colon. &lt;/p&gt;

&lt;h2&gt;
  
  
  Changing the Synth
&lt;/h2&gt;

&lt;p&gt;One thing you'll notice, that these notes sound very different than a normal piano. That's because Sonic Pi uses a default synthesizer and we can change the synth with the &lt;code&gt;use_synth&lt;/code&gt; command. A bunch of synth options will appear in a list like so: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fc40xlk4jl3f5c5mv9wwi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fc40xlk4jl3f5c5mv9wwi.png" alt="Alt Text" width="580" height="223"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'm using the &lt;code&gt;piano&lt;/code&gt; synth and after that, all my notes will sound like a piano&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;use_synth&lt;/span&gt; &lt;span class="ss"&gt;:piano&lt;/span&gt;
&lt;span class="n"&gt;play&lt;/span&gt; &lt;span class="ss"&gt;:c4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Awesome right? You can try playing with different synth options available to you by Sonic Pi. &lt;/p&gt;

&lt;h2&gt;
  
  
  Sleep
&lt;/h2&gt;

&lt;p&gt;We just played a single note up until now, but what if we want to play more than one note? I can just play another note below my first note, simple right?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;play&lt;/span&gt; &lt;span class="ss"&gt;:c4&lt;/span&gt;
&lt;span class="n"&gt;play&lt;/span&gt; &lt;span class="ss"&gt;:c2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No. If you tried it yourself you will know what's wrong. The problem is that Sonic Pi executes lines of code quickly. So technically, before my first note even finishes playing, my second note will start which is almost instant. So what's the workaround? For that, we have the &lt;code&gt;sleep&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;play&lt;/span&gt; &lt;span class="ss"&gt;:c4&lt;/span&gt;
&lt;span class="nb"&gt;sleep&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="n"&gt;play&lt;/span&gt; &lt;span class="ss"&gt;:c2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the first note is played, the sleep command will tell Sonic Pi to stop for &lt;em&gt;a second&lt;/em&gt; and &lt;em&gt;then&lt;/em&gt; play the next note. The time is in seconds so if you type &lt;code&gt;sleep 0.25&lt;/code&gt;, it will sleep for a Quarter of a second. &lt;/p&gt;

&lt;h2&gt;
  
  
  Parameters
&lt;/h2&gt;

&lt;p&gt;You can tweak notes by changing their different parameters. You can add parameters to your notes separated by a comma.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8pfzxwxea1fscsegtw6l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8pfzxwxea1fscsegtw6l.png" alt="Alt Text" width="602" height="226"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You'll see a bunch of parameter options that we can use. We'll go through a few of them. &lt;/p&gt;

&lt;h3&gt;
  
  
  Pan
&lt;/h3&gt;

&lt;p&gt;With the &lt;code&gt;pan&lt;/code&gt; parameter, you can change the direction of your sound. For example, you can play your sound on the right or left or in the middle or anywhere in-between left and right. The value ranges from &lt;code&gt;-1 to 1&lt;/code&gt;. -1 being extreme left and 1 being extreme right.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;play&lt;/span&gt; &lt;span class="ss"&gt;:c3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;pan: &lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;#plays the note on left&lt;/span&gt;
&lt;span class="nb"&gt;sleep&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="n"&gt;play&lt;/span&gt; &lt;span class="ss"&gt;:c3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;pan: &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;#plays the note on right&lt;/span&gt;
&lt;span class="nb"&gt;sleep&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="n"&gt;play&lt;/span&gt; &lt;span class="ss"&gt;:c3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;pan: &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="c1"&gt;#plays the note in the middle(default)&lt;/span&gt;
&lt;span class="nb"&gt;sleep&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="n"&gt;play&lt;/span&gt; &lt;span class="ss"&gt;:c3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;pan: &lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;0.25&lt;/span&gt; &lt;span class="c1"&gt;#plays the note a little bit to the left&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Amp
&lt;/h3&gt;

&lt;p&gt;With the &lt;code&gt;amp&lt;/code&gt; parameter, you can change the amp of your notes. Which basically means you can change the volume.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;play&lt;/span&gt; &lt;span class="ss"&gt;:c4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;amp: &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you run the above snippet, you'll hear the volume has increased. The default amp value is 1 and it starts from 0. If you put &lt;code&gt;amp: 0&lt;/code&gt;, the volume will be 0 and you won't be able to hear anything. &lt;/p&gt;

&lt;h2&gt;
  
  
  Samples
&lt;/h2&gt;

&lt;p&gt;We'll explore something really interesting called samples. Samples are basically pre-recorded sounds that you can use in your music. Sonic Pi has around 130 of such samples and also gives you the option to use external samples that aren't available in Sonic Pi. You can play different samples using the &lt;code&gt;sample&lt;/code&gt; keyword and you'll get a list of samples you can select from. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7fgtaw8pcthpwqwybrt5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7fgtaw8pcthpwqwybrt5.png" alt="Alt Text" width="510" height="219"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are a lot of different cool samples that you can explore. For example, the following sample will give you a &lt;code&gt;kick&lt;/code&gt; or a &lt;code&gt;beat&lt;/code&gt; sound.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;sample&lt;/span&gt; &lt;span class="ss"&gt;:bd_haus&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just like the &lt;code&gt;play&lt;/code&gt; command, samples also have parameters that you can use to tweak how your samples sound. You can play with different samples and different parameters. &lt;/p&gt;

&lt;h2&gt;
  
  
  Loops
&lt;/h2&gt;

&lt;p&gt;That was nice, in fact, it was so nice that we'd like to play it over and over again. So rather than playing the sample on every line, which will take me forever to do so, we can use the loop command given to us by Sonic Pi. The syntax goes something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="kp"&gt;loop&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="c1"&gt;#code that you want to loop&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that every loop &lt;em&gt;must have at least one sleep command&lt;/em&gt;. Otherwise, Sonic Pi will try to play everything at once, which will completely cause it to break and will throw you an error about the loop not sleeping or syncing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="kp"&gt;loop&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;sample&lt;/span&gt; &lt;span class="ss"&gt;:bd_haus&lt;/span&gt;
  &lt;span class="nb"&gt;sleep&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you might have guessed, the above code snippet will play the &lt;code&gt;bd_haus&lt;/code&gt; sample every second over and over again. &lt;/p&gt;

&lt;h2&gt;
  
  
  Done and Dusted?
&lt;/h2&gt;

&lt;p&gt;That's about it for now! Play around with notes, samples, loops, and try changing different parameters and get creative! Remember, it's all fun and games. There is no such thing as a right and a wrong approach in art! It all depends on your creativity. &lt;/p&gt;

&lt;h3&gt;
  
  
  Part 2
&lt;/h3&gt;

&lt;p&gt;In &lt;a href="https://dev.to/obitodarky/create-music-with-code-2-57je"&gt;Part 2&lt;/a&gt;, I have covered the basics of &lt;code&gt;live_loop&lt;/code&gt;, adding effects, and using randomness in your music! Do check out if you like this part :) &lt;/p&gt;

&lt;h3&gt;
  
  
  Contact Me
&lt;/h3&gt;

&lt;p&gt;You can contact me if you have any doubts and you can also let me know on social media and do give a feedback! :D &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/obitodarky" rel="noopener noreferrer"&gt;Find me on Twitter&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/obitodarky" rel="noopener noreferrer"&gt;Find me on GitHub&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Stay Home and Stay Safe :) &amp;lt;3&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>music</category>
    </item>
    <item>
      <title>Filter Images in Flutter</title>
      <dc:creator>Harsh Mehta</dc:creator>
      <pubDate>Wed, 29 Jan 2020 07:46:22 +0000</pubDate>
      <link>https://dev.to/obitodarky/color-filters-in-flutter-2g71</link>
      <guid>https://dev.to/obitodarky/color-filters-in-flutter-2g71</guid>
      <description>&lt;p&gt;I've been finding some really interesting "hacks" that have helped me a lot to come up with easy solutions during my internship as a Flutter Dev. &lt;/p&gt;

&lt;p&gt;One such solution was using &lt;code&gt;ColorFiltered&lt;/code&gt; Widget in Flutter to Filter my images. Initially, our designer could provide any necessary images or assets required to build UI. But the problem came when there were multiple "versions" of the same image. By versions, I mean the same image in different colors and transparency. In fact, if I calculate, there were approximately 15 images that had around 3 versions each that had just color changes. &lt;/p&gt;

&lt;p&gt;That's around 45 images. As a developer, keeping all the images that were only different in color wouldn't make much sense. This would increase the App Size and my work as I would have to manage all the images with their proper locations and whatnot. Apart from that, I would have to load a different image every time, which again doesn't make sense as it would probably add unnecessary load to the App. &lt;/p&gt;

&lt;p&gt;Luckily, Flutter has the widget called &lt;code&gt;ColorFiltered&lt;/code&gt; that can handle all of that for me. Here's what I did. &lt;/p&gt;

&lt;p&gt;I had this original image:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fes6olx9enzdsmacarj5s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fes6olx9enzdsmacarj5s.png" width="512" height="512"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And this was one of the versions of the same image: (Ignore the background)&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0y8065odea8uskqpj532.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0y8065odea8uskqpj532.png" width="142" height="105"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Similarly, as stated before, there were 15 different images and 3 such "versions" of these images. Here's an example for the above 2 images I used in Flutter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ColorFiltered(
child: Image.asset("assets/images/originalImage.png", scale: 5,),
colorFilter: ColorFilter.mode(Colors.white, BlendMode.srcIn),)
),
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;ColorFiltered&lt;/code&gt; widget has the &lt;code&gt;colorFilter&lt;/code&gt; property that we can use to add filters to a Widget. I added a white color and used &lt;code&gt;BlendMode&lt;/code&gt; class that can be used to blend pixels. &lt;code&gt;srcIn&lt;/code&gt; is just a property that overlaps both the color and on the original image. There are a lot of properties for the &lt;code&gt;BlendMode&lt;/code&gt; class that are provided by the folks at Flutter and you can check them out &lt;a href="https://api.flutter.dev/flutter/dart-ui/BlendMode-class.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This gave me exactly the image I wanted: &lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0y8065odea8uskqpj532.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0y8065odea8uskqpj532.png" width="142" height="105"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With just 2 lines of code, I was able to reduce around 45 images 15, and what's better? I didn't even have to trouble the designer for the images and did it myself without much hassle and the help of Flutter, of course!&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>dart</category>
      <category>flutter</category>
    </item>
  </channel>
</rss>
