From: slack Date: Fri, 17 Jul 2009 17:30:58 +0000 (+0200) Subject: Core is more or less finished, now it's time to compose X-Git-Url: http://slack.codemaniacs.com/git/?a=commitdiff_plain;h=b946924bd332a0ada6c85171ecacdd6c62b53e36;p=lolailo.git Core is more or less finished, now it's time to compose --- diff --git a/play.c b/play.c index d8b2957..bc4d97c 100644 --- a/play.c +++ b/play.c @@ -6,9 +6,9 @@ #include -#define NUM_VOICES 1 +#define NUM_VOICES 3 -float row_duration=60.0f/360.0f; // denominator == RPM +float row_duration=60.0f/570.0f; // denominator == RPM Voice voices[NUM_VOICES]; float mix_buf[BUFFER_SIZE]={0}; uint8 last_voice_used=0; @@ -100,8 +100,8 @@ void play(void *s, uint8 *stream, int len) { float tmp = mix_buf[i]; if (tmp>1.0f) tmp=1.0f; - else if(tmp<0.0f) tmp=0.0f; - buf[i]=(int16)(tmp*64000.0f)-32000; + else if(tmp<-1.0f) tmp=-1.0f; + buf[i]=(int16)(tmp*32767.0f); } #ifdef OUTFILE diff --git a/synth.c b/synth.c index cfd0d8c..5ce0111 100644 --- a/synth.c +++ b/synth.c @@ -4,6 +4,8 @@ #include #include +extern float current_time; + float detune(float base, int coarse, int fine) { return base*powf(2.0f, (coarse+(fine/100.0f))/12.0f); @@ -26,19 +28,23 @@ uint8 synth(Voice *v, int len, float *mix_buf) //printf("wave %d, ins=%d (%d, %d)\n", wave, ins, op1, op2); for (int i=0; ipos+i)*invSAMPLE_RATE; + float global_time = current_time+i*invSAMPLE_RATE; switch (ins) { case CONST: { - float value = ((int8) op1/100.0); + float value = ((int8) op1/10.0); v->waves[wave][i] = value; break; } case SINE: { - // op1: signed coarse detune, op2: signed fine detune - float pitch = detune(v->pitch, (int8) op1, (int8) op2); - v->waves[wave][i] = 0.5f*sinf(pitch*TWOPI*time)+0.5f; + // op1: signed coarse detune, op2: use global time + float pitch = detune(v->pitch, (int8) op1, 0); + if (op2) + v->waves[wave][i] = sinf(pitch*TWOPI*global_time); + else + v->waves[wave][i] = sinf(pitch*TWOPI*time); break; } @@ -50,30 +56,26 @@ uint8 synth(Voice *v, int len, float *mix_buf) if (fmodf(time, period) > (period*pw)) v->waves[wave][i] = 1.0f; else - v->waves[wave][i] = 0.0f; + v->waves[wave][i] = -1.0f; break; } case NOISE: - v->waves[wave][i] = rand()/(float)RAND_MAX; + v->waves[wave][i] = 2.0f*(rand()/(float)RAND_MAX)-1.0f; break; case SUM: + v->waves[wave][i] = v->waves[wave-op1][i]+v->waves[wave-op2][i]; + break; case MUL: - { - // op1,2: wave indices to sum/mul - if (ins == SUM) - v->waves[wave][i] = 0.5f*(v->waves[op1][i]+v->waves[op2][i]); - else - v->waves[wave][i] = v->waves[op1][i]*v->waves[op2][i]; - break; - } + v->waves[wave][i] = v->waves[wave-op1][i]*v->waves[wave-op2][i]; + break; case LPF: case HPF: { // TODO: cutoff should be a wave // op1, op2: cutoff, resonance - float cutoff = v->waves[op1][i]; + float cutoff = v->waves[wave-op1][i]; float r = (op2)/5.0f; //printf("op1=%d, op2=%d, cutoff=%f; r=%f\n", op1, op2, cutoff, r); @@ -97,6 +99,14 @@ uint8 synth(Voice *v, int len, float *mix_buf) //printf("AFTER: input=%f, pole1=%f, pole2=%f, pole3=%f, pole4=%f\n", input, data[0], data[1], data[2], data[3]); break; } + case SIGMOID: + { + float f = (float)((int8)op1); + float d = ((int8)op2)/10.0f; + v->waves[wave][i] = 1.0f - 1.0f/(1.0f+expf(-f*(time-d))); + break; + } + default: goto end; } diff --git a/synth.h b/synth.h index 045e62e..ea3a180 100644 --- a/synth.h +++ b/synth.h @@ -44,17 +44,17 @@ typedef struct float waves[MAX_OPS][BUFFER_SIZE]; } Voice; -#define CONST 0 -#define SINE 1 -#define SQUARE 2 -#define SAW 3 -#define SUM 4 -#define MUL 5 -#define LPF 6 -#define HPF 7 -#define TO_0_1 8 -#define NOISE 9 -#define END 255 +#define CONST 0 +#define SINE 1 +#define SQUARE 2 +#define SAW 3 +#define SUM 4 +#define MUL 5 +#define LPF 6 +#define HPF 7 +#define NOISE 8 +#define SIGMOID 9 +#define END 255 extern uint8 synth(Voice *v, int len, float *mix_buf); extern float detune(float base, int coarse, int fine); diff --git a/thesong.c b/thesong.c index 6cc4a08..7c35ee1 100644 --- a/thesong.c +++ b/thesong.c @@ -3,19 +3,18 @@ Song thesong={ { // begin patterns - {0x01, 0x00, 0x00, 0x00}, + {0x01, 0x02, 0x03, 0x00}, }, // end patterns { // begin txpose {-12, 0, 0, 0}, }, // end txpose { // begin tracks - {{45,0,0,0,43,0,0,0,41,0,0,0,40,0,0,0}, 0}, + {{45,0,0,0,45,0,0,0,45,0,0,0,45,0,0,0}, 0}, }, // end tracks { // begin instrument data - SINE,-80,0, - SQUARE,0,50, - LPF,0,6, - MUL,0,2, + SIGMOID,15,0, + NOISE,0,30, + LPF,2,40, END, }, // end instrument data { // begin playlist