Live data from Hacker News

Algorithmic symphonies from one line of code

countercomplex.blogspot.com

31–38 of 38 posts

Re: Algorithmic symphonies from one line of code

#31

Earlier quoted context omitted.

This simple formula produces an interesting sound in your a1k0n.net player: (t % 1000) & t Multiplying the whole thing by t adds to it. Edit: t >> (t % 8) also produces an interesting sound; changing the number 8 changes the pitch. Edit 2: On the theory that primes might create an interesting beat frequency, I tried this which almost sounds like a 3 part harmony: (t >> (t % 7)) & (t >> (t % 5)) & (t >> (t % 3))

Those are really cool! Is there any way to explain what the different symbols and operators are doing? I'm a web developer and have a very vague understanding of a little bit of the code but am also a musician and would love to understand a little more so I could try to make my own (without just randomly typing numbers and symbols). For example, is there some correlation between adding a ">>" or "%" operator and how…

I'm not a musician, so this is just my understanding of the changes as a programmer and after trial and error.

The '>' are bit-wise shift operators[1].

The '>>' is a right-shift which is equivalent to dividing by a factor of two. e.g. t>>n == t/(2^n) This can be used to generate a beat.

The 'The modulo operator[2], '%', has very little effect, in combinations it can generate a looping set of ranged variance, which is particularly pronounced when combined with a division operator to create clear stepping. It monotonically increases to n, and then falls back to 0 and starts increasing again. e.g. t[asterisk](t%16/64) This increases through 16 phases, holding for 4 periods.

Addition, '+', and subtraction, '-' haven't been very useful for me except when using sin(t) or cos(t) which oscillate between -1 and 1 resulting in some interesting changes to the beat. e.g.t>>4+cos(t) is shifting between t>>3 and t>>5.

The AND '&', OR '|', a short circuiting OR, '||' come in during composition. Plain OR tends to generate better results, but that's trial and error talking. Honestly, too tired to think about this.

[1] http://en.wikipedia.org/wiki/Bitwise_operation#Logical_Shift [2] http://en.wikipedia.org/wiki/Modular_arithmetic

Re: Algorithmic symphonies from one line of code

#37
This one, from HAKMEM's circle-drawing hack, generates a simple pure sine wave:

    main(t,u){for(t=u=85;;t+=u>>2,u-=t>>2)putchar(t+128);}
BTW, I think the comments saying that aplay or pacat are equivalent to /dev/audio are slightly mistaken. /dev/audio is μ-law, like aplay -f MU_LAW. aplay defaults to linear unsigned 8-bit. With the sine wave above, the difference is very noticeable. With the various distorted sawtooths and white-noises in the original videos, it's harder to tell, but I'm reasonably sure that they're using μ-law, not linear.

BTW, aplay refuses to play 8-bit audio on my Logitech USB speakers. So I ended up using sox to convert:

    ./viznut1 | sox -r 8000 -U -t u8 - -t s16 - | aplay -f S16_LE -D hw:1,0
The -U specifies μ-law conversion.

I created a Git repository for these programs at https://github.com/kragen/viznut-music.

Post reply on HN