Core is more or less finished, now it's time to compose
authorslack <slack@codemaniacs.com>
Fri, 17 Jul 2009 17:30:58 +0000 (19:30 +0200)
committerslack <slack@codemaniacs.com>
Fri, 17 Jul 2009 17:30:58 +0000 (19:30 +0200)
play.c
synth.c
synth.h
thesong.c

diff --git a/play.c b/play.c
index d8b29575da2bba3f854e6a73e0352fdbc735f62e..bc4d97c6d1d9ae7a1ff7d10f82f2e3c525a477f9 100644 (file)
--- a/play.c
+++ b/play.c
@@ -6,9 +6,9 @@
 #include <string.h>
 
 
-#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 cfd0d8c0d5311dbe40236492d75b7d09420a1afb..5ce011198025a10b8961f194213fb7263ec26cad 100644 (file)
--- a/synth.c
+++ b/synth.c
@@ -4,6 +4,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+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; i<len; i++) {
             float time=(v->pos+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 045e62e6d1733a096352762f65497cf57b903958..ea3a180e79e95fa7cf5be833da5abf093ef68186 100644 (file)
--- 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);
index 6cc4a08c4d8c8b968a9b6c3005263276543609c2..7c35ee161281a001218583f7e7bf68ecda66fbec 100644 (file)
--- 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