order make
[ITMCK.git] / itmck.c
blobe5df966f35024d5f9fe73495201621e692a70752
1 /*
2 ITMCK
3 Version 0.1
4 Licensed under GNU GPL v3 or later version
5 */
7 #define itmck_version "ITMCK v0.1"
8 #define itmck_version_code 0x7010
9 #define impulse_tracker_version_code 0x0204
11 #include <fcntl.h>
12 #include <math.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
17 #define FX(x) ((x)&0x1F)
18 #define FXC(x) (FX(x)+26)
19 #define C_pitch 261.6255653
20 #define tau (2.0*M_PI)
21 #define rand_double() (2.0 * ((double) rand() / (double)RAND_MAX - 0.5))
22 #define unequal3(x,y,z) ((x)!=(y) && (x)!=(z) && (y)!=(z))
23 #define unequal3lt(x,y,z) ((x)!=(y) && (x)<(z) && (y)<(z))
24 #define ITout stdout
26 typedef unsigned char byte;
27 typedef byte boolean;
29 typedef struct chan_info {
30 short transpose;
31 byte octave;
32 short length;
33 byte instrument;
34 byte effectid; // 0=not set
35 byte effectpar;
36 byte volumeset; // 255=not set
37 short quantize; // <0=deduct frames >0=fraction of time
38 byte note_cuts; // 255=off, 254=cut, 253=fade, 250=continue
39 byte ch_vol; // use for .IT header: 0-64
40 byte ch_pan; // use for .IT header: 0-64, 32=center, 100=surround
41 int row;
42 int row_cut; // use for slurs/ties and auto portamento
43 int row_tie; // use for ties
44 int loopstart;
45 int loopskip;
46 int loopend;
47 byte keytable; // 255=unuse
48 byte customfx; // 255=unuse
49 boolean auto_portamento; // set if next command should auto portamento
50 byte prev_note; // use for tie and auto portamento
51 } chan_info;
53 typedef struct message_line {
54 char*data;
55 struct message_line*next;
56 } message_line;
58 typedef struct chan_row {
59 byte note; // 200=unuse
60 byte instrument; // 255=unuse
61 byte effectid; // 255=unuse
62 byte effectpar;
63 byte volumeset; // 255=unuse
64 byte continue_flag; // 0x01=effect 0x02=volume 0x04=zero next effect 0x80=used
65 } chan_row;
67 typedef struct chan_row_block {
68 chan_row data[17]; // data[16]=global effects
69 boolean used; // keep track of which row contains data
70 } chan_row_block;
72 typedef struct key_setting {
73 byte note;
74 byte instrument; // 255=unuse
75 byte effectid; // 0=unuse
76 byte effectpar;
77 byte volumeset; // 255=unuse
78 byte customfx; // 255=unuse
79 } key_setting;
81 typedef struct instrument_data {
82 byte heading[11]; // address 0x0011 to 0x001B
83 byte ifc; // cutoff 0-127; bit7=enable
84 byte ifr; // resonance 0-127; bit7=enable
85 byte keyboard[240];
86 byte vol_env[82];
87 byte pan_env[82];
88 byte pitch_env[82]; // bit7 of flag enables filter envelope
89 boolean used; // set if instrument is selected in MML
90 } instrument_data;
92 typedef struct sample_data {
93 byte global_volume;
94 byte flag;
95 byte default_volume;
96 byte convert;
97 byte pan;
98 int length;
99 int loop_start;
100 int loop_end;
101 int c5speed;
102 int sustain_start;
103 int sustain_end;
104 byte vibrato[4];
105 byte make; // Mode to make data: 1=raw-file 2=synth 3=copy 4=IMPS-file
106 byte link; // Depend on 'make'
107 char*name; // Filename or depend on 'make'
108 boolean used; // set when compiling instruments, before compiling samples
109 } sample_data;
111 typedef struct custom_fx {
112 short transpose; // 255=omit note
113 byte instrument; // 255=unuse
114 byte effectid; // 0=unuse
115 byte effectpar;
116 byte volumeset; // 255=unuse
117 byte continue_flag; // 0x01=effect 0x02=volume 0x04=zero next effect 0x80=continue pass note
118 int delta;
119 struct custom_fx*next;
120 } custom_fx;
122 byte it_flags=0x0E;
123 chan_info channels[16];
124 message_line*song_message;
125 message_line**next_song_message=&song_message;
126 int song_message_length=0;
127 short octave_dir=1;
128 char song_title[26];
129 byte gate_denom=8;
130 byte whole_note=48;
131 byte panning_separation=128;
132 byte init_tempo=120;
133 char scale_base='c';
134 char*macro_text[256][16];
135 byte g_vol=128;
136 byte g_mixer=48;
137 key_setting keytab[64][120];
138 custom_fx fxtab[64];
139 unsigned short tracker_version=itmck_version_code; // Cwt
140 sample_data samples[100];
141 instrument_data instruments[100];
142 byte max_sample=0;
143 byte max_instrument=0;
144 byte compatibility=0; // 0=Impulse Tracker
145 int numeric_register[27]; // register 0 ('`') is special
146 char*filename_prefix="";
147 byte export_sample=0;
148 chan_row_block*frames;
149 int max_frames=0;
150 boolean synth16bits=0;
151 int synth_len_mult=1;
152 boolean trace_input=0;
153 unsigned long out_offset=0x00000000UL;
155 // A ' B | C ' D ' E | F ' G ' (h) (i) (j)
156 short note_letter[10] = { -3, -1, 0, 2, 4, 5, 7, 0, 0, 0 };
157 int octave_tones=12;
159 //Predeclare
160 void read_file(FILE*fp);
162 inline void write8(int x) {
163 putchar(x&0xFF);
166 inline void write16(int x) {
167 putchar(x&0xFF);
168 putchar((x>>8)&0xFF);
171 inline void write32(int x) {
172 putchar(x&0xFF);
173 putchar((x>>8)&0xFF);
174 putchar((x>>16)&0xFF);
175 putchar((x>>24)&0xFF);
178 int read_int(char**s) {
179 char c;
180 int r=0;
181 int q=1;
182 int b=10;
183 if(**s==',') (*s)++;
184 if(**s=='$' && (*s)[1]>='`' && (*s)[1]<='z') return numeric_register[(*s)[1]&0x1F];
185 if(**s=='+') (*s)++;
186 if(**s=='-') { q=-1; (*s)++; }
187 if(**s=='$') { b=16; (*s)++; }
188 while((**s>='0' && **s<='9') || (b==16 && **s>='A'&& **s<='F')) {
189 c=*((*s)++);
190 r=r*b+c-('0'+7*(c>='A'));
192 return q*r;
195 int read_note(char**s) {
196 int x;
197 if(**s=='n') {
198 ++*s;
199 return read_int(s);
201 if(**s<'a' || **s>'j') return -1;
202 x=*((*s)++);
203 x=note_letter[x-'a']+octave_tones*(x<scale_base);
204 while(**s) {
205 if(**s=='+') { x++; ++*s; }
206 else if(**s=='-') { x--; ++*s; }
207 else if(**s=='\'') { x+=octave_tones; ++*s; }
208 else if(**s=='"') { x+=octave_tones*2; ++*s; }
209 else break;
211 return x+octave_tones*read_int(s);
214 void skip_spaces(char**s) {
215 if(!s || !*s || !**s) return;
216 while(**s && **s<=' ') (*s)++;
219 void skip_equals_brace(char**s) {
220 if(!s || !*s || !**s) return;
221 while(**s && (**s<=' ' || **s=='=')) (*s)++;
222 if(**s=='{') (*s)++;
223 while(**s && **s<=' ') (*s)++;
226 void allocate_frames(int n) {
227 int x;
228 if(n<=max_frames) return;
229 if(n<0 || !(frames=realloc(frames,sizeof(chan_row_block)*n))) {
230 fprintf(stderr,"*FATAL* Out of frame memory");
231 exit(1);
233 while(max_frames<n) {
234 frames[max_frames].used=0;
235 for(x=0;x<17;x++) {
236 frames[max_frames].data[x].note=200;
237 frames[max_frames].data[x].effectid=frames[max_frames].data[x].instrument=frames[max_frames].data[x].volumeset=255;
238 frames[max_frames].data[x].continue_flag=0;
240 max_frames++;
244 void send_channel(byte ch,byte note,byte instrument,byte effectid,byte effectpar,byte volumeset,byte continue_flag,int rows) {
245 if(channels[ch].row<0) {
246 rows+=channels[ch].row;
247 channels[ch].row=0;
249 if(rows<0) return;
250 if(instrument<=max_instrument) instruments[instrument].used=1;
251 allocate_frames(channels[ch].row+1);
252 frames[channels[ch].row].used=1;
253 if(unequal3lt(frames[channels[ch].row].data[ch].note,note,200)) fprintf(stderr,"Conflicting notes on channel %d.\n",ch);
254 if(note!=200) frames[channels[ch].row].data[ch].note=note;
255 if(note<200) channels[ch].prev_note=note;
256 if(unequal3(frames[channels[ch].row].data[ch].instrument,instrument,255)) fprintf(stderr,"Conflicting instruments on channel %d.\n",ch);
257 if(instrument!=255) frames[channels[ch].row].data[ch].instrument=instrument;
258 if(unequal3(frames[channels[ch].row].data[ch].effectid,effectid,255)) fprintf(stderr,"Conflicting effects on channel %d.\n",ch);
259 if(effectid!=255) {
260 frames[channels[ch].row].data[ch].effectid=effectid;
261 frames[channels[ch].row].data[ch].effectpar=effectpar;
263 if(unequal3(frames[channels[ch].row].data[ch].volumeset,volumeset,255)) fprintf(stderr,"Conflicting volumes on channel %d.\n",ch);
264 if(volumeset!=255) frames[channels[ch].row].data[ch].volumeset=volumeset;
265 frames[channels[ch].row].data[ch].continue_flag|=continue_flag|0x80;
266 channels[ch].row+=rows;
267 if(continue_flag) send_channel(ch,200,255,255,0,255,0,0);
270 void send_global(int row,byte effectid,byte effectpar,byte volumeset,byte continue_flag,int rows) {
271 if(row<0) {
272 rows+=row;
273 row=0;
275 if(rows<0) return;
276 allocate_frames(row+1);
277 frames[row].used=1;
278 if(unequal3(frames[row].data[16].effectid,effectid,255)) fprintf(stderr,"Conflicting global effects on row %d.\n",row);
279 if(effectid!=255) {
280 frames[row].data[16].effectid=effectid;
281 frames[row].data[16].effectpar=effectpar;
283 if(unequal3(frames[row].data[16].volumeset,volumeset,255)) fprintf(stderr,"Conflicting global volumes on row %d.\n",row);
284 if(volumeset!=255) frames[row].data[16].volumeset=volumeset;
285 frames[row].data[16].continue_flag|=continue_flag|0x80;
286 if(continue_flag && rows) send_global(row+rows,255,0,255,0,0);
289 int at_backtick(byte ch,byte v,char c) {
290 switch(c) {
291 case '?': return v;
292 case 'C': return channels[ch&15].row_cut%(*numeric_register?:1);
293 case 'I': return channels[ch&15].instrument;
294 case 'K': return channels[ch&15].transpose;
295 case 'L': return channels[ch&15].length;
296 case 'N': return channels[ch&15].prev_note;
297 case 'O': return channels[ch&15].octave;
298 case 'R': return channels[ch&15].row%(*numeric_register?:1);
299 case 'T': return channels[ch&15].row_tie%(*numeric_register?:1);
300 case 'i': return keytab[ch&63][v%120].instrument;
301 case 'n': return keytab[ch&63][v%120].note;
302 case 'u': return keytab[ch&63][v%120].customfx;
303 case 'v': return keytab[ch&63][v%120].volumeset;
304 default: fprintf(stderr,"Wrong @` command: %c\n",c);
306 return 0;
309 void hash_directive(char*s) {
310 // s points to character after '#'
311 char*t=s;
312 while(*t>32) t++;
313 if(*t) *t++=0;
314 else t=0;
315 skip_spaces(&t);
316 if(!strcmp(s,"AMIGA-SLIDES")) {
317 it_flags&=~0x08;
318 } else if(!strcmp(s,"CHANNEL")) {
319 int x;
320 for(x=0;x<16;x++) {
321 channels[x].ch_vol=read_int(&t);
322 if(channels[x].ch_vol<0 || channels[x].ch_vol>64) fprintf(stderr,"Abnormal channel volume (%c).\n",x+'A');
323 skip_spaces(&t);
324 channels[x].ch_pan=read_int(&t);
325 if((channels[x].ch_pan<0 || channels[x].ch_pan>64) && channels[x].ch_pan!=100)
326 fprintf(stderr,"Abnormal channel panning (%c).\n",x+'A');
327 skip_spaces(&t);
329 } else if(!strcmp(s,"COMPATIBILITY")) {
330 compatibility=read_int(&t);
331 } else if(!strcmp(s,"GATE-DENOM")) {
332 gate_denom=read_int(&t);
333 if(gate_denom<1) fprintf(stderr,"Abnormal gate denominator.\n");
334 } else if(!strcmp(s,"INCLUDE")) {
335 FILE*fp=fopen(t,"r");
336 if(fp) {
337 read_file(fp);
338 fclose(fp);
339 } else {
340 fprintf(stderr,"File cannot be loaded: %s\n",t);
342 } else if(!strcmp(s,"LINEAR-SLIDES")) {
343 it_flags|=0x08;
344 } else if(!strcmp(s,"MINIMUM-VERSION")) {
345 if(read_int(&t)>(itmck_version_code&0x0FFF)) {
346 fprintf(stderr,"*FATAL* Input file requires later version of ITMCK.\n");
347 exit(1);
349 } else if(!strcmp(s,"MIXER")) {
350 g_mixer=read_int(&t);
351 if(g_mixer==0 || g_mixer>128) fprintf(stderr,"Abnormal mix volume.\n");
352 } else if(!strcmp(s,"MONO")) {
353 it_flags&=~0x01;
354 } else if(!strcmp(s,"OCTAVE-REV")) {
355 octave_dir=read_int(&t)?-1:1;
356 } else if(!strcmp(s,"OLD-EFFECTS")) {
357 it_flags|=0x10;
358 } else if(!strcmp(s,"PANNING-SEPARATION")) {
359 panning_separation=read_int(&t);
360 if(panning_separation<0 || panning_separation>128) fprintf(stderr,"Abnormal panning separation.\n");
361 } else if(!strcmp(s,"PORTAMENTO-SHARE")) {
362 it_flags|=0x20;
363 } else if(!strcmp(s,"SCALE")) {
364 int x=0;
365 int b=0;
366 char*y=t;
367 while(*t++) {
368 if(*t=='|') b=x;
369 x+=(*t>' ');
371 x=-b;
372 while(*y++) {
373 if(*y>='a' && *y<='j') note_letter[*y-'a']=x;
374 x+=(*y>' ' && *y!='|');
376 } else if(!strcmp(s,"SCALE-BASE")) {
377 scale_base=*t;
378 if(*t<'a' || *t>'j') fprintf(stderr,"Abnormal scale base (%s).\n",t);
379 } else if(!strcmp(s,"STEREO")) {
380 it_flags|=0x01;
381 } else if(!strcmp(s,"SYNTH-LENGTH")) {
382 synth_len_mult=read_int(&t);
383 } else if(!strcmp(s,"SYNTH16")) {
384 synth16bits=1;
385 } else if(!strcmp(s,"TEMPO")) {
386 init_tempo=read_int(&t);
387 if(init_tempo<32) fprintf(stderr,"Abnormal tempo.\n");
388 } else if(!strcmp(s,"TITLE")) {
389 strncpy(song_title,t,26);
390 } else if(!strcmp(s,"TRACKER-VERSION")) {
391 tracker_version=read_int(&t);
392 } else if(!strcmp(s,"VOL0MIXOPT")) {
393 it_flags|=0x02;
394 } else if(!strcmp(s,"VOLUME")) {
395 g_vol=read_int(&t);
396 if(g_vol>128) fprintf(stderr,"Abnormal global volume.\n");
397 } else if(!strcmp(s,"WHOLE-NOTE")) {
398 whole_note=read_int(&t);
399 if(!whole_note) fprintf(stderr,"Abnormal whole note time.\n");
400 } else {
401 fprintf(stderr,"Unknown command: #%s\n",s);
405 void read_sample_def(byte n, char*s) {
406 char*t;
407 int x,y;
408 while(skip_spaces(&s),*s) {
409 switch(*s++) {
411 case '@': // Copy sample
412 if(*s=='s') s++;
413 x=read_int(&s);
414 if(x>=n || !x) {
415 fprintf(stderr,"Cannot copy sample %d to %d.\n",x,n);
416 } else if(samples[n].make || samples[x].make==3) {
417 fprintf(stderr,"Cannot copy a copy of a sample or change an existing sample into a copy.\n",x,n);
418 } else {
419 samples[n].make=3;
420 samples[n].link=x;
421 samples[n].global_volume=samples[x].global_volume;
422 samples[n].flag=samples[x].flag;
423 samples[n].default_volume=samples[x].default_volume;
424 samples[n].convert=samples[x].convert;
425 samples[n].pan=samples[x].pan;
426 samples[n].loop_start=samples[x].loop_start;
427 samples[n].loop_end=samples[x].loop_end;
428 samples[n].c5speed=samples[x].c5speed;
429 samples[n].sustain_start=samples[x].sustain_start;
430 samples[n].sustain_end=samples[x].sustain_end;
432 break;
434 case 'F': // File(IMPS)
435 if(!samples[n].make) samples[n].make=4;
436 if(samples[n].make!=4) fprintf(stderr,"Conflicting sample loading modes (%d).\n",n);
437 if(*s++!='=') fprintf(stderr,"Equal sign expected before filename.\n",n);
438 t=s;
439 while(*s++>' ');
440 *s++=0;
441 samples[n].name=strdup(t);
442 //todo
443 break;
445 case 'G': // Global volume
446 samples[n].global_volume=read_int(&s);
447 if(samples[n].global_volume>64) fprintf(stderr,"Abnormal sample global volume.\n");
448 break;
450 case 'L': // Loop
451 samples[n].flag|=0x10;
452 x=read_int(&s); y=read_int(&s);
453 if(x<y) {
454 samples[n].flag|=0x40;
455 samples[n].loop_start=y;
456 samples[n].loop_end=x;
457 } else {
458 samples[n].loop_start=x;
459 samples[n].loop_end=y;
461 break;
463 case 'S': // Sustain
464 samples[n].flag|=0x20;
465 x=read_int(&s); y=read_int(&s);
466 if(x<y) {
467 samples[n].flag|=0x80;
468 samples[n].sustain_start=y;
469 samples[n].sustain_end=x;
470 } else {
471 samples[n].sustain_start=x;
472 samples[n].sustain_end=y;
474 break;
476 case 'a': // Additive synthesizer
477 case 'b': // Manual synthesizer
478 if(!samples[n].make) samples[n].make=2;
479 if(samples[n].make!=2) fprintf(stderr,"Conflicting sample loading modes (%d).\n",n);
480 for(t=s;*t && *t!=']';t++);
481 *t=0;
482 samples[n].name=strdup(s-1);
483 s=t+1;
484 break;
486 case 'd': // Default volume
487 samples[n].default_volume=read_int(&s);
488 if(samples[n].default_volume>64) fprintf(stderr,"Abnormal sample default volume.\n");
489 break;
491 case 'f': // File(raw)
492 if(!samples[n].make) samples[n].make=1;
493 if(samples[n].make!=1) fprintf(stderr,"Conflicting sample loading modes (%d).\n",n);
494 if(*s++!='=') fprintf(stderr,"Equal sign expected before filename.\n",n);
495 t=s;
496 while(*s++>' ');
497 *s++=0;
498 samples[n].name=strdup(t);
499 break;
501 case 'r': // C5 speed
502 if(!(samples[n].c5speed=read_int(&s))) fprintf(stderr,"Abnormal C5speed.\n");
503 break;
505 case 'v': // Vibrato
506 x=*s++;
507 if(x=='U') samples[n].vibrato[3]=0;
508 if(x=='N') samples[n].vibrato[3]=1;
509 if(x=='L') samples[n].vibrato[3]=2;
510 if(x=='R') samples[n].vibrato[3]=3;
511 else fprintf(stderr,"Invalid vibrato waveform: %c.\n",x);
512 samples[n].vibrato[2]=read_int(&s);
513 samples[n].vibrato[1]=read_int(&s);
514 samples[n].vibrato[0]=read_int(&s);
515 if(samples[n].vibrato[2]>64) fprintf(stderr,"Abnormal sample vibrato rate.\n");
516 if(samples[n].vibrato[1]>64) fprintf(stderr,"Abnormal sample vibrato depth.\n");
517 if(samples[n].vibrato[0]>64) fprintf(stderr,"Abnormal sample vibrato speed.\n");
518 break;
520 case 'y': // Pan
521 samples[n].pan=read_int(&s)|128;
522 break;
524 case '}': // End of sample definition
525 skip_spaces(&s);
526 if(*s) fprintf(stderr,"Sample definition ends early.\n");
527 return;
529 default:
530 fprintf(stderr,"Unknown command in sample definition: %c\n",s[-1]);
535 void read_envelope(byte*env,char**s) {
536 int x;
537 int skip=1;
538 int ticks=0;
539 byte*e2;
540 if(1&*env) {
541 fprintf(stderr,"This envelope is already set.\n");
542 return;
544 *env|=1;
545 while(skip_spaces(s),**s) {
546 switch(*((*s)++)) {
548 case '[': case '{': case '=': // Ignore
549 break;
551 case ']': case '}': // End
552 if(!env[1]) fprintf(stderr,"This envelope has no nodes. (How does it smell?)\n");
553 return;
555 case '(': // Begin loop
556 env[2]=env[1];
557 break;
559 case ')': // End loop
560 env[3]=env[1]-1;
561 *env|=2;
562 break;
564 case '<': // Begin sustain
565 env[4]=env[1];
566 break;
568 case '>': // End sustain
569 env[5]=env[1]-1;
570 *env|=4;
571 break;
573 case '@': // Copy
574 x=read_int(s);
575 if(x<1 || x>max_instrument) {
576 fprintf(stderr,"Attempting to copy envelope from nonexisting instrument.\n");
577 } else {
578 if(**s=='f' || **s=='i') e2=instruments[x].pitch_env;
579 else if(**s=='v') e2=instruments[x].vol_env;
580 else if(**s=='p') e2=instruments[x].pan_env;
581 else return;
582 *env|=*e2;
583 memcpy(env+1,e2+1,61);
585 (*s)++;
586 return;
588 case 'a': // Advance
589 ticks+=read_int(s);
590 break;
592 case 's': // Set skip
593 skip=read_int(s);
594 break;
596 case '$': case '0' ... '9': case '-': case '+': // Values
597 env[env[1]*3+6]=x=read_int(s);
598 if(x<-32 || x>64) fprintf(stderr,"Abnormal envelope value.\n");
599 env[env[1]*3+7]=ticks&0xFF;
600 env[env[1]*3+8]=ticks>>8;
601 env[1]++;
602 ticks+=skip;
603 break;
605 default:
606 fprintf(stderr,"Unknown command in envelope: %c\n", (*s)[-1]);
608 if(env[1]>25) {
609 fprintf(stderr,"*FATAL* Too many nodes in envelope.\n");
610 exit(1);
613 if(!env[1]) fprintf(stderr,"This envelope has no nodes.\n");
616 void read_instrument_key_table(byte n,char**s) {
617 int x,y,z1,z2,z3,z4;
618 while(skip_spaces(s),**s) {
619 switch(*((*s)++)) {
621 case '[': case '{': case '=': // Ignore
622 break;
624 case ']': case '}': // End
625 return;
627 case '%': // Copy key assign along octaves
628 x=read_int(s); y=read_int(s);
629 if(y>0 && x>=y) {
630 for(z1=z2=z3=0;z1<120;z1++,z2=(z2+1)%x,z3+=y*!z2) {
631 instruments[n].keyboard[z1<<1]=instruments[n].keyboard[z2<<1]+z3;
632 instruments[n].keyboard[1|(z1<<1)]=instruments[n].keyboard[1|(z2<<1)];
634 } else {
635 fprintf(stderr,"Abnormal %% command in instruments key table.\n");
637 break;
639 case 'a' ... 'j': case 'n': // Assign keys
640 --*s;
641 x=read_note(s);
642 if(**s==':') {
643 y=read_note(s);
644 if(y==-1) fprintf(stderr,"Wrong note of range of assign key instrument table.\n");
645 else if(y<x) fprintf(stderr,"Backward range of assign key instrument table.\n");
646 } else {
647 y=x;
649 if(**s=='=') ++*s;
650 z2=read_note(s);
651 if(z2>-1) {
652 z1=0;
653 } else {
654 z1=1;
655 z2=read_int(s);
657 if(**s=='@') {
658 ++*s;
659 if(**s=='s') ++*s;
660 z3=read_int(s);
661 } else {
662 z3=0;
664 z4=(**s=='+'); *s+=z4;
665 while(x<=y) {
666 instruments[n].keyboard[x<<1]=x*z1+z2;
667 if(z3) instruments[n].keyboard[1|(x<<1)]=z3;
668 x++;
669 z3+=z4;
671 break;
673 default:
674 fprintf(stderr,"Unknown command in instrument key table: %c\n", (*s)[-1]);
679 void read_instrument_def(byte n, char*s) {
680 char*t;
681 int x,y;
682 while(skip_spaces(&s),*s) {
683 switch(*s++) {
685 case '@': // Set sample for all notes, or copy instrument
686 if(*s=='s') {
687 s++;
688 x=read_int(&s);
689 for(y=0;y<120;y++) instruments[n].keyboard[1|(y<<1)]=x;
690 } else {
691 x=read_int(&s);
692 if(x<1 || x>99 || x==n) fprintf(stderr,"Instrument number out of range during copy.\n");
693 else memcpy(instruments+n,instruments+x,sizeof(instrument_data));
695 break;
697 case 'A': // Note action
698 x=read_int(&s);
699 instruments[n].heading[0]=x&3; // NNA
700 instruments[n].heading[1]=(x/10)&3; // DCT
701 instruments[n].heading[2]=(x/100)&3; // DCA
702 if(x<0 || x%10>3 || (x/10)%10>3 || x/100>2) fprintf(stderr,"Abnormal NNA/DCT/DCA.\n");
703 break;
705 case 'G': // Global volume
706 instruments[n].heading[7]=x=read_int(&s);
707 if(x<0 || x>128) fprintf(stderr,"Abnormal instrument global volume.\n");
708 break;
710 case 'R': // Random volume/panning variation
711 instruments[n].heading[9]=x=read_int(&s);
712 if(x<0 || x>100) fprintf(stderr,"Abnormal random volume variation.\n");
713 instruments[n].heading[10]=read_int(&s);
714 break;
716 case 'Y': // Pitch-pan separation/center
717 x=read_int(&s);
718 if(x<-32 || x>32) fprintf(stderr,"Abnormal pitch-pan separation.\n");
719 instruments[n].heading[5]=x;
720 if(!(x=*s++)) {
721 fprintf(stderr,"Instrument definition ends in a pitch-pan specification.\n");
722 return;
724 if(x=='n') {
725 x=read_int(&s);
726 } else {
727 if(x<'a' || x>'j') {
728 fprintf(stderr,"*FATAL* Invalid pitch-pan center note letter.\n");
729 exit(1);
731 x=(x<scale_base)*octave_tones+note_letter[x-'a'];
732 while(*s=='+' || *s=='-') x+=*s++=='+'?:-1;
733 x+=octave_tones*read_int(&s);
735 if(x<0 || x>119) fprintf(stderr,"Abnormal pitch-pan center.\n");
736 instruments[n].heading[6]=x;
737 break;
739 case 'c': // Initial filter cutoff
740 instruments[n].ifc=(x=read_int(&s))|128;
741 if(x<0 || x>127) fprintf(stderr,"Abnormal initial filter cutoff.\n");
742 break;
744 case 'f': // Filter envelope
745 instruments[n].pitch_env[0]|=0x80;
746 read_envelope(instruments[n].pitch_env,&s);
747 break;
749 case 'k': // Key/sample table
750 read_instrument_key_table(n,&s);
751 break;
753 case 'i': // Pitch envelope
754 instruments[n].pitch_env[0]&=~0x80;
755 read_envelope(instruments[n].pitch_env,&s);
756 break;
758 case 'p': // Pan envelope
759 read_envelope(instruments[n].pan_env,&s);
760 break;
762 case 'r': // Initial filter resonance
763 instruments[n].ifr=(x=read_int(&s))|128;
764 if(x<0 || x>127) fprintf(stderr,"Abnormal initial filter resonance.\n");
765 break;
767 case 'v': // Volume envelope
768 read_envelope(instruments[n].vol_env,&s);
769 break;
771 case 'y': // Default pan
772 instruments[n].heading[8]=x=read_int(&s);
773 if(x<0 || x>64) fprintf(stderr,"Abnormal instrument default pan.\n");
774 break;
776 case '}': // End of instrument definition
777 skip_spaces(&s);
778 if(*s) fprintf(stderr,"Instrument definition ends early.\n");
779 return;
781 default:
782 fprintf(stderr,"Unknown command in instrument definition: %c\n",s[-1]);
787 void read_key_table_def(byte n,char*s) {
788 char*t;
789 int k=0;
790 int x;
791 char*loop=0;
792 while(skip_spaces(&s),*s) {
793 switch(*s++) {
795 case '*': // Volume
796 keytab[n][k].volumeset=read_int(&s);
797 break;
799 case '@': // Adjust variable/direct effects/instrument/copy key table/cusfom effect sequence
800 if(*s=='=') {
801 s++; x=(*s++&0x1F)%27; numeric_register[x]=read_int(&s);
802 } else if(*s=='+') {
803 s++; x=(*s++&0x1F)%27; numeric_register[x]+=read_int(&s);
804 } else if(*s=='-') {
805 s++; x=(*s++&0x1F)%27; numeric_register[x]-=read_int(&s);
806 } else if(*s=='*') {
807 s++; x=(*s++&0x1F)%27; numeric_register[x]*=read_int(&s);
808 } else if(*s=='e') {
809 s++; keytab[n][k].effectid=read_int(&s); keytab[n][k].effectpar=read_int(&s); keytab[n][k].volumeset=read_int(&s);
810 } else if((*s>='0' && *s<='9') || *s=='$') {
811 keytab[n][k].instrument=read_int(&s)?:255;
812 if(keytab[n][k].instrument>max_instrument) fprintf(stderr,"Unuse instrument %d.\n",channels[ch].instrument);
813 } else if(*s=='@') {
814 t=++s; x=read_int(&s);
815 if(x!=n && x>=0 && x<64) memcpy(keytab[n]+k,keytab[x]+k,sizeof(key_setting));
816 else fprintf(stderr,"Wrong key table number %d.\n",x);
817 } else if(*s=='u') {
818 t=++s; x=read_int(&s); keytab[n][k].customfx=(t==s)?255:x;
819 } else if(*s=='`') {
820 *numeric_register=at_backtick(n,k,*++s);
821 } else {
822 fprintf(stderr,"Unknown command in channel %c: @%c\n", ch+'A', *s++);
824 break;
826 case '[': // Begin loop
827 if(loop) fprintf(stderr,"Nested loop in key table definition.\n");
828 loop=s;
829 break;
831 case ']': // End loop
832 x=read_note(&s);
833 if(x<0 || x>119) x=119;
834 if(loop) {
835 if(k<x) {
836 k++;
837 s=loop;
838 } else {
839 k=0;
840 loop=0;
842 } else {
843 fprintf(stderr,"Loop end too much in key table definition.\n");
845 break;
847 case 'I': // Tremor
848 keytab[n][k].effectid=FX('I');
849 keytab[n][k].effectpar=read_int(&s)<<4;
850 keytab[n][k].effectpar|=read_int(&s);
851 break;
853 case 'J': // Arpeggio
854 keytab[n][k].effectid=FX('J');
855 keytab[n][k].effectpar=read_int(&s)<<4;
856 keytab[n][k].effectpar|=read_int(&s);
857 break;
859 case 'K': // Transpose
860 keytab[n][k].note=k+read_int(&s);
861 break;
863 case 'Q': // Retrigger
864 keytab[n][k].effectid=FX('Q');
865 keytab[n][k].effectpar=read_int(&s)<<4;
866 keytab[n][k].effectpar|=read_int(&s);
867 break;
869 case 'R': // Portamento
870 x=read_int(&s);
871 if(x<-0xDF || !x || x>0xDF) {
872 fprintf(stderr,"Abnormal portamento rate.\n");
873 } else if(x>0) {
874 keytab[n][k].effectid=FX('F');
875 keytab[n][k].effectpar=x;
876 } else {
877 keytab[n][k].effectid=FX('E');
878 keytab[n][k].effectpar=-x;
880 break;
882 case 'T': // Tremolo
883 keytab[n][k].effectid=FX('R');
884 keytab[n][k].effectpar=read_int(&s)<<4;
885 keytab[n][k].effectpar|=read_int(&s);
886 break;
888 case 'V': // Vibrato
889 x=read_int(&s);
890 if(x<1 || x>15) fprintf(stderr,"Abnormal vibrato speed.\n");
891 keytab[n][k].effectpar=x<<4;
892 x=read_int(&s);
893 if(x>0 && x<16) {
894 keytab[n][k].effectid=FX('U');
895 keytab[n][k].effectpar|=x;
896 } else if(x>0 && x<=60 && !(x&3)) {
897 keytab[n][k].effectid=FX('H');
898 keytab[n][k].effectpar|=x>>2;
899 } else {
900 fprintf(stderr,"Abnormal vibrato depth.\n");
902 break;
904 case 'Y': // Panbello
905 keytab[n][k].effectid=FX('Y');
906 keytab[n][k].effectpar=read_int(&s)<<4;
907 keytab[n][k].effectpar|=read_int(&s);
908 break;
910 case 'a' ... 'j': case 'n': // Note entry
911 s--;
912 k=read_note(&s);
913 if(k<0 || k>119) {
914 fprintf(stderr,"Note out-of-range.\n");
915 k=0;
917 break;
919 case '}': // End of key table definition
920 skip_spaces(&s);
921 if(*s) fprintf(stderr,"Key table definition ends early.\n");
922 return;
924 default:
925 fprintf(stderr,"Unknown command in key table definition: %c\n",s[-1]);
930 void read_custom_fx_def(byte n,char*s) {
931 custom_fx*cfx=fxtab+n;
932 custom_fx*tmp;
933 short cuts=255;
934 byte cont=0; // 0x80=continue sequence before/after note stopped
935 int x;
936 char*loop=0;
937 int loopc=0;
938 while(skip_spaces(&s),*s) {
939 switch(*s++) {
941 case '-': case '+': case '$': case '0' ... '9': // Delta
942 s--;
943 cfx->delta=read_int(&s);
944 tmp=cfx->next;
945 cfx->next=malloc(sizeof(custom_fx));
946 cfx=cfx->next;
947 cfx->transpose=0;
948 cfx->instrument=255;
949 cfx->effectid=0;
950 cfx->volumeset=255;
951 cfx->continue_flag=cont;
952 cfx->delta=0;
953 cfx->next=tmp;
954 break;
956 case '(': // Begin continue sequence
957 cfx->continue_flag|=(cont=0x80);
958 break;
960 case ')': // End continue sequence
961 cont=0;
962 break;
964 case '*': // Note volume
965 cfx->volumeset=read_int(&s);
966 cfx->continue_flag&=~0x02;
967 break;
969 case ':': // Final delta
970 cfx->delta=read_int(&s);
971 break;
973 case '@': // Direct effect entry, chain to sequence, instrument select
974 if(*s=='=') {
975 s++; x=(*s++&0x1F)%27; numeric_register[x]=read_int(&s);
976 } else if(*s=='+') {
977 s++; x=(*s++&0x1F)%27; numeric_register[x]+=read_int(&s);
978 } else if(*s=='-') {
979 s++; x=(*s++&0x1F)%27; numeric_register[x]-=read_int(&s);
980 } else if(*s=='*') {
981 s++; x=(*s++&0x1F)%27; numeric_register[x]*=read_int(&s);
982 } else if(*s=='e') {
983 s++; cfx->effectid=read_int(&s); cfx->effectpar=read_int(&s); cfx->volumeset=read_int(&s);
984 cfx->continue_flag|=0x03;
985 } else if(*s=='f') {
986 s++; cfx->effectid=read_int(&s); cfx->effectpar=read_int(&s);
987 cfx->continue_flag&=~0x05;
988 } else if(*s=='u') {
989 s++; cfx->next=fxtab+(63&read_int(&s));
990 } else if(*s=='v') {
991 s++; cfx->volumeset=read_int(&s);
992 cfx->continue_flag&=~0x02;
993 } else if(*s=='z') {
994 cfx->continue_flag|=0x04;
995 } else if((*s>='0' && *s<='9') || *s=='$') {
996 cfx->instrument=read_int(&s);
997 } else {
998 fprintf(stderr,"Unknown command in custom effect sequence: @%c\n", *s++);
1000 break;
1002 case 'C': // Set note cuts
1003 x=*s++;
1004 if(x=='o') cfx->transpose=cuts=255;
1005 else if(x=='c') cfx->transpose=cuts=254;
1006 else if(x=='f') cfx->transpose=cuts=253;
1007 else fprintf(stderr,"Unknown command in custom effect sequence: C%c\n",x);
1008 break;
1010 case 'I': // Tremor
1011 cfx->effectid=FX('I');
1012 cfx->effectpar=read_int(&s)<<4;
1013 cfx->effectpar|=read_int(&s);
1014 cfx->continue_flag|=0x01;
1015 break;
1017 case 'J': // Arpeggio
1018 cfx->effectid=FX('J');
1019 cfx->effectpar=read_int(&s)<<4;
1020 cfx->effectpar|=read_int(&s);
1021 cfx->continue_flag|=0x01;
1022 break;
1024 case 'K': // Transpose
1025 cfx->transpose=read_int(&s);
1026 break;
1028 case 'Q': // Retrigger
1029 cfx->effectid=FX('Q');
1030 cfx->effectpar=read_int(&s)<<4;
1031 cfx->effectpar|=read_int(&s);
1032 cfx->continue_flag|=0x01;
1033 break;
1035 case 'R': // Portamento
1036 x=read_int(&s);
1037 if(x<-0xDF || !x || x>0xDF) {
1038 fprintf(stderr,"Abnormal portamento rate.\n");
1039 } else if(x>0) {
1040 cfx->effectid=FX('F');
1041 cfx->effectpar=x;
1042 } else {
1043 cfx->effectid=FX('E');
1044 cfx->effectpar=-x;
1046 cfx->continue_flag|=0x01;
1047 break;
1049 case 'T': // Tremolo
1050 cfx->effectid=FX('R');
1051 cfx->effectpar=read_int(&s)<<4;
1052 cfx->effectpar|=read_int(&s);
1053 cfx->continue_flag|=0x01;
1054 break;
1056 case 'V': // Vibrato
1057 x=read_int(&s);
1058 if(x<1 || x>15) fprintf(stderr,"Abnormal vibrato speed.\n");
1059 cfx->effectpar=x<<4;
1060 x=read_int(&s);
1061 if(x>0 && x<16) {
1062 cfx->effectid=FX('U');
1063 cfx->effectpar|=x;
1064 } else if(x>0 && x<=60 && !(x&3)) {
1065 cfx->effectid=FX('H');
1066 cfx->effectpar|=x>>2;
1067 } else {
1068 fprintf(stderr,"Abnormal vibrato depth.\n");
1070 cfx->continue_flag|=0x01;
1071 break;
1073 case 'Y': // Panbello
1074 cfx->effectid=FX('Y');
1075 cfx->effectpar=read_int(&s)<<4;
1076 cfx->effectpar|=read_int(&s);
1077 cfx->continue_flag|=0x01;
1078 break;
1080 case '[': // Repeat begin
1081 if(loop) fprintf(stderr,"Prohibit nesting repeats.\n");
1082 loop=s;
1083 loopc=0;
1084 break;
1086 case ']': // Repeat end
1087 if(!loop) {
1088 fprintf(stderr,"*FATAL* Repeat block ends without starting.\n");
1089 exit(1);
1091 if((x=read_int(&s))<0) fprintf(stderr,"Abnormal repeat count.\n");
1092 if(x==++loopc) loop=0; else s=loop;
1093 break;
1095 case 'r': // Rest
1096 cfx->transpose=cuts;
1097 break;
1099 case 'w': // Wait
1100 cfx->transpose=200;
1101 break;
1103 case '|': // Loop mark
1104 cfx->next=cfx;
1105 break;
1107 case '}': // End of definition
1108 skip_spaces(&s);
1109 if(*s) fprintf(stderr,"Custom effect sequence definition ends early.\n");
1110 goto check_custom_fx;
1112 default:
1113 fprintf(stderr,"Unknown command in custom effect sequence definition: %c\n",s[-1]);
1116 check_custom_fx:
1117 if(!cfx->next) return;
1118 //todo
1121 void at_definition(char*s) {
1122 int x,y;
1123 char*t;
1124 char*p;
1125 switch(*s++) {
1127 case '0' ... '9': // Instrument
1128 case '$': // Instrument (hex)
1129 s--;
1130 x=read_int(&s);
1131 if(x<=0 || x>99) {
1132 fprintf(stderr,"Wrong instrument number (%d).\n",x);
1133 return;
1135 if(max_instrument<x) max_instrument=x;
1136 skip_equals_brace(&s);
1137 read_instrument_def(x,s);
1138 break;
1140 case '@': // Key table
1141 x=read_int(&s);
1142 if(x<0 || x>63) {
1143 fprintf(stderr,"Wrong key table number (%d).\n",x);
1144 return;
1146 skip_equals_brace(&s);
1147 read_key_table_def(x,s);
1148 break;
1150 case 'A' ... 'P': // Channel setting
1151 x=s[-1]-'A';
1152 skip_equals_brace(&s); channels[x].ch_vol=read_int(&s);
1153 skip_spaces(&s); channels[x].ch_pan=read_int(&s);
1154 if(channels[x].ch_vol<0 || channels[x].ch_vol>64) fprintf(stderr,"Abnormal channel volume (%c).\n",x+'A');
1155 if((channels[x].ch_pan<0 || channels[x].ch_pan>64) && channels[x].ch_pan!=100)
1156 fprintf(stderr,"Abnormal channel panning (%c).\n",x+'A');
1157 break;
1159 case 'X': // Text macro
1160 x=read_int(&s);
1161 if(x&~0xFF) {
1162 fprintf(stderr,"Wrong macro number (%d).\n",x);
1163 return;
1165 t=s; y=(*t>='A' && *t<='P');
1166 while(*s && *s!='=') s++;
1167 if(!*s) {
1168 fprintf(stderr,"Incomplete macro definition (%d).\n",x);
1169 return;
1171 s++; skip_spaces(&s); p=strdup(s);
1172 if(y) {
1173 while(*t>='A' && *t<='P') macro_text[x][*t++-'A']=p;
1174 } else {
1175 while(y<16) macro_text[x][y++]=p;
1177 break;
1179 case 's': // Sample
1180 x=read_int(&s);
1181 if(x<=0 || x>99) {
1182 fprintf(stderr,"Wrong sample number (%d).\n",x);
1183 return;
1185 if(max_sample<x) max_sample=x;
1186 samples[x].flag=1; // sample associated with header
1187 skip_equals_brace(&s);
1188 read_sample_def(x,s);
1189 break;
1191 case 'u': // Custom effect sequence
1192 x=read_int(&s);
1193 if(x<0 || x>63) {
1194 fprintf(stderr,"Wrong custom effect sequence number (%d).\n",x);
1195 return;
1197 skip_equals_brace(&s);
1198 read_custom_fx_def(x,s);
1199 break;
1201 default:
1202 fprintf(stderr,"Unknown command: @%c\n",*s);
1206 void send_note(byte ch,short no,int prelen,char**s) {
1207 short dot;
1208 short quan;
1209 int len;
1210 byte vol=255;
1211 byte inst=channels[ch].instrument;
1212 byte fxid=channels[ch].effectid;
1213 byte fxpar=channels[ch].effectpar;
1214 byte volfx=channels[ch].volumeset;
1215 byte cusfx=channels[ch].customfx;
1216 custom_fx*cfx;
1217 int x;
1218 boolean contfx=0;
1219 channels[ch].row_tie=channels[ch].row;
1220 if(s) {
1221 while(**s) {
1222 if(**s=='\'') no+=octave_tones;
1223 else if(**s=='"') no+=octave_tones*2;
1224 else if(**s=='-') no--;
1225 else if(**s=='+') no++;
1226 else break;
1227 (*s)++;
1229 len=read_int(s);
1230 if(!len) len=channels[ch].length?:1;
1231 if(whole_note%len) fprintf(stderr,"Inexact note length (%d).\n",(int)len);
1232 dot=len=whole_note/len;
1233 while(**s) {
1234 if(**s=='.') len+=(dot/=2);
1235 else break;
1236 (*s)++;
1238 if(**s=='*') {
1239 (*s)++; vol=read_int(s);
1242 if(!dot) fprintf(stderr,"Inexact note length.\n");
1243 if(channels[ch].quantize>0) {
1244 len-=quan=(len*channels[ch].quantize)/gate_denom;
1245 } else {
1246 len-=quan=-channels[ch].quantize;
1248 len+=prelen;
1249 if(len<1) {
1250 len=1;
1251 quan=0;
1252 fprintf(stderr,"Quantize results in too short note.\n");
1254 if(channels[ch].keytable!=255) {
1255 x=channels[ch].keytable;
1256 inst=keytab[x][no].instrument==255?inst:keytab[x][no].instrument;
1257 fxid=keytab[x][no].effectid?:fxid;
1258 fxpar=keytab[x][no].effectid?keytab[x][no].effectpar:fxpar;
1259 volfx=keytab[x][no].volumeset==255?volfx:keytab[x][no].volumeset;
1260 cusfx=keytab[x][no].customfx==255?cusfx:keytab[x][no].customfx;
1261 no=keytab[x][no].note;
1263 if(volfx!=255 && vol!=255 && vol!=volfx) fprintf(stderr,"Conflicting volume effects during note.\n");
1264 volfx=vol;
1265 cfx=cusfx==255?0:fxtab+cusfx;
1266 if(cfx && no<200) {
1267 while(cfx && (len || contfx)) {
1268 contfx=!!(cfx->continue_flag&0x80);
1269 if(cfx->transpose<200) no+=cfx->transpose;
1270 if(no<0 || no>119) {
1271 no=0; fprintf(stderr,"Custom effect table causes note out of range error.\n");
1273 x=cfx->delta;
1274 if(x>len && !contfx) x=len;
1275 send_channel(ch,cfx->transpose>=200?cfx->transpose:no,(cfx->instrument==255 && cfx->transpose<200255)?inst:cfx->instrument,
1276 cfx->effectid,cfx->effectpar,cfx->volumeset,cfx->continue_flag,x);
1277 cfx=cfx->next;
1278 len-=x;
1280 channels[ch].row+=len;
1281 } else {
1282 send_channel(ch,no,inst,fxid,fxpar,volfx,1,len);
1284 //todo: auto portamento
1285 channels[ch].row_cut=channels[ch].row;
1286 if(quan) send_channel(channels[ch].note_cut,no,inst,255,0,volfx,0,quan);
1287 channels[ch].auto_portamento=0;
1290 void process_channel(byte ch,char*s) {
1291 char c;
1292 char*t;
1293 int x;
1294 char*loop=0;
1295 int loopc=0;
1296 skip_spaces(&s);
1297 while(*s) {
1298 switch(c=*s++) {
1300 case 'a' ... 'j': // Notes
1301 send_note(ch,channels[ch].transpose+(channels[ch].octave+(c<scale_base))*octave_tones+note_letter[c-'a'],0,&s);
1302 break;
1304 case 'r': // Rest
1305 send_note(ch,channels[ch].note_cuts,0,&s);
1306 break;
1308 case 'n': // Direct note
1309 send_note(ch,read_int(&s)+channels[ch].transpose,0,&s);
1310 break;
1312 case '^': // Tie
1313 if(channels[ch].row_cut<max_frames) frames[channels[ch].row_cut].data[ch].continue_flag=0;
1314 x=channels[ch].row-channels[ch].row_tie;
1315 channels[ch].row=channels[ch].row_tie;
1316 send_note(ch,channels[ch].prev_note,x,&s);
1317 break;
1319 case '&': // Slur
1320 if(channels[ch].row_cut<max_frames) frames[channels[ch].row_cut].data[ch].continue_flag=0;
1321 break;
1323 case '/': // Auto portamento
1324 channels[ch].auto_portamento=1;
1325 break;
1327 case 'L': // Loop restart point
1328 allocate_frames(channels[ch].row+1);
1329 frames[channels[ch].row].used=1;
1330 channels[ch].loopstart=channels[ch].row;
1331 break;
1333 case '|': // Go back to loop restart, or skip if not looping
1334 c=*s++;
1335 if(c=='|') {
1336 allocate_frames(channels[ch].row+1);
1337 frames[channels[ch].row].used=1;
1338 channels[ch].loopskip=channels[ch].row;
1339 } else if(c=='L') {
1340 allocate_frames(channels[ch].row+1);
1341 frames[channels[ch].row].used=1;
1342 channels[ch].loopend=channels[ch].row;
1343 } else if(c==':') {
1344 //todo (begin runtime repeat block)
1345 } else {
1346 fprintf(stderr,"Unknown command in channel %c: |%c\n", ch+'A', c);
1348 break;
1350 case ':': // Runtime repeat block end/entry point by order number
1351 c=*s++;
1352 if(c=='|') {
1353 //todo (end runtime repeat block)
1354 } else if(c=='$' || (c>='0' && c<='9')) {
1355 //todo (entry point by order number)
1356 } else {
1357 fprintf(stderr,"Unknown command in channel %c: :%c\n", ch+'A', c);
1359 break;
1361 case '@': // Set instrument or key table or quantize or other channel or etc
1362 if(*s=='{' || *s=='}') {
1363 s++; // for use with text macros
1364 } else if(*s=='=') {
1365 s++; x=(*s++&0x1F)%27; numeric_register[x]=read_int(&s);
1366 } else if(*s=='+') {
1367 s++; x=(*s++&0x1F)%27; numeric_register[x]+=read_int(&s);
1368 } else if(*s=='-') {
1369 s++; x=(*s++&0x1F)%27; numeric_register[x]-=read_int(&s);
1370 } else if(*s=='*') {
1371 s++; x=(*s++&0x1F)%27; numeric_register[x]*=read_int(&s);
1372 } else if(*s=='q') {
1373 s++; channels[ch].quantize=-read_int(&s);
1374 } else if(*s=='e') {
1375 s++; channels[ch].effectid=read_int(&s); channels[ch].effectpar=read_int(&s); channels[ch].volumeset=read_int(&s);
1376 } else if(*s>='A' && *s<='P') {
1377 ch=*s++-'A';
1378 } else if((*s>='0' && *s<='9') || *s=='$') {
1379 channels[ch].instrument=read_int(&s); channels[ch].keytable=255;
1380 if(channels[ch].instrument>max_instrument) fprintf(stderr,"Unuse instrument %d.\n",channels[ch].instrument);
1381 } else if(*s=='@') {
1382 t=++s; x=read_int(&s); channels[ch].keytable=(t==s)?255:x;
1383 } else if(*s=='u') {
1384 t=++s; x=read_int(&s); channels[ch].customfx=(t==s)?255:x;
1385 } else if(*s=='`') {
1386 *numeric_register=at_backtick(ch,loopc,*++s);
1387 } else {
1388 fprintf(stderr,"Unknown command in channel %c: @%c\n", ch+'A', *s++);
1390 break;
1392 case 'o': // Octave set
1393 channels[ch].octave=read_int(&s);
1394 if(channels[ch].octave>9) fprintf(stderr,"Abnormal octave number (o).\n");
1395 break;
1397 case '<': // Low octave
1398 channels[ch].octave-=octave_dir;
1399 if(channels[ch].octave>9) fprintf(stderr,"Abnormal octave number (<).\n");
1400 break;
1402 case '>': // High octave
1403 channels[ch].octave+=octave_dir;
1404 if(channels[ch].octave>9) fprintf(stderr,"Abnormal octave number (>).\n");
1405 break;
1407 case 'l': // Length
1408 channels[ch].length=read_int(&s);
1409 if(channels[ch].length<=0 || whole_note%channels[ch].length)
1410 fprintf(stderr,"Abnormal note length (%d/%d).\n",whole_note,channels[ch].length);
1411 break;
1413 case 'K': // Transpose
1414 channels[ch].transpose=read_int(&s);
1415 break;
1417 case 'v': // Volume
1418 send_channel(ch,200,FX('M'),read_int(&s),0,255,0,0);
1419 break;
1421 case 'G': // Global volume
1422 send_global(channels[ch].row,FX('V'),read_int(&s),255,0,1);
1423 break;
1425 case 'X': // Call macro
1426 x=read_int(&s); skip_spaces(&s);
1427 if(x&~0xFF) {
1428 fprintf(stderr,"Wrong macro number (%d).\n",x);
1429 break;
1431 if(*s) {
1432 process_channel(ch,macro_text[x][ch]);
1433 } else {
1434 s=macro_text[x][ch]; // tail recursive
1436 break;
1438 case 'C': // Set note cuts
1439 x=*s++;
1440 if(x=='o') channels[ch].note_cuts=255;
1441 else if(x=='c') channels[ch].note_cuts=254;
1442 else if(x=='f') channels[ch].note_cuts=253;
1443 else fprintf(stderr,"Unknown command in channel %c: C%c\n",ch+'A',x);
1444 break;
1446 case 'q': // Set quantize
1447 channels[ch].quantize=read_int(&s);
1448 break;
1450 case 't': // Set tempo
1451 send_global(channels[ch].row,FX('T'),read_int(&s),255,0,1);
1452 break;
1454 case '[': // Repeat begin
1455 if(loop) fprintf(stderr,"Prohibit nesting repeats.\n");
1456 loop=s;
1457 loopc=0;
1458 break;
1460 case ']': // Repeat end
1461 if(!loop) {
1462 fprintf(stderr,"*FATAL* Repeat block ends without starting.\n");
1463 exit(1);
1465 if((x=read_int(&s))<0) fprintf(stderr,"Abnormal repeat count.\n");
1466 if(x==++loopc) loop=0; else s=loop;
1467 break;
1469 case 'I': // Tremor
1470 channels[ch].effectid=FX('I');
1471 channels[ch].effectpar=read_int(&s)<<4;
1472 channels[ch].effectpar|=read_int(&s);
1473 break;
1475 case 'J': // Arpeggio
1476 channels[ch].effectid=FX('J');
1477 channels[ch].effectpar=read_int(&s)<<4;
1478 channels[ch].effectpar|=read_int(&s);
1479 break;
1481 case 'T': // Tremolo
1482 channels[ch].effectid=FX('R');
1483 channels[ch].effectpar=read_int(&s)<<4;
1484 channels[ch].effectpar|=read_int(&s);
1485 break;
1487 case 'P': // Panning (volume column)
1488 channels[ch].volumeset=read_int(&s)|128;
1489 break;
1491 case 'Q': // Retrigger
1492 channels[ch].effectid=FX('Q');
1493 channels[ch].effectpar=read_int(&s)<<4;
1494 channels[ch].effectpar|=read_int(&s);
1495 break;
1497 case 'V': // Vibrato
1498 x=read_int(&s);
1499 if(x<1 || x>15) fprintf(stderr,"Abnormal vibrato speed.\n");
1500 channels[ch].effectpar=x<<4;
1501 x=read_int(&s);
1502 if(x>0 && x<16) {
1503 channels[ch].effectid=FX('U');
1504 channels[ch].effectpar|=x;
1505 } else if(x>0 && x<=60 && !(x&3)) {
1506 channels[ch].effectid=FX('H');
1507 channels[ch].effectpar|=x>>2;
1508 } else {
1509 fprintf(stderr,"Abnormal vibrato depth.\n");
1511 break;
1513 case 'Y': // Panbrello
1514 channels[ch].effectid=FX('Y');
1515 channels[ch].effectpar=read_int(&s)<<4;
1516 channels[ch].effectpar|=read_int(&s);
1517 break;
1519 case 'R': // Portamento
1520 x=read_int(&s);
1521 if(x<-0xDF || !x || x>0xDF) {
1522 fprintf(stderr,"Abnormal portamento rate.\n");
1523 } else if(x>0) {
1524 channels[ch].effectid=FX('F');
1525 channels[ch].effectpar=x;
1526 } else {
1527 channels[ch].effectid=FX('E');
1528 channels[ch].effectpar=-x;
1530 break;
1532 case 'z': // Cancel effects
1533 channels[ch].effectid=0;
1534 channels[ch].volumeset=255;
1535 break;
1537 case '\\': // Synchronize channel
1538 x=*s++-'A';
1539 if(x<0 || x>15) {
1540 fprintf(stderr,"Cannot synchronize wrong channel.\n");
1541 } else {
1542 channels[ch].row=channels[x].row;
1543 channels[ch].row_cut=channels[x].row_cut;
1544 channels[ch].row_tie=channels[x].row_tie;
1545 channels[ch].loopstart=channels[x].loopstart;
1546 channels[ch].loopskip=channels[x].loopskip;
1547 channels[ch].loopend=channels[x].loopend;
1549 break;
1551 default:
1552 fprintf(stderr,"Unknown command in channel %c: %c\n", ch+'A', c);
1554 skip_spaces(&s);
1556 if(loop) fprintf(stderr,"Repeat block not ended (repeat is ignored).\n");
1559 void process_line(char*s) {
1560 char*t;
1561 switch(*s) {
1562 case 0: case ';': break;
1563 case '"':
1564 *next_song_message=malloc(sizeof(message_line));
1565 (*next_song_message)->data=strdup(s+1);
1566 (*next_song_message)->next=0;
1567 next_song_message=&((*next_song_message)->next);
1568 song_message_length+=strlen(s); // Includes the '"' which is the same length as newline marker
1569 if(song_message_length>8000) fprintf(stderr,"Overlong song message.\n");
1570 break;
1571 case '#': hash_directive(s+1); break;
1572 case '@': at_definition(s+1); break;
1573 case 'A' ... 'P':
1574 for(t=s;0x40&*t;t++);
1575 while(*s>='A' && *s<='P') process_channel(*s++-'A',t);
1576 break;
1577 default:
1578 fprintf(stderr,"Illegal character at start of line: %c\n", *s);
1582 void read_file(FILE*fp) {
1583 char buf[4096];
1584 int bp=0; // buffer mark
1585 int ns=0; // not space
1586 int c;
1587 boolean bl=1; // 0 to block of multi lines
1588 boolean com=1; // 0 to comment out
1589 *buf=0;
1590 while(!feof(fp)) {
1591 if((c=fgetc(fp))==EOF) break;
1593 if(com) {
1594 if(c>' ') ns=bp+1;
1595 if(c>' ' || bp) {
1596 buf[bp++]=c>' '?c:' ';
1597 if(bp>=4096) {
1598 fprintf(stderr,"*FATAL* Overlong line of input.\n");
1599 exit(1);
1602 if(bp) buf[bp-1]*=(com=(*buf<36 || c!=';')); // cut off comment
1603 if(*buf=='@') {
1604 if(c=='{') bl=0;
1605 if(c=='}') bl=1;
1609 if(c=='\r' || c=='\n') {
1610 com=1;
1611 if(bl && bp) {
1612 buf[ns]=0;
1613 if(trace_input) fprintf(stderr,"+ %s\n",buf);
1614 process_line(buf);
1615 bp=ns=0;
1620 if(trace_input) fprintf(stderr,"<EOF>\n");
1623 void make_defaults(void) {
1624 int x,y;
1625 for(x=1;x<100;x++) {
1626 // Sample
1627 samples[x].global_volume=64;
1628 samples[x].flag=0x00;
1629 samples[x].default_volume=64;
1630 samples[x].convert=0x00;
1631 samples[x].pan=64;
1632 samples[x].length=samples[x].loop_start=samples[x].loop_end=0;
1633 samples[x].c5speed=8363;
1634 samples[x].sustain_start=samples[x].sustain_end=0;
1635 samples[x].make=0;
1636 samples[x].name=0;
1637 samples[x].used=0;
1638 // Instrument
1639 for(y=0;y<11;y++) instruments[x].heading[y]=0;
1640 instruments[x].heading[6]=60;
1641 instruments[x].heading[7]=0x40;
1642 instruments[x].heading[8]=0xA0;
1643 instruments[x].ifc=instruments[x].ifr=0;
1644 for(y=0;y<120;y++) {
1645 instruments[x].keyboard[y<<1]=y;
1646 instruments[x].keyboard[1|(y<<1)]=0;
1648 instruments[x].used=0;
1649 instruments[x].vol_env[0]=instruments[x].vol_env[1]=0;
1650 instruments[x].pan_env[0]=instruments[x].pan_env[1]=0;
1651 instruments[x].pitch_env[0]=instruments[x].pitch_env[1]=0;
1653 for(x=0;x<64;x++) {
1654 // Key table
1655 for(y=0;y<120;y++) {
1656 keytab[x][y].note=y;
1657 keytab[x][y].effectid=0;
1658 keytab[x][y].instrument=keytab[x][y].volumeset=keytab[x][y].customfx=255;
1660 // Effect table
1661 fxtab[x].transpose=0;
1662 fxtab[x].instrument=255;
1663 fxtab[x].effectid=0;
1664 fxtab[x].volumeset=255;
1665 fxtab[x].continue_flag=0;
1666 fxtab[x].delta=0;
1667 fxtab[x].next=0;
1669 for(x=0;x<16;x++) {
1670 // Channel
1671 channels[x].transpose=0;
1672 channels[x].octave=5;
1673 channels[x].length=4;
1674 channels[x].instrument=1;
1675 channels[x].effectid=0;
1676 channels[x].volumeset=255;
1677 channels[x].quantize=0;
1678 channels[x].note_cuts=255;
1679 channels[x].ch_vol=64;
1680 channels[x].ch_pan=32;
1681 channels[x].row=channels[x].row_cut=channels[x].row_tie=0;
1682 channels[x].loopstart=channels[x].loopskip=channels[x].loopend=-1;
1683 channels[x].keytable=255;
1684 channels[x].customfx=255;
1685 channels[x].auto_portamento=0;
1686 channels[x].prev_note=254;
1690 byte*synthesizer_a(byte n) {
1691 char*cmd=samples[n].name+1;
1692 int per=lround(((double)samples[n].c5speed)/C_pitch); // period (before multiply)
1693 int len=lround(((double)(samples[n].c5speed*synth_len_mult))/C_pitch);
1694 double*dbl;
1695 byte*buf;
1696 int x,z1,z2,z3;
1697 double q,r;
1698 double amp=1.0;
1700 // Amplitude/length
1701 x=read_int(&cmd)?:1000; amp=0.001*(double)x;
1702 len*=read_int(&cmd)?:1;
1703 dbl=malloc(len*sizeof(double));
1704 buf=malloc(len<<synth16bits);
1706 // Set samples
1707 samples[n].length=samples[n].loop_end=len;
1708 samples[n].flag|=0x10;
1709 samples[n].convert=0x00;
1710 samples[n].loop_start=0;
1712 // Reset buffer
1713 for(x=0;x<len;x++) dbl[x]=0;
1715 // Calculation
1716 while(*cmd) {
1717 skip_spaces(&cmd);
1718 if(*cmd=='[') cmd++;
1719 if(!*cmd) break;
1720 if(*cmd=='L') {
1721 // square wave
1722 cmd++;
1723 z1=read_int(&cmd); // duty
1724 z2=read_int(&cmd); // amplitude
1725 if(!z2) {
1726 fprintf(stderr,"*FATAL* Improper synthesizer for sample %d.\n",n);
1727 exit(1);
1729 for(x=0;x<len;x++) dbl[x]=(x%per<(z1*per)/1000)?(1.0/(double)z2):(dbl[x]-1.0/(double)z2);
1730 } else if(*cmd=='N') {
1731 // saw wave
1732 cmd++;
1733 z1=read_int(&cmd); // amplitude
1734 if(!z1) {
1735 fprintf(stderr,"*FATAL* Improper synthesizer for sample %d.\n",n);
1736 exit(1);
1738 q=1.0/(double)z1;
1739 for(x=0;x<len;x++) {
1740 r=q*(double)(x%per-per/2);
1741 dbl[x]=fabs(dbl[x])<r?r:dbl[x];
1743 } else if(*cmd=='V') {
1744 // triangle wave
1745 cmd++;
1746 //todo
1747 } else if(*cmd=='R') {
1748 // random noise
1749 cmd++;
1750 z1=read_int(&cmd); // divider
1751 z2=read_int(&cmd); // amplitude
1752 z3=read_int(&cmd); // exponent
1753 if(!z3) z3=1;
1754 if(z1<=0 || !z2) {
1755 fprintf(stderr,"*FATAL* Improper synthesizer for sample %d.\n",n);
1756 exit(1);
1758 for(x=0;x<len;x++) {
1759 if(!(x%z1)) q=pow(rand_double(),z3)/(double)z2;
1760 dbl[x]+=q;
1762 } else if(0x5F&*cmd) {
1763 // sine wave
1764 z1=read_int(&cmd); // frequency
1765 z2=read_int(&cmd); // amplitude
1766 z3=read_int(&cmd); // phase
1767 q=(tau*(double)z3)/1000.0;
1768 if(!z2) {
1769 fprintf(stderr,"*FATAL* Improper synthesizer for sample %d.\n",n);
1770 exit(1);
1772 for(x=0;x<len;x++) dbl[x]+=sin((q+tau*(double)(z1*x))/per)/(double)z2;
1776 // Return byte buffer
1777 if(synth16bits) {
1778 samples[n].flag|=0x02;
1779 samples[n].convert|=0x01;
1780 for(x=0;x<len;x++) {
1781 dbl[x]*=amp;
1782 z1=lround(dbl[x]*0x8000);
1783 if(z1<-0x7FFF) z1=-0x7FFF;
1784 if(z1>0x7FFF) z1=0x7FFF;
1785 buf[x<<1]=z1;
1786 buf[1|(x<<1)]=z1>>8;
1788 } else {
1789 for(x=0;x<len;x++) {
1790 dbl[x]*=amp;
1791 z1=lround((dbl[x]+1)*128);
1792 if(z1>255) z1=255;
1793 if(z1<0) z1=0;
1794 buf[x]=z1;
1797 free(dbl);
1798 return buf;
1801 byte*synthesizer_b(byte n) {
1802 char*cmd=samples[n].name+1;
1803 byte*buf=malloc(0x5000); // limit=0x4000
1804 int len=0;
1805 int loops[16];
1806 int slows[16];
1807 int loopnest=0;
1808 int slow=1;
1809 int x,y,z;
1811 // Read commands and make buffer
1812 while(*cmd) {
1813 skip_spaces(&cmd);
1814 if(*cmd=='[') cmd++;
1815 if(!*cmd) break;
1816 if(*cmd=='|') {
1817 // loop point
1818 cmd++;
1819 samples[n].flag|=0x10;
1820 samples[n].loop_start=len;
1821 } else if(*cmd=='(' || *cmd=='$' || (*cmd>='0' && *cmd<='9') || *cmd=='-' || *cmd=='+') {
1822 x=read_int(&cmd);
1823 if(*cmd=='(') {
1824 if(loopnest==15) {
1825 fprintf(stderr,"*FATAL* Too many nested repeats in sample %d.\n",n);
1826 exit(1);
1828 loops[loopnest]=len>>synth16bits;
1829 slows[loopnest++]=slow;
1830 slow*=x?:1;
1831 } else {
1832 y=slow;
1833 while(y--) {
1834 buf[len++]=x;
1835 if(synth16bits) buf[len++]=x>>8;
1838 } else if(*cmd==')') {
1839 cmd++;
1840 if(loopnest) {
1841 x=read_int(&cmd)?:1;
1842 z=len;
1843 --loopnest;
1844 while(x--) {
1845 y=loops[loopnest];
1846 while(y<z) buf[len++]=buf[y++];
1848 slow=slows[loopnest];
1849 } else {
1850 fprintf(stderr,"Stack underflow in sample %d.\n",n);
1852 } else if(*cmd>' ') {
1853 cmd++;
1854 fprintf(stderr,"Improper synthesizer for sample %d.\n",n);
1856 if(len>=0x3FFF) {
1857 fprintf(stderr,"*FATAL* Overlong sample %d.\n",n);
1858 exit(1);
1862 // Send buffer
1863 samples[n].convert=0x00;
1864 samples[n].length=samples[n].loop_end=len>>synth16bits;
1865 if(synth16bits) {
1866 samples[n].flag|=0x02;
1867 samples[n].convert|=0x01;
1869 return buf;
1872 void do_export_sample(void) {
1873 sample_data*sam=samples+export_sample;
1874 byte*buf;
1875 int x;
1876 if(export_sample<=max_sample && sam->make==2) {
1877 if(sam->name[0]=='a') buf=synthesizer_a(export_sample);
1878 if(sam->name[0]=='b') buf=synthesizer_b(export_sample);
1879 fwrite(buf,1,sam->length<<synth16bits,stdout);
1880 free(buf);
1881 } else {
1882 fprintf(stderr,"*FATAL* Cannot export sample %d to stdout.\n",export_sample);
1883 exit(1);
1887 //=========================================//
1889 typedef struct pat_row {
1890 byte note[18]; // 200=unuse
1891 byte instrument[18]; // 255=unuse
1892 byte volume[18]; // 255=unuse
1893 byte effectid[18]; // 255=unuse (ch.16=globals, ch.17=speed, ch.18=break)
1894 byte effectpar[18];
1895 struct pat_row*next;
1896 } pat_row;
1898 pat_row*pat_start;
1899 pat_row*pat_loop_begin;
1900 pat_row*pat_loop_skip;
1901 pat_row*pat_loop_end;
1902 byte*pat_data;
1903 int where_loopstart_ord; // offset +pat_data
1904 int where_loopskip_ord;
1905 int where_loopend_ord;
1906 byte pattern_loopstart=255; // pattern number
1907 byte pattern_loopskip=255;
1908 byte pattern_loopend=255;
1909 int pat_data_len=0;
1910 int pat_count=0;
1911 byte order[256];
1912 unsigned long pat_offs[200];
1913 unsigned long sam_offs;
1914 unsigned long sam_raw_offs[100];
1915 unsigned long inst_offs;
1916 unsigned long msg_offs=0;
1918 pat_row*new_pat_row(void) {
1919 int x;
1920 pat_row*row=malloc(sizeof(pat_row));
1921 if(!row) {
1922 fprintf(stderr,"*FATAL* Out of memory to make pattern rows.\n");
1923 exit(1);
1925 for(x=0;x<18;x++) {
1926 row->note[x]=200;
1927 row->instrument[x]=row->volume[x]=row->effectid[x]=255;
1929 row->next=0;
1930 return row;
1933 inline void make_pattern_rows(void) {
1934 pat_row*row;
1935 int loop_start=0;
1936 int loop_skip=0;
1937 int loop_end=0;
1938 int x,y;
1939 byte chve[17]; // volume
1940 byte chxi[17]; // effect ID
1941 byte chxp[17]; // effect parameter
1942 int frameskip=0;
1943 pat_start=pat_loop_begin=pat_loop_skip=pat_loop_end=0;
1944 for(x=0;x<17;x++) chve[x]=chxi[x]=chxp[x]=255;
1945 for(x=0;x<16;x++) {
1946 if(channels[x].loopstart) {
1947 if(loop_start && channels[x].loopstart!=loop_start) fprintf(stderr,"Desynchronized loop start (%c).\n",x+'A');
1948 loop_start=channels[x].loopstart;
1950 if(channels[x].loopskip) {
1951 if(loop_skip && channels[x].loopskip!=loop_skip) fprintf(stderr,"Desynchronized loop skip (%c).\n",x+'A');
1952 loop_skip=channels[x].loopskip;
1954 if(channels[x].loopend) {
1955 if(loop_end && channels[x].loopend!=loop_end) fprintf(stderr,"Desynchronized loop end (%c).\n",x+'A');
1956 loop_end=channels[x].loopend;
1959 for(x=0;x<max_frames;x++) {
1960 if(frames[x].used || frameskip==15) {
1961 // use frame
1962 if(pat_start) {
1963 if(frameskip) {
1964 row->effectid[17]=FX('S');
1965 row->effectpar[17]=0xE0|frameskip;
1967 row->next=new_pat_row();
1968 row=row->next;
1969 } else {
1970 row=pat_start=new_pat_row();
1972 if(x==loop_start) pat_loop_begin=row;
1973 if(x==loop_skip) pat_loop_skip=row;
1974 if(x==loop_end) pat_loop_end=row;
1975 frameskip=0;
1976 for(y=0;y<17;y++) {
1977 chan_row*fr=frames[x].data+y;
1978 row->note[y]=200;
1979 row->volume[y]=chve[y];
1980 row->effectid[y]=chxi[y];
1981 row->effectpar[y]=chxp[y];
1982 if(fr->continue_flag&0x80) {
1983 row->note[y]=fr->note;
1984 row->instrument[y]=fr->instrument;
1985 row->volume[y]=fr->volumeset;
1986 row->effectid[y]=fr->effectid;
1987 row->effectpar[y]=fr->effectpar;
1988 if(fr->continue_flag&0x01) {
1989 chxi[y]=fr->effectid; chxp[y]=fr->effectpar;
1990 } else {
1991 chxi[y]=chxp[y]=255;
1993 if(fr->continue_flag&0x02) chve[y]=fr->volumeset; else chve[y]=255;
1994 if(fr->continue_flag&0x04) chxp[y]=0;
1997 } else {
1998 // unuse frame
1999 frameskip++;
2004 inline void make_pattern_data(void) {
2005 int rc; // row count per pattern
2006 pat_row*row=pat_start;
2007 byte*pat;
2008 byte*pp;
2009 byte lastnote[32];
2010 byte lastinstrument[32];
2011 byte lastvolumepan[32];
2012 byte lastcommand[32];
2013 byte lastcommandvalue[32];
2014 byte lastmask[32];
2015 byte mask;
2016 int x;
2017 boolean bs;
2019 pat_data=malloc(0);
2021 next_pattern:
2022 bs=1;
2023 pat_offs[pat_count++]=pat_data_len;
2024 pat_data=realloc(pat_data,pat_data_len+=65536); // Note that the pattern + the 8 byte header will ALWAYS be less than 64k (if <47 channels)
2025 if(!pat_data) {
2026 fprintf("*FATAL* Out of pattern data memory for pattern %d.\n",pat_count);
2027 exit(1);
2029 pat=pat_data+(pat_data_len-65536);
2030 pat[3]=pat[4]=pat[5]=pat[6]=pat[7]=0; // Len x x x x
2031 pp=pat+8;
2032 rc=0;
2033 for(x=0;x<32;x++) {
2034 lastnote[x]=200;
2035 lastinstrument[x]=lastvolumepan[x]=lastcommand[x]=lastcommandvalue[x]=lastmask[x]=255;
2038 next_row:
2039 if(rc==200) goto done_pattern;
2040 for(x=0;x<18;x++) {
2041 #define rowMask(xx,yy,zzz,zz) ((row->xx[x]!=yy)<<(zzz+4*(row->xx[x]!=zz[x])))
2042 #define do_rowMask rowMask(note,200,0,lastnote)|rowMask(instrument,255,1,lastinstrument)|rowMask(volume,255,2,lastvolumepan)|rowMask(effectid,255,3,lastcommand)
2043 mask=do_rowMask;
2044 if((mask&128) && row->effectpar[x]!=lastcommandvalue[x]) mask-=120;
2045 #undef rowMask
2046 #define rowMask(xx,yy,zzz,zz) (zz[x]=row->xx[x]!=yy?row->xx[x]:zz[x])
2047 do_rowMask;
2048 if(row->effectid[x]!=255) lastcommandvalue[x]=row->effectpar[x];
2049 #undef rowMask
2050 #undef do_rowMask
2051 *pp++=((mask==lastmask[x])<<7)|(x+1);
2052 if(mask!=lastmask[x]) *pp++=mask;
2053 if(mask&1) *pp++=row->note[x];
2054 if(mask&2) *pp++=row->instrument[x];
2055 if(mask&4) *pp++=row->volume[x];
2056 if(mask&8) *pp++=row->effectid[x],*pp++=row->effectpar[x];
2057 lastmask[x]=mask;
2059 if(row==pat_loop_begin || row==pat_loop_skip || row==pat_loop_end) {
2060 *pp++=128|19;
2061 *pp++=FX('B');
2062 if(row==pat_loop_begin) { where_loopstart_ord=pp-pat_data; pattern_loopstart=pat_count-1; }
2063 if(row==pat_loop_skip) { where_loopskip_ord=pp-pat_data; pattern_loopskip=pat_count-1; }
2064 if(row==pat_loop_end) { where_loopend_ord=pp-pat_data; pattern_loopend=pat_count-1; }
2065 *pp++=0xFF; // placeholder
2066 bs=0;
2068 *pp++=0;
2069 rc++;
2070 if(row=row->next) if(bs) goto next_row;
2072 done_pattern:
2073 if(bs && rc<32) {
2074 pp[-1]=128|19; // Channel 18 (override last terminator)
2075 *pp++=8; // use effect
2076 *pp++=FX('C'); // break to row
2077 *pp++=0x00; // row 0
2078 *pp++=0; // terminator
2080 while(rc<32) rc++,*pp++=0;
2081 pat[2]=rc;
2082 pat[1]=((pp-pat)-8)>>8;
2083 pat[0]=((pp-pat)-8)&0xFF;
2084 pat_data=realloc(pat_data,pat_data_len+=(pp-pat)-65536);
2085 if(row) goto next_pattern;
2088 inline void make_orders(void) {
2089 int o,p;
2091 // Reset orders
2092 for(o=0;o<256;o++) order[o]=255;
2093 o=p=0;
2095 if(pattern_loopstart==255) {
2096 // Make simple order
2097 while(p<pat_count) order[o++]=p++;
2098 } else {
2099 // Make order before loop start
2100 p=0;
2101 while(p<=pattern_loopstart) order[o++]=p++;
2102 pat_data[where_loopend_ord]=o;
2104 if(pattern_loopskip!=255) {
2105 // Make order between loop skip and loop end (order is switch between skip..end/start..skip)
2106 p=pattern_loopskip+1;
2107 while(p<=pattern_loopend) order[o++]=p++;
2108 pat_data[where_loopskip_ord]=o;
2110 // Make order between loop start and loop skip
2111 p=pattern_loopstart+1;
2112 while(p<=pattern_loopskip) order[o++]=p++;
2113 pat_data[where_loopstart_ord]=o;
2116 // Make order after loop end
2117 p=pattern_loopend+1;
2118 while(p<pat_count) order[o++]=p++;
2122 inline void output_calc(void) {
2123 make_pattern_rows();
2124 make_pattern_data();
2125 if(pat_count>200) fprintf(stderr,"Too many patterns.\n");
2126 make_orders();
2127 //todo
2130 inline void output_main(void) {
2131 fwrite("IMPM",1,4,stdout); // File type identification
2132 fwrite(song_title,1,26,stdout); // Song Name
2133 write16(0); // PHiligt
2134 //todo
2137 //=========================================//
2139 int main(int argc,char**argv) {
2140 argv+=!!argc; argc-=!!argc;
2141 while(argc--) {
2142 if(**argv=='-') {
2143 char*o=*argv+1;
2144 char*s;
2145 while(*o) {
2146 switch(*o) {
2147 case 'c': // Compatibility setting
2148 s=*++argv; argc--;
2149 compatibility=read_int(&s);
2150 break;
2151 case 'e': // Export sample to stdout
2152 s=*++argv; argc--;
2153 export_sample=read_int(&s);
2154 break;
2155 case 'p': // Prefix for filename when loading external samples
2156 filename_prefix=*++argv; argc--;
2157 break;
2158 case 't': // Trace input
2159 trace_input=1;
2160 break;
2161 case 'v': // Version
2162 puts(itmck_version);
2163 return 0;
2164 case 'w': // Set Cwt header
2165 s=*++argv; argc--;
2166 tracker_version=read_int(&s);
2167 break;
2168 case 'x': // Volume 0 mix optimization
2169 it_flags|=0x02;
2170 break;
2171 default:
2172 fprintf(stderr,"*FATAL* Unrecognized switch: -%c\n",*o);
2173 return 1;
2175 o++;
2177 } else if(**argv) {
2178 fprintf(stderr,"*FATAL* Unrecognized command-line option: %s\n",*argv);
2179 return 1;
2181 argv+=!!argc;
2184 make_defaults();
2185 read_file(stdin);
2187 #ifdef _WIN32
2188 _setmode(_fileno(stdout),_O_BINARY);
2189 #endif
2191 // Export sample
2192 if(export_sample) {
2193 do_export_sample();
2194 return 0;
2197 // Output main
2198 output_calc();
2199 output_main();
2200 return 0;
2203 // http://16-bits.org/it/ for information of .IT format
2204 // http://woolyss.com/chipmusic/chipmusic-mml/ppmck_guide.php for MCK