<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by naivesound on Medium]]></title>
        <description><![CDATA[Stories by naivesound on Medium]]></description>
        <link>https://medium.com/@naive_sound?source=rss-35e89cb7a0f2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*b7ToJRUZqxuAHASEi9l5Bg.png</url>
            <title>Stories by naivesound on Medium</title>
            <link>https://medium.com/@naive_sound?source=rss-35e89cb7a0f2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 24 May 2026 02:28:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@naive_sound/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Fugue, a music tracker concept]]></title>
            <link>https://medium.com/@naive_sound/fugue-a-music-tracker-concept-7a1ff3ec7905?source=rss-35e89cb7a0f2------2</link>
            <guid isPermaLink="false">https://medium.com/p/7a1ff3ec7905</guid>
            <category><![CDATA[retrocomputing]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[music]]></category>
            <dc:creator><![CDATA[naivesound]]></dc:creator>
            <pubDate>Sun, 12 Nov 2017 08:06:54 GMT</pubDate>
            <atom:updated>2017-11-12T08:08:13.042Z</atom:updated>
            <content:encoded><![CDATA[<p>I always liked the simplicity of music trackers. As a developer it is very appealing to think of music as a sequence of time-based repetitive events. It resembles the flow of a program written in an imperative language.</p><p>Now, what if a tracker could be scripted by some programming language to save time spent on typing and copy-pasting notes/effects in the patterns? Or, what if some AI was trained to construct rhythms and chords and melodies for you?</p><p>This is what Fugue is about.</p><p>It started as an idea for the ProductHunt hackathon and it is only the beginning of the project. I have hardly written a few pages of code so far. I am using Go because it feels much more productive comparing to C++ or Rust (it’s very subjective, and all these languages are nice, but I feel the most productive with Go).</p><p>I will be using <a href="https://github.com/zserge/webview">github.com/zserge/webview</a> for desktop UI, <a href="https://github.com/thestk/rtaudio">github.com/thestk/rtaudio</a> for audio playback. Potentially, I am thinking about using gopherjs to get a web version as well as gomobile to support Android/iOS.</p><p>So, wish me good luck and stay tuned!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7a1ff3ec7905" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Glitch after one month]]></title>
            <link>https://medium.com/@naive_sound/glitch-after-one-month-b8f634c7850e?source=rss-35e89cb7a0f2------2</link>
            <guid isPermaLink="false">https://medium.com/p/b8f634c7850e</guid>
            <category><![CDATA[music]]></category>
            <category><![CDATA[livecoding]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[naivesound]]></dc:creator>
            <pubDate>Thu, 28 Jul 2016 04:52:25 GMT</pubDate>
            <atom:updated>2016-07-28T04:52:25.793Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="http://naivesound.com/glitch"><em>Glitch</em></a><em> is a minimal environment for making algorithmic music and live coding. It uses simple arithmetic expressions as a programming language and has only a few functions to learn, yet it allows creating some interesting music pieces.</em></p><p>It’s been a month since I last talked about Glitch here, but it has been a very productive time, which has totally changed my vision of Glitch and its future.</p><h4>And old ox makes a straight furrow</h4><p>The original Glitch was written in React and ES6 and worked good enough in modern Chrome browsers. It was a quick and “trendy” way to make web apps. I foolishly hoped the same webapp would run on mobile and on desktop (using Electron), I also hoped people would extend it by writing JavaScript plugins.</p><blockquote>It’s 2016 here and web is still a disappointment.</blockquote><p>But Glitch never worked well even on the most high-end Android phones, Glitch wasn’t capable to using PulseAudio or Jack on Linux to let me play along using other synthesizer apps, Glitch even failed to play more-or-less complex music on some decent multi-core PCs!</p><p>That’s where I had to recall that I’m still a good low-level programmer and rewriting Glitch in C should not be that hard. The core of glitch — the expression evaluation engine — has been rewritten in a couple of days, covered with tests and all the memory leaks have been fixed. It’s now available here — <a href="https://github.com/zserge/expr">https://github.com/zserge/expr</a>.</p><p>The library of music functions (instruments, sequencers, effects) took some time, but now all of the original glitch functions are supported. Then I <a href="http://zserge.com/blog/my-experience-with-asmjs.html">used Emscripten to translate C into JavaScript</a> and it really boosted the performance of the web app, especially on Firefox! Chrome also did a much better job at optimizing asm.js code comparing to the full-featured JavaScript.</p><p>As you know, these days JavaScript is not a language that one can write in notepad and press <em>F5</em> to see the changes. It requires a number of compilers (for JSX, for ES6, for Less/SASS), a build system (webpack) and a heap of other stuff behind the curtains. So after I plugged my new fast JavaScript code into the old Glitch setup — my builds slowed down to ~40 seconds each time.</p><blockquote>At this point I started really miss the old-school JavaScript development, but <a href="http://mithril.js.org">Mithril.js</a> came to the rescue.</blockquote><p>I opened vim, started index.html and styles.css, dropped in <a href="https://github.com/lhorie/mithril.js">mithril.js</a> for UI components and then wrote just one big JS file — the identical Glitch web app, written in one day, using no build system and no compilers. Yes, it won’t run on some old browsers, but the original Glitch failed to run there, too, because of its slowness.</p><p>So this new Glitch-rewritten-in-C is the app that you can see at <a href="http://naivesound.com/glitch">http://naivesound.com/glitch</a>. It has the same look, the same functionality, but works much faster. That’s how old school technologies have saved a project.</p><h4>New features</h4><p>The general syntax of Glitch remained unchanged, but there are some new things added.</p><p>The most important one is <strong>constants for notes and drums</strong>. These are just variables written in caps that should not be modified as they contain some useful pre-defined values. For example, to define a sequence of notes you may now write their names:</p><pre>sin(hz(seq(240, C4, D#4, F4, D#4)))</pre><p>Also, for TR808 drums you may not keep in memory drum numbers, you can use abbreviations — BD (bass drum), SD (snare drum), MT (middle tom), HH (high-hat), OH (open hat), RS (rimshot), CP (clap), MA (maracas):</p><pre>mix(tr808(BD, seq(120,1,0)), tr808(SD, seq(120,0,1)), tr808(HH,seq(120*2,1)))</pre><p>Next, sequencers now can take a pair of values for tempo — the optional first number would be the initial offset in beats. That’s how the drum pattern above could be rewritten as shown below (snare drum is offset by half of a beat from the bass drum):</p><pre>mix(tr808(BD,seq(60,1)), tr808(SD,seq((0.5,60),1)),tr808(HH,seq(240,1)))</pre><p>I’ve also added the <em>hpf(signal, cutoff) </em>function. Although it can be implemented by subtracting the low-pass filter output from the original signal, I find it too long to type.</p><p>Finally, I’ve added the great <em>delay(signal, time, level, feedback)</em> function, which enriches sound a lot. It takes a current signal value, delay time in seconds, a delay mixer level (0..1) and a feedback amount (0..1) which affects the repetition of the delayed signal.</p><p>You can <a href="http://naivesound.com/glitch/#bpm%3D120%2C%0Aroot%3Dseq(bpm%2F8%2C%200%2C%202)%2C%0Abass%3Dseq(bpm%2C%20C1%2C%20C1%2C%20D%231%2C%20F1)%2Broot%2C%0Amix(%0Atr808(BD%2C%20seq(bpm%2C%201))%2C%0Atr808(HH%2C%20seq(bpm*2%2C%201))%2C%0Adelay(env(0.01%2C%200.1%2C%20lpf(saw(hz(bass))))%2C%200.125%2C%20x%2C%20y)%0A)">hear delay in action</a> — try moving your mouse to modify delay level and feedback parameters.</p><h4>What’s next?</h4><p>Well, I had to give up on Electron version because it wouldn’t support JavaScript plugins and it would be too slow. I’m waiting for WebAssembly to become a mainstream, that would make web version of Glitch even faster.</p><p>Meanwhile, I’m working on a <a href="https://github.com/naivesound/glitch"><strong>desktop version</strong></a><strong> of Glitch</strong>. It’s a command-line app at the moment and it runs well on Linux, Window and Mac, using low-level audio libraries. I was able to make the beat and bass in Glitch and played the piano using Pianoteq at the same time without any sound lags.</p><p>But the major difference of desktop Glitch comparing to the web app is that <strong>you can use your own samples </strong>— each sample would appear as a Glitch function, samples can be grouped in folders and a function parameter can pick one of the sample variants. So Glitch is now a programmable sampler!</p><p>Also I find it much more convenient to use a decent text editor that updates the playback when I save the file, than using a browser.</p><p>I’m thinking about adding live MIDI input and output capabilities to Glitch, as well as maybe live audio input so Glitch could be used to process guitar sound or voice. Also, I can’t stop thinking about Glitch on mobile, I think that would be a nice toy to play with.</p><p>And of course I’m looking forward for the feedback from Glitch users! If you miss some sound effects or some helpful functions — please file an <a href="http://github.com/naivesound/glitch/issues">issue on Github</a> or submit a pull request.</p><p>So, what would you like to see in the next version of Glitch? Or what would you like to hear in the next post?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b8f634c7850e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Glitch: how “Get Yucky” was made]]></title>
            <link>https://medium.com/@naive_sound/glitch-how-get-yucky-was-made-43862f7198d1?source=rss-35e89cb7a0f2------2</link>
            <guid isPermaLink="false">https://medium.com/p/43862f7198d1</guid>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[music]]></category>
            <category><![CDATA[minimalism]]></category>
            <category><![CDATA[electronic-music]]></category>
            <dc:creator><![CDATA[naivesound]]></dc:creator>
            <pubDate>Tue, 21 Jun 2016 05:56:35 GMT</pubDate>
            <atom:updated>2016-06-21T05:56:35.622Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://soundcloud.com/naivesound/get-yucky">“Get Yucky”</a> is a four-chord looping melody made from scratch using <a href="http://naivesound.com/glitch">Glitch</a>. It was written to show that music can be written with just arithmetic expressions.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fw.soundcloud.com%2Fplayer%2F%3Furl%3Dhttp%253A%252F%252Fapi.soundcloud.com%252Ftracks%252F265813413%26show_artwork%3Dtrue&amp;url=https%3A%2F%2Fsoundcloud.com%2Fnaivesound%2Fget-yucky&amp;image=http%3A%2F%2Fi1.sndcdn.com%2Fartworks-000164377863-l0etjx-t500x500.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=soundcloud" width="500" height="166" frameborder="0" scrolling="no"><a href="https://medium.com/media/2855b9fe2fa4935d142d0bf0a918b48c/href">https://medium.com/media/2855b9fe2fa4935d142d0bf0a918b48c/href</a></iframe><h4>At the beginning there were drums</h4><p>Let’s define the tempo of the song and drop in some drum synthesizers like the ones <a href="https://medium.com/@naive_sound/glitch-how-to-synthesize-drums-793d4293082e">created in the previous article</a>.</p><pre>bpm=120,</pre><pre>kicks=seq(bpm, 1, 0),<br>snares=seq(bpm, 0, 1),<br>hats=seq(bpm*4, 0.1, 0.2, 0.1, 0.2, 0.1, 0.2, 0.2, 0.1),</pre><pre>kick=env(0.01, 0.12, 0.3, 0.1, 0, (sin(83)-128)*kicks+128),</pre><pre>snaretone=env(0.005, 0.02, 0.3, 0.05, 0, (sin(220)-128)*snares+128),<br>snarenoise=env(0.02, 0.03, 0.2, 0.04, 0, (r(15)*snares)+128),<br>snare = (snaretone + snarenoise + 128 — lpf(snarenoise, 100))/2,</pre><pre>hat=env(0.01, 0.05, 0.2, 0.05, 0, r(30)*hats+128),<br>hat=hat+128-lpf(hat, 10000),</pre><pre>mix(kick,hat,snare)</pre><p>The tempo is a common one, 120 beats per minute. Every even beat is a kick, every odd beat is a snare. It’s a typical pattern for a very simple drum track, boom-tsch-boom-tsch-…</p><p>Adding the quiet percussive sound of hi-hats makes our drum track less monotonic.</p><p>The drums are synthesized using sine wave and white noise. Kick is a low-pitch sine wave that simulates the membrane of a kick drum quickly fading out after it’s kicked. Hi-hats is just a short impulse of noise. Snare is both, tone of a drum at the beginning and noise at the end mixed together.</p><p>You can hear the result <a href="http://naivesound.com/glitch/#bpm%3D120%2C%0A%0Akicks%3Dseq(bpm%2C%201%2C%200)%2C%0Asnares%3Dseq(bpm%2C%200%2C%201)%2C%0Ahats%3Dseq(bpm*4%2C%200.1%2C%200.2%2C%200.1%2C%200.2%2C%200.1%2C%200.2%2C%200.2%2C%200.1)%2C%0A%0Akick%3Denv(0.01%2C%200.12%2C%200.3%2C%200.1%2C%200%2C%20(sin(83)-128)*kicks%2B128)%2C%0A%0Asnaretone%3Denv(0.005%2C%200.02%2C%200.3%2C%200.05%2C%200%2C%20(sin(220)-128)*snares%2B128)%2C%0Asnarenoise%3Denv(0.02%2C%200.03%2C%200.2%2C%200.04%2C%200%2C%20(r(15)*snares)%2B128)%2C%0Asnare%20%3D%20(snaretone%20%2B%20snarenoise%20%2B%20128%20-%20lpf(snarenoise%2C%20100))%2F2%2C%0A%0Ahat%3Denv(0.01%2C%200.05%2C%200.2%2C%200.05%2C%200%2C%20r(30)*hats%2B128)%2C%0Ahat%3Dhat%2B128-lpf(hat%2C%2010000)%2C%0A%0Amix(kick%2Chat%2Csnare)">here</a>.</p><h4>The present has no rhythm</h4><p>Let’s implement the chord progression of the song. Quick googling shows that it’s <strong><em>Bm, D, F#m, E</em></strong> changing in a loop. Each chord is being played during 4 beats.</p><p>Here chords are simple triads, meaning that each of them consists of three notes — the root, the third and the fifth. Glitch uses numbers for note positions where zero is A4 (note A of 4th octave):</p><pre>A  A# B  C  C# D  D# E  F  F# G   G#  A   A#  B   C   C#<br>0  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16</pre><p>There are some <a href="http://www.michael-thomas.com/music/class/chords_notesinchords.htm">cheatsheets</a> to look up the notes of a chord quickly.</p><p>Bm chord includes B, D and F#. That is <strong>[2, 5, 9]</strong>. D chord includes D, F# and A, or <strong>[5, 9, 12]</strong>. F#m chord includes F#, A, C#, or <strong>[9, 12, 16]</strong>. Finally, E chord includes E, G#and B. That is <strong>[7, 11, 14]</strong>.</p><pre>bpm=120,</pre><pre>i  =seq(bpm/4, 2, 5,  9,  7)-12,<br>iii=seq(bpm/4, 5, 9,  12, 11)-12,<br>v  =seq(bpm/4, 9, 12, 16, 14)-12,<br>pad=(sin(hz(i))+sin(hz(iii))+sin(hz(v)))/3,<br>pad=0.5*env(0.01, 0.45, 0.7, 1.3, 0, pad)</pre><p>Here we have three sequences for root notes, triads and fifths of each chord. Each column is a chord. We transpose each chord one octave down to make it sound lower. Each note is played with a sine oscillator which has a soft sound. Oscillators are mixed by adding the amplitudes and dividing the sum by three to normalize the range. Resulting signal is wrapped into an envelope with fast attack and slow release. You can hear it <a href="http://naivesound.com/glitch/#bpm%3D120%2C%0A%0Ai%20%20%3Dseq(bpm%2F4%2C%202%2C%205%2C%20%209%2C%20%207)-12%2C%0Aiii%3Dseq(bpm%2F4%2C%205%2C%209%2C%20%2012%2C%2011)-12%2C%0Av%20%20%3Dseq(bpm%2F4%2C%209%2C%2012%2C%2016%2C%2014)-12%2C%0Apad%3D(sin(hz(i))%2Bsin(hz(iii))%2Bsin(hz(v)))%2F3%2C%0Apad%3D0.5*env(0.01%2C%200.45%2C%200.7%2C%201.3%2C%200%2C%20pad)">here</a>.</p><h4>Arpeggio</h4><p>Just strumming chords one at a time is boring, but we can easily add some arpeggios. We already have the notes of each chord in the given order. Let’s play them quickly: root, third, fifth, third, root, third etc. Let’s use an FM-synthesizer as an instrument — it’s very easy to setup and it still sounds more interesting than a single oscillator:</p><pre>bpm=120,</pre><pre>i  =seq(bpm/4, 2, 5,  9,  7)-12,<br>iii=seq(bpm/4, 5, 9,  12, 11)-12,<br>v  =seq(bpm/4, 9, 12, 16, 14)-12,<br>pad=(sin(hz(i))+sin(hz(iii))+sin(hz(v)))/3,<br>pad=0.5*env(0.01, 0.45, 0.7, 1.3, 0, pad),</pre><pre>arp=0.2*fm(hz(seq(bpm*2, i, iii, v, iii)), 1, 0.2)+127,</pre><pre>mix(pad, arp)</pre><p>As you can see it’s the same code we used for chords with one more arpeggio sequence. You may try to adjust some parameters. For example, changing arpeggio tempo from <em>bpm*2</em> to <em>bpm*4</em> or <em>bpm*3 </em>gives interesting results. Also try changing FM operator parameters from 1 and 0.2 to some other numbers and see how the sound changes.</p><h4>Bass</h4><p>Our bass line will be simple, it will be using the same notes from the chords, as we did with arpeggios. Let’s use the root (i) and the fifth (v) notes only and let’s use different oscillators, sine and triangular. Here we transpose the notes two octaves down because bass is a low-pitch instrument and should sound lower than chords or arpeggios:</p><pre>bass=(tri(hz(i-24))+sin(hz(v-24)))/2</pre><p>Mixing bass to pad and arpeggio adds some solid bass sound, but we definitely can improve it. Let’s add some tremolo effect like if someone quickly picks the bass string with fingers.</p><p>This means that for each bass note in the bass line we need to play it several times, quickly, wrapping it with some quickly fading envelope:</p><pre>tremolo = env(0.01, 0.1, 0.03, 0.1, 0, bass*seq(bpm*4, 1, 0.5, 0.6, 0.8))</pre><p>Here we use the bass signal and modulate its amplitude using sequence of volumes (1, 0.5, 0.6, 0.8) for each note. Using slightly different (or even random) volumes for notes is an easy way to avoid “artificial” or “robotic” sound in algorithmic music. The tempo is four times higher, so each note is played four times with its own envelope as if it were four separate notes.</p><p>Let’s <a href="http://naivesound.com/glitch/#bpm%3D120%2C%0A%0Ai%20%20%3Dseq(bpm%2F4%2C%202%2C%205%2C%20%209%2C%20%207)-12%2C%0Aiii%3Dseq(bpm%2F4%2C%205%2C%209%2C%20%2012%2C%2011)-12%2C%0Av%20%20%3Dseq(bpm%2F4%2C%209%2C%2012%2C%2016%2C%2014)-12%2C%0Apad%3D(sin(hz(i))%2Bsin(hz(iii))%2Bsin(hz(v)))%2F3%2C%0Apad%3D0.5*env(0.01%2C%200.45%2C%200.7%2C%201.3%2C%200%2C%20pad)%2C%0A%0Aarp%3D0.2*fm(hz(loop(bpm*2%2C%20i%2C%20iii%2C%20v%2C%20iii))%2C%201%2C%200.2)%2B127%2C%0A%0Abass%3D(tri(hz(i-24))%2Bsin(hz(v-24)))%2F2%2C%0Atremolo%20%3D%20env(0.01%2C%200.1%2C%200.03%2C%200.1%2C%200%2C%20bass*seq(bpm*4%2C%201%2C%200.5%2C%200.6%2C%200.8))%2C%0A%0Amix(pad%2C%20arp%2C%20tremolo)">hear chords, arpeggio and bass mixed together</a>.</p><h4>Ends with beginnings</h4><p>Our song now has a drum track, a synth pad track with chords, FM-synth doing arpeggio and a tremolo bass. The final touch would be to turn this four-chord loop into a full song. Let’s add the instruments one by one and then slowly remove them.</p><p>Such layering is a very popular orchestration technique, not only limited to electronic sequencer music. You can hear it in Wim Mertens’ works, in many canons and rounds, even in Ravel’s “Bolero”.</p><p>To implement layering we could write lots of if/else operators (in glitch they are &amp;&amp; and ||), but this is a perfect case for bit-wise arithmetic to shine in all its glory.</p><p>Let’s assign a number to each part. Drums would be zero, synth pad - 1, arpeggio - 2 and bass would be 3. If a corresponding bit in a number is set — the instrument is playing, otherwise it’s muted. For example, if a song part code is 5 (or 101 in binary), it should play drums and arpeggio, but if the song part code is 6 (or 110 in binary), it should play chords and arpeggio with no drums or bass. We can now create a sequence of song parts and use mixer to mute/unmute certain instruments.</p><p>Here’s the full code of the song:</p><pre>bpm=120,</pre><pre>part=seq(bpm/4/4, 1, 3, 7, 15, 15, 15, 15, 10, 3),</pre><pre>kicks=seq(bpm, 1, 0),<br>snares=seq(bpm, 0, 1),<br>hats=seq(bpm*4, 0.1, 0.2, 0.1, 0.2, 0.1, 0.2, 0.2, 0.1),</pre><pre>kick=env(0.01, 0.12, 0.3, 0.1, 0, (sin(83)-128)*kicks+128),<br>snaretone=env(0.005, 0.02, 0.3, 0.05, 0, (sin(220)-128)*snares+128),<br>snarenoise=env(0.02, 0.03, 0.2, 0.04, 0, (r(15)*snares)+128),<br>snare = (snaretone + snarenoise + 128 — lpf(snarenoise, 100))/2,<br>hat=env(0.01, 0.05, 0.2, 0.05, 0, r(30)*hats+128),<br>hat=hat+128-lpf(hat, 10000),</pre><pre>i =seq(bpm/4, 2, 5, 9, 7)-12,<br>iii=seq(bpm/4, 5, 9, 12, 11)-12,<br>v =seq(bpm/4, 9, 12, 16, 14)-12,<br>pad=(sin(hz(i))+sin(hz(iii))+sin(hz(v)))/3,<br>pad=0.5*env(0.01, 0.45, 0.7, 1.3, 0, pad),</pre><pre>arp=0.2*fm(hz(loop(bpm*2, i, iii, v, iii)), 1, 0.2)+127,</pre><pre>bass=(tri(hz(i-24))+sin(hz(v-24)))/2,<br>tremolo = env(0.01, 0.1, 0.03, 0.1, 0, bass*seq(bpm*4, 1, 0.5, 0.6, 0.8)),</pre><pre>mix((part&amp;1,kick), (part&amp;1,snare), (part&amp;1,hat), ((part&gt;&gt;1)&amp;1,pad), ((part&gt;&gt;2)&amp;1,arp), ((part&gt;&gt;3)&amp;1,tremolo))</pre><p>It starts with just drums, then chords are added, then arpeggio, then bass. Then after a few bars it goes back to drums+bass+chords, drums+chords and just drums again.</p><p>You can hear or tweak the final version of the song <a href="http://naivesound.com/glitch/#bpm%3D120%2C%0A%0Apart%3Dseq(bpm%2F4%2F4%2C%201%2C%203%2C%207%2C%2015%2C%2015%2C%2015%2C%2015%2C%2010%2C%203)%2C%0A%0Akicks%3Dseq(bpm%2C%201%2C%200)%2C%0Asnares%3Dseq(bpm%2C%200%2C%201)%2C%0Ahats%3Dseq(bpm*4%2C%200.1%2C%200.2%2C%200.1%2C%200.2%2C%200.1%2C%200.2%2C%200.2%2C%200.1)%2C%0A%0Akick%3Denv(0.01%2C%200.12%2C%200.3%2C%200.1%2C%200%2C%20(sin(83)-128)*kicks%2B128)%2C%0Asnaretone%3Denv(0.005%2C%200.02%2C%200.3%2C%200.05%2C%200%2C%20(sin(220)-128)*snares%2B128)%2C%0Asnarenoise%3Denv(0.02%2C%200.03%2C%200.2%2C%200.04%2C%200%2C%20(r(15)*snares)%2B128)%2C%0Asnare%20%3D%20(snaretone%20%2B%20snarenoise%20%2B%20128%20-%20lpf(snarenoise%2C%20100))%2F2%2C%0Ahat%3Denv(0.01%2C%200.05%2C%200.2%2C%200.05%2C%200%2C%20r(30)*hats%2B128)%2C%0Ahat%3Dhat%2B128-lpf(hat%2C%2010000)%2C%0A%0Ai%20%20%3Dseq(bpm%2F4%2C%202%2C%205%2C%20%209%2C%20%207)-12%2C%0Aiii%3Dseq(bpm%2F4%2C%205%2C%209%2C%20%2012%2C%2011)-12%2C%0Av%20%20%3Dseq(bpm%2F4%2C%209%2C%2012%2C%2016%2C%2014)-12%2C%0Apad%3D(sin(hz(i))%2Bsin(hz(iii))%2Bsin(hz(v)))%2F3%2C%0Apad%3D0.5*env(0.01%2C%200.45%2C%200.7%2C%201.3%2C%200%2C%20pad)%2C%0A%0Aarp%3D0.2*fm(hz(loop(bpm*2%2C%20i%2C%20iii%2C%20v%2C%20iii))%2C%201%2C%200.2)%2B127%2C%0A%0Abass%3D(tri(hz(i-24))%2Bsin(hz(v-24)))%2F2%2C%0Atremolo%20%3D%20env(0.01%2C%200.1%2C%200.03%2C%200.1%2C%200%2C%20bass*seq(bpm*4%2C%201%2C%200.5%2C%200.6%2C%200.8))%2C%0A%0Amix((part%261%2Ckick)%2C%20(part%261%2Csnare)%2C%20(part%261%2Chat)%2C%20((part%3E%3E1)%261%2Cpad)%2C%20((part%3E%3E2)%261%2Carp)%2C%20((part%3E%3E3)%261%2Ctremolo))">here</a>.</p><p><a href="http://naivesound.com/glitch"><em>Glitch</em></a><em> is a minimal </em><a href="https://github.com/naivesound/glitch"><em>open source</em></a><em> algorithmic music composer that uses simple mathematical formulas to describe music. It’s easy to learn (it’s all just arithmetic expressions), easy to use (runs in your browser), easy to share (just send a link).</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=43862f7198d1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Glitch: how to synthesize drums]]></title>
            <link>https://medium.com/@naive_sound/glitch-how-to-synthesize-drums-793d4293082e?source=rss-35e89cb7a0f2------2</link>
            <guid isPermaLink="false">https://medium.com/p/793d4293082e</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[music]]></category>
            <category><![CDATA[mathematics]]></category>
            <dc:creator><![CDATA[naivesound]]></dc:creator>
            <pubDate>Wed, 25 May 2016 16:12:09 GMT</pubDate>
            <atom:updated>2017-03-14T22:03:31.779Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="http://naivesound.com/glitch">Glitch</a> is a great tool if you want to start with algorithmic music. It’s simple and straightforward. To master Glitch you only need to remember 15 functions.</p><p>But moving from <a href="https://medium.com/@naive_sound/glitch-a-noisy-fusion-of-math-and-music-6a9b24e7f5b5">bytebeat</a> and <a href="https://medium.com/@naive_sound/glitch-beyond-the-bytebeat-603478a03686">oscillators</a> to actual music might be difficult for newcomers. Let’s see how you can make synthesized kick drum, snare and hi-hats with just a few lines of Glitch.</p><h4>Hi-hats</h4><p>Hi-hats are probably the easiest ones to synthesize because they are basically white noise. It can be generated by the <em>r() </em>function (a random number generator). Adding an envelope to the white noise makes it sound more like a drum. But white noise contains some low frequencies in it, so to make our hats sound higher we will subtract a low-pass filter signal from it:</p><pre><em>hat=env(r(128), (0.01, 1), (0.01, 0.1), (0.05, 0))<br>hpf(hat, 10000)</em></pre><p>Here’s an example of playing some sequence of beats with hi-hats:</p><pre>bpm = 120<br>hats = seq(bpm*4, 0.1, 0.2)<br>hat=env(r(128)*hats, (0.01, 0.1), (0.05, 0))<br>hpf(hat, 10000)</pre><p>You can try it <a href="http://naivesound.com/glitch/#bpm%20%3D%20120%0Ahats%20%3D%20seq(bpm*4%2C%200.1%2C%200.2)%0Ahat%3Denv(r(128)*hats%2C%20(0.01%2C%200.1)%2C%20(0.05%2C%200))%0Ahpf(hat%2C%2010000)">here</a>.</p><h4>Kick</h4><p>Kick (bass drum) has a low pitch. Let’s start with <em>sin(50) </em>as a drum oscillator. Now let’s make a contour of the sound similar to a real kick drum. It should have a fast attack and a short release tail (click the link to play it in Glitch):</p><pre>kickhz=50, env(sin(kickhz), (0.001, 1), (0.1, 0))</pre><p>A slightly better sound can be achieved if we an envelope curved:</p><pre>bpm=80,<br>kicks=seq(bpm*4, 1, 0, 0, 0.6),<br>kickhz=50,<br>kick=env(kicks*sin(kickhz), (0.1, 0.3), (0.1, 0)),<br>kick=lpf(kick, 250)</pre><p>It wouldn’t hurt to add a low-pass filter to make our bass sound deeper. You can try it <a href="http://naivesound.com/glitch/#bpm%3D80%2C%0Akicks%3Dseq(bpm*4%2C%201%2C%200%2C%200%2C%200.6)%2C%0Akickhz%3D50%2C%0Akick%3Denv(kicks*sin(kickhz)%2C%20(0.1%2C%200.3)%2C%20(0.1%2C%200))%2C%0Akick%3Dlpf(kick%2C%20250)">here</a>.</p><h4>Snare</h4><p>It’s an interesting drum because it starts with a short clear pitch that is quickly mixed with some high-pitch noise. It’s loud and has staccato sound which means it’s short and sharp.</p><p>Let’s start with a tone, say 220 Hz. I picked it randomly, I think snares are tuned somewhere in the range 200..300 Hz.</p><pre>snarehz=220<br>snaretone=env(sin(snarehz), (0.005, 1), (0.01, 0.2), (0.03, 0))</pre><p>You’re right, it sounds more like kick (because we used a similar envelope and the same oscillator). Now let’s add some noise to it. Noise will have slower attack:</p><pre>snarehz=220<br>snaretone=env(sin(snarehz), (0.005, 1), (0.01, 0.2), (0.03, 0))<br>snarenoise=env(r(), (0.02, 1), (0.03, 0.2), (0.04, 0))<br>(snaretone + snarenoise)/2</pre><p>A better sound can be achieved by tweaking the envelope curve and filtering out the low frequencies from the noise:</p><pre>bpm=120<br>snarehz=220<br>snares=seq(bpm*2, 0.5, 0.1, 0.5, 0.2)<br>snaretone=env(snares*sin(snarehz), (0.005, 1), (0.01, 0.2), (0.03, 0))<br>snarenoise=env(snares*r(), (0.001, 1), (0.02, 0.01), (0.01, 0))<br>snare = (snaretone + snarenoise)/2</pre><p>The resulting drum can be heard <a href="http://naivesound.com/glitch/#bpm%3D120%0Asnarehz%3D220%0Asnares%3Dseq(bpm*2%2C%200.5%2C%200.1%2C%200.5%2C%200.2)%0Asnaretone%3Denv(snares*sin(snarehz)%2C%20(0.005%2C%201)%2C%20(0.01%2C%200.2)%2C%20(0.03%2C%200))%0Asnarenoise%3Denv(snares*r()%2C%20(0.001%2C%201)%2C%20(0.02%2C%200.01)%2C%20(0.01%2C%200))%0Asnare%20%3D%20(snaretone%20%2B%20snarenoise)%2F2">here</a>.</p><h4>Example</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fw.soundcloud.com%2Fplayer%2F%3Furl%3Dhttp%253A%252F%252Fapi.soundcloud.com%252Ftracks%252F265867507%26show_artwork%3Dtrue&amp;url=https%3A%2F%2Fsoundcloud.com%2Fnaivesound%2Fglitch-drums&amp;image=http%3A%2F%2Fi1.sndcdn.com%2Fartworks-000164431654-ur6vt8-t500x500.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=soundcloud" width="500" height="166" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd10c9a88de561970823f6d9c2d80b0a/href">https://medium.com/media/cd10c9a88de561970823f6d9c2d80b0a/href</a></iframe><p>Here I combined all three drums into a simple drum pattern used in many rock songs. It’s built upon the same formulas, just with slightly different coefficients:</p><pre>bpm=80<br>kicks=seq(bpm*4, 1, 0, 0, 0.6, 1, 0, 0, 1, 0, 0, 0, 0.2, 1, 0, 0, 0)<br>kickhz=50<br>kick=env(kicks*sin(kickhz), (0.001, 1), (0.05, 0.3), (0.1, 0))<br>kick=lpf(kick, 150)<br>hats=seq(bpm*4, 0.7, 0.5, 1, 0.5, 0.7, 0.5, 1, 0.5, 0.7, 0.5, 1, 0.7, 0.7, 0.5, 0.7, 0.5)<br>hatfilters=seq(bpm*4, 1, 1, 2, 1, 1, 1, 2, 1, 1, 2, 2.5, 1, 1, 2, 1)<br>hat=env(r()*hats, (0.01, 1), (0.03, 0.2), (0.02, 0))<br>hat=hpf(hat, 1000*hatfilters)<br>snares=seq(bpm*4, 0, 0, 0.5, 0)<br>snarehz=220<br>snaretone=env(sin(snarehz)*snares, (0.005, 1), (0.01, 0.2), (0.03, 0))<br>snarenoise=env(r()*snares,(0.02, 1), (0.03, 0.2), (0.04, 0))<br>snare = (snaretone + snarenoise)/2<br>mix(kick, 0.3*hat, snare)</pre><p>You can <a href="http://naivesound.com/glitch/#bpm%3D80%0Akicks%3Dseq(bpm*4%2C%201%2C%200%2C%200%2C%200.6%2C%201%2C%200%2C%200%2C%201%2C%200%2C%200%2C%200%2C%200.2%2C%201%2C%200%2C%200%2C%200)%0Akickhz%3D50%0Akick%3Denv(kicks*sin(kickhz)%2C%20(0.001%2C%201)%2C%20(0.05%2C%200.3)%2C%20(0.1%2C%200))%0Akick%3Dlpf(kick%2C%20150)%0Ahats%3Dseq(bpm*4%2C%200.7%2C%200.5%2C%201%2C%200.5%2C%200.7%2C%200.5%2C%201%2C%200.5%2C%200.7%2C%200.5%2C%201%2C%200.7%2C%200.7%2C%200.5%2C%200.7%2C%200.5)%0Ahatfilters%3Dseq(bpm*4%2C%201%2C%201%2C%202%2C%201%2C%201%2C%201%2C%202%2C%201%2C%201%2C%202%2C%202.5%2C%201%2C%201%2C%202%2C%201)%0Ahat%3Denv(r()*hats%2C%20(0.01%2C%201)%2C%20(0.03%2C%200.2)%2C%20(0.02%2C%200))%0Ahat%3Dhpf(hat%2C%201000*hatfilters)%0Asnares%3Dseq(bpm*4%2C%200%2C%200%2C%200.5%2C%200)%0Asnarehz%3D220%0Asnaretone%3Denv(sin(snarehz)*snares%2C%20(0.005%2C%201)%2C%20(0.01%2C%200.2)%2C%20(0.03%2C%200))%0Asnarenoise%3Denv(r()*snares%2C(0.02%2C%201)%2C%20(0.03%2C%200.2)%2C%20(0.04%2C%200))%0Asnare%20%3D%20(snaretone%20%2B%20snarenoise)%2F2%0Amix(kick%2C%200.3*hat%2C%20snare)">open it in Glitch</a> and adjust to your own taste. Or would you like to build your own drum pattern with Glitch?</p><p><a href="http://naivesound.com/glitch"><em>Glitch</em></a><em> is a minimal </em><a href="https://github.com/naivesound/glitch"><em>open source</em></a><em> algorithmic music composer that uses simple mathematical formulas. It’s easy to learn (it’s all just arithmetic expressions), easy to use (runs in your browser), easy to share (just send a link).</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=793d4293082e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Glitch: beyond the bytebeat]]></title>
            <link>https://medium.com/@naive_sound/glitch-beyond-the-bytebeat-603478a03686?source=rss-35e89cb7a0f2------2</link>
            <guid isPermaLink="false">https://medium.com/p/603478a03686</guid>
            <category><![CDATA[music]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[synthesizers]]></category>
            <dc:creator><![CDATA[naivesound]]></dc:creator>
            <pubDate>Wed, 25 May 2016 13:14:18 GMT</pubDate>
            <atom:updated>2017-03-14T21:42:47.412Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="http://naivesound.com/glitch">Glitch</a> started as a <a href="https://medium.com/@naive_sound/glitch-a-noisy-fusion-of-math-and-music-6a9b24e7f5b5">Bytebeat music synthesizer</a>. Bytebeat has some really unique features: simple concept, terse notation, dirty old-school sound. But at the same point Bytebeat <a href="http://narf.pl/posts/bytebeat">is limited</a> by its ultra-simplicity.</p><p>Many curious minds today experiment with algorithmic music composition and programmable music. In most cases this means configuring some software and learning some new programming languages.</p><blockquote>Glitch aims to combine the simplicity of Bytebeat with the flexibility of a full-featured programmable music environment.</blockquote><p>The core idea of Glitch is borrowed from Bytebeat — a musician enters a math expression, then Glitch evaluates it for each discrete time frame and the returned values describe the amplitude of the waveform. Output is assumed to be in the range [-1..1] and it is clipped if it goes beyond the given range. You can use mathematical expressions, variables and some functions. Everything is a number, which means you can combine things easily by passing numbers from one function to another. Now, how many functions shall we add to turn Glitch into a real music programming tool?</p><h4>Oscillators</h4><p>With Bytebeat techniques turning input time variable <strong><em>t</em></strong> into a waveform is likely to result in a saw-tooth or square wave and that’s why you get that buzzing sound. To get something more melodic we would probably need to have sine and triangular oscillators. So, Glitch now has some oscillator functions:</p><ul><li><strong><em>sin(freq)</em></strong> — generates sine wave of the given frequency</li><li><strong><em>tri(freq)</em></strong> — generates triangular wave of the given frequency</li><li><strong><em>saw(freq)</em></strong> — generates saw-tooth wave of the given frequency</li><li><strong><em>sqr(freq, [width])</em></strong> — generates square wave of the given frequency. Pulse width can be specified, too. By default it’s 0.5.</li></ul><p>All these functions advance the oscillator phase internally and apply the frequency changes in a smart way to reduce clicks. For example, the formula <a href="http://naivesound.com/glitch/#w%3D(t%3E%3E10)%267%2C(tri(w*220)%2Bsin(w*220))%2F2"><em>w=(t&gt;&gt;10)&amp;7,(tri(w*220)+sin(w*220))/2</em></a> (click it to open in Glitch) mixes together triangular and sine oscillators playing the same frequency (which varies as time <strong><em>t</em></strong> increases). Here we normalize the volume dividing it by two to avoid buzzing when amplitude overflows.</p><p>Oscillators can be combined in parallel (like in the example above when amplitude values are added) or sequentially, when output of one oscillator becomes the input of another one, e.g. <a href="http://naivesound.com/glitch/#w%3D(t%3E%3E7)%2631%2Csin(100*tri(w*4)%2B400)"><em>w=(t&gt;&gt;7)&amp;31,sin(100*tri(w*4)+400)</em></a>. Here the <strong><em>w</em></strong> value (which varies in time in the range 0..31) is an input to the triangular oscillator which generates a signal with some low frequency. The output of that oscillator modifies the frequency of the outer sine oscillator and it results in a frequency-modulated sound in the diapason 400..500 Hz.</p><p>Don’t forget that there is also a special <strong><em>r([max])</em> </strong>function which returns a random number in the range [0..max). It can be used as an oscillator in some cases (e.g. synthesizing drums).</p><h4>FM synthesizer</h4><p>A better way to build frequency-modulated sound is to use a special function <strong><em>fm(freq, [f1, a1, f2, a2, f3, a3])</em></strong>. The function imitates a 3-operator FM-synth with the following topology:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*K-ZQVrvz04XCexITL7g-ew.png" /><figcaption>FM synth topology</figcaption></figure><p>If you’re unfamiliar with how FM synthesis works — you can read more <a href="http://www.soundonsound.com/sos/apr00/articles/synthsecrets.htm">here</a>.</p><p>The <strong><em>fm</em></strong> function is a roughly equivalent of <em>sin(freq + a1*sin(freq*f1 + a3*sin(freq*f3)) + a2*sin(freq*f2))</em>.</p><p>Don’t panic if this formula looks complicated. Let’s see what happens if all parameters except for the frequency are set to zero. In this case <a href="http://naivesound.com/glitch/#fm(440)"><em>fm(440)</em></a> is identical to <em>sin(440)</em> and you will hear a simple sine wave. Now, let’s configure the first operator. It affects the frequency of the main (carrier) oscillator. The first parameter <strong><em>f1</em></strong> is the frequency multiplier. Let’s set it to 2 so that our oscillator #1 will be running at frequency 2*440= 480 Hz. Also, to make it audible let’s set amplitude gain <strong><em>a1</em></strong> to 1. We get <a href="http://naivesound.com/glitch/#fm(440%2C%202%2C%201)"><em>fm(440, 2, 1)</em></a> function which is identical to <em>sin(440 + sin(880))</em> and you can hear how the sound gets some higher pitches and how it varies in time. Try tweaking <strong><em>f1</em></strong> and <strong><em>a1</em></strong> parameters to hear how they affect the main oscillator.</p><p>Now you can enhance your synthesizer by adding another oscillator in parallel (changing <strong><em>f2</em></strong> and <strong><em>a2</em></strong>). In this case the output of those two oscillators will be summed and used to shift the frequency of the main oscillator. Or you can set <strong><em>f2</em></strong> and <strong><em>a2</em></strong> to zero using <strong><em>f3</em></strong> and <strong><em>a3</em></strong> instead. The output of the third oscillator will be forwarded into the first oscillator that controls the main oscillator. Compare how <a href="http://fm(440, 0.5, 0.5, 0, 0, 0.5, 0.8)"><em>fm(440, 0.5, 0.5, 0, 0, 0.5, 0.8)</em></a> differs from <a href="http://naivesound.com/glitch/#fm(440%2C%200.5%2C%200.5)"><em>fm(440, 0.5, 0.5)</em></a>. Of course nothing stops you from setting parameters to all three oscillators, so feel free to share if you get an interesting sound!</p><p>This FM topology is simple enough to understand and allows to build a basic single-operator FM synth, two-operator FM synths <em>O1+O2</em> and <em>O1(O3)</em>, and a more complex <em>O1(O3)+O2</em> variant. Did I mention that you can combine <a href="http://naivesound.com/glitch/#freq%3D440%2C%0Ao1%3Dfm(freq%2C%202%2C%200.5%2C%204%2C%202%2C%200.1%2C%200.3)%2C%0Afm(freq%2Bo1%2C%202%2C%200.7%2C%200.1%2C%200.2%2C%200.1%2C%200.3)">multiple FM synths</a> as well since they are just functions?</p><h4>Notes</h4><p>Playing a single tone is boring. Let’s play some tunes. But how can we write a melody without notes? Well, we can use frequencies… In the first version of Glitch there was a special “array” function <strong><em>a(index, [a0, a1, a2, …])</em></strong> which returns its argument defined by the index. Index will overflow if it goes out of the array range and it is the most simple sequencer you can build — for example <a href="http://naivesound.com/glitch/#sin(a(t%3E%3E10%2C%20440%2C%20523%2C%20659))"><em>sin(a(t&gt;&gt;10, 440, 523, 659))</em></a> plays a simple arpeggio looping though A4, C5 and E5 notes.</p><p>But nobody wants to remember all note frequencies. That’s why new Glitch has a helper <strong><em>hz(note)</em></strong> that returns note frequency for a given note. Zero stands for A note of the 4th octave. You can also use floating point note values to get microtonal music.</p><p>Another helper function is <strong><em>scale(note, [mode]) </em></strong>which returns the note at the given position in the given scale. Default mode is Ionian mode, which is a traditional major scale. Modes 1..6 are dorian, phrygian, lydian, mixolydian, aeolian and locrian modes. If you’re unfamiliar with music theory just remember that zero is major scale and five is minor scale. There are two other minor scales — harmonic minor which is mode #7 and melodic minor mode #8. Guitar players may probably check out the pentatonic scales: #9 is major pentatonic and #10 is minor pentatonic. #11 is a blues scale. #12 is whole tone scale and #13 is octatonic scale. Values above 13 fall back to the chromatic scale, the one that includes all 12 notes in a row.</p><h4>Sequencers</h4><p>Thinking in notes instead of pitches simplifies things, but we also want to think about rhythmic patterns, not just arrays of numbers. So here two sequencer functions:</p><ul><li><strong>loop(bpm, …) </strong>— loops though its arguments at the given tempo</li><li><strong>seq(bpm, …) </strong>— very similar to loop, but evaluates arguments only once per beat</li></ul><p>You might be confused with the difference, so let’s see some examples.</p><p><a href="http://naivesound.com/glitch/#sin(hz(loop(240%2C%200%2C%205%2C%209%2C%205)))"><em>sin(hz(seq(240, 0, 5, 9, 5)))</em></a> — plays arpeggio from the major scale at a tempo of 240 beats per minute.</p><p>Now let’s play nested sequences, for example playing two arpeggios changing each other. If you write <em>sin(hz(seq(30, seq(240, 0, 5, 9, 5)</em>, <em>seq(240, 1, 6, 10, 6))))</em> you <a href="http://naivesound.com/glitch/#sin(hz(seq(30%2C%20seq(240%2C%200%2C%205%2C%209%2C%205)%2C%20seq(240%2C%201%2C%206%2C%2010%2C%206))))">won’t hear</a> the expected result. That’s because <strong><em>seq</em></strong> function evaluates the arguments only once and stores it for the rest of the beat. Changing the outermost <strong><em>seq</em></strong> to <a href="http://naivesound.com/glitch/#sin(hz(loop(30%2C%20seq(240%2C%200%2C%205%2C%209%2C%205)%2C%20seq(240%2C%201%2C%206%2C%2010%2C%206))))"><strong><em>loop</em></strong></a> fixes it.</p><blockquote>As a rule of thumb — use <em>seq</em> for sequencing plain notes, use <strong><em>loop</em></strong> for scheduling inner sequences.</blockquote><p>Inside <strong><em>seq</em></strong> you’re safe to use <strong><em>r()</em></strong> function because it will be evaluated once and will not produce the white noise. Instead it will return a single random number for each beat. Using <strong><em>loop</em></strong> and <strong><em>seq</em></strong> also produces an interesting side effect when the inner sequence is longer than the time granted by the outer <strong><em>loop</em></strong>. In this case the inner sequence will be <a href="http://naivesound.com/glitch/#sin(hz(loop(480%2F3%2C%20seq(480%2C%200%2C%202%2C%204%2C%207%2C%205)%2C%20seq(480%2C%200%2C%203%2C%205%2C%203))))">rolled on each call</a>. You may experiment with irregular tempo ratios to get interesting rhythmic patterns.</p><h4>Shape of sound</h4><p>Now we can play tunes but our oscillators have constant volume. You can provide an envelope to your signal to make it sound like a real instrument — with attack (rapid volume rise), release (volume fade out) and other parameters. There is a special variadic function <strong><em>env(signal, (dt, val), (dt, val)…)</em></strong> that wraps signal into some envelope shape. Without parameters <em>env(sin(440))</em> plays an unaltered sine wave.</p><p>It’s quite common to use so-called percussive envelopes (the ones with attack and release only). In Glitch <em>env(sin(440), (0.5, 0))</em> becomes a fading out beep of 500 milliseconds, <em>env(sin(440), (0.5, 1), (0.1, 0))</em> has attack time of 0.5 second and release time of 0.1 second which sounds like fade-in and cutoff. You can give a more complex envelope, for example <em>env(sin(440), (0.01, 1), (0.1, 0.7), (0.1, 0.3), (0.1, 0))</em> which results in quick attack (10ms), at 110ms the volume will be 70%, at 210ms it will be 30% and at 310ms it will be back to zero.</p><p>Now, you may have noticed that an envelope is played only once. How to reset it? All functions in Glitch take numeric parameters and return numbers. However there is a special “not-a-number” value that has very specific meaning, namely — to reset envelops, effects and oscillators. Not-a-number value is returned by the sequencers automatically when the beat changes and this value is propagated from one function to another so the envelope function will definitely receive it.</p><h4>Effects</h4><p>To keep Glitch simple there is only one sound effect available at the moment — a low pass filter. The function <strong><em>lpf(signal, [freq])</em></strong> cuts off all the frequencies below the given one (which defaults to 200 Hz) and returns a modified signal. What about high-pass filters? Since <strong><em>lpf</em></strong> returns a signal without high pitch frequencies — you can build high-pass filters by subtracting the <strong><em>lpf</em></strong> value from the main signal. You can nest low pass filters and apply them multiple times. You can also vary the cutoff frequency value with some low-frequency oscillator and get some interesting <a href="http://naivesound.com/glitch/#pitch%3Dhz(seq(120%2C%200%2C%204%2C%207%2C%204))%0Ax%3Denv(tri(pitch)%2C%20(0.01%2C%201)%2C%20(0.2%2C%200.7)%2C%20(0.2%2C%200.3)%2C%20(0.2%2C%200))%0Alpf(x%2Csin(4)*800)">vibrato</a> effect.</p><h4>Mixing signals</h4><p>In your music you are likely to have a number of instruments and parts. If you just add their signals, you can also divide the sum of signals to normalize them, but there is a special helper function <strong><em>mix(…)</em></strong> that does it for you. Depending on the number of inputs, <strong><em>mix</em></strong> adds them and normalizes. It also softly clips the output signal to avoid the unexpected overdrive effect.</p><h4>Summary</h4><p>Here’s a full list of all Glitch functions available. We’ve added 15 functions which are easy to remember and to use:</p><ul><li>Oscillators: sin(freq), tri(freq), saw(freq), sqr(freq, [width]), fm(freq, …)</li><li>Sequencers: loop(bpm, …), seq(bpm, …), slide(bpm, …)</li><li>Utilities: a(index, …), scale(index, [mode]), r([max]), hz(note), lpf(signal, [freq]), env(signal, …), mix(…)</li></ul><p>To sum up my theoretical story — here’s a quick demo song I’ve recently made with Glitch:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fw.soundcloud.com%2Fplayer%2F%3Furl%3Dhttp%253A%252F%252Fapi.soundcloud.com%252Ftracks%252F265813413%26show_artwork%3Dtrue&amp;url=https%3A%2F%2Fsoundcloud.com%2Fnaivesound%2Fget-yucky&amp;image=http%3A%2F%2Fi1.sndcdn.com%2Fartworks-000164377863-l0etjx-t500x500.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=soundcloud" width="500" height="166" frameborder="0" scrolling="no"><a href="https://medium.com/media/2855b9fe2fa4935d142d0bf0a918b48c/href">https://medium.com/media/2855b9fe2fa4935d142d0bf0a918b48c/href</a></iframe><p><a href="http://naivesound.com/glitch"><em>Glitch</em></a><em> is a minimal </em><a href="https://github.com/naivesound/glitch"><em>open source</em></a><em> algorithmic music composer that uses simple mathematical formulas. It’s easy to learn (it’s all just arithmetic expressions), easy to use (runs in your browser), easy to share (just send a link).</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=603478a03686" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Glitch: a noisy fusion of math and music]]></title>
            <link>https://medium.com/@naive_sound/glitch-a-noisy-fusion-of-math-and-music-6a9b24e7f5b5?source=rss-35e89cb7a0f2------2</link>
            <guid isPermaLink="false">https://medium.com/p/6a9b24e7f5b5</guid>
            <category><![CDATA[music]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[minimalism]]></category>
            <dc:creator><![CDATA[naivesound]]></dc:creator>
            <pubDate>Tue, 15 Mar 2016 00:47:24 GMT</pubDate>
            <atom:updated>2017-03-14T21:23:52.725Z</atom:updated>
            <content:encoded><![CDATA[<p>Meet <a href="http://naivesound.com/glitch">Glitch</a> — a minimal, modern and easy-to-use synthesizer for Bytebeat music.</p><p>Bytebeat (sometimes known as glitch music) is a relatively new experimental genre of electronic music. If you are familiar with retro “chiptune” music you can imagine how it sounds — triangular or square waves making rhythmical scratching noises.</p><p>What’s so cool about Bytebeat music if it sounds so harsh? Mathematics. If numbers and formulas is a hard subject to you — most likely you will find Bytebeat music boring. Otherwise, let’s start making sound.</p><h4>Audio engine</h4><p>Glitch has no conventional musical notation — there are no notes or chords or even instruments. All you have is a mathematical expression that is evaluated for each time frame to generate the next amplitude value of the sound wave.</p><p>Historically, Bytebeat sound is created using the uint8_t data type, which is an unsigned 8-bit integer. It means the lowest possible amplitude level would be zero, and the highest possible would be 255. If you attempt to put values outside of this range — there will be an overflow (256 becomes 1, 257 becomes 2, -1 becomes 255, -2 becomes 254 etc).</p><p>Since Glitch was designed to create different kinds of music, it uses a range from -1 to 1 which is typical for the digital sound. In order to convert the historical [0, 255] range to a common [-1, 1] range one can use the built-in “byte()” function, which also controls the overflow if needed.</p><p>Glitch provides you with a special variable “t” that gets increased for each evaluation cycle. A simple expression of “byte(t)” produces a sound wave that linearly increases in time and overflows once it reaches the upper limit of 255. That’s how you can play a saw-tooth sound wave.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/1*U6JiuSy6Okfzn9J9hLA8tA.png" /><figcaption>Glitch: the saw-tooth wave of “byte(t)”</figcaption></figure><p>The frequency of the wave depends on how fast the “t” variable is increased. Most Bytebeat songs are played at 8000 Hz sample rate, so “t” is increased once in every 1/8000 sec = 0.125 ms. The maximum allowed amplitude will be reached in 256 steps, so “t” will reach the overflow point in 256*0.125 ms=0.032 sec. Which means the frequency of the sound wave will be 1/0.032=31.25 Hz. It’s within the audible range, but most likely will sound more like a low-frequency clicking rather than an oscillating sound. You may multiply “t” by some constant value to speed up the increment making the overflows happening more often and increasing the frequency of the sound. For example, “byte(t*8)” results in the saw-tooth wave at 32.25*8=250 Hz.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/606/1*JI453qC6lkPyO6aTSqkRDQ.png" /><figcaption>Glitch: the sawtooth wave of “byte(t*8)”</figcaption></figure><p>Saw-tooth wave is probably the most common oscillator function used in Bytebeat music. Now, how can we put a melody into a formula?</p><h4>Bitwise magic</h4><p>Probably Bytebeat music gained its popularity when <a href="https://www.youtube.com/watch?v=vCEUyx-SxPw">SIGINT13 gave this talk “Making music with C compiler”</a>. He has shown a really minimal approach of making digital music on a computer without any audio workstations or other traditional audio software. He wrote a short C program, ran it and redirected its output into a command-line audio player (aplay). Here’s the sample program:</p><pre>main<strong>(</strong>t<strong>)</strong> <strong>{</strong><br>  <strong>for(</strong>t<strong>=</strong>0<strong>;;</strong>t<strong>++)</strong><br>    putchar<strong>(</strong>t<strong>*(((</strong>t<strong>&gt;&gt;</strong>12<strong>)|(</strong>t<strong>&gt;&gt;</strong>8<strong>))&amp;(</strong>63<strong>&amp;(</strong>t<strong>&gt;&gt;</strong>4<strong>))));</strong><br><strong>}</strong></pre><p>And here’s how it sounds:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fw.soundcloud.com%2Fplayer%2F%3Furl%3Dhttp%253A%252F%252Fapi.soundcloud.com%252Ftracks%252F251453633%26show_artwork%3Dtrue&amp;url=https%3A%2F%2Fsoundcloud.com%2Fnaivesound%2Fglitch63&amp;image=http%3A%2F%2Fi1.sndcdn.com%2Fartworks-000150588348-wxfs3c-t500x500.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=soundcloud" width="500" height="166" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc7fbda5605685e54a9294766a60341f/href">https://medium.com/media/fc7fbda5605685e54a9294766a60341f/href</a></iframe><p>The formula looks like magic, but it produces rhythmic pattern and even some kind of a polyphonic melody!</p><p>There have been some great <a href="http://countercomplex.blogspot.sg/2011/10/some-deep-analysis-of-one-line-music.html">attempts</a> to create the theory of Bytebeat music but it’s still more about random discovery rather than logical step-by-step composition.</p><p>What is known is that the pitch depends on the multiplier of “t”. If the multiplier also depends on “t” somehow — you get a pitch that changes in time. “byte(t*t)” still sounds like instant buzz, but “byte(t*(t/100))” is a loop where pitch is increasing and decreasing. Restricting the pitch multiplier to certain integer values (using bitwise shift instead of the division operator and using a bitwise mask to put it into a valid range) one can get predictable multipliers.</p><p>Now, there are some “good” ratios in frequencies. For example, the frequency ratio between octaves is 1:2:4:8 etc, and the ratio between the notes in a major chord is 4:5:6.</p><p>Let’s make a major chord arpeggio (a chord broken into a sequence of notes like C, E, F, E). The tempo should be slow enough to hear the individual notes. We will be using “t&gt;&gt;11” as our note index. It will result in a number that increases ~4 times per second. We need to put it into a range of [4, 5, 6]. A simple approach is to make a bitwise AND with 3 then add 4. Bitwise AND will return a sequence of [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3…], while adding 4 to each number gives a sequence of [4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7…]. Seven is a bit out of the chord notes range, but for the sake of simplicity we will keep it, hopefully it won’t break our melody too much. Finally, we will multiply “t” by some constant pitch multiplier and then by our variable note multiplier:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/739/1*FZ1dRP8ns8bllNxS5kXbbQ.png" /><figcaption>Glitch: almost a major chord arpeggio</figcaption></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fw.soundcloud.com%2Fplayer%2F%3Furl%3Dhttp%253A%252F%252Fapi.soundcloud.com%252Ftracks%252F251455980%26show_artwork%3Dtrue&amp;url=https%3A%2F%2Fsoundcloud.com%2Fnaivesound%2Fglitch-arpeggio&amp;image=http%3A%2F%2Fi1.sndcdn.com%2Fartworks-000150590068-avd4nu-t500x500.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=soundcloud" width="500" height="166" frameborder="0" scrolling="no"><a href="https://medium.com/media/d31dd38367dbba070512fc3b04073bf9/href">https://medium.com/media/d31dd38367dbba070512fc3b04073bf9/href</a></iframe><p>You may learn much more about different concepts of Bytebeat formulas here: <a href="http://countercomplex.blogspot.sg/2011/10/some-deep-analysis-of-one-line-music.html">http://countercomplex.blogspot.sg/2011/10/some-deep-analysis-of-one-line-music.html</a></p><h4>Sequencer</h4><p>If a melody can be represented as a sequence of algorithmically generated numbers — great, but if not — there should be a more traditional way to encode a sequence of numbers. Some composers used arrays or C strings (that are no different from arrays, really). Glitch has an array function for that — “a(index, a0, a1, a2, a3, …)” — which returns an element at the given index by modulo of the array length. For example a(t&gt;&gt;11, 4, 5, 6) will return a sequence of [4, 5, 6, 4, 5, 6, 4, …].</p><p>But to make our formulas look more obfuscated (and more in the spirit of Bytebeat) let’s try to encode note numbers as 4-bit parts of a 32-bit number. For example a major scale would be [0, 2, 4, 5, 7, 9, 11, 12], when written in hexadecimal it becomes 0x024579bc (or 38107580 converted back to a decimal number).</p><p><a href="http://naivesound.com/glitch">Glitch</a> allows you to use variables and to separate multiple expressions with comma, and commas can be omitted at the end of line, so I will take advantage of this syntax to simplify the expressions. Let’s use “n” variable to denote our “sequence-encoded-as-a-number”.</p><p>Now we can take a certain 4-bit nibble at the certain offset (increased as time goes): “p=n&gt;&gt;(((t&gt;&gt;11)&amp;7)&lt;&lt;2)”. Offset is ((t&gt;&gt;11)&amp;7) and we multiply it by 4 because each nibble takes 4 bits.</p><p>Remember, this is a note index, not a note frequency. There is a simple way to convert note index into a frequency — (2**(1/12))**note, where “**” is a power operation. The note of 49 is A4 (fourth octave A). And 2**(1/12) is ~1.059. So the frequency of note “p” would be “t*(1.059**(p+51))”.</p><p>Easy, huh? Each 32-bit number is an 8-step sequencer where each note is in the range of 16 semi-tones, which is a bit more than one octave. Now we can juggle the sequences and make complex melodies. This is where our array function can be handy, but one can also use a short-circuit operator (&amp;&amp;, ||) to simulate if/then/else. For example “((t&gt;&gt;14)&amp;1)&amp;&amp;verse||chorus” toggles verse and chorus signals every 2 seconds.</p><p>Here’s an example of four 8-step sequencers that play some C-major scale tunes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/732/1*o5U-sfvlyy09vypelWIfoQ.png" /><figcaption>Glitch: Fast arpeggios in C major</figcaption></figure><p>You may listen to the result <a href="http://naivesound.com/glitch/#seq%3Da(t%3E%3E14%2C%203415692320%2C%2038107580%2C%2075068528%2C%0A%20%20%20%20%20%203415692320)%2C%0Aoff%3D((t%3E%3E10)%267)%3C%3C2%2C%0Ap%3D((seq%3E%3Eoff)%2615)%2B51%2C%0At*(1.059**p)">here</a> (just click the play button).</p><h4>Synthesizers</h4><p>So far it’s been mostly saw-tooth waves as they are the easiest ones to implement. For your convenience Glitch also has a sine function “s(t)”. It takes a phase value in the range [0..1] and returns a sine value for the given phase. The sine wave “s(t/256)” has the same frequency as a saw-tooth wave “byte(t)”.</p><p>If you take the last formula and replace “t*(1.059**p)” with “s(t/256*(1.059**p))” — you will hear a pretty sine wave playing the same melody. You can mix multiple waves by adding them. For example “w=t*(1.059**p)/256,(s(w)+s(w/2)+s(w*4))/4” is a mix of three sine waves playing in different octaves.</p><p>You may also use “r(x)” function to get random numbers in the range [0..x] — this is how you can simulate some drums.</p><p>Using only basic arithmetics you can build complex instruments such as FM synthesizer. Here’s an example built with a regular sine wave and some rhythmic but chirpy noise along with the main melody:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*wLdL-mblEZH07bIwiaRPqg.png" /><figcaption>FM synth playing along with the main melody</figcaption></figure><p>Here’s a <a href="http://naivesound.com/glitch/#seq%3Da(t%3E%3E14%2C%203415692320%2C%2038107580%2C%2075068528%2C%0A%20%20%20%20%20%203415692320)%2C%0Aoff%3D((t%3E%3E10)%267)%3C%3C2%2C%0Ap%3D((seq%3E%3Eoff)%2615)%2B51%2C%0Aw%3Dt*(1.059**p)%2C%0A(s(w%2Bs(w*t%3E%3E15))%2Bs(w))%2F2">link</a> to the formula above.</p><h4>Glitch expression language</h4><p>Glitch expression language is very simple and supports the following operations:</p><ul><li>Basic math: +, -, *, /, % (modulo) and ** (power)</li><li>Bitwise math: &amp;, |, ^ (XOR or unary negation), &lt;&lt;, &gt;&gt;</li><li>Logical operations: &lt;, &gt;, &lt;=, &gt;=, ==, !=, &amp;&amp;, ||</li><li>Others: comma to separate expressions, assignment to modify the value of the variables, parenthesis to group subexpressions.</li></ul><p>There are many build-in function, but only 4 of them are likely to be used in Bytebeat: s(t) — sine wave, r(x) — random values, a(i, …) — get array element at a specific index and l(i) — binary logarithm (can be used to convert frequency back into a note).</p><p>Glitch works with floating-point numbers and operates at a higher sample rate (normally 44100Hz) which makes it possible to create sound of higher quality than 8-bit at 8 kHz. However “t” increment speed always matches the 8000Hz sample rate (it often takes rational values depending on your native sample rate). Same about the output values. It can be any floating point number in the range [-1..1] so technically it’s higher sound quality than 8-bit unless you truncate it.</p><p>Bytebeat changes the way you think about music. Instead of classical notes, instruments and effects you start thinking about signal functions and try to decode the sound you want to hear into a mathematical expression. You’re a music decomposer now.</p><p>Spend some time exploring Glitch library and try to modify existing tunes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/884/1*i_TiqipNg8rqIHDyo9eeiw.png" /><figcaption>Glitch: library of Bytebeat songs</figcaption></figure><p>Or try your luck and put random operations with random values (that’s how most great Bytebeat pieces have been composed). Or write repetitive sequences of notes and play them with manually written synthesizers. Then share your creations via Twitter or by simply sending a link.</p><h4>Future of Glitch</h4><p>I find it very impressive when simple things turn into a complex beauty when carefully combined. Forth programming language, lambda calculus, UNIX tools and now Bytebeat music.</p><p>That’s why I want to keep Glitch clean and minimal. But what are the features you would like to see in the future versions of Glitch? Or perhaps you found a glitch in Glitch — then feel free to report it on Github: <a href="https://github.com/naivesound/glitch/issues">https://github.com/naivesound/glitch/issues</a></p><p><em>If you still think that Glitch might be too limited for you — check out the </em><a href="https://medium.com/@naive_sound/glitch-beyond-the-bytebeat-603478a03686"><em>update</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6a9b24e7f5b5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>