

Generate raw WAV output by hooking malloc and read - anigbrowl
https://github.com/gordol/malloc-ld_preload-sounds

======
ericHosick
Actually a decent tune here:

[https://soundcloud.com/glowdon/jingy-
compiler-1#t=6:00](https://soundcloud.com/glowdon/jingy-compiler-1#t=6:00)

------
thegeomaster

        void* read( int fd, void * data, size_t count)
        {
          ...
          gen_square_wave( 44100 , CLAMP(count, 20, 20000 ), CLAMP( sizeof(data), 100 , 1700), 0.7 );
    

I don't get it, isn't sizeof(data) always the same, and usually either 4 or 8?

~~~
fredophile
It's always the same for your compiler. Using sizeof improves the portability
of the code. Because sizeof is calculated at compile time it doesn't make the
code any less efficient.

We often think about pointers as being 4 bytes but that isn't necessarily the
case. How would a 32 bit pointer map to the addressable memory of 64 bit
hardware?

~~~
reubenmorais
I think his point is more about clamping the value than about using sizeof. It
indicates the author expected that value to vary, but it doesn't.

~~~
fredophile
Depending on your application I can see where you might still want to clamp
the result. However, I think you're right in this case since the clamped range
doesn't make much sense here.

------
ejr
This bit is amazing. I could have sworn this was in a game already.

[https://soundcloud.com/glowdon/the-sounds-of-
malloc#t=7:18](https://soundcloud.com/glowdon/the-sounds-of-malloc#t=7:18)

[https://soundcloud.com/glowdon/the-sounds-of-
malloc#t=8:40](https://soundcloud.com/glowdon/the-sounds-of-malloc#t=8:40)

[https://soundcloud.com/glowdon/the-sounds-of-
malloc#t=12:00](https://soundcloud.com/glowdon/the-sounds-of-malloc#t=12:00)

------
pronoiac
It sounds like when I piped /dev/random to a midi device. (Or was it
/dev/urandom and TiMidity++?)

It was like demos; I would be stunned at the emergent complexity and
compactness, and it would only elicit shrugs from friends.

~~~
ibisum
I'm reminded of some of my favourite C 1-liners for synthesis:

    
    
        main(t){
            for(t=0;;t++)
                putchar(t*(((t>>12)|(t>>8))&(63&(t>>4))));
                //putchar(t>>7|t%45)&(t>>8|t%35)&(t>>11|t%20); // try this too!
        }
    

.. there's a whole world of 1-liners for synthesis out there, and some of
them, frankly, are astounding. Sound is such a beautiful way to understand
math ..

~~~
akx
These are called bytebeat nowadays.

See viznut's (seminal) intro to the genre.

[http://countercomplex.blogspot.fi/2011/10/algorithmic-
sympho...](http://countercomplex.blogspot.fi/2011/10/algorithmic-symphonies-
from-one-line-of.html)

[https://www.youtube.com/watch?v=GtQdIYUtAHg](https://www.youtube.com/watch?v=GtQdIYUtAHg)

~~~
ibisum
Thanks for the update - bytebeat sounds like something thats a lot more
searchable than 'c one-liners that generate sound' .. ;)

Great stuff, eh?

------
Newtie
Very cool, though I'm having trouble getting any audio out trying it myself. I
can build & LD_PRELOAD the .so fine, but the pipe to aplay produces jibberish
in the terminal without audio.

------
cowpewter
Reminds me of when I was a kid, and I had a Tandy CoCo and a tape recorder
deck that was the only way to save any programs you wrote on it. I tried
playing the data tape a few times.

~~~
mikecsh
Yes! I used to have an Amstrad CPC 464 with a tape deck and it would play the
tape through its internal speaker as it was loading a program into memory. The
amount of time sat around watching screens like this:

[https://www.youtube.com/watch?v=OvChkOHgDIo](https://www.youtube.com/watch?v=OvChkOHgDIo)

------
acannon828
This seems cool, though I don't entirely understand it. "Generates raw WAV
output by hooking malloc() and read()." What does that mean exactly?

~~~
delinka
It uses hooking to intercept calls to malloc() and read(), calls the real
functions, generates a square wave whose frequency is based on the size of the
operation, dumps the generated sound data to WAV, returns the output of the
real function calls.

------
viciousplant
transform logic to sound? hmm interesting idea

~~~
andrewchambers
memory allocations to sound.

------
niix
Heh really cool

------
andyzweb
oh wow this is fun

------
MBCook
To the tune of Sound of Silence by Simon & Garfunkel:

    
    
        Hello malloc() my old friend,
        I've come for mem'ry once again,
        Because a pointer silently creeping,
        Filled buffer that I was keeping,
        And the signal that I trapped was a bus error.
        Didn't care.
        Because I still... have malloc()
    
        There were pages that I missed.
        My OS had sent them to disk.
        Try my best to not hit swap,
        Looking for data I could safely drop,
        I compressed some bits that I kept stored in place
        Freed some space.
        But then I still... used malloc().

