Live data from Hacker News

Generate raw WAV output by hooking malloc and read

github.com

21–26 of 26 posts

Re: Generate raw WAV output by hooking malloc and read

#21

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?

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?

Re: Generate raw WAV output by hooking malloc and read

#24

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?

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?

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.

Re: Generate raw WAV output by hooking malloc and read

#25

Earlier quoted context omitted.

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?

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.

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.

Re: Generate raw WAV output by hooking malloc and read

#26
post #18
post #17

Earlier quoted context omitted.

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

These are called bytebeat nowadays. See viznut's (seminal) intro to the genre. http://countercomplex.blogspot.fi/2011/10/algorithmic-sympho... https://www.youtube.com/watch?v=GtQdIYUtAHg

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

Great stuff, eh?

Post reply on HN