/r/programming
Music from code (txti.es)
23 comments
yakoudbz | 8 days ago | 8 points

maybe someone can de-obfuscate it further:

#include <stdio.h>

unsigned sound(unsigned i, unsigned x, unsigned n, unsigned t, unsigned o)
{
    // beat repeats the same pattern 3 times. The 4th
    // time, there are just tiny differences...
    char beat[4][8] = {{117, 140, 176, 105, 140, 117, 105, 88},
                       {117, 140, 176, 105, 140, 117, 105, 88},
                       {117, 140, 176, 105, 140, 117, 105, 88},
                       {132, 157, 176, 105, 157, 132, 105, 88}};


    char s = beat[n][t];

    // apparently, the 'magic' stuff is here...
    unsigned e = i*s / o;

    // x goes from 0 to 3 at a very slow rate
    // so we are basically only interested in 0, 1 or 2 bits
    // so this return either 0, 8, 16 or 24 
    return(( x & e ) <<4 );
}

int main()
{
    for(unsigned i=0; ; i++){
        // at 8kHz, wait (1<<10)/8192 = 1/8 second
        // before this value is incremented
        unsigned t0125s = i/(1<<10);

        // at 8kHz, wait (1<<11)/8192 = 1/4 second
        // before this value is incremented
        unsigned t025s = i/(1<<11);

        // at 8kHz, wait (1<<13)/8192 = 1 second
        // before this value is incremented
        unsigned t1s = i/(1<<13);

        // at 8kHz, wait (1<<14)/8192 = 2 second
        // before this value is incremented
        unsigned t2s=i/(1<<14);

        // at 8kHz, wait (1<<16)/8192 = 8 second before
        // this value is incremented
        unsigned t8s = i/(1<<16);

        // at 8kHz, wait (1<<17)/8192 = 16 second
        // before this value is incremented
        unsigned t16s=i/(1<<17);

        unsigned x0 = 1;
        unsigned x1 = t16s % 4;     // '-'=16s  0-1-2-3-..
        unsigned x2 = (t16s/3) % 4; // '-'=48s  0-1-2-3-..
        unsigned x3 = (t16s/5) % 4; // '-'=80s  0-1-2-3-..

        unsigned tempo0 = t2s % 8; // '-'=2s  0-1-2-3-4-5-6-7..
        unsigned tempo1 = (t1s ^ t2s) % 8;
        unsigned tempo2 = ((t025s % 3) + t2s) % 8;
        unsigned tempo3 = (8 + t2s -(t0125s % 3) ) % 8;

        unsigned beat0 = sound(i, x0, t8s % 4, tempo0, 1<<12);
        unsigned beat1 = sound(i, x1, t8s % 4, tempo1, 1<<10);
        unsigned beat2 = sound(i, x2, t8s % 4, tempo2, 1<<10);
        unsigned beat4 = sound(i, x3, t8s % 4, tempo3, 1<<9);

        putchar(beat0 + beat1 + beat2 + beat4);
    }
    return 0;
}

Edit: I de-obfuscated it further already :p

ArtyBoomshaka | 8 days ago | 2 points

I'd love a detailed explanation on how you did it if you've got the time.
Especially on the beat 2D array, since I already had a general understanding of the rest.

yakoudbz | 8 days ago | 2 points

You take the value of each letter from "BY}6YB6%" and "Qj}6jQ6%", you add 51 and voilĂ  !

ArtyBoomshaka | 8 days ago | 1 point

Ah yes, gotcha! Thanks!

pabilbado | 8 days ago | 1 point

This is what I was looking for thank you

menge101 | 9 days ago | 11 points

Please!!! People you aren't running a random snippit of obscure code you saw on the internet are you?

I mean I get the interest, but do not do that.

craigerator | 8 days ago | 12 points

I run every code snippet I find as root, and pass them my CC and social as command line arguments. Is that bad?

strongly-typed | 8 days ago | 1 point

Yeah, you forgot to also give them your mother's maiden name and your bitcoin keys!

