Tiny Drums

Tiny Drums

Tiny program that writes a synthesized drumloop to a WAV file.

<<tindrums_wav.c>>=
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define SR 44100
#define F float
F P(F p,F f){p+=f/SR;while(p>=1.0)p--;return p;}F S(F
p){return sin(p*2*M_PI);}F C(F x){return x*x*x;}F L(F x,F
mn,F mx){return x*(mx-mn)+mn;}F D(F x,F d){if (x>d) return
1;else return x/d;}F N(void){return rand()/(F)RAND_MAX;}F
K(F x,int d){return fmod(x*d,1.0);}F G(F x,int s,int m){int
p;p=floor(x*s);return ((1<<p) & m)>0;}F H(F x,F *y,F c){F
o;*y=o=c*((*y)+x);*y=o-x;return o;}char
wh[]={0x52,0x49,0x46,0x46,0xc4,0xea,0x1a,0x00,0x57,0x41,0x56
,0x45,0x66,0x6d,0x74,0x20,0x10,0x00,0x00,0x00,0x03,0x00,0x01
,0x00,0x44,0xac,0x00,0x00,0x10,0xb1,0x02,0x00,0x04,0x00,0x20
,0x00,0x64,0x61,0x74,0x61,0xa0,0xea,0x1a,0x00,0xa0,0x2f,0x40
,0xbc};int main(void){unsigned long n;unsigned long s;FILE
*fp;F inc;F t;F m;F p;F f;F y;s=SR*10;fp=fopen("o.wav","w");
t=0;inc=1.0/SR;n=0;p=0;m=0;f =2.0-cos(8000*2*M_PI*inc);
f=f-sqrt(f*f-1.0);y=0;fwrite(wh,1,48,fp);while(n < s){F
o,c,d;t=n*inc;m=P(m,0.5);c=K(m,4);d=K(m,16);p=P(p,L(C(1-D
(c,0.1)),60,500));o=S(p)*C(1-c)*G(m,4,0xF);o+=L(N(),-1,1)*C
(1-D(c,.9))*G(m,4,0xa);o+=H(L(N(),-1,1)*C(1-D(d,.3)),&y,f);
o *=.3;n++;fwrite(&o,sizeof(F),1,fp);}fclose(fp);return 0;}

It should sound something like this:


home | index