All pastes #1003732 Raw Edit

anonymous

public text v1 · immutable
#1003732 ·published 2008-05-01 09:03 UTC
rendered paste body
/* gcc noise.c -o noise -lm */#include <math.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/time.h>#include <time.h>#include <string.h>#define error(msg) { fprintf(stderr, msg); fflush(stderr); _exit(1); }#define lhrandom(MIN,MAX) (((double)rand() / RAND_MAX) * ((MAX)-(MIN)) + (MIN))#define clamp(VAL,MIN,MAX) ((VAL)>(MAX) ? (MAX) : (VAL) < (MIN) ? (MIN) : (VAL))double sys_get_time (void){    struct timeval t;    gettimeofday(&t, 0);    return (double)t.tv_sec + ((double)t.tv_usec)/1000000.0;}#define SCALE 24#define BLAST 5.5int main (void){    int f = open("/dev/dsp", O_WRONLY);    if (f < 0)        error("Failed to open /dev/dsp for writing\n");    srand(time(NULL));    while (1)    {        uint i;        unsigned char b[SCALE];        double t = sys_get_time()*BLAST;        static unsigned char c = 0, inc = 1, c2 = 0;        static int k = 2;        memset(b, 0, sizeof(b));        if (c)        {            for (i = 0; i < sizeof(b) ;i+=k)                b[i] = (inc+1)*sin(t)*100 + lhrandom(0, k*100);            c = 0;        }        else        {            for (i = 0; i < sizeof(b) ;i+=k + inc - 1)                b[i] = k + cos(t)*k*5 + lhrandom(-32*inc, 32);            c = 1;        }        if (inc)        {            if (++k > 64)            {                inc = 0;                k = 64;            }        }        else        {            if (--k < 2)                inc = k = 2;        }        if (k == 64)        {            c2++;            if (c2 > 3)                c2 = 0;        }        if (sin(t/k) > 0.9 || BLAST*sin(t*4) > 0.8)        {            for (i = 0; i < SCALE ;i+=1+(k&9)+c2)                if (c)                    b[i] |= ~(int)(64*k*(inc ? cos(t*5) : sin(t*3)) + lhrandom(-100, 100));                else                    b[i] |=  (int)(64*k*(inc ? cos(t*5) : sin(t*3)) + lhrandom(-100, 100));        }        write(f, b, sizeof(b));        write(f, b + sizeof(b)/2 + k/3, sizeof(b)/2 - sizeof(b)/(1+inc));    }    close(f);    return 0;}