Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // run.sh
- #!/bin/bash
- gcc -x c main.c
- # aplay's -d option specifies duration. So even though main.c
- # is an infinite loop, it'll kill it after 2 seconds.
- ./a.out < /dev/urandom | aplay -d 4
- // main.c
- #include <unistd.h>
- #include <stdio.h>
- #include <stdint.h>
- #define FREQ 220
- // 16000 = 16khz comes from the duration (2 seconds).
- // Note that aplay will eat bytes at 8khz by default.
- // This means if FREQ = 1, it's just outputing from /dev/urandom.
- // Probably gets 220 from here: https://en.wikipedia.org/wiki/Piano_key_frequencies
- int size = 16000/FREQ;
- void main(){
- int i = 0;
- uint8_t v[size];
- read(STDIN_FILENO,v,size); // write "size" bytes from stdin to v.
- // v now contains random data.
- while(1){
- // This algorithm basically averages out the random buffer.
- // Over time, it'll reach a steady state (this produces the fade out effect).
- // How long this takes depends on "size".
- // size = O(1/FREQ), and a large buffer takes longer to reach steady state.
- // this is because freq = 1/wavelength, so our random buffer represents our wave. (I THINK.)
- // Notice that we don't have to generate some kind of sine wave.
- // This sets the index BEFORE the one we output.
- // So the first run outputs directly from /dev/urandom.
- v[i%size] = (v[i%size]+v[++i%size]) / 2;
- putchar(v[i%size]);
- }
- }
Add Comment
Please, Sign In to add comment