Almost final version, HORA DE LAS REBAJAS!
authorslack <slack@codemaniacs.com>
Sat, 25 Jul 2009 13:05:25 +0000 (15:05 +0200)
committerslack <slack@codemaniacs.com>
Sat, 25 Jul 2009 13:05:25 +0000 (15:05 +0200)
play.c
play.h
synth.c
synth.h
thesong.c

diff --git a/play.c b/play.c
index 0aa708851cc76a1e915954bd86f11675167b1df1..91a79bb98c44a7b5e2d2c1d3dcc93b365baea07f 100644 (file)
--- a/play.c
+++ b/play.c
@@ -78,7 +78,9 @@ void play(void *s, uint8 *stream, int len)
             synth(&voices[i], num_samples, mix_buf);
     }
 
+#ifdef IM_IN_UR_PREPROCESER_DRAWIN_UR_WAVES
     memcpy(drawbuf, mix_buf, num_samples*sizeof(float));
+#endif
 
     // convertimos a Sint16
     for (int i=0; i<num_samples; ++i)
diff --git a/play.h b/play.h
index e9f0c11f18b27543701b4e10e7f917d20c1130c6..c781062ac1a3b6efaf16245a0eddd33cebcb378c 100644 (file)
--- a/play.h
+++ b/play.h
@@ -4,6 +4,9 @@
 #include "sizedtypes.h"
 
 void play(void *songdata, uint8 *stream, int len);
+extern float current_time;
+
 //#define OUTFILE "salida.raw"
+#define IM_IN_UR_PREPROCESER_DRAWIN_UR_WAVES
 
 #endif // PLAY_H
