Sunday, 2 October 2011

Algorithmic symphonies from one line of code -- how and why?

Lately, there has been a lot of experimentation with very short programs that synthesize something that sounds like music. I now want to share some information and thoughts about these experiments.

First, some background. On 2011-09-26, I released the following video on Youtube, presenting seven programs and their musical output:

This video gathered a lot of interest, inspiring many programmers to experiment on their own and share their findings. This was further boosted by Bemmu's on-line Javascript utility that made it easy for anyone (even non-programmers, I guess) to jump in the bandwagon. In just a couple of days, people had found so many new formulas that I just had to release another video to show them off.

It all started a couple of months ago, when I encountered a 23-byte C-64 demo, Wallflower by 4mat of Ate Bit, that was like nothing I had ever seen on that size class on any platform. Glitchy, yes, but it had a musical structure that vastly outgrew its size. I started to experiment on my own and came up with a 16-byte VIC-20 program whose musical output totally blew my mind. My earlier blog post, "The 16-byte frontier", reports these findings and speculates why they work.

Some time later, I resumed the experimentation with a slightly more scientific mindset. In order to better understand what was going on, I needed a simpler and "purer" environment. Something that lacked the arbitrary quirks and hidden complexities of 8-bit soundchips and processors. I chose to experiment with short C programs that dump raw PCM audio data. I had written tiny "/dev/dsp softsynths" before, and I had even had one in my email/usenet signature in the late 1990s. However, the programs I would now be experimenting with would be shorter and less planned than my previous ones.

I chose to replicate the essentials of my earlier 8-bit experiments: a wave generator whose pitch is controlled by a function consisting of shifts and logical operators. The simplest waveform for /dev/dsp programs is sawtooth. A simple for(;;)putchar(t++) generates a sawtooth wave with a cycle length of 256 bytes, resulting in a frequency of 31.25 Hz when using the the default sample rate of 8000 Hz. The pitch can be changed with multiplication. t++*2 is an octave higher, t++*3 goes up by 7 semitones from there, t++*(t>>8) produces a rising sound. After a couple of trials, I came up with something that I wanted to share on an IRC channel:

main(t){for(t=0;;t++)putchar(t*(((t>>12)|(t>>8))&(63&(t>>4))));}

