Glitch after one month

Glitch 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.

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.

And old ox makes a straight furrow

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.

It’s 2016 here and web is still a disappointment.

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!

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 — https://github.com/zserge/expr.

The library of music functions (instruments, sequencers, effects) took some time, but now all of the original glitch functions are supported. Then I used Emscripten to translate C into JavaScript 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.

As you know, these days JavaScript is not a language that one can write in notepad and press F5 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.

At this point I started really miss the old-school JavaScript development, but Mithril.js came to the rescue.

I opened vim, started index.html and styles.css, dropped in mithril.js 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.

So this new Glitch-rewritten-in-C is the app that you can see at http://naivesound.com/glitch. It has the same look, the same functionality, but works much faster. That’s how old school technologies have saved a project.

New features

The general syntax of Glitch remained unchanged, but there are some new things added.

The most important one is constants for notes and drums. 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:

sin(hz(seq(240, C4, D#4, F4, D#4)))

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):

mix(tr808(BD, seq(120,1,0)), tr808(SD, seq(120,0,1)), tr808(HH,seq(120*2,1)))

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):

mix(tr808(BD,seq(60,1)), tr808(SD,seq((0.5,60),1)),tr808(HH,seq(240,1)))

I’ve also added the hpf(signal, cutoff) function. Although it can be implemented by subtracting the low-pass filter output from the original signal, I find it too long to type.

Finally, I’ve added the great delay(signal, time, level, feedback) 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.

You can hear delay in action — try moving your mouse to modify delay level and feedback parameters.

What’s next?

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.

Meanwhile, I’m working on a desktop version of Glitch. 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.

But the major difference of desktop Glitch comparing to the web app is that you can use your own samples — 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!

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.

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.

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 issue on Github or submit a pull request.

So, what would you like to see in the next version of Glitch? Or what would you like to hear in the next post?

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.