diff --git a/synth.c b/synth.c
index e213aa562248f4af651fc5340c95b53c0e0d0022..06ca91a412772e2961ff60b3463c13ce981188a1 100644 (file)
--- a/synth.c
+++ b/synth.c
@@ -34,13 +34,14 @@ void synth(Voice *v, int len, float *mix_buf)
         uint8 op2 = v->ins[iPC++];
         //printf("wave %d, ins=%d (%d, %d)\n", wave, ins, op1, op2);
         for (int i=0; i<len; i++) {
+            float pitch = detune(v->pitch, (int8) op1, 0);
             float time=(v->pos+i)*invSAMPLE_RATE;
             //float global_time = current_time+i*invSAMPLE_RATE;
             switch (ins) {
                 case CONST:
                     {
                         // op1: integer part, op2: decimal part * 100
-                        float value = ((int8) op1) + ((int8) op2/100.0);
+                        float value = ((int8) op1) + ((int8) op2/100.0f);
                         v->waves[wave][i] = value;
                         break;
                     }
@@ -48,7 +49,6 @@ void synth(Voice *v, int len, float *mix_buf)
                 case SINE:
                     {
                         // op1: signed coarse detune, op2: phase (0-255)
-                        float pitch = detune(v->pitch, (int8) op1, 0);
                         v->waves[wave][i] = sinf(pitch*TWOPI*time+op2*PI/255.0f);
                         //if (op2)
                         //    v->waves[wave][i] = sinf(pitch*TWOPI*global_time);
@@ -60,15 +60,28 @@ void synth(Voice *v, int len, float *mix_buf)
                 case SQUARE:
                     {
                         // op1: signed coarse detune, op2: pulse width (%)
-                        float pitch = detune(v->pitch, (int8) op1, 0);
                         float pw = op2/100.0f;
                         float period = 1.0f/pitch;
-                        if (fmodf(time, period) > (period*pw))
+                        if (fmodf(time, period) < (period*pw))
                             v->waves[wave][i] = 1.0f;
                         else
                             v->waves[wave][i] = -1.0f;
                         break;
                     }
+                case TRIANGLE:
+                    {
+                        float pw = op2/100.0f;
+                        float period = 1.0f/pitch; // wave period (in seconds)
+                        float half_period = pw*period;
+                        float t = fmodf(time,period);
+                        if (t < half_period)
+                            v->waves[wave][i] = 2.0f*(t/half_period)-1.0f;
+                        else {
+                            t-=half_period;
+                            v->waves[wave][i] = 1.0f-2.0f*t/(period-half_period);
+                        }
+                        break;
+                    }
                 case NOISE:
                     v->waves[wave][i] = noise();
                     break;
@@ -113,22 +126,22 @@ void synth(Voice *v, int len, float *mix_buf)
                         v->waves[wave][i] = 1.0f - 1.0f/(1.0f+expf(-f*(time-d)));
                         break;
                     }
-                    
-                /*
 
-                case DELAY:
-                    {
-                        // op1: delay (ms), op2: volume (%)
-                        uint32 delay = op1*SAMPLE_RATE/1000.0f;
-                        float *data = (float*)(v->data[wave]+4);
-                        int *p      = (int*)(v->data[wave]);
-                        v->waves[wave][i] = (op2/100.0f)*data[*p];
-                        // circular buffer
-                        data[*p] = v->waves[wave-1][i];
-                        *p = (*p + 1)%delay;
-                        break;
-                    }
-                */
+                    /*
+
+                       case DELAY:
+                       {
+                    // op1: delay (ms), op2: volume (%)
+                    uint32 delay = op1*SAMPLE_RATE/1000.0f;
+                    float *data = (float*)(v->data[wave]+4);
+                    int *p      = (int*)(v->data[wave]);
+                    v->waves[wave][i] = (op2/100.0f)*data[*p];
+                    // circular buffer
+                    data[*p] = v->waves[wave-1][i];
+                     *p = (*p + 1)%delay;
+                     break;
+                     }
+                     */
                 default:
                     goto end;
             }
diff --git a/synth.h b/synth.h
index 9b635b4159ee522025d2a484e1d2dc8b229e59a8..19118128ff764f0d16ac565fd99c1f1b374a19f5 100644 (file)
--- a/synth.h
+++ b/synth.h
@@ -46,7 +46,7 @@ typedef struct
 #define CONST    0
 #define SINE     1
 #define SQUARE   2
-#define SAW      3
+#define TRIANGLE 3
 #define SUM      4
 #define MUL      5
 #define LPF      6
index 867a3d90d2c38c7503f0de07eb04fe3b5488e397..0cba04888a7005dfd31c33af379116a436699709 100644 (file)
--- a/thesong.c
+++ b/thesong.c
@@ -18,7 +18,8 @@ Song thesong={
         {0x05, 0x07, 0x06, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 12 - bongo+bass1+piano
         {0x05, 0x07, 0x06, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 13 - bongo+bass1+piano
         {0x05, 0x07, 0x06, 0x06, 0x06, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 11 - bongo+piano+bass1+melody5
-        {0x05, 0x07, 0x06, 0x06, 0x06, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 11 - bongo+piano+bass1+melody5
+        {0x05, 0x07, 0x06, 0x06, 0x06, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 11 - bongo+piano+bass1+melody6
+        {0x05, 0x07, 0x06, 0x06, 0x06, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 11 - bongo+piano+bass1+melody7
     }, // end patterns
     {  // begin txpose
         {-15, 0, 0, 0, 0}, // wind1
@@ -37,6 +38,7 @@ Song thesong={
         {24,6,7,11,16}, // bongo+bass1+piano
         {24,9,7,11,14,25,}, // bongo+piano+bass1+melody5
         {24,9,7,11,14,25,}, // bongo+piano+bass1+melody6
+        {24,6,7,11,16,25,}, // bongo+piano+bass1+melody6
     }, // end txpose
     {  // begin tracks
        {{0,0,0,0,0,0,0,0,0,0,0,0}, 0},
@@ -52,17 +54,18 @@ Song thesong={
        {{0,0,0,0,0,0,0,0,0,0,0,0x6e}, 143}, // a
        {{0x6b,0,0x69,0,0,0x67,0,0,0,0,0,0}, 115}, // b
        {{0,0,0,0,0,0,0,0,0,0,0,0x6e}, 171}, // c
-       {{0x6e,0,0,0,0,0x6c,0x6b,0,0x6c,0,0,0x6e}, 115}, // d
+       {{0x6e,0,0,0,0,0x6c,0x6b,0,0x6c,0,0,0x6e}, 171}, // d
        {{0x6f,0,0,0x6e,0,0,0x6c,0,0,0x6a,0,0}, 115}, // e - melody 5
        {{0x67,0,0,0x65,0,0x63,0,0,0,0,0,0}, 115}, // f - melody 6
+       {{0x6c,0,0,0x60,0,0x60,0,0,0x62,0x63,0,0}, 115}, // 10 - melody 7
     }, // end tracks
     {  // begin instrument data
-        // 0 (bongo)
+       // 0 (bongo)
        SIGMOID,10,1,
-        SQUARE,0,20,
+        SINE,0,20,
         LPF,2,40,
         MUL,3,0,
-       CONST,10,0,
+       CONST,8,0,
        MUL,2,0,
         ENDI,
        // 19 (wind)
@@ -82,7 +85,7 @@ Song thesong={
        CONST,0,30,
        CONST,0,10,
        SIGMOID,10,1,
-       SQUARE,0,15,
+       TRIANGLE,0,15,
        SQUARE,12,10,
        SQUARE,-12,20,
        SUM,2,0,
@@ -95,12 +98,13 @@ Song thesong={
        ENDI,
        // 93 (bass)
        SIGMOID, 8,3,
-       SINE,-20,20,
+       TRIANGLE,-20,1,
        SINE,-8,10,
        SUM,2,0,
+       LPF,4,0,
        CONST,0,80,
        MUL,2,0,
-       MUL,6,0,
+       MUL,7,0,
        ENDI,
        // 115 (lead)
        CONST,0,30,
@@ -127,7 +131,7 @@ Song thesong={
        // 171 (pad)
        CONST,0,80,
        SIGMOID, 1,14,
-       SINE,-8,0,
+       TRIANGLE,-8,50,
        CONST,0,5,
        MUL,2,0,
        MUL,4,0,
@@ -135,7 +139,7 @@ Song thesong={
 
     }, // end instrument data
     {  // begin playlist
-        0,1,2,1,3,3,3,3,4,5,6,7,4,5,6,7,8,9,10,11,8,9,10,11,12,12,13,13,12,12,13,13,14,15,14,15,255,
+       0,1,2,1,3,3,3,3,4,5,6,7,4,5,6,7,8,9,10,11,8,9,10,11,12,12,13,13,12,12,13,13,14,15,16,13,14,15,16,13,14,15,16,13,14,15,16,13,255,
     }, // end playlist
 };