From 4329ed339a3072dc3108c96cadc1b19900576eab Mon Sep 17 00:00:00 2001 From: slack Date: Sat, 25 Jul 2009 05:11:12 +0200 Subject: [PATCH] checkpoint! :P --- lolailo.c | 31 +++++++++++- play.c | 79 ++++++++++++------------------ song.h | 4 +- synth.c | 13 ++--- synth.h | 3 +- thesong.c | 144 +++++++++++++++++++++++++++++++----------------------- 6 files changed, 152 insertions(+), 122 deletions(-) diff --git a/lolailo.c b/lolailo.c index 9c21641..5b1834f 100644 --- a/lolailo.c +++ b/lolailo.c @@ -29,6 +29,7 @@ extern uint8 cur_row; extern uint32 rows_played; extern float current_time; extern float row_duration; +extern Voice voices[NUM_CHANNELS]; #ifdef DEBUG int main(int argc, char *argv[]) @@ -39,6 +40,7 @@ void _start() #ifdef OUTFILE fout = fopen(OUTFILE, "wb"); #endif + //memset(voices, 0, NUM_CHANNELS*sizeof(Voice)); SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO); SDL_Surface *video = SDL_SetVideoMode(BUFFER_SIZE,480,32,SDL_SWSURFACE | SDL_DOUBLEBUF); @@ -63,6 +65,7 @@ void _start() //int error = a.exec(); SDL_Event ev; int paused=0; + float last=0; while(1) { while (SDL_PollEvent(&ev)) @@ -78,9 +81,10 @@ void _start() case SDLK_LEFT: cur_pos-=2; cur_row=TRACK_LENGTH-1; + last = 0; rows_played = cur_pos*TRACK_LENGTH; current_time = rows_played*row_duration; - printf("cur_pos = %d\n", cur_pos+1); + printf("cur_pos = %d, pattern %d\n", cur_pos+1, thesong.playlist[cur_pos+1]); break; case SDLK_RIGHT: @@ -88,7 +92,24 @@ void _start() cur_row=TRACK_LENGTH-1; rows_played = cur_pos*TRACK_LENGTH; current_time = rows_played*row_duration; - printf("cur_pos = %d\n", cur_pos+1); + printf("cur_pos = %d, pattern %d\n", cur_pos+1, thesong.playlist[cur_pos+1]); + break; + + case SDLK_DOWN: + cur_pos-=5; + cur_row=TRACK_LENGTH-1; + last = 0; + rows_played = cur_pos*TRACK_LENGTH; + current_time = rows_played*row_duration; + printf("cur_pos = %d, pattern %d\n", cur_pos+1, thesong.playlist[cur_pos+1]); + break; + + case SDLK_UP: + cur_pos+=4; + cur_row=TRACK_LENGTH-1; + rows_played = cur_pos*TRACK_LENGTH; + current_time = rows_played*row_duration; + printf("cur_pos = %d, pattern %d\n", cur_pos+1, thesong.playlist[cur_pos+1]); break; default: @@ -122,6 +143,12 @@ void _start() } SDL_Flip(video); SDL_Delay(20); + if ((current_time - last) > 1.0f) + { + last = current_time; + printf("\r\r\r\r\r\r%2d:%02d", (int)(current_time/60), (int)(current_time)%60); + fflush(stdout); + } } } diff --git a/play.c b/play.c index 34e86b9..f127f5f 100644 --- a/play.c +++ b/play.c @@ -6,17 +6,15 @@ #include -#define NUM_VOICES 20 - float row_duration=60.0f/320.0f; // denominator == RPM -Voice voices[NUM_VOICES]; +Voice voices[NUM_CHANNELS]; float mix_buf[BUFFER_SIZE]={0}; uint8 last_voice_used=0; float current_time=0.0f; uint32 rows_played=0; // total of rows played uint8 cur_pos= 255; // current position in the pattern playlist -int8 cur_row=-1; // current row in the current pattern +uint8 cur_row=TRACK_LENGTH-1; // current row in the current pattern extern float drawbuf[BUFFER_SIZE]; @@ -36,65 +34,48 @@ void play(void *s, uint8 *stream, int len) float time_since_last_row_played = current_time - rows_played * row_duration; if (time_since_last_row_played > row_duration ) { ++rows_played; - cur_row = (cur_row+1)%TRACK_LENGTH; - if (!cur_row) ++cur_pos; + ++cur_row; + if (cur_row == TRACK_LENGTH) + { + cur_row=0; + ++cur_pos; + } int buf_start = ((time_since_last_row_played+row_duration)*SAMPLE_RATE)-num_samples; - + if (song->playlist[cur_pos]==255) { // loop at end of song cur_pos=0; } + uint8 cur_ptn=song->playlist[cur_pos]; //printf("rows_played=%d, cur_pos=%d, cur_ptn=%d, cur_row=%d\n", rows_played, cur_pos, cur_ptn, cur_row); - /* - for (i=0; irhythms[i]; - if (steps & (1<<(15-cur_row))) { - voices[i].pitch = 440.0f*detune(song->rhythm_notes[i]-45,0); - voices[i].pos = buf_start; - voices[i].ins = &song->insts[song->rhythm_insts[i]]; - voices[i].vol = 0.1796875f; - voices[i].active = 1; - } - } - */ - for (uint8 i=0; ipatterns[cur_ptn][i]; - if (trknum) { - trknum--; - Track *cur_trk = &song->tracks[trknum]; - int8 trans = song->transpose[cur_ptn][i]; - - uint8 cell=cur_trk->notes[cur_row]; - if (cell) { - float vol = (cell>>4) * 0.125f; - unsigned int note = cell & 0x0F; - note+=trans; - - memset(&voices[i], 0, sizeof(Voice)); - voices[i].pitch = detune(440.0f,note-21,0); - voices[i].pos = buf_start; - voices[i].ins = &song->instrument_data[cur_trk->instrument]; - voices[i].vol = vol; - voices[i].active = 1; - } - } + Track *cur_trk = &song->tracks[trknum]; + int8 trans = song->transpose[cur_ptn][i]; + + uint8 cell=cur_trk->notes[cur_row]; + if (cell) { + float vol = (cell>>4) * 0.125f; + unsigned int note = cell & 0x0F; + note+=trans; + + memset(&voices[i], 0, sizeof(Voice)); + voices[i].pitch = detune(440.0f,note-21,0); + voices[i].pos = buf_start; + voices[i].ins = &song->instrument_data[cur_trk->instrument]; + voices[i].vol = vol; + } } - - } + // Ahora vamos sintetizando las voces - for (int i=0; i < NUM_VOICES; ++i) + for (int i=0; i < NUM_CHANNELS; ++i) { - if (voices[i].active) - { - voices[i].active=synth(&voices[i], num_samples, mix_buf); - } + synth(&voices[i], num_samples, mix_buf); } memcpy(drawbuf, mix_buf, num_samples*sizeof(float)); @@ -103,8 +84,8 @@ void play(void *s, uint8 *stream, int len) for (int i=0; i1.0f) tmp=1.0f; - else if(tmp<-1.0f) tmp=-1.0f; + //if (tmp>1.0f) tmp=1.0f; + //else if(tmp<-1.0f) tmp=-1.0f; buf[i]=(int16)(tmp*32767.0f); } diff --git a/song.h b/song.h index 2ae18f2..a0ae0a3 100644 --- a/song.h +++ b/song.h @@ -10,10 +10,10 @@ // + PLAYLIST_LENGTH #define NUM_PATTERNS 20 -#define NUM_CHANNELS 9 +#define NUM_CHANNELS 12 #define NUM_TRACKS 15 #define TRACK_LENGTH 12 -#define INSTRUMENT_DATA_LENGTH 200 +#define INSTRUMENT_DATA_LENGTH 300 #define PLAYLIST_LENGTH 50 typedef struct diff --git a/synth.c b/synth.c index 56b8d4c..e213aa5 100644 --- a/synth.c +++ b/synth.c @@ -16,8 +16,10 @@ float detune(float base, int coarse, int fine) return base*powf(2.0f, (coarse+(fine/100.0f))/12.0f); } -uint8 synth(Voice *v, int len, float *mix_buf) +void synth(Voice *v, int len, float *mix_buf) { + if (!v->ins) return; + if (v->pos < 0) { mix_buf -= v->pos; len += v->pos; @@ -72,17 +74,16 @@ uint8 synth(Voice *v, int len, float *mix_buf) break; case SUM: - v->waves[wave][i] = v->waves[wave-op1][i]+v->waves[wave-op2][i]; + v->waves[wave][i] = v->waves[wave-op1][i]+v->waves[wave-1][i]; break; case MUL: - v->waves[wave][i] = v->waves[wave-op1][i]*v->waves[wave-op2][i]; + v->waves[wave][i] = v->waves[wave-op1][i]*v->waves[wave-1][i]; break; case LPF: case HPF: { - // TODO: cutoff should be a wave - // op1, op2: cutoff, resonance + // op1, op2: cutoff(wave), resonance float cutoff = v->waves[wave-op1][i]; float r = (op2)/10.0f; //printf("op1=%d, op2=%d, cutoff=%f; r=%f\n", op1, op2, cutoff, r); @@ -139,7 +140,7 @@ end: v->pos+=len; for (int i=0; iwaves[wave-1][i]*v->vol; - return 1; + } diff --git a/synth.h b/synth.h index 856f366..9b635b4 100644 --- a/synth.h +++ b/synth.h @@ -33,7 +33,6 @@ typedef struct uint8 *ins; // instrument definition int pos; // position in samples since the start - uint8 active; // { 0 ==> inactive, (not 0) ==> active } // data buffer to store, for example, previous inputs/outputs for // filtering. It is as big as the wave buffer. Anyway, voices will be @@ -58,7 +57,7 @@ typedef struct #define ENDI 254 #define END 255 -extern uint8 synth(Voice *v, int len, float *mix_buf); +extern void synth(Voice *v, int len, float *mix_buf); extern float detune(float base, int coarse, int fine); extern float noise(); diff --git a/thesong.c b/thesong.c index 40a3f3d..97f4703 100644 --- a/thesong.c +++ b/thesong.c @@ -3,69 +3,74 @@ Song thesong={ { // begin patterns - {0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // wind 1 - {0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // wind 2 - {0x00, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00}, // wind 3 - {0x01, 0x02, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // bongo - {0x00, 0x00, 0x05, 0x07, 0x00, 0x00, 0x00, 0x00, 0x0B}, // bongo+bass1 - {0x00, 0x00, 0x05, 0x08, 0x00, 0x00, 0x00, 0x00, 0x0B}, // bongo+bass2 - {0x00, 0x00, 0x05, 0x09, 0x00, 0x00, 0x00, 0x00, 0x0B}, // bongo+bass3 - {0x00, 0x00, 0x05, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x0B}, // bongo+bass2end - {0x00, 0x00, 0x06, 0x06, 0x06, 0x06, 0x05, 0x07, 0x0B}, // piano chord 1+bongo+bass - {0x00, 0x00, 0x06, 0x06, 0x06, 0x06, 0x05, 0x08, 0x0B}, // piano chord 2+bongo+bass - {0x00, 0x00, 0x06, 0x06, 0x06, 0x06, 0x05, 0x09, 0x0B}, // piano chord 3+bongo+bass - {0x00, 0x00, 0x06, 0x06, 0x06, 0x06, 0x05, 0x0A, 0x0B}, // piano chord 2+bongo+bass_end - {0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0B}, // bongo+maraca + {0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0 - wind 1 + {0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 1 - wind 2 + {0x00, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 2 - wind 3 + {0x05, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 3 - bongo+wind + {0x05, 0x07, 0x00, 0x00, 0x00, 0x0d, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00}, // 4 - bongo+bass1 + {0x05, 0x07, 0x00, 0x00, 0x00, 0x0c, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00}, // 5 - bongo+bass2 + {0x05, 0x07, 0x00, 0x00, 0x00, 0x0c, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00}, // 6 - bongo+bass1 + {0x05, 0x07, 0x00, 0x00, 0x00, 0x00, 0x03, 0x02, 0x00, 0x00, 0x00, 0x00}, // 7 - bongo+bass2 + {0x05, 0x07, 0x06, 0x06, 0x06, 0x09, 0x0a, 0x0d, 0x0d, 0x00, 0x00, 0x00}, // 8 - bongo+piano+bass1+melody1 + {0x05, 0x07, 0x06, 0x06, 0x06, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00}, // 9 - bongo+piano+bass1+melody2 + {0x05, 0x07, 0x06, 0x06, 0x06, 0x09, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00}, // 10 - bongo+piano+bass1+melody3 + {0x05, 0x07, 0x06, 0x06, 0x06, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 11 - bongo+piano+bass1+melody4 + {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 }, // end patterns { // begin txpose {-15, 0, 0, 0, 0}, // wind1 {-9, 0, -3, 0, 0}, // wind2 {0, -20, 0, -5, 0}, // wind3 - {-15,0,24,0,0}, // bongo - {-15,0,24,2,0}, // bongo+bass1 - {-15,0,24,2,0}, // bongo+bass2 - {-15,0,24,2,0}, // bongo+bass3 - {-15,0,24,2,0}, // bongo+bass2end - {-15, 0, 7, 12, 15, 19, 24, 2}, // piano chord 1+bongo+bass - {-15, 0, 7, 11, 14, 19, 24, 2}, // piano chord 2+bongo+bass - {-15, 0, 8, 12, 15, 20, 24, 2}, // piano chord 3+bongo+bass - {-15, 0, 7, 11, 14, 19, 24, 2}, // piano chord 2+bongo+bass - {-15,0,24,0,0}, // bongo+maraca + {24,-15,0,0,0}, // bongo + {24,9,7,11,14,21,-15,-9}, // bongo+bass1 + {24,9,7,11,14,23,-15,-3}, // bongo+bass1 + {24,6,7,11,16,21,-15,-9}, // bongo+bass1 + {24,6,7,11,16,21,-3,-9}, // bongo+bass1 + {24,9,7,11,14,21,21,21,21}, // bongo+piano+bass1+melody1 + {24,9,7,11,14,21,23,23}, // bongo+piano+bass1+melody2 + {24,6,7,11,16,21,21,21}, // bongo+piano+bass1+melody3 + {24,6,7,11,16,21,21,21}, // bongo+piano+bass1+melody4 + {24,9,7,11,14}, // bongo+bass1+piano + {24,6,7,11,16}, // bongo+bass1+piano }, // end txpose { // begin tracks - {{0x44,0,0,0,0,0,0,0,0,0,0,0}, 19}, // wind - {{0,0,0,0,0,0,0x31,0,0,0,0,0}, 19}, // wind - {{0,0,0,0,0,0,0,0,0,0,0x21,0}, 19}, // wind - {{0,0,0,0x51,0,0,0,0,0,0,0,0}, 19}, // wind - {{0x94,0x54,0x14,0x94,0x54,0x14,0x94,0x54,0x14,0x94,0x54,0x14,}, 0}, // bongo rhythm - {{0xf1,0,0xf1,0,0xf1,0xf1,0,0xf1,0,0xf1,0,0xf1}, 53}, // piano rhythm - {{0x37,0,0,0x3e,0,0,0x37,0,0x3e,0,0x32,0,}, 93}, // bassline 1 - {{0x32,0,0,0x3e,0,0,0x32,0,0x3e,0,0x39,0,}, 93}, // bassline 2 - {{0x33,0,0,0x3f,0,0,0x33,0,0x3f,0,0x3a,0,}, 93}, // bassline 3 - {{0x32,0,0,0x3e,0,0,0x32,0x3e,0,0x3c,0,0x3a}, 93}, // bassline 2+end - {{0xf0,0x70,0xf0,0x70,0xf0,0x70,0xf0,0x70,0xf0,0x70,0xf0,0x70}, 115}, // maraca + {{0,0,0,0,0,0,0,0,0,0,0,0}, 0}, + {{0x44,0,0,0,0,0,0,0,0,0,0,0}, 19}, // 1 - wind + {{0,0,0,0,0,0,0x31,0,0,0,0,0}, 19}, // 2 - wind + {{0,0,0,0,0,0,0,0,0,0,0x21,0}, 19}, // 3 - wind + {{0,0,0,0x51,0,0,0,0,0,0,0,0}, 19}, // 4 - wind + {{0,0,0,0xb4,0x74,0xa4,0,0,0,0xa4,0,0xa4,}, 0}, // 5 - bongo rhythm + {{0,0,0,0xf1,0,0xb1,0,0,0,0xf1,0,0}, 53}, // 6 - piano rhythm + {{0x57,0,0x17,0x37,0,0,0x52,0,0,0x37,0,0,}, 93}, // 7 - bassline 1 + {{0x54,0,0x14,0x34,0,0,0x59,0,0,0x39,0,0,}, 93}, // 8 - bassline 2 (FREE) + {{0x6b,0,0,0x6b,0,0,0x6b,0,0,0x6c,0,0}, 115}, // 9 + {{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}, 171}, // d }, // end tracks { // begin instrument data // 0 (bongo) SIGMOID,10,1, SQUARE,0,20, LPF,2,40, - MUL,1,3, + MUL,3,0, CONST,10,0, - MUL,1,2, + MUL,2,0, ENDI, // 19 (wind) SIGMOID,2,15, SIGMOID,-10,6, - MUL,1,2, + MUL,2,0, SINE,-110,200, CONST,0,5, - MUL,1,2, + MUL,2,0, CONST,0,10, - SUM,1,2, + SUM,2,0, NOISE,0,0, LPF,2,10, - MUL,1,8, + MUL,8,0, ENDI, // 53 (piano-ish) CONST,0,30, @@ -74,40 +79,57 @@ Song thesong={ SQUARE,0,15, SQUARE,12,10, SQUARE,-12,20, - SUM,1,2, - SUM,1,4, - MUL,1,6, + SUM,2,0, + SUM,4,0, + MUL,6,0, HPF,8,20, LPF,10,20, - CONST,0,40, - MUL,1,2, + CONST,0,70, + MUL,2,0, ENDI, // 93 (bass) SIGMOID, 8,3, SINE,-20,20, SINE,-8,10, - SUM,1,2, + SUM,2,0, CONST,0,80, - MUL,1,2, - MUL,1,6, + MUL,2,0, + MUL,6,0, ENDI, - // 115 (maraca) - CONST,0,1, - SIGMOID,30,1, - NOISE,0,0, - HPF,3,20, - NOISE,0,0, - LPF,5,0, - MUL,1,6, - SUM,1,4, - MUL,1,7, - CONST,10,0, - MUL,1,2, + // 115 (lead) + CONST,0,30, + SIGMOID, 8,4, + SQUARE,-20,50, + SQUARE,-8,10, + SUM,2,0, + LPF,5,20, + CONST,0,20, + MUL,2,0, + MUL,7,0, + ENDI, + // 143 (lead) + CONST,0,30, + SIGMOID, 3,10, + SQUARE,-20,50, + SQUARE,-8,10, + SUM,2,0, + LPF,5,20, + CONST,0,10, + MUL,2,0, + MUL,7,0, + ENDI, + // 171 (pad) + CONST,0,80, + SIGMOID, 1,14, + SINE,-8,0, + CONST,0,5, + MUL,2,0, + MUL,4,0, END, }, // end instrument data { // begin playlist - 0,1,2,1,3,3,3,3,8,8,9,9,10,10,9,11,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,255, }, // end playlist }; -- 2.34.1