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 channels
[ch
].loopstart
=channels
[ch
].row
;
1331 case '|': // Go back to loop restart, or skip if not looping
1334 channels
[ch
].loopskip
=channels
[ch
].row
;
1336 channels
[ch
].loopend
=channels
[ch
].row
;
1338 //todo (begin runtime repeat block)
1340 fprintf(stderr
,"Unknown command in channel %c: |%c\n", ch
+'A', c
);
1344 case ':': // Runtime repeat block end/entry point by order number
1347 //todo (end runtime repeat block)
1348 } else if(c
=='$' || (c
>='0' && c
<='9')) {
1349 //todo (entry point by order number)
1351 fprintf(stderr
,"Unknown command in channel %c: :%c\n", ch
+'A', c
);
1355 case '@': // Set instrument or key table or quantize or other channel or etc
1356 if(*s
=='{' || *s
=='}') {
1357 s
++; // for use with text macros
1358 } else if(*s
=='=') {
1359 s
++; x
=(*s
++&0x1F)%27; numeric_register
[x
]=read_int(&s
);
1360 } else if(*s
=='+') {
1361 s
++; x
=(*s
++&0x1F)%27; numeric_register
[x
]+=read_int(&s
);
1362 } else if(*s
=='-') {
1363 s
++; x
=(*s
++&0x1F)%27; numeric_register
[x
]-=read_int(&s
);
1364 } else if(*s
=='*') {
1365 s
++; x
=(*s
++&0x1F)%27; numeric_register
[x
]*=read_int(&s
);
1366 } else if(*s
=='q') {
1367 s
++; channels
[ch
].quantize
=-read_int(&s
);
1368 } else if(*s
=='e') {
1369 s
++; channels
[ch
].effectid
=read_int(&s
); channels
[ch
].effectpar
=read_int(&s
); channels
[ch
].volumeset
=read_int(&s
);
1370 } else if(*s
>='A' && *s
<='P') {
1372 } else if((*s
>='0' && *s
<='9') || *s
=='$') {
1373 channels
[ch
].instrument
=read_int(&s
); channels
[ch
].keytable
=255;
1374 if(channels
[ch
].instrument
>max_instrument
) fprintf(stderr
,"Unuse instrument %d.\n",channels
[ch
].instrument
);
1375 } else if(*s
=='@') {
1376 t
=++s
; x
=read_int(&s
); channels
[ch
].keytable
=(t
==s
)?255:x
;
1377 } else if(*s
=='u') {
1378 t
=++s
; x
=read_int(&s
); channels
[ch
].customfx
=(t
==s
)?255:x
;
1379 } else if(*s
=='`') {
1380 *numeric_register
=at_backtick(ch
,loopc
,*++s
);
1382 fprintf(stderr
,"Unknown command in channel %c: @%c\n", ch
+'A', *s
++);
1386 case 'o': // Octave set
1387 channels
[ch
].octave
=read_int(&s
);
1388 if(channels
[ch
].octave
>9) fprintf(stderr
,"Abnormal octave number (o).\n");
1391 case '<': // Low octave
1392 channels
[ch
].octave
-=octave_dir
;
1393 if(channels
[ch
].octave
>9) fprintf(stderr
,"Abnormal octave number (<).\n");
1396 case '>': // High octave
1397 channels
[ch
].octave
+=octave_dir
;
1398 if(channels
[ch
].octave
>9) fprintf(stderr
,"Abnormal octave number (>).\n");
1402 channels
[ch
].length
=read_int(&s
);
1403 if(channels
[ch
].length
<=0 || whole_note
%channels
[ch
].length
)
1404 fprintf(stderr
,"Abnormal note length (%d/%d).\n",whole_note
,channels
[ch
].length
);
1407 case 'K': // Transpose
1408 channels
[ch
].transpose
=read_int(&s
);
1412 send_channel(ch
,200,FX('M'),read_int(&s
),0,255,0,0);
1415 case 'G': // Global volume
1416 send_global(channels
[ch
].row
,FX('V'),read_int(&s
),255,0,1);
1419 case 'X': // Call macro
1420 x
=read_int(&s
); skip_spaces(&s
);
1422 fprintf(stderr
,"Wrong macro number (%d).\n",x
);
1426 process_channel(ch
,macro_text
[x
][ch
]);
1428 s
=macro_text
[x
][ch
]; // tail recursive
1432 case 'C': // Set note cuts
1434 if(x
=='o') channels
[ch
].note_cuts
=255;
1435 else if(x
=='c') channels
[ch
].note_cuts
=254;
1436 else if(x
=='f') channels
[ch
].note_cuts
=253;
1437 else fprintf(stderr
,"Unknown command in channel %c: C%c\n",ch
+'A',x
);
1440 case 'q': // Set quantize
1441 channels
[ch
].quantize
=read_int(&s
);
1444 case 't': // Set tempo
1445 send_global(channels
[ch
].row
,FX('T'),read_int(&s
),255,0,1);
1448 case '[': // Repeat begin
1449 if(loop
) fprintf(stderr
,"Prohibit nesting repeats.\n");
1454 case ']': // Repeat end
1456 fprintf(stderr
,"*FATAL* Repeat block ends without starting.\n");
1459 if((x
=read_int(&s
))<0) fprintf(stderr
,"Abnormal repeat count.\n");
1460 if(x
==++loopc
) loop
=0; else s
=loop
;
1464 channels
[ch
].effectid
=FX('I');
1465 channels
[ch
].effectpar
=read_int(&s
)<<4;
1466 channels
[ch
].effectpar
|=read_int(&s
);
1469 case 'J': // Arpeggio
1470 channels
[ch
].effectid
=FX('J');
1471 channels
[ch
].effectpar
=read_int(&s
)<<4;
1472 channels
[ch
].effectpar
|=read_int(&s
);
1475 case 'T': // Tremolo
1476 channels
[ch
].effectid
=FX('R');
1477 channels
[ch
].effectpar
=read_int(&s
)<<4;
1478 channels
[ch
].effectpar
|=read_int(&s
);
1481 case 'P': // Panning (volume column)
1482 channels
[ch
].volumeset
=read_int(&s
)|128;
1485 case 'Q': // Retrigger
1486 channels
[ch
].effectid
=FX('Q');
1487 channels
[ch
].effectpar
=read_int(&s
)<<4;
1488 channels
[ch
].effectpar
|=read_int(&s
);
1491 case 'V': // Vibrato
1493 if(x
<1 || x
>15) fprintf(stderr
,"Abnormal vibrato speed.\n");
1494 channels
[ch
].effectpar
=x
<<4;
1497 channels
[ch
].effectid
=FX('U');
1498 channels
[ch
].effectpar
|=x
;
1499 } else if(x
>0 && x
<=60 && !(x
&3)) {
1500 channels
[ch
].effectid
=FX('H');
1501 channels
[ch
].effectpar
|=x
>>2;
1503 fprintf(stderr
,"Abnormal vibrato depth.\n");
1507 case 'Y': // Panbrello
1508 channels
[ch
].effectid
=FX('Y');
1509 channels
[ch
].effectpar
=read_int(&s
)<<4;
1510 channels
[ch
].effectpar
|=read_int(&s
);
1513 case 'R': // Portamento
1515 if(x
<-0xDF || !x
|| x
>0xDF) {
1516 fprintf(stderr
,"Abnormal portamento rate.\n");
1518 channels
[ch
].effectid
=FX('F');
1519 channels
[ch
].effectpar
=x
;
1521 channels
[ch
].effectid
=FX('E');
1522 channels
[ch
].effectpar
=-x
;
1526 case 'z': // Cancel effects
1527 channels
[ch
].effectid
=0;
1528 channels
[ch
].volumeset
=255;
1531 case '\\': // Synchronize channel
1534 fprintf(stderr
,"Cannot synchronize wrong channel.\n");
1536 channels
[ch
].row
=channels
[x
].row
;
1537 channels
[ch
].row_cut
=channels
[x
].row_cut
;
1538 channels
[ch
].row_tie
=channels
[x
].row_tie
;
1539 channels
[ch
].loopstart
=channels
[x
].loopstart
;
1540 channels
[ch
].loopskip
=channels
[x
].loopskip
;
1541 channels
[ch
].loopend
=channels
[x
].loopend
;
1546 fprintf(stderr
,"Unknown command in channel %c: %c\n", ch
+'A', c
);
1550 if(loop
) fprintf(stderr
,"Repeat block not ended (repeat is ignored).\n");
1553 void process_line(char*s
) {
1556 case 0: case ';': break;
1558 *next_song_message
=malloc(sizeof(message_line
));
1559 (*next_song_message
)->data
=strdup(s
+1);
1560 (*next_song_message
)->next
=0;
1561 next_song_message
=&((*next_song_message
)->next
);
1562 song_message_length
+=strlen(s
); // Includes the '"' which is the same length as newline marker
1563 if(song_message_length
>8000) fprintf(stderr
,"Overlong song message.\n");
1565 case '#': hash_directive(s
+1); break;
1566 case '@': at_definition(s
+1); break;
1568 for(t
=s
;0x40&*t
;t
++);
1569 while(*s
>='A' && *s
<='P') process_channel(*s
++-'A',t
);
1572 fprintf(stderr
,"Illegal character at start of line: %c\n", *s
);
1576 void read_file(FILE*fp
) {
1578 int bp
=0; // buffer mark
1579 int ns
=0; // not space
1581 boolean bl
=1; // 0 to block of multi lines
1582 boolean com
=1; // 0 to comment out
1585 if((c
=fgetc(fp
))==EOF
) break;
1590 buf
[bp
++]=c
>' '?c
:' ';
1592 fprintf(stderr
,"*FATAL* Overlong line of input.\n");
1596 if(bp
) buf
[bp
-1]*=(com
=(*buf
<36 || c
!=';')); // cut off comment
1603 if(c
=='\r' || c
=='\n') {
1607 if(trace_input
) fprintf(stderr
,"+ %s\n",buf
);
1614 if(trace_input
) fprintf(stderr
,"<EOF>\n");
1617 void make_defaults(void) {
1619 for(x
=1;x
<100;x
++) {
1621 samples
[x
].global_volume
=64;
1622 samples
[x
].flag
=0x00;
1623 samples
[x
].default_volume
=64;
1624 samples
[x
].convert
=0x00;
1626 samples
[x
].length
=samples
[x
].loop_start
=samples
[x
].loop_end
=0;
1627 samples
[x
].c5speed
=8363;
1628 samples
[x
].sustain_start
=samples
[x
].sustain_end
=0;
1633 for(y
=0;y
<11;y
++) instruments
[x
].heading
[y
]=0;
1634 instruments
[x
].heading
[6]=60;
1635 instruments
[x
].heading
[7]=0x40;
1636 instruments
[x
].heading
[8]=0xA0;
1637 instruments
[x
].ifc
=instruments
[x
].ifr
=0;
1638 for(y
=0;y
<120;y
++) {
1639 instruments
[x
].keyboard
[y
<<1]=y
;
1640 instruments
[x
].keyboard
[1|(y
<<1)]=0;
1642 instruments
[x
].used
=0;
1643 instruments
[x
].vol_env
[0]=instruments
[x
].vol_env
[1]=0;
1644 instruments
[x
].pan_env
[0]=instruments
[x
].pan_env
[1]=0;
1645 instruments
[x
].pitch_env
[0]=instruments
[x
].pitch_env
[1]=0;
1649 for(y
=0;y
<120;y
++) {
1650 keytab
[x
][y
].note
=y
;
1651 keytab
[x
][y
].effectid
=0;
1652 keytab
[x
][y
].instrument
=keytab
[x
][y
].volumeset
=keytab
[x
][y
].customfx
=255;
1655 fxtab
[x
].transpose
=0;
1656 fxtab
[x
].instrument
=255;
1657 fxtab
[x
].effectid
=0;
1658 fxtab
[x
].volumeset
=255;
1659 fxtab
[x
].continue_flag
=0;
1665 channels
[x
].transpose
=0;
1666 channels
[x
].octave
=5;
1667 channels
[x
].length
=4;
1668 channels
[x
].instrument
=1;
1669 channels
[x
].effectid
=0;
1670 channels
[x
].volumeset
=255;
1671 channels
[x
].quantize
=0;
1672 channels
[x
].note_cuts
=255;
1673 channels
[x
].ch_vol
=64;
1674 channels
[x
].ch_pan
=32;
1675 channels
[x
].row
=channels
[x
].row_cut
=channels
[x
].row_tie
=0;
1676 channels
[x
].loopstart
=channels
[x
].loopskip
=channels
[x
].loopend
=-1;
1677 channels
[x
].keytable
=255;
1678 channels
[x
].customfx
=255;
1679 channels
[x
].auto_portamento
=0;
1680 channels
[x
].prev_note
=254;
1684 byte
*synthesizer_a(byte n
) {
1685 char*cmd
=samples
[n
].name
+1;
1686 int per
=lround(((double)samples
[n
].c5speed
)/C_pitch
); // period (before multiply)
1687 int len
=lround(((double)(samples
[n
].c5speed
*synth_len_mult
))/C_pitch
);
1695 x
=read_int(&cmd
)?:1000; amp
=0.001*(double)x
;
1696 len
*=read_int(&cmd
)?:1;
1697 dbl
=malloc(len
*sizeof(double));
1698 buf
=malloc(len
<<synth16bits
);
1701 samples
[n
].length
=samples
[n
].loop_end
=len
;
1702 samples
[n
].flag
|=0x10;
1703 samples
[n
].convert
=0x00;
1704 samples
[n
].loop_start
=0;
1707 for(x
=0;x
<len
;x
++) dbl
[x
]=0;
1712 if(*cmd
=='[') cmd
++;
1717 z1
=read_int(&cmd
); // duty
1718 z2
=read_int(&cmd
); // amplitude
1720 fprintf(stderr
,"*FATAL* Improper synthesizer for sample %d.\n",n
);
1723 for(x
=0;x
<len
;x
++) dbl
[x
]=(x
%per
<(z1
*per
)/1000)?(1.0/(double)z2
):(dbl
[x
]-1.0/(double)z2
);
1724 } else if(*cmd
=='N') {
1727 z1
=read_int(&cmd
); // amplitude
1729 fprintf(stderr
,"*FATAL* Improper synthesizer for sample %d.\n",n
);
1733 for(x
=0;x
<len
;x
++) {
1734 r
=q
*(double)(x
%per
-per
/2);
1735 dbl
[x
]=fabs(dbl
[x
])<r
?r
:dbl
[x
];
1737 } else if(*cmd
=='V') {
1741 } else if(*cmd
=='R') {
1744 z1
=read_int(&cmd
); // divider
1745 z2
=read_int(&cmd
); // amplitude
1746 z3
=read_int(&cmd
); // exponent
1749 fprintf(stderr
,"*FATAL* Improper synthesizer for sample %d.\n",n
);
1752 for(x
=0;x
<len
;x
++) {
1753 if(!(x
%z1
)) q
=pow(rand_double(),z3
)/(double)z2
;
1756 } else if(0x5F&*cmd
) {
1758 z1
=read_int(&cmd
); // frequency
1759 z2
=read_int(&cmd
); // amplitude
1760 z3
=read_int(&cmd
); // phase
1761 q
=(tau
*(double)z3
)/1000.0;
1763 fprintf(stderr
,"*FATAL* Improper synthesizer for sample %d.\n",n
);
1766 for(x
=0;x
<len
;x
++) dbl
[x
]+=sin((q
+tau
*(double)(z1
*x
))/per
)/(double)z2
;
1770 // Return byte buffer
1772 samples
[n
].flag
|=0x02;
1773 samples
[n
].convert
|=0x01;
1774 for(x
=0;x
<len
;x
++) {
1776 z1
=lround(dbl
[x
]*0x8000);
1777 if(z1
<-0x7FFF) z1
=-0x7FFF;
1778 if(z1
>0x7FFF) z1
=0x7FFF;
1780 buf
[1|(x
<<1)]=z1
>>8;
1783 for(x
=0;x
<len
;x
++) {
1785 z1
=lround((dbl
[x
]+1)*128);
1795 byte
*synthesizer_b(byte n
) {
1796 char*cmd
=samples
[n
].name
+1;
1797 byte
*buf
=malloc(0x5000); // limit=0x4000
1805 // Read commands and make buffer
1808 if(*cmd
=='[') cmd
++;
1813 samples
[n
].flag
|=0x10;
1814 samples
[n
].loop_start
=len
;
1815 } else if(*cmd
=='(' || *cmd
=='$' || (*cmd
>='0' && *cmd
<='9') || *cmd
=='-' || *cmd
=='+') {
1819 fprintf(stderr
,"*FATAL* Too many nested repeats in sample %d.\n",n
);
1822 loops
[loopnest
]=len
>>synth16bits
;
1823 slows
[loopnest
++]=slow
;
1829 if(synth16bits
) buf
[len
++]=x
>>8;
1832 } else if(*cmd
==')') {
1835 x
=read_int(&cmd
)?:1;
1840 while(y
<z
) buf
[len
++]=buf
[y
++];
1842 slow
=slows
[loopnest
];
1844 fprintf(stderr
,"Stack underflow in sample %d.\n",n
);
1846 } else if(*cmd
>' ') {
1848 fprintf(stderr
,"Improper synthesizer for sample %d.\n",n
);
1851 fprintf(stderr
,"*FATAL* Overlong sample %d.\n",n
);
1857 samples
[n
].convert
=0x00;
1858 samples
[n
].length
=samples
[n
].loop_end
=len
>>synth16bits
;
1860 samples
[n
].flag
|=0x02;
1861 samples
[n
].convert
|=0x01;
1866 void do_export_sample(void) {
1867 sample_data
*sam
=samples
+export_sample
;
1870 if(export_sample
<=max_sample
&& sam
->make
==2) {
1871 if(sam
->name
[0]=='a') buf
=synthesizer_a(export_sample
);
1872 if(sam
->name
[0]=='b') buf
=synthesizer_b(export_sample
);
1873 fwrite(buf
,1,sam
->length
<<synth16bits
,stdout
);
1876 fprintf(stderr
,"*FATAL* Cannot export sample %d to stdout.\n",export_sample
);
1881 //=========================================//
1883 typedef struct pat_row
{
1884 byte note
[18]; // 200=unuse
1885 byte instrument
[18]; // 255=unuse
1886 byte volume
[18]; // 255=unuse
1887 byte effectid
[18]; // 255=unuse
1889 struct pat_row
*next
;
1893 pat_row
*pat_loop_begin
;
1894 pat_row
*pat_loop_skip
;
1895 pat_row
*pat_loop_end
;
1899 unsigned long pat_offs
[200];
1900 unsigned long sam_offs
;
1901 unsigned long sam_raw_offs
[100];
1902 unsigned long inst_offs
;
1903 unsigned long msg_offs
=0;
1905 void output_calc(void) {
1909 void output_main(void) {
1910 fwrite("IMPM",1,4,stdout
); // File type identification
1911 fwrite(song_title
,1,26,stdout
); // Song Name
1912 write16(0); // PHiligt
1916 //=========================================//
1918 int main(int argc
,char**argv
) {
1919 argv
+=!!argc
; argc
-=!!argc
;
1926 case 'c': // Compatibility setting
1928 compatibility
=read_int(&s
);
1930 case 'e': // Export sample to stdout
1932 export_sample
=read_int(&s
);
1934 case 'p': // Prefix for filename when loading external samples
1935 filename_prefix
=*++argv
; argc
--;
1937 case 't': // Trace input
1940 case 'v': // Version
1941 puts(itmck_version
);
1943 case 'w': // Set Cwt header
1945 tracker_version
=read_int(&s
);
1947 case 'x': // Volume 0 mix optimization
1951 fprintf(stderr
,"*FATAL* Unrecognized switch: -%c\n",*o
);
1957 fprintf(stderr
,"*FATAL* Unrecognized command-line option: %s\n",*argv
);
1967 _setmode(_fileno(stdout
),_O_BINARY
);
1982 // http://16-bits.org/it/ for information of .IT format
1983 // http://woolyss.com/chipmusic/chipmusic-mml/ppmck_guide.php for MCK