4 Licensed under GNU GPL v3 or later version
7 #define itmck_version "ITMCK v0.1"
8 #define itmck_version_code 0x7010
9 #define impulse_tracker_version_code 0x0204
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))
26 typedef unsigned char byte
;
29 typedef struct chan_info
{
34 byte effectid
; // 0=not set
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
42 int row_cut
; // use for slurs/ties and auto portamento
43 int row_tie
; // use for ties
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
53 typedef struct message_line
{
55 struct message_line
*next
;
58 typedef struct chan_row
{
59 byte note
; // 200=unuse
60 byte instrument
; // 255=unuse
61 byte effectid
; // 255=unuse
63 byte volumeset
; // 255=unuse
64 byte continue_flag
; // 0x01=effect 0x02=volume 0x04=zero next effect 0x80=used
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
72 typedef struct key_setting
{
74 byte instrument
; // 255=unuse
75 byte effectid
; // 0=unuse
77 byte volumeset
; // 255=unuse
78 byte customfx
; // 255=unuse
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
88 byte pitch_env
[82]; // bit7 of flag enables filter envelope
89 boolean used
; // set if instrument is selected in MML
92 typedef struct sample_data
{
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
111 typedef struct custom_fx
{
112 short transpose
; // 255=omit note
113 byte instrument
; // 255=unuse
114 byte effectid
; // 0=unuse
116 byte volumeset
; // 255=unuse
117 byte continue_flag
; // 0x01=effect 0x02=volume 0x04=zero next effect 0x80=continue pass note
119 struct custom_fx
*next
;
123 chan_info channels
[16];
124 message_line
*song_message
;
125 message_line
**next_song_message
=&song_message
;
126 int song_message_length
=0;
131 byte panning_separation
=128;
134 char*macro_text
[256][16];
137 key_setting keytab
[64][120];
139 unsigned short tracker_version
=itmck_version_code
; // Cwt
140 sample_data samples
[100];
141 instrument_data instruments
[100];
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
;
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 };
160 void read_file(FILE*fp
);
162 inline void write8(int x
) {
166 inline void write16(int x
) {
168 putchar((x
>>8)&0xFF);
171 inline void write32(int x
) {
173 putchar((x
>>8)&0xFF);
174 putchar((x
>>16)&0xFF);
175 putchar((x
>>24)&0xFF);
178 int read_int(char**s
) {
184 if(**s
=='$' && (*s
)[1]>='`' && (*s
)[1]<='z') return numeric_register
[(*s
)[1]&0x1F];
186 if(**s
=='-') { q
=-1; (*s
)++; }
187 if(**s
=='$') { b
=16; (*s
)++; }
188 while((**s
>='0' && **s
<='9') || (b
==16 && **s
>='A'&& **s
<='F')) {
190 r
=r
*b
+c
-('0'+7*(c
>='A'));
195 int read_note(char**s
) {
201 if(**s
<'a' || **s
>'j') return -1;
203 x
=note_letter
[x
-'a']+octave_tones
*(x
<scale_base
);
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
; }
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
)++;
223 while(**s
&& **s
<=' ') (*s
)++;
226 void allocate_frames(int n
) {
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");
233 while(max_frames
<n
) {
234 frames
[max_frames
].used
=0;
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;
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
;
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
);
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
) {
276 allocate_frames(row
+1);
278 if(unequal3(frames
[row
].data
[16].effectid
,effectid
,255)) fprintf(stderr
,"Conflicting global effects on row %d.\n",row
);
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
) {
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
);
309 void hash_directive(char*s
) {
310 // s points to character after '#'
316 if(!strcmp(s
,"AMIGA-SLIDES")) {
318 } else if(!strcmp(s
,"CHANNEL")) {
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');
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');
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");
340 fprintf(stderr
,"File cannot be loaded: %s\n",t
);
342 } else if(!strcmp(s
,"LINEAR-SLIDES")) {
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");
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")) {
354 } else if(!strcmp(s
,"OCTAVE-REV")) {
355 octave_dir
=read_int(&t
)?-1:1;
356 } else if(!strcmp(s
,"OLD-EFFECTS")) {
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")) {
363 } else if(!strcmp(s
,"SCALE")) {
373 if(*y
>='a' && *y
<='j') note_letter
[*y
-'a']=x
;
374 x
+=(*y
>' ' && *y
!='|');
376 } else if(!strcmp(s
,"SCALE-BASE")) {
378 if(*t
<'a' || *t
>'j') fprintf(stderr
,"Abnormal scale base (%s).\n",t
);
379 } else if(!strcmp(s
,"STEREO")) {
381 } else if(!strcmp(s
,"SYNTH-LENGTH")) {
382 synth_len_mult
=read_int(&t
);
383 } else if(!strcmp(s
,"SYNTH16")) {
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")) {
394 } else if(!strcmp(s
,"VOLUME")) {
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");
401 fprintf(stderr
,"Unknown command: #%s\n",s
);
405 void read_sample_def(byte n
, char*s
) {
408 while(skip_spaces(&s
),*s
) {
411 case '@': // Copy sample
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
);
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
;
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
);
441 samples
[n
].name
=strdup(t
);
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");
451 samples
[n
].flag
|=0x10;
452 x
=read_int(&s
); y
=read_int(&s
);
454 samples
[n
].flag
|=0x40;
455 samples
[n
].loop_start
=y
;
456 samples
[n
].loop_end
=x
;
458 samples
[n
].loop_start
=x
;
459 samples
[n
].loop_end
=y
;
464 samples
[n
].flag
|=0x20;
465 x
=read_int(&s
); y
=read_int(&s
);
467 samples
[n
].flag
|=0x80;
468 samples
[n
].sustain_start
=y
;
469 samples
[n
].sustain_end
=x
;
471 samples
[n
].sustain_start
=x
;
472 samples
[n
].sustain_end
=y
;
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
++);
482 samples
[n
].name
=strdup(s
-1);
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");
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
);
498 samples
[n
].name
=strdup(t
);
501 case 'r': // C5 speed
502 if(!(samples
[n
].c5speed
=read_int(&s
))) fprintf(stderr
,"Abnormal C5speed.\n");
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");
521 samples
[n
].pan
=read_int(&s
)|128;
524 case '}': // End of sample definition
526 if(*s
) fprintf(stderr
,"Sample definition ends early.\n");
530 fprintf(stderr
,"Unknown command in sample definition: %c\n",s
[-1]);
535 void read_envelope(byte
*env
,char**s
) {
541 fprintf(stderr
,"This envelope is already set.\n");
545 while(skip_spaces(s
),**s
) {
548 case '[': case '{': case '=': // Ignore
551 case ']': case '}': // End
552 if(!env
[1]) fprintf(stderr
,"This envelope has no nodes. (How does it smell?)\n");
555 case '(': // Begin loop
559 case ')': // End loop
564 case '<': // Begin sustain
568 case '>': // End sustain
575 if(x
<1 || x
>max_instrument
) {
576 fprintf(stderr
,"Attempting to copy envelope from nonexisting instrument.\n");
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
;
583 memcpy(env
+1,e2
+1,61);
592 case 's': // Set skip
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;
606 fprintf(stderr
,"Unknown command in envelope: %c\n", (*s
)[-1]);
609 fprintf(stderr
,"*FATAL* Too many nodes in envelope.\n");
613 if(!env
[1]) fprintf(stderr
,"This envelope has no nodes.\n");
616 void read_instrument_key_table(byte n
,char**s
) {
618 while(skip_spaces(s
),**s
) {
621 case '[': case '{': case '=': // Ignore
624 case ']': case '}': // End
627 case '%': // Copy key assign along octaves
628 x
=read_int(s
); y
=read_int(s
);
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)];
635 fprintf(stderr
,"Abnormal %% command in instruments key table.\n");
639 case 'a' ... 'j': case 'n': // Assign keys
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");
664 z4
=(**s
=='+'); *s
+=z4
;
666 instruments
[n
].keyboard
[x
<<1]=x
*z1
+z2
;
667 if(z3
) instruments
[n
].keyboard
[1|(x
<<1)]=z3
;
674 fprintf(stderr
,"Unknown command in instrument key table: %c\n", (*s
)[-1]);
679 void read_instrument_def(byte n
, char*s
) {
682 while(skip_spaces(&s
),*s
) {
685 case '@': // Set sample for all notes, or copy instrument
689 for(y
=0;y
<120;y
++) instruments
[n
].keyboard
[1|(y
<<1)]=x
;
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
));
697 case 'A': // Note action
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");
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");
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
);
716 case 'Y': // Pitch-pan separation/center
718 if(x
<-32 || x
>32) fprintf(stderr
,"Abnormal pitch-pan separation.\n");
719 instruments
[n
].heading
[5]=x
;
721 fprintf(stderr
,"Instrument definition ends in a pitch-pan specification.\n");
728 fprintf(stderr
,"*FATAL* Invalid pitch-pan center note letter.\n");
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
;
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");
744 case 'f': // Filter envelope
745 instruments
[n
].pitch_env
[0]|=0x80;
746 read_envelope(instruments
[n
].pitch_env
,&s
);
749 case 'k': // Key/sample table
750 read_instrument_key_table(n
,&s
);
753 case 'i': // Pitch envelope
754 instruments
[n
].pitch_env
[0]&=~0x80;
755 read_envelope(instruments
[n
].pitch_env
,&s
);
758 case 'p': // Pan envelope
759 read_envelope(instruments
[n
].pan_env
,&s
);
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");
767 case 'v': // Volume envelope
768 read_envelope(instruments
[n
].vol_env
,&s
);
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");
776 case '}': // End of instrument definition
778 if(*s
) fprintf(stderr
,"Instrument definition ends early.\n");
782 fprintf(stderr
,"Unknown command in instrument definition: %c\n",s
[-1]);
787 void read_key_table_def(byte n
,char*s
) {
792 while(skip_spaces(&s
),*s
) {
796 keytab
[n
][k
].volumeset
=read_int(&s
);
799 case '@': // Adjust variable/direct effects/instrument/copy key table/cusfom effect sequence
801 s
++; x
=(*s
++&0x1F)%27; numeric_register
[x
]=read_int(&s
);
803 s
++; x
=(*s
++&0x1F)%27; numeric_register
[x
]+=read_int(&s
);
805 s
++; x
=(*s
++&0x1F)%27; numeric_register
[x
]-=read_int(&s
);
807 s
++; x
=(*s
++&0x1F)%27; numeric_register
[x
]*=read_int(&s
);
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
);
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
);
818 t
=++s
; x
=read_int(&s
); keytab
[n
][k
].customfx
=(t
==s
)?255:x
;
820 *numeric_register
=at_backtick(n
,k
,*++s
);
822 fprintf(stderr
,"Unknown command in channel %c: @%c\n", ch
+'A', *s
++);
826 case '[': // Begin loop
827 if(loop
) fprintf(stderr
,"Nested loop in key table definition.\n");
831 case ']': // End loop
833 if(x
<0 || x
>119) x
=119;
843 fprintf(stderr
,"Loop end too much in key table definition.\n");
848 keytab
[n
][k
].effectid
=FX('I');
849 keytab
[n
][k
].effectpar
=read_int(&s
)<<4;
850 keytab
[n
][k
].effectpar
|=read_int(&s
);
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
);
859 case 'K': // Transpose
860 keytab
[n
][k
].note
=k
+read_int(&s
);
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
);
869 case 'R': // Portamento
871 if(x
<-0xDF || !x
|| x
>0xDF) {
872 fprintf(stderr
,"Abnormal portamento rate.\n");
874 keytab
[n
][k
].effectid
=FX('F');
875 keytab
[n
][k
].effectpar
=x
;
877 keytab
[n
][k
].effectid
=FX('E');
878 keytab
[n
][k
].effectpar
=-x
;
883 keytab
[n
][k
].effectid
=FX('R');
884 keytab
[n
][k
].effectpar
=read_int(&s
)<<4;
885 keytab
[n
][k
].effectpar
|=read_int(&s
);
890 if(x
<1 || x
>15) fprintf(stderr
,"Abnormal vibrato speed.\n");
891 keytab
[n
][k
].effectpar
=x
<<4;
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;
900 fprintf(stderr
,"Abnormal vibrato depth.\n");
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
);
910 case 'a' ... 'j': case 'n': // Note entry
914 fprintf(stderr
,"Note out-of-range.\n");
919 case '}': // End of key table definition
921 if(*s
) fprintf(stderr
,"Key table definition ends early.\n");
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
;
934 byte cont
=0; // 0x80=continue sequence before/after note stopped
938 while(skip_spaces(&s
),*s
) {
941 case '-': case '+': case '$': case '0' ... '9': // Delta
943 cfx
->delta
=read_int(&s
);
945 cfx
->next
=malloc(sizeof(custom_fx
));
951 cfx
->continue_flag
=cont
;
956 case '(': // Begin continue sequence
957 cfx
->continue_flag
|=(cont
=0x80);
960 case ')': // End continue sequence
964 case '*': // Note volume
965 cfx
->volumeset
=read_int(&s
);
966 cfx
->continue_flag
&=~0x02;
969 case ':': // Final delta
970 cfx
->delta
=read_int(&s
);
973 case '@': // Direct effect entry, chain to sequence, instrument select
975 s
++; x
=(*s
++&0x1F)%27; numeric_register
[x
]=read_int(&s
);
977 s
++; x
=(*s
++&0x1F)%27; numeric_register
[x
]+=read_int(&s
);
979 s
++; x
=(*s
++&0x1F)%27; numeric_register
[x
]-=read_int(&s
);
981 s
++; x
=(*s
++&0x1F)%27; numeric_register
[x
]*=read_int(&s
);
983 s
++; cfx
->effectid
=read_int(&s
); cfx
->effectpar
=read_int(&s
); cfx
->volumeset
=read_int(&s
);
984 cfx
->continue_flag
|=0x03;
986 s
++; cfx
->effectid
=read_int(&s
); cfx
->effectpar
=read_int(&s
);
987 cfx
->continue_flag
&=~0x05;
989 s
++; cfx
->next
=fxtab
+(63&read_int(&s
));
991 s
++; cfx
->volumeset
=read_int(&s
);
992 cfx
->continue_flag
&=~0x02;
994 cfx
->continue_flag
|=0x04;
995 } else if((*s
>='0' && *s
<='9') || *s
=='$') {
996 cfx
->instrument
=read_int(&s
);
998 fprintf(stderr
,"Unknown command in custom effect sequence: @%c\n", *s
++);
1002 case 'C': // Set note cuts
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
);
1011 cfx
->effectid
=FX('I');
1012 cfx
->effectpar
=read_int(&s
)<<4;
1013 cfx
->effectpar
|=read_int(&s
);
1014 cfx
->continue_flag
|=0x01;
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;
1024 case 'K': // Transpose
1025 cfx
->transpose
=read_int(&s
);
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;
1035 case 'R': // Portamento
1037 if(x
<-0xDF || !x
|| x
>0xDF) {
1038 fprintf(stderr
,"Abnormal portamento rate.\n");
1040 cfx
->effectid
=FX('F');
1043 cfx
->effectid
=FX('E');
1046 cfx
->continue_flag
|=0x01;
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;
1056 case 'V': // Vibrato
1058 if(x
<1 || x
>15) fprintf(stderr
,"Abnormal vibrato speed.\n");
1059 cfx
->effectpar
=x
<<4;
1062 cfx
->effectid
=FX('U');
1064 } else if(x
>0 && x
<=60 && !(x
&3)) {
1065 cfx
->effectid
=FX('H');
1066 cfx
->effectpar
|=x
>>2;
1068 fprintf(stderr
,"Abnormal vibrato depth.\n");
1070 cfx
->continue_flag
|=0x01;
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;
1080 case '[': // Repeat begin
1081 if(loop
) fprintf(stderr
,"Prohibit nesting repeats.\n");
1086 case ']': // Repeat end
1088 fprintf(stderr
,"*FATAL* Repeat block ends without starting.\n");
1091 if((x
=read_int(&s
))<0) fprintf(stderr
,"Abnormal repeat count.\n");
1092 if(x
==++loopc
) loop
=0; else s
=loop
;
1096 cfx
->transpose
=cuts
;
1103 case '|': // Loop mark
1107 case '}': // End of definition
1109 if(*s
) fprintf(stderr
,"Custom effect sequence definition ends early.\n");
1110 goto check_custom_fx
;
1113 fprintf(stderr
,"Unknown command in custom effect sequence definition: %c\n",s
[-1]);
1117 if(!cfx
->next
) return;
1121 void at_definition(char*s
) {
1127 case '0' ... '9': // Instrument
1128 case '$': // Instrument (hex)
1132 fprintf(stderr
,"Wrong instrument number (%d).\n",x
);
1135 if(max_instrument
<x
) max_instrument
=x
;
1136 skip_equals_brace(&s
);
1137 read_instrument_def(x
,s
);
1140 case '@': // Key table
1143 fprintf(stderr
,"Wrong key table number (%d).\n",x
);
1146 skip_equals_brace(&s
);
1147 read_key_table_def(x
,s
);
1150 case 'A' ... 'P': // Channel setting
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');
1159 case 'X': // Text macro
1162 fprintf(stderr
,"Wrong macro number (%d).\n",x
);
1165 t
=s
; y
=(*t
>='A' && *t
<='P');
1166 while(*s
&& *s
!='=') s
++;
1168 fprintf(stderr
,"Incomplete macro definition (%d).\n",x
);
1171 s
++; skip_spaces(&s
); p
=strdup(s
);
1173 while(*t
>='A' && *t
<='P') macro_text
[x
][*t
++-'A']=p
;
1175 while(y
<16) macro_text
[x
][y
++]=p
;
1182 fprintf(stderr
,"Wrong sample number (%d).\n",x
);
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
);
1191 case 'u': // Custom effect sequence
1194 fprintf(stderr
,"Wrong custom effect sequence number (%d).\n",x
);
1197 skip_equals_brace(&s
);
1198 read_custom_fx_def(x
,s
);
1202 fprintf(stderr
,"Unknown command: @%c\n",*s
);
1206 void send_note(byte ch
,short no
,int prelen
,char**s
) {
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
;
1219 channels
[ch
].row_tie
=channels
[ch
].row
;
1222 if(**s
=='\'') no
+=octave_tones
;
1223 else if(**s
=='"') no
+=octave_tones
*2;
1224 else if(**s
=='-') no
--;
1225 else if(**s
=='+') no
++;
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
;
1234 if(**s
=='.') len
+=(dot
/=2);
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
;
1246 len
-=quan
=-channels
[ch
].quantize
;
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");
1265 cfx
=cusfx
==255?0:fxtab
+cusfx
;
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");
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
);
1280 channels
[ch
].row
+=len
;
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
) {
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
);
1305 send_note(ch
,channels
[ch
].note_cuts
,0,&s
);
1308 case 'n': // Direct note
1309 send_note(ch
,read_int(&s
)+channels
[ch
].transpose
,0,&s
);
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
);
1320 if(channels
[ch
].row_cut
<max_frames
) frames
[channels
[ch
].row_cut
].data
[ch
].continue_flag
=0;
1323 case '/': // Auto portamento
1324 channels
[ch
].auto_portamento
=1;
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
;
1333 case '|': // Go back to loop restart, or skip if not looping
1336 allocate_frames(channels
[ch
].row
+1);
1337 frames
[channels
[ch
].row
].used
=1;
1338 channels
[ch
].loopskip
=channels
[ch
].row
;
1340 allocate_frames(channels
[ch
].row
+1);
1341 frames
[channels
[ch
].row
].used
=1;
1342 channels
[ch
].loopend
=channels
[ch
].row
;
1344 //todo (begin runtime repeat block)
1346 fprintf(stderr
,"Unknown command in channel %c: |%c\n", ch
+'A', c
);
1350 case ':': // Runtime repeat block end/entry point by order number
1353 //todo (end runtime repeat block)
1354 } else if(c
=='$' || (c
>='0' && c
<='9')) {
1355 //todo (entry point by order number)
1357 fprintf(stderr
,"Unknown command in channel %c: :%c\n", ch
+'A', c
);
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') {
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
);
1388 fprintf(stderr
,"Unknown command in channel %c: @%c\n", ch
+'A', *s
++);
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");
1397 case '<': // Low octave
1398 channels
[ch
].octave
-=octave_dir
;
1399 if(channels
[ch
].octave
>9) fprintf(stderr
,"Abnormal octave number (<).\n");
1402 case '>': // High octave
1403 channels
[ch
].octave
+=octave_dir
;
1404 if(channels
[ch
].octave
>9) fprintf(stderr
,"Abnormal octave number (>).\n");
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
);
1413 case 'K': // Transpose
1414 channels
[ch
].transpose
=read_int(&s
);
1418 send_channel(ch
,200,FX('M'),read_int(&s
),0,255,0,0);
1421 case 'G': // Global volume
1422 send_global(channels
[ch
].row
,FX('V'),read_int(&s
),255,0,1);
1425 case 'X': // Call macro
1426 x
=read_int(&s
); skip_spaces(&s
);
1428 fprintf(stderr
,"Wrong macro number (%d).\n",x
);
1432 process_channel(ch
,macro_text
[x
][ch
]);
1434 s
=macro_text
[x
][ch
]; // tail recursive
1438 case 'C': // Set note cuts
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
);
1446 case 'q': // Set quantize
1447 channels
[ch
].quantize
=read_int(&s
);
1450 case 't': // Set tempo
1451 send_global(channels
[ch
].row
,FX('T'),read_int(&s
),255,0,1);
1454 case '[': // Repeat begin
1455 if(loop
) fprintf(stderr
,"Prohibit nesting repeats.\n");
1460 case ']': // Repeat end
1462 fprintf(stderr
,"*FATAL* Repeat block ends without starting.\n");
1465 if((x
=read_int(&s
))<0) fprintf(stderr
,"Abnormal repeat count.\n");
1466 if(x
==++loopc
) loop
=0; else s
=loop
;
1470 channels
[ch
].effectid
=FX('I');
1471 channels
[ch
].effectpar
=read_int(&s
)<<4;
1472 channels
[ch
].effectpar
|=read_int(&s
);
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
);
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
);
1487 case 'P': // Panning (volume column)
1488 channels
[ch
].volumeset
=read_int(&s
)|128;
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
);
1497 case 'V': // Vibrato
1499 if(x
<1 || x
>15) fprintf(stderr
,"Abnormal vibrato speed.\n");
1500 channels
[ch
].effectpar
=x
<<4;
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;
1509 fprintf(stderr
,"Abnormal vibrato depth.\n");
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
);
1519 case 'R': // Portamento
1521 if(x
<-0xDF || !x
|| x
>0xDF) {
1522 fprintf(stderr
,"Abnormal portamento rate.\n");
1524 channels
[ch
].effectid
=FX('F');
1525 channels
[ch
].effectpar
=x
;
1527 channels
[ch
].effectid
=FX('E');
1528 channels
[ch
].effectpar
=-x
;
1532 case 'z': // Cancel effects
1533 channels
[ch
].effectid
=0;
1534 channels
[ch
].volumeset
=255;
1537 case '\\': // Synchronize channel
1540 fprintf(stderr
,"Cannot synchronize wrong channel.\n");
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
;
1552 fprintf(stderr
,"Unknown command in channel %c: %c\n", ch
+'A', c
);
1556 if(loop
) fprintf(stderr
,"Repeat block not ended (repeat is ignored).\n");
1559 void process_line(char*s
) {
1562 case 0: case ';': break;
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");
1571 case '#': hash_directive(s
+1); break;
1572 case '@': at_definition(s
+1); break;
1574 for(t
=s
;0x40&*t
;t
++);
1575 while(*s
>='A' && *s
<='P') process_channel(*s
++-'A',t
);
1578 fprintf(stderr
,"Illegal character at start of line: %c\n", *s
);
1582 void read_file(FILE*fp
) {
1584 int bp
=0; // buffer mark
1585 int ns
=0; // not space
1587 boolean bl
=1; // 0 to block of multi lines
1588 boolean com
=1; // 0 to comment out
1591 if((c
=fgetc(fp
))==EOF
) break;
1596 buf
[bp
++]=c
>' '?c
:' ';
1598 fprintf(stderr
,"*FATAL* Overlong line of input.\n");
1602 if(bp
) buf
[bp
-1]*=(com
=(*buf
<36 || c
!=';')); // cut off comment
1609 if(c
=='\r' || c
=='\n') {
1613 if(trace_input
) fprintf(stderr
,"+ %s\n",buf
);
1620 if(trace_input
) fprintf(stderr
,"<EOF>\n");
1623 void make_defaults(void) {
1625 for(x
=1;x
<100;x
++) {
1627 samples
[x
].global_volume
=64;
1628 samples
[x
].flag
=0x00;
1629 samples
[x
].default_volume
=64;
1630 samples
[x
].convert
=0x00;
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;
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;
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;
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;
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
);
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
);
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;
1713 for(x
=0;x
<len
;x
++) dbl
[x
]=0;
1718 if(*cmd
=='[') cmd
++;
1723 z1
=read_int(&cmd
); // duty
1724 z2
=read_int(&cmd
); // amplitude
1726 fprintf(stderr
,"*FATAL* Improper synthesizer for sample %d.\n",n
);
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') {
1733 z1
=read_int(&cmd
); // amplitude
1735 fprintf(stderr
,"*FATAL* Improper synthesizer for sample %d.\n",n
);
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') {
1747 } else if(*cmd
=='R') {
1750 z1
=read_int(&cmd
); // divider
1751 z2
=read_int(&cmd
); // amplitude
1752 z3
=read_int(&cmd
); // exponent
1755 fprintf(stderr
,"*FATAL* Improper synthesizer for sample %d.\n",n
);
1758 for(x
=0;x
<len
;x
++) {
1759 if(!(x
%z1
)) q
=pow(rand_double(),z3
)/(double)z2
;
1762 } else if(0x5F&*cmd
) {
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;
1769 fprintf(stderr
,"*FATAL* Improper synthesizer for sample %d.\n",n
);
1772 for(x
=0;x
<len
;x
++) dbl
[x
]+=sin((q
+tau
*(double)(z1
*x
))/per
)/(double)z2
;
1776 // Return byte buffer
1778 samples
[n
].flag
|=0x02;
1779 samples
[n
].convert
|=0x01;
1780 for(x
=0;x
<len
;x
++) {
1782 z1
=lround(dbl
[x
]*0x8000);
1783 if(z1
<-0x7FFF) z1
=-0x7FFF;
1784 if(z1
>0x7FFF) z1
=0x7FFF;
1786 buf
[1|(x
<<1)]=z1
>>8;
1789 for(x
=0;x
<len
;x
++) {
1791 z1
=lround((dbl
[x
]+1)*128);
1801 byte
*synthesizer_b(byte n
) {
1802 char*cmd
=samples
[n
].name
+1;
1803 byte
*buf
=malloc(0x5000); // limit=0x4000
1811 // Read commands and make buffer
1814 if(*cmd
=='[') cmd
++;
1819 samples
[n
].flag
|=0x10;
1820 samples
[n
].loop_start
=len
;
1821 } else if(*cmd
=='(' || *cmd
=='$' || (*cmd
>='0' && *cmd
<='9') || *cmd
=='-' || *cmd
=='+') {
1825 fprintf(stderr
,"*FATAL* Too many nested repeats in sample %d.\n",n
);
1828 loops
[loopnest
]=len
>>synth16bits
;
1829 slows
[loopnest
++]=slow
;
1835 if(synth16bits
) buf
[len
++]=x
>>8;
1838 } else if(*cmd
==')') {
1841 x
=read_int(&cmd
)?:1;
1846 while(y
<z
) buf
[len
++]=buf
[y
++];
1848 slow
=slows
[loopnest
];
1850 fprintf(stderr
,"Stack underflow in sample %d.\n",n
);
1852 } else if(*cmd
>' ') {
1854 fprintf(stderr
,"Improper synthesizer for sample %d.\n",n
);
1857 fprintf(stderr
,"*FATAL* Overlong sample %d.\n",n
);
1863 samples
[n
].convert
=0x00;
1864 samples
[n
].length
=samples
[n
].loop_end
=len
>>synth16bits
;
1866 samples
[n
].flag
|=0x02;
1867 samples
[n
].convert
|=0x01;
1872 void do_export_sample(void) {
1873 sample_data
*sam
=samples
+export_sample
;
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
);
1882 fprintf(stderr
,"*FATAL* Cannot export sample %d to stdout.\n",export_sample
);
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)
1895 struct pat_row
*next
;
1899 pat_row
*pat_loop_begin
;
1900 pat_row
*pat_loop_skip
;
1901 pat_row
*pat_loop_end
;
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;
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) {
1920 pat_row
*row
=malloc(sizeof(pat_row
));
1922 fprintf(stderr
,"*FATAL* Out of memory to make pattern rows.\n");
1927 row
->instrument
[x
]=row
->volume
[x
]=row
->effectid
[x
]=255;
1933 inline void make_pattern_rows(void) {
1939 byte chve
[17]; // volume
1940 byte chxi
[17]; // effect ID
1941 byte chxp
[17]; // effect parameter
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;
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) {
1964 row
->effectid
[17]=FX('S');
1965 row
->effectpar
[17]=0xE0|frameskip
;
1967 row
->next
=new_pat_row();
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
;
1977 chan_row
*fr
=frames
[x
].data
+y
;
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
;
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;
2004 inline void make_pattern_data(void) {
2005 int rc
; // row count per pattern
2006 pat_row
*row
=pat_start
;
2010 byte lastinstrument
[32];
2011 byte lastvolumepan
[32];
2012 byte lastcommand
[32];
2013 byte lastcommandvalue
[32];
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)
2026 fprintf("*FATAL* Out of pattern data memory for pattern %d.\n",pat_count
);
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
2035 lastinstrument
[x
]=lastvolumepan
[x
]=lastcommand
[x
]=lastcommandvalue
[x
]=lastmask
[x
]=255;
2039 if(rc
==200) goto done_pattern
;
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)
2044 if((mask
&128) && row
->effectpar
[x
]!=lastcommandvalue
[x
]) mask
-=120;
2046 #define rowMask(xx,yy,zzz,zz) (zz[x]=row->xx[x]!=yy?row->xx[x]:zz[x])
2048 if(row
->effectid
[x
]!=255) lastcommandvalue
[x
]=row
->effectpar
[x
];
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
];
2059 if(row
==pat_loop_begin
|| row
==pat_loop_skip
|| row
==pat_loop_end
) {
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
2070 if(row
=row
->next
) if(bs
) goto next_row
;
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;
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) {
2092 for(o
=0;o
<256;o
++) order
[o
]=255;
2095 if(pattern_loopstart
==255) {
2096 // Make simple order
2097 while(p
<pat_count
) order
[o
++]=p
++;
2099 // Make order before loop start
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");
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
2137 //=========================================//
2139 int main(int argc
,char**argv
) {
2140 argv
+=!!argc
; argc
-=!!argc
;
2147 case 'c': // Compatibility setting
2149 compatibility
=read_int(&s
);
2151 case 'e': // Export sample to stdout
2153 export_sample
=read_int(&s
);
2155 case 'p': // Prefix for filename when loading external samples
2156 filename_prefix
=*++argv
; argc
--;
2158 case 't': // Trace input
2161 case 'v': // Version
2162 puts(itmck_version
);
2164 case 'w': // Set Cwt header
2166 tracker_version
=read_int(&s
);
2168 case 'x': // Volume 0 mix optimization
2172 fprintf(stderr
,"*FATAL* Unrecognized switch: -%c\n",*o
);
2178 fprintf(stderr
,"*FATAL* Unrecognized command-line option: %s\n",*argv
);
2188 _setmode(_fileno(stdout
),_O_BINARY
);
2203 // http://16-bits.org/it/ for information of .IT format
2204 // http://woolyss.com/chipmusic/chipmusic-mml/ppmck_guide.php for MCK