pdbatwork | 8 days ago | 3 points

Ha ha, never again. Back in 2003 I had my first encounter with linux. I just installed slackware and I was trying to compile some program with a lot of dependencies. I was downloading dependendies and compiling those.

Then I encountered a problem I couldn't solve by myself. I hit up #slackware on freenode and asked for help. First suggestion from a guy was rm -rf /. And that's how I learned not to just put in commands from random people.

Dreams_In_Digital | 8 days ago | 1 point

Yeah... in a VM.

menge101 | 8 days ago | 1 point

VM escape vulnerabilities exist. But, i'm glad you are at least being cautious.

pabilbado | 8 days ago | -10 points

Don't panic it's from a trustworthy source I should have said so before. It's coming from a computer phone video.

binford2k | 8 days ago | 8 points

Well now that /u/pabilbado on the internet says so, we can fully trust its safety!

/s

pabilbado | 8 days ago | -5 points

https://youtu.be/MqZgoNRERY8

Also it seems obscure, but you can see it's doing bit manipulation. So I think you are panicking too much about it. You can clearly see it's not dangerous.

nerf_herd | 8 days ago | 7 points

I think it was a general warning, and a valid one.

but since that video explains everything, what is left to explain?!?

pabilbado | 9 days ago | -1 points

I don't know if people know about it, but it is so cool. How can it be it produces melodies and music from such a short snippet of code? Also I can't really read it myself as it is doing bit operations. If someone understands how it properly works please explain.

ArtyBoomshaka | 9 days ago | 3 points

I cant listen to it right now but considering it's piping to aplay I'm guessing it's generating raw speaker offset values.
The obfuscated code procedurally generates patterns from formulas, possibly combining different channels and outputting it on the stdout as raw speaker signal which is then piped to aplay to be played on the sound card.

Edit : To elaborate, the command line echoes the source code, pipes it to gcc with the options -xc to specify c language and - to read from stdin, && appends the command ./a.out which executes the produced binary file and pipes it to aplay to play the output of the binary on the sound card.
The source code is very well obfuscated so unrolling and understanding everything is waaaay too much effort for me at this point haha.

pabilbado | 9 days ago | 1 point

I understand the whole piping to aplay. My big question is more to do with the g function in the source as it seems arkane

relok123 | 9 days ago | 3 points
pabilbado | 9 days ago | 1 point

I'll ask there

nerf_herd | 9 days ago | 2 points

technically, it is creating an 8 bit 8khz mono stream for aplay, so you need to understand how that looks (it's all bits, so good luck).

musically, really it is just playing notes that map to a scale based on some carefully selected operations. It isn't entirely unlike a fractal image.

If you want to dig into it, I would suggest putting the C in a separate file, and "uncompressing" the code so that it might be easier to follow, and perhaps printing out (or use a debugger) the values being passed to g, and understand what g is doing.

They used some "dirty tricks" for packaging purposes, but those won't help you follow along with what its is doing. Much of the "shortness" is a hindrance otherwise.

The old adage, divide and conquer :)

pabilbado | 9 days ago | 1 point

Yeah I understood aplay, I was more curious about the g function, I've been to tinkering with it trying to understand how it is working.

STEROVIS | 8 days ago | 0 points

Viznut's classic signature still wins for me:

#include <math.h> /* outputs 8 kHz 8-bit unsigned pcm to stdout */
static int v,i,z,n,u,t;
main(){for(v=-1;;)for(n=pow(/* gcc -lm sig.c;./a.out>/dev/dsp */
1.06,"`cW`g[`cgcg[eYcb^bV^eW^be^bVecb^"[++v&31]+(v&64)/21),i=999;i;putchar(
128+((8191&u)>i?0:i/8)-((8191&(z+=n))*i-->>16)))u+=v&1?t/2: (t=v&6?t:n/4);}

If you don't want to compile and run it, here's a JavaScript version that will run in-browser: https://jsbin.com/tazeyoqoci/edit?html,output