# Executing Obfuscated C Code
Created at 2024-12-02 12:36:50 UTC
> @ 2024-12-02 12:36:50 UTC
>
> Provide the cleaned up version of this:
>
> ```
> echo "main(i){for(i=0;;i++)putchar(((i*i>>8&46&i>>8))^i&i>>13);}" | gcc -x c - &&
./a.out | aplay
> ```
Let me provide a cleaner, more readable version of this audio-generating program:
```c
#include <stdio.h>
int main() {
int i;
// Generate an infinite sequence of audio samples
for (i = 0; ; i++) {
// Audio generation formula:
// - i*i>>8: Square and shift right by 8
// - &46: Bitwise AND with 46 (00101110 in binary)
// - &i>>8: AND with i shifted right by 8
// - ^(i&i>>13): XOR with i AND i shifted right by 13
unsigned char sample = ((i * i >> 8 & 46 & i >> 8)) ^ (i & i >> 13);
putchar(sample);
}
return 0;
}
```
To compile and play:
```sh
gcc audio.c -o audio
./audio | aplay
```
This is a bytebeat music generator that creates sound patterns through bitwise
operations. The ```aplay``` command plays the generated audio stream as raw 8-bit
unsigned PCM data at a default rate of 8000 Hz.
Note: You'll need ALSA utils installed (for ```aplay```) on Linux systems to play
the audio.
---
> @ 2024-12-02 12:37:10 UTC
>
> I’m on a Mac, though
Ah, for macOS you'll want to use ```afplay``` instead, but it needs a different
format. Here's the Mac-compatible version:
```c
#include <stdio.h>
int main() {
int i;
// Set audio format header for macOS (raw audio won't work with afplay)
// We'll pipe to 'play' from SoX instead, which can handle raw audio
for (i = 0; ; i++) {
unsigned char sample = ((i * i >> 8 & 46 & i >> 8)) ^ (i & i >> 13);
putchar(sample);
}
return 0;
}
```
To compile and play on macOS, you have two options:
1. Using SoX (recommended):
```sh
brew install sox
gcc audio.c -o audio
./audio | play -t raw -b 8 -c 1 -r 8000 -u -
```
2. Using ```afplay``` (requires conversion):
```sh
brew install sox
gcc audio.c -o audio
./audio | sox -t raw -b 8 -c 1 -r 8000 -u - -t wav - | play -
```
The ```-t raw -b 8 -c 1 -r 8000 -u``` parameters mean:
- ```-t raw```: Raw audio format
- ```-b 8```: 8-bit depth
- ```-c 1```: Mono channel
- ```-r 8000```: 8000 Hz sample rate
- ```-u```: Unsigned samples
If you don't have Homebrew installed, you can get it from
[brew.sh](https://brew.sh) to install SoX.
---
> @ 2024-12-02 12:38:38 UTC
>
> markus@~ [0] % ./audio | play -t raw -b 8 -c 1 -r 8000 -u -
>
> play WARN getopt: option `u' not recognized
>
Ah, I apologize - the newer versions of SoX don't need the ```-u``` flag. Here's
the correct command for macOS:
```sh
./audio | play -t raw -b 8 -c 1 -r 8000 -
```
The parameters are:
- ```-t raw```: Raw audio format
- ```-b 8```: 8-bit depth
- ```-c 1```: Mono channel
- ```-r 8000```: 8000 Hz sample rate
- ```-```: Read from stdin
This should work now with SoX on macOS. You should hear the generated audio pattern
playing through your speakers.
---
> @ 2024-12-02 12:38:59 UTC
>
> markus@~ [1] % ./audio | play -t raw -b 8 -c 1 -r 8000 -
>
> play FAIL formats: bad input format for `-': data encoding or sample size was
not specified
>
Ah yes, we need to specify the encoding type. Here's the correct command for macOS
with SoX:
```sh
./audio | play -t raw -b 8 -c 1 -r 8000 -e unsigned-integer -
```
The complete parameters are:
- ```-t raw```: Raw audio format
- ```-b 8```: 8-bit depth
- ```-c 1```: Mono channel
- ```-r 8000```: 8000 Hz sample rate
- ```-e unsigned-integer```: Unsigned integer encoding
- ```-```: Read from stdin
This should now work correctly on macOS with SoX. You should hear the generated
audio pattern playing through your speakers.
---
> @ 2024-12-02 12:40:33 UTC
>
> Wow, that audio’s very nice.
Yes, it's a fascinating example of algorithmic music! This particular formula
creates a pleasant, ambient-like drone through simple bitwise operations. It's part
of a genre called "bytebeat" music, invented around 2011.
The formula:
```c
((i * i >> 8 & 46 & i >> 8)) ^ (i & i >> 13)
```
Creates this sound by:
- Using the square of the counter (i * i) for harmonic content
- Bitwise operations (&, >>, ^) for modulation and texture
- The number 46 (binary 00101110) acts as a tonal filter
- The XOR (^) with shifted versions of i creates rhythmic patterns
It's remarkable how such a small piece of code can create continuous, evolving
soundscapes. If you're interested, you can experiment by slightly modifying the
numbers in the formula to create different sounds.
---
> @ 2024-12-02 12:41:01 UTC
>
> How do we remove the wave?
To stop the audio playback on macOS, you can press ```Ctrl+C``` in the terminal.
If you're asking about removing the wave file that SoX might have created (though
in this case we're streaming directly so no file is saved), there isn't one - we're
using a direct pipe from the program to the audio output.
If you want to stop the program and make sure no audio processes are left running,
you can use:
```sh
killall play
```
or
```sh
pkill -f play
```
These commands will stop any running instances of the ```play``` command.
---
> @ 2024-12-02 12:41:11 UTC
>
> No, I meant the siren
Ah, you want to modify the sound to remove the siren-like quality! Here are a few
variations you can try by modifying the formula. Each will produce a different
character of sound:
1. More drone-like, less siren:
```c
#include <stdio.h>
int main() {
int i;
for (i = 0; ; i++) {
unsigned char sample = (i * i >> 8 & i >> 3) ^ (i & i >> 13);
putchar(sample);
}
}
```
2. Softer, ambient texture:
```c
#include <stdio.h>
int main() {
int i;
for (i = 0; ; i++) {
unsigned char sample = (i >> 3) & (i >> 8) ^ (i & i >> 13);
putchar(sample);
}
}
```
Compile and run the same way:
```sh
gcc audio.c -o audio
./audio | play -t raw -b 8 -c 1 -r 8000 -e unsigned-integer -
```
The siren quality in the original comes from the ```46``` constant and the square
operation. Removing or modifying these changes the tonal character significantly.
---
> @ 2024-12-02 12:55:14 UTC
>
> How come I never encountered this in my life?
Bytebeat music is a relatively niche and experimental form of computer art/music
that emerged around 2011, primarily from the demoscene and creative coding
communities. It was popularized by viznut (Ville-Matias Heikkilä) who posted about
it on YouTube and forums.
The reason it's not widely known is because:
1. It's extremely experimental - it's basically direct manipulation of audio
samples through math formulas
2. It's more of a programming curiosity than a traditional music-making technique
3. The output is often quite abstract/harsh compared to conventional music
4. It mainly spread through programming and computer art communities rather than
mainstream music channels
What makes it fascinating is that incredibly complex sounds can emerge from tiny
formulas - it's a perfect example of "emergent complexity from simple rules,"
similar to cellular automata or fractals.
If you're interested in exploring more, there are online bytebeat editors like:
- [Greggman's
Bytebeat](https://greggman.com/downloads/examples/html5bytebeat/html5bytebeat.html)
- [Viznut's collection](http://viznut.fi/demos/unix/bytebeat_formulas.txt)
These let you experiment with different formulas without having to compile code
locally.
The fact that such a simple formula can create continuous, evolving music is part
of what makes it so surprising and delightful when people first discover it!
---
_Generated by [The Assistant](https://kagi.com/assistant)_