MOAR WIP! MOAR!
authorslack <slack@codemaniacs.com>
Mon, 15 Jun 2009 17:27:13 +0000 (19:27 +0200)
committerslack <slack@codemaniacs.com>
Mon, 15 Jun 2009 17:27:13 +0000 (19:27 +0200)
lolailo.c
play.c
song.h
synth.c
thesong.c

index 08de771ee06d16ff0a41a05a12a5f3979a5b9c8d..8f4e4846830d5ed1d30b0f80e3a411610c5776e2 100644 (file)
--- a/lolailo.c
+++ b/lolailo.c
@@ -17,13 +17,17 @@ SDL_AudioSpec desired={44100,
                        0,
                        (void (*)(void *, Uint8 *, int)) play,
                        &thesong};
+
+float drawbuf[BUFFER_SIZE];
+
 #ifdef DEBUG
 int main(int argc, char *argv[])
 #else
 void _start()
 #endif
 {
-    SDL_Init(SDL_INIT_AUDIO);
+    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
+    SDL_Surface *video = SDL_SetVideoMode(BUFFER_SIZE,480,32,SDL_SWSURFACE | SDL_DOUBLEBUF);
     SDL_OpenAudio(&desired, NULL);
     SDL_PauseAudio(0);
 
@@ -43,7 +47,51 @@ void _start()
     //MainWindow w(NULL);
     //w.show();
     //int error = a.exec();
-    getchar();
+    SDL_Event ev;
+    int paused=0;
+    while(1)
+    {
+        while (SDL_PollEvent(&ev))
+        {
+            if (ev.type == SDL_KEYDOWN)
+            {
+                if (ev.key.keysym.sym == SDLK_SPACE) {
+                    paused = !paused;
+                    SDL_PauseAudio(paused);
+                }
+                else
+                    goto end;
+            }
+
+            if (ev.type == SDL_QUIT) goto end;
+        }
+
+        if (!paused)
+        {
+            memset(video->pixels, 0, video->pitch*video->h);
+            uint32 *p = (uint32*) video->pixels;
+            int y0=240;
+            for (int i=0; i<BUFFER_SIZE; i++)
+            {
+                float v = drawbuf[i];
+                if (v>1.0f) v = 1.0f;
+                if (v<0.0f) v = 0.0f;
+
+                int y1 = (1.0f-v)*479.0f;
+                float ymin, ymax;
+                if (y0 < y1) { ymin=y0; ymax=y1; }
+                else { ymin=y1; ymax=y0; }
+
+                for (int y=ymin; y<ymax; y++)
+                    p[(video->pitch/4)*y+i] = 0x00FFFFFF;
+                p[(video->pitch/4)*y1+i] = 0x00FFFFFF;
+                y0=y1;
+            }
+            SDL_Flip(video);
+        }
+    }
+    
+end:
     SDL_Quit();
     _exit(0);
     //return error;
diff --git a/play.c b/play.c
index 21acb61a55958ab8ea86a96b127f3d76cb04b1a8..d8b29575da2bba3f854e6a73e0352fdbc735f62e 100644 (file)
--- a/play.c
+++ b/play.c
@@ -18,6 +18,8 @@ uint32 rows_played=0; // total of rows played
 uint8  cur_pos= 0;    // current position in the pattern playlist
 uint8  cur_row=-1;    // current row in the current pattern
 
+extern float drawbuf[BUFFER_SIZE];
+
 void play(void *s, uint8 *stream, int len)
 {
     Song *song = (Song*) s;
@@ -57,11 +59,11 @@ void play(void *s, uint8 *stream, int len)
         */
 
         for (uint8 i=0; i<NUM_CHANNELS; ++i) {
-            unsigned int chanval=song->patterns[cur_ptn][i];
-            if (chanval) {
-                unsigned int trknum=(chanval & 0x0F)-1;
+            unsigned int trknum=song->patterns[cur_ptn][i];
+            if (trknum) {
+                trknum--;
                 Track *cur_trk = &song->tracks[trknum];
-                uint8 trans = (chanval) >> 4;
+                int8 trans = song->transpose[cur_ptn][i];
 
                 unsigned int note = (cur_trk->notes[cur_row] & 0x0F);
                 if (note) {
@@ -91,13 +93,15 @@ void play(void *s, uint8 *stream, int len)
         }
     }
 
+    memcpy(drawbuf, mix_buf, num_samples*sizeof(float));
+
     // convertimos a Sint16
     for (int i=0; i<num_samples; ++i)
     {
         float tmp = mix_buf[i];
         if (tmp>1.0f) tmp=1.0f;
-        else if(tmp<-1.0f) tmp=-1.0f;
-        buf[i]=(int16)(tmp*32640.0f);
+        else if(tmp<0.0f) tmp=0.0f;
+        buf[i]=(int16)(tmp*64000.0f)-32000;
     }
 
 #ifdef OUTFILE
diff --git a/song.h b/song.h
index 0e023898042c542d6dd64f065d0675d6b2e1d134..c4af8dc1c61f26eb7ea7b172c5313a869f503b34 100644 (file)
--- a/song.h
+++ b/song.h
@@ -25,6 +25,7 @@ typedef struct
 typedef struct
 {
        uint8 patterns[NUM_PATTERNS][NUM_CHANNELS];
+        int8 transpose[NUM_PATTERNS][NUM_CHANNELS];
        Track tracks[NUM_TRACKS];
        uint8 instrument_data[INSTRUMENT_DATA_LENGTH];
        uint8 playlist[PLAYLIST_LENGTH];
diff --git a/synth.c b/synth.c
index 4898b250a02e4a5baf47cf629dc5f730ca0b4750..bd1f3f1a73795f01ee9ffeb94e23691804ab0786 100644 (file)
--- a/synth.c
+++ b/synth.c
@@ -37,7 +37,7 @@ uint8 synth(Voice *v, int len, float *mix_buf)
                 {
                     // op1: signed coarse detune, op2: signed fine detune
                     float pitch = detune(v->pitch, (int8) op1, (int8) op2);
-                    v->waves[wave][i] = sinf(pitch*TWOPI*time);
+                    v->waves[wave][i] = 0.5f*sinf(pitch*TWOPI*time)+0.5f;
                     break;
                 }
                 
@@ -45,28 +45,23 @@ uint8 synth(Voice *v, int len, float *mix_buf)
                 {
                     float pitch = detune(v->pitch, (int8) op1, 0);
                     float pw = op2/100.0f;
-                    float period = 1.0/pitch;
+                    float period = 1.0f/pitch;
                     if (fmodf(time, period) > (period*pw))
                         v->waves[wave][i] = 1.0f;
                     else
-                        v->waves[wave][i] = -1.0f;
+                        v->waves[wave][i] = 0.0f;
                     break;
                 }
                 case NOISE:
                     v->waves[wave][i] = rand()/(float)RAND_MAX;
                     break;
-                /*
-                case TO_0_1:
-                    v->waves[wave][i] = v->waves[wave][i-1]*0.5f+0.5f;
-                    break;
-                */
 
                 case SUM:
                 case MUL:
                 {
                     // op1,2: wave indices to sum/mul
                     if (ins == SUM)
-                        v->waves[wave][i] = v->waves[op1][i]+v->waves[op2][i];
+                        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;
index 6eb55a63ecf2034b2928b1413b86e4d60666fb50..bbeda3ee41ed6ec4837cef50d863f92d37b54a2e 100644 (file)
--- a/thesong.c
+++ b/thesong.c
@@ -5,13 +5,17 @@ Song thesong={
     {  // begin patterns
         {0x01, 0x00, 0x00, 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},
     }, // end tracks
     {  // begin instrument data
-        SINE,-70,0,
-        NOISE,0,0,
-        MUL,0,1,
+        SINE,24,0,
+        SQUARE,0,10,
+        SUM,0,1,
+        LPF,100,10,
         END,
     }, // end instrument data
     {  // begin playlist