In just over an hour, Visy and Tejeez had contributed six more programs on the channel, mostly varying the constants and changing some parts of the function. On the following day, Visy shared our discoveries on Google+. I reshared them. A surprising flood of interested comments came up. Some people wanted to hear an MP3 rendering, so I produced one. All these reactions eventually led me to release the MP3 rendering on Youtube with some accompanying text screens. (In case you are wondering, I generated the screens with an old piece of code that simulates a non-existing text mode device, so it's just as "fakebit" as the sounds are).

When the first video was released, I was still unsure whether it would be possible for one line of C code to reach the sophistication of the earlier 8-bit experiments. Simultaneities, percussions, where are they? It would also have been great to find nice basslines and progressions as well, as those would be useful for tiny demoscene productions.

At some point of time, some people noticed that by getting rid of the t*part altogether and just applying logical operators on shifted time values one could get percussion patterns as well as some harmonies. Even a formula as simple as t&t>>8, an aural corollary of "munching squares", has interesting harmonic properties. Some small features can be made loud by adding a constant to the output. A simple logical operator is enough for combining two good-sounding formulas together (often with interesting artifacts that add to the richness of the sound). All this provided material for the "second iteration" video.

If the experimentation continues at this pace, it won't take many weeks until we have found the grail: a very short program, maybe even shorter than a Spotify link, that synthesizes all the elements commonly associated with a pop song: rhythm, melody, bassline, harmonic progression, macrostructure. Perhaps even something that sounds a little bit like vocals? We'll see.

Hasn't this been done before?

We've had the technology for all this for decades. People have been building musical circuits that operate on digital logic, creating short pieces of software that output music, experimenting with chaotic audiovisual programs and trying out various algorithms for musical composition. Mathematical theory of music has a history of over two millennia. Based on this, I find it quite mind-boggling that I have never before encountered anything similar to our discoveries despite my very long interest in computing and algorithmic sound synthesis. I've made some Google Scholar searches for related papers but haven't find anything. Still, I'm quite sure that at many individuals have come up with these formulas before, but, for some reason, their discoveries remained in obscurity.

Maybe it's just about technological mismatch: to builders of digital musical circuits, things like LFSRs may have been more appealing than very wide sequential counters. In the early days of the microcomputer, there was already enough RAM available to hold some musical structure, so there was never a real urge to simulate it with simple logic. Or maybe it's about the problems of an avant-garde mindset: if you're someone who likes to experiment with random circuit configurations or strange bit-shifting formulas, you're likely someone who has learned to appreciate the glitch esthetics and never really wants to go far beyond that.

Demoscene is in a special position here, as technological mismatch is irrelevant there. In the era of gigabytes and terabytes, demoscene coders are exploring the potential of ever shorter program sizes. And despite this, the sense of esthetics is more traditional than with circuit-benders and avant-garde artists. The hack value of a tiny softsynth depends on how much its output resembles "real, big music" such as Italo disco.

The softsynths used in the 4-kilobyte size class are still quite engineered. They often use tight code to simulate the construction of an analog synthesizer controlled by a stored sequence of musical events. However, as 256 bytes is becoming the new 4K, there has been ever more need to play decent music in the 256-byte size class. It is still possible to follow the constructivist approach in this size class -- for example, I've coded some simple 128-byte players for the VIC-20 when I had very little memory left. However, since the recent findings suggest that an approach with a lot of random experimentation may give better results than deterministic hacking, people have been competing in finding more and more impressive musical formulas. Perhaps all this was something that just had to come out of the demoscene and nowhere else.

Something I particularly like in this "movement" is its immediate, hands-on collaborative nature, with people sharing the source code of their findings and basing their own experimentation on other people's efforts. Anyone can participate in it and discover new, mind-boggling stuff, even with very little programming expertise. I don't know how long this exploration phase is going to last, but things like this might be useful for a "Pan-Hacker movement" that advocates hands-on hard-core hacking to greater masses. I definitely want to see more projects like this.

How profound is this?

Apart from some deterministic efforts that quickly bloat the code up to hundreds of source-code characters, the exploration process so far has been mostly trial-and-error. Some trial-and-error experimenters, however, seem to have been gradually developing an intuitive sense of what kind of formulas can serve as ingredients for something greater. Perhaps, at some time in the future, someone will release some enlightening mathematical and music-theoretical analysis that will explain why and how our algorithms work.

It already seems apparent, however, that stuff like this stuff works in contexts far beyond PCM audio. The earlier 8-bit experiments, such as the C-64 Wallflower, quite blindly write values to sound and video chip registers and still manage to produce interesting output. Media artist Kyle McDonald has rendered the first bunch of sounds into monochrome bitmaps that show an interesting, "glitchy" structure. Usually, music looks quite bad when rendered as bitmaps -- and this applies even to small chiptunes that sound a lot like our experiments, so it was interesting to notice the visual potential as well.

I envision that, in the context of generative audiovisual works, simple bitwise formulas could generate source data not only for the musical output but also drive various visual parameters as a function of time. This would make it possible, for example, for a 256-byte demoscene production to have an interesting and varying audiovisual structure with a strong, inherent synchronization between the effects and the music. As the formulas we've been experimenting with can produce both microstructure and macrostructure, we might assume that they can be used to drive low-level and high-level parameters equally well. From wave amplitudes and pixel colors to layer selection, camera paths, and 3D scene construction. But so far, this is mere speculation, until someone extends the experimentation to these parameters.

I can't really tell if there's anything very profound in this stuff -- after all, we already have fractals and chaos theory. But at least it's great for the kind of art I'm involved with, and that's what matters to me. I'll probably be exploring and embracing the audiovisual potential for some time, and you can expect me to blog about it as well.

477 comments:

Regarding the theory behind it: this (and particularly the graphics at the bottom) reminds me a lot of wavelets, particularly the dyadic or fast wavelet transforms with very simple wavelets (e.g., the haar wavelet).

As to why it produces so nice harmonical and musical structures: if you look at it, musical structure is just kind of "macro-harmonics". Play 4 bars of this, then play 4 bars of this, then change to another theme, like the poplar ABAB, AABA structures. It's just a very low frequency square wave :-).

Strange though that there has been no scientific research going on there. Normally scientists go crazy if they find even the smallest topic that hasn't been grinded to death yet.

I kind of mentioned the "macro-harmonics" in my earlier post, noting that microcomputer music has always used fixed binary lengths for optimizing the player code. In classic ProTracker configuration, for example, there's 4 rows per beat, 16 beats per pattern, and the macrostructure is a list of pattern indices.

No, it isn't. C-64 music typically spends kilobytes for the player code and data. The playback mechanisms are very straightforward, giving the composer a lot of control over the output. A lot of order, very little chaos.

In this article, we're exploring a totally different complexity class. A class with only dozens of bytes of code. Something where parametric exploration rules over deterministic composition.

I just had a go implementing this on an AVR microcontroller. It works pretty well, however my 'reception method' results in low pass filtering the output at about 4KHz, which makes some of the pieces sound a bit different.

Actually, in your formula, all the conditionals are either redundant (a&1?1:0 -> a&1) or can be replaced with bitshifts (a&128?1:0 -> (a&128)>>7). More complex ternaries can be replaced with plain arithmetic as well, it just takes a few more bytes.

Anyway, I don't see any philosophical problems with conditionals or additional variables. The main point for me is the shortness of the program, not functional purity or anything like that.

The visual rendering of the audio patterns reminds me of two things that may be of interest from an algorithm perspective.

http://www.isi.edu/ant/address/ uses a single Hilbert Curve to compress the entire Internet IP allocation space into a single diagram. Imagine if you used a 256 byte algorithm to produce a Hilbert curve graphic, and then played it as audio. Or multiple overlapping curve trails, playing the image in a continuous loop as the ants (over)write new trails onto the audio-graphic.

http://www.derlien.com/ uses something called "Treemaps" to represent the hierarchical space usage of a hard drive full of information as a simple, human-understandable graph. On most of my hard drives, the result resembles very strongly like the black-and-white graphic above. So perhaps it would be possible to feed a generic treemap algorithm a list of simple inputs and have it produce an audio-graphic for playback use as well.

I've created an Arduino sketch that plays these algorithmic compositions through the PWM port. Get it from here:http://www.gjcp.net/~gordonjcp/countercomplex.pde

Upload it to your Arduino and connect an amplifier to pin 11 (PWM output). Look in the source code for details of how it works. I've added the algorithms from the two videos on your site, plus a couple of my own.

You can even add some pots and switches and read them in the main loop() function, and make your algorithms tweakable!

Having been obsessed with, what I'd call a tune, "Lost in Space" from xpansive and after @paul_hayes updated http://entropedia.co.uk/generative_music/ to display a visual translating the formula, I've found out that the (t&t>>13|t>>6) part of the song is a formula representing the Sierpinski triangle, a well known fractal pattern.No wonder the song is ever changing!More info: http://en.wikipedia.org/wiki/Sierpinski_triangle

The "Entropedia" visualization algorithm indeed plots Sierpinski triangles from all kinds of structures that contain AND/OR operations. The simple but harmonically interesting formula t&t>>8 I gave in the article plots a pure Sierpinski triangle as well.

Another interesting formula is (t*5&t>>7)|(t*3&t>>10) which has a very pleasant harmony as well as an interesting visual form: a Sierpinski triangle breaking down into squares that contain many smaller Sierpinski triangles.

Hey guys, that really got me stuck for a while. When I was 16 i experimented on similar stuff only i would output it to the graphic adapter. So anyway haven't seen anyone trying irrational numbers, yet.

So here is my little experiment. I guess I used some formulars i read here oder elsewhere. so no offence....

Love this stuff, especially the links to maths and fractals! The javascript/actionscript generators are great but I would like to try it on my mac directly. Anybody know if there is a simple way (via bash) or does it take some meddling with CoreAudio?

Oh, did I forget my contact information? You can email me at (my_pseudonym)@low.fi.

I'll probably release the third video during this weekend. This video will include a visualization of the new songs by using three quite obvious schemes (that still manage to make some formulas look quite pretty). I hope this will encourage more visually-oriented experimentation as well.

Adding periodic inclusion of stochastic elements to the code might be interesting, for example by generating a small pseudorandom table from a performance-time key on the command line, and using that periodically to change the direction of a section. Looks like I may have something else to do now!

That said, i just wanted to add my experiments. Not really too proud of 'em, as i am humbled by skurk's and miiro's and more than all by Lost in Space which i can literally sleep to.

I was trying to find longer phrases, so i used a lot of divisions, which don't seem to be very common on your examples. Anyway, i liked this experiment:

((t+13217)/1211)&(t>>2|t>>4|t>>6)

though it is too silent. I managed to amplify it with:

((t+13217)/1211)&(t>>2|t>>4|t>>6)|(t>>2)|(t%256)

Then i found this that sounds a little like "vocalize" exercises:

t*((t+13217)/1211)&(t>>2|t>>4|t>>6)/512

Those 13217 and 1211 i seem to have generated by sheer keyboard-bashing...

Much further i got

((t*(t>>8|t>>9)&(t/256+45)&t>>8))^(t&t>>13|t>>6)

and

(t/384)&(t<<3|t>>3)+(t/896)&(t<<5|t>>5)+(t/1664)&(t<<7|t>>7)

Then there is this last one i like a lot, specially @32Hz

(t*t/256)&( t>>((t/1024)%16) )

I guess my mind just keeps on classifying all those sounds as parts that could be tied together (in say audacity or something) and that is why i like the unbalanced macro-structures...

Anyway, the whole thing seems to hinge upon /dev/audio discarding anything above 8 bits, so that 75% of the t variable (which should default to int which should be 32 bits) never goes anywhere. But on the other hand this kinda sucks because we can't really sum two different formulas. Isn't there a smarter way to do this? I did some experiments with aplay --format but didn't seem promising...

there were some old implementations of BASIC with the BEEP (or other related) command that could create sounds close to this back in the day of 8 computers. One in particular HP 85 Hewlett Packard Computer which I experimented with - the computer was in a analytical chemistry laboratory and was supposed to be used to collect gas chromatography data. The lab even had a tape with VisiCalc on it (the original PC spreadsheet program). The sounds were not one liners though - they were generated as loops in BASIC. So, since BASIC isn't as space efficient as C it was more or less equivalent.

Thanks for introducing me to this entire new world. The sounds will become interesting once someone figures out how to use the simple logic to generate melodies, basslines, chords, and structure to create real music.

I was deeply shocked by the Algorithmic Symphonies and, as an electronic musician, wanted to explore some of them in another context, carefully rendering each of them into samples and applying FX and/or different sample rates. I wanted to ask you a couple of questions.

Please forget my poor C understanding. I'm just a BASH script guy friendly enough with the command line to understand some of the code.

I wrote this on OSX since I don't know how to pipe to CoreAudio and wanted to render a 30 seconds file and study it under Audacity. I've compiled it and piped to a .raw file

Reminds me of what Martin Galway said about his 10+ minute, partially algorithmly-generated intro to Times of Lore on the Commodore 64 - that it was the most work he'd put into a song, and wondered just how many people would actually listen to it long enough to hear the "guitar solo" part that was generated...

Change the 60 to change the rate at which the sound evolves. Change the outer 1.0 to adjust how much the inner oscilator modulates the outer on (max of tau, higher = more modulation). To add more operators just replace the 60 with yet another fmOp call.

Yea, it isn't quite one line, but it takes just 192 bytes (not counting libraries).

Where and what is the online JS tool mentioned in the third video ? I have been testing some of these on an atmega328 with a 6bit r2rdac wired up to it, (inspired by "little scale" but i would like a quicker easier way of "prototyping" songs

I've managed to create what almost sounds like a voice :)I have yet to analyze it but I intentionally left variables a and b in there. I find that this works best with a=5 and b=8. And if b is increased, the whole sample slows down.

Very interesting progressions come out of golden ratio numbers & various combinations of Sierpinski triangles, listen for 500 seconds or so, just keep volume down as there are some less than subtle transitions. Thank you for sharing, this is very addictive!

I have already read this post and I'm very happy to read this post. I strongly believe that this site definitely helpful because we can get more helpful information from this site so I suggest to everyone that please visit this site and take more important and helpful information. I will visit this site regularly. authorswriterspublishers

http://wurstcaptures.untergrund.net/music/?oneliner=(((5%26t)*(t%3E%3E35)%7C(t%2B3%26t%3E%3E18))%26(t%3E%3E46%7Ct%3E%3E37))%7C(t%26((4*t-1)%7C14)%3E%3E16)*t%26((t%3E%3E387%7Ct%3E%3E92)%2B1)&oneliner2=&t0=0&tmod=0&duration=98.25&separation=100&rate=11025It evolves as the parts interact in different ways...

I hear things like this but i never tried this, i program in c and c++ but its so different that this one, myabe some day i can do that but first i am trying to modify whatsapp for pc that i just downloaded

Perhaps the just reason why these types of teenagers hotel to online pay day loans is principally since they generate much less but are very driven to take pleasure from stuff various other teenagers using increased earnings include. An additional acceptable reason can be that these teenagers are nevertheless from that will get older if they are having their 1st taste of freedom and therefore are nonetheless wanton within where did they commit their salary. They're whoever has the particular trend to splurge about the most current artist clothes, dedicate to the newest tools, obtain the trendiest autos as well as apartments as well as commit the most of what exactly they generate going to watering holes as well as other cultural areas. Before they learn that, they've already maxed on their credit cards along with should count on the particular mercy of cash advance loan providers to present them a primary lender cash advance to purchase rent along with ammenities.text loans direct lenders UK

Once you submit the loan application, lending company will verify it and right after the inatant patdat loans approval, your requested loan amount will be transferred into your bank account without any delay. So, get ready to have without any trouble and meet the needs on time. http://instantpaydayloanszone.co.uk/

Frequently within the existence you need to encounter a few difficulties and people difficulties could be associated with something actually, that may be associated with economic crisis. When you are via any kind of financial crunches that point you'll need cash really urgently however in those days you cannot obtain which the help of all of your member of the family as well as your near family member. If you find financial difficulty that point just that individual may realize that scenario which exactly how trouble scenario they're dealing with therefore terribly too. Throughout the second you cannot actually realize what you need to precisely perform or even exactly what not really as well as exactly how encounter which severe scenario that you experienced. However anyway you need to overcome through which hard scenario and thus; you'll need cash in order to manage your own home as well as every thing.

I've written a program that outputs the results of these forumalas directly to .wav files, for my convenience.Does anyone know where the data repeats? Obviously it must repeat after t reaches the maximum int value and then goes back to 0, but that is after generating 2 GB of data! Does the data cycle/repeat before that, or change continuously? I ask because I want my program to generate one complete cycle only (by default) for each formula.

The total amount availed may be used through the person within readily within the satisfaction of the numerous requirements such as paying down their house lease, purchasing brand new points, food expenses, college or even college tuition costs, debt consolidation reduction, unexpected fixing from the cars and so forth. http://www.waytoloans.com/unemployed-payday-loans.htmlhttp://www.waytoloans.com/payday-loans.htmlhttp://www.waytoloans.com/unsecured-loans-for-bad-credit.htmlhttp://www.waytoloans.com/unsecured-tenant-loans.htmlhttp://www.waytoloans.com/faxless-payday-loans.html

In the event of any kind of lapse to pay for the total amount, you could get the possibility associated with having to pay curiosity. The eye needs to be compensated till time the whole quantity isn't compensated. http://textloansu.co.uk/http://doorsteploanszone.co.uk/

It's that a person needs to search through the internet and search through this carefully. Everything it requires is actually a couple of minutes that you should utilize online.bad credit payday loans online @ http://badcreditpaydayloansx.co.uk/bad credit loans online @ http://badcreditpaydayloansx.co.uk/

Hello Everybody,My name is Mrs Sharon Sim. I live in singapore and i am a happy woman today? and i told my self that any lender that rescue my family from our poor situation, i will refer any person that is looking for loan to him, he gave me happiness to me and my family, i was in need of a loan of S$250,000.00 to start my life all over as i am a single mother with 3 kids I met this honest and GOD fearing man loan lender that help me with a loan of S$250,000.00 SG. Dollar, he is a GOD fearing man, if you are in need of loan and you will pay back the loan please contact him tell him that is Mrs Sharon, that refer you to him. contact Dr Purva Pius,via email:(urgentloan22@gmail.com) +918376918351 Thank you.

The following code is for bitwiz, the synth for iDevices. The sample rate is 44.1k. It's determined by the r "( equals 1. The c variable controls stereo output. // music with drumsand some symbols ^|<<>>r=1, (t/128)&((t>>17+c)&t>>12+c) * (t/3)+c^(t>>(t/32+c))^(t/96-c)

Here is a code for some coolmusic followed by cool sound effects, then more music. sound effects. After I paste in that code, ILl paste in another musical one, which involves quarter tones. youLl need to shuck off your western music brain for this one.The sound effects are shrill at first so turn your volume down.r=6, t%(((t%254)>>11)-c&(t>>15)+c&(t>>12)-c|(t>>15)&t>>10)*5

Now here is the musical one, which my band, Blind Labyrinth, are using in an electroacoustic piece.r=3, t%((t>>10)-c&(t>>14)+c&(t>>11)-c|(t>>18))*2r=3, t%((t>>10)-c&(t>>14)+c&(t>>11)-c|(t>>18))*2

I'm really glad I've came across this informative post, this really helped me out quite a bit, by answering all my questions properly, hopefully the site will grow bigger and more popular as ever it was. Keep up the good work and I'm sure I'll visit you up someday soon for more great articles. You may find great articles and music production tools at: http://www.lucidsamples.com by the way!

made a really good complicated one that turned out to have multiple "movements," but i made it on the wurstcaptures online machine and saved the resulting wav, but i neglected to record the source formula :( is there any way to analyze a piece of music and sort of "decompile" it back into an algorithm?

that's either something known or something theoretical, but here's the result of me taking the clip, slowing it down, putting a light phaser on it, duplicating it and adding a wahwah to the dupe, taking the wahwah dupe file and opening it in nano and hacking away bits of the machine code (bytecode?), adding the glitched version of wahwah dupe as another track, kinda mixing it https://soundcloud.com/facebookuser7037018/5qicgrvx-more

the original, slowed, and glitch track only versions are there too, along with other music i've made partiall using this technique (via the online wurstcapture implementation as i'm still learning pretty basic coding stuff)~

//mary had a little lamb//m is melody, s is speed, first & variable, &15, tells the app we are using hex numbers, 0 through f. Second one has to b four less than a power of two, like 28 or 60. The higher this number, the longer he pauses between cycles. Perhaps you can find a way to make longer mlodies? r=3,m=0x0cccbabc,s=t>>10&60,t*(15&m>>s)&128&t>>4

I love Indian music. I have collected the best quality ringtones as my phone ringtone. Unique ringtone. Ringtones are attractive. Visit the link below for reference and free ringtone installation: https://bestringtonesfree.net/

Also you can refer to the installation of the most recently downloaded songs:

Thank you for this post. Thats all I are able to say. You most absolutely have built this blog website into something speciel. You clearly know what you are working on, youve insured so many corners.thanksDigital Marketing Training in Chennai

Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.Digital Marketing online training

Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done an outstanding job.

Do you need personal loan? Does your firm,company or industry need financial assistance? Do you need finance to start your business? Do you need finance to expand your business? We give out loan to interested individuals who are seeking loan with good faith. Are you seriously in need of an urgent loan contact us at Email: flourishloancredite@gmail.comAPPLICATION DETAILSYour Full Details:Full Name:Loan Amount Need:Loan Duration:Phone Number:Applied before?State:Monthly Income:Country:Where did you hear about us;..You are to send this to our Company Email;flourishloancredite@gmail.com

What is the future of data science?Current approaches to AI and ML (Machine Learning) are statistic in nature and cannot generate models or discover causal mechanisms from data (sometimes scientists do helped by AI and ML but not AI or ML themselves, and people get confused believing it is AI or ML).If You Want more details contact us: 9384409662, #DataSciencewithR,#DataSciencewithRTraininginChennai,#DataSciencewithRTrainingInstituteinChennai,#TraininginVelachery.