4 Ann Hell Ex Machina - Music Software
5 Copyright (C) 2003/2006 Angel Ortega <angel@triptico.com>
7 compiler.y - Scripting language YACC parser
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 http://www.triptico.com
41 void yyerror(char * s
);
43 /* injecting code functions (defined in compiler.l) */
44 int push_code
(char * code
);
45 int push_code_from_file
(char * file
);
57 static double staccato
;
61 /* parser debugging */
63 #define DEBUGF if(verbose >= 2)printf
74 static struct block
* blocks
= NULL
;
75 static int n_blocks
= 0;
85 static struct group
* groups
= NULL
;
86 static int n_groups
= 0;
87 static int groups_size
= 0;
96 static struct mark
* marks
= NULL
;
97 static int n_marks
= 0;
108 static struct arp
* arps
= NULL
;
109 static int n_arps
= 0;
110 static int arps_size
= 0;
112 static double arp_delay
;
113 static int arp_transpose
;
114 static float arp_volume
;
115 static int arp_track_off
;
117 int compiler_error
= 0;
121 /* alterations for each note */
125 unsigned long block_seed
= 0;
129 ********************/
131 unsigned long ah_rnd
(unsigned long * seed
)
132 /* special randomizer */
134 *seed
= (*seed
* 58321) + 11113;
140 void c_err
(char * e1
, char * e2
, char * e3
)
141 /* reports an error from the compiler */
144 if
(e1
!= NULL
) printf
(" %s", e1
);
145 if
(e2
!= NULL
) printf
(" %s", e2
);
146 if
(e3
!= NULL
) printf
(" %s", e3
);
147 printf
(" in line %d\n", yyline
);
153 static void forward
(double step
)
154 /* moves forward current time */
159 /* quantizations could be done here */
165 static int find_block
(char * name
)
170 for
(n
= 0;n
< n_blocks
;n
++)
172 if
(strcmp
(name
, blocks
[n
].name
) == 0)
180 static int set_block
(char * name
, char * block
)
181 /* defines a block */
188 /* if block already exists, free it */
189 if
((n
= find_block
(name
)) >= 0)
193 /* free all subblocks */
194 for
(n
= 0;n
< b
->n_sblocks
;n
++)
197 /* free the subblock array */
202 GROW
(blocks
, n_blocks
, struct block
);
203 b
= &blocks
[n_blocks
++];
205 b
->name
= strdup
(name
);
212 /* now split in subblocks (delimited by : ) */
215 while
((stop
= strchr
(start
, ':')) != NULL
)
220 /* add the subblock */
221 GROW
(b
->sblocks
, b
->n_sblocks
, char *);
222 b
->sblocks
[b
->n_sblocks
++] = strdup
(start
);
227 /* no more separators? store the rest */
228 GROW
(b
->sblocks
, b
->n_sblocks
, char *);
229 b
->sblocks
[b
->n_sblocks
++] = strdup
(start
);
231 /* the original block is no longer needed */
238 static void insert_block
(char * name
)
239 /* inserts a block */
243 if
((n
= find_block
(name
)) >= 0)
249 /* get one of them, randomly */
250 n
= ah_rnd
(&block_seed
) % b
->n_sblocks
;
252 push_code
(strdup
(b
->sblocks
[n
]));
255 c_err
("block-not-found", name
, NULL
);
259 static int insert_file
(char * filename
)
261 if
(!push_code_from_file
(filename
))
263 c_err
("script-not-found", filename
, NULL
);
273 static int push_group
(void)
274 /* starts a new group of notes */
278 if
(n_groups
== groups_size
)
280 GROW
(groups
, groups_size
, struct group
);
284 g
= &groups
[n_groups
++];
294 static int next_group_part
(void)
301 c_err
("missing-start-of-group", NULL
, NULL
);
305 g
= &groups
[n_groups
- 1];
307 /* store maximum frame */
308 if
(g
->max
< cur_time
)
311 /* add glissando delay */
315 cur_time
= g
->start
+ g
->gliss
;
321 static int pop_group
(void)
322 /* finishes a group, moving the frame to the end of the longer part */
326 c_err
("missing-start-of-group", NULL
, NULL
);
332 /* if other parts of group were longer than the last one,
333 move pointer there */
334 if
(groups
[n_groups
].max
> cur_time
)
335 cur_time
= groups
[n_groups
].max
;
343 static int seek_mark
(char * name
)
344 /* seeks a mark by name; returns its offset or -1 */
348 for
(n
= 0;n
< n_marks
;n
++)
349 if
(strcmp
(marks
[n
].name
, name
) == 0)
356 static void add_mark
(char * name
)
357 /* creates a new mark */
361 if
((n
= seek_mark
(name
)) == -1)
364 GROW
(marks
, n
, struct mark
);
365 marks
[n
].name
= strdup
(name
);
368 marks
[n
].time
= cur_time
;
372 static void find_mark
(char * name
, int set
)
373 /* finds a mark by name, optionally moving time cursor there */
377 if
((n
= seek_mark
(name
)) != -1)
381 if
(cur_time
> marks
[n
].time
)
382 c_err
("mark-overpassed", name
, NULL
);
384 cur_time
= marks
[n
].time
;
387 if
(cur_time
!= marks
[n
].time
)
388 c_err
("mark-mismatch", name
, NULL
);
393 c_err
("mark-not-found", name
, NULL
);
399 static void arp_default
(void)
400 /* resets arpeggiator values to the default ones */
409 static void add_arp
(void)
410 /* adds an arpeggiator note */
414 /* if the note is exactly the same, do nothing */
415 if
(arp_delay
== 0.0 && arp_transpose
== 0 &&
416 arp_volume
== 1.0 && arp_track_off
== 0)
419 if
(n_arps
== arps_size
)
421 GROW
(arps
, arps_size
, struct arp
);
427 a
->delay
= arp_delay
;
428 a
->transpose
= arp_transpose
;
429 a
->volume
= arp_volume
;
430 a
->track_off
= arp_track_off
;
437 static void set_alteration
(char * altstr
)
438 /* sets the alterations from altstr */
440 int n
, steps
[] = { 2, 0, 2, 0, 1, 2, 0, 2, 0, 2, 0, 1 };
442 /* reset alterations */
443 for
(n
= 0;n
< 12;n
++) alterations
[n
] = 0;
445 /* changed according the altstr spec */
446 for
(n
= 0;*altstr
!= '\0' && n
< 12;altstr
++)
450 case
'&': alterations
[n
] = -1; break
;
451 case
'#': alterations
[n
] = 1; break
;
454 /* move to next natural note */
462 static void add_note_event
(int note
)
463 /* adds a note event */
469 /* sum the alteration */
470 if
((n
= note %
12) < 0) n
+= 12;
471 note
+= alterations
[n
];
473 /* calculate the note */
474 np
= note
+ transpose
+ (octave
* 12);
476 /* is note out of range? */
477 if
(np
< 0 || np
> 127)
479 c_err
("note-out-of-range", NULL
, NULL
);
483 e.note.trk_id
= track
;
485 e.note.len
= length
* staccato
;
488 add_song_ev
(SONG_EV_NOTE
, cur_time
, &e
);
490 /* add arpeggiator repetitions */
491 for
(n
= 0;n
< n_arps
;n
++)
493 e.note.trk_id
= track
+ arps
[n
].track_off
;
494 e.note.note
= np
+ arps
[n
].transpose
;
495 e.note.vol
= volume
* arps
[n
].volume
;
497 add_song_ev
(SONG_EV_NOTE
, cur_time
+ arps
[n
].delay
, &e
);
502 static void add_back_event
(void)
506 e.back.trk_id
= track
;
508 add_song_ev
(SONG_EV_BACK
, cur_time
, &e
);
512 static void add_tempo_event
(int trk_id
, double tempo
)
516 e.tempo.trk_id
= trk_id
;
517 e.tempo.tempo
= tempo
;
518 add_song_ev
(SONG_EV_TEMPO
, cur_time
, &e
);
522 static void add_meter_event
(int trk_id
, int num
, int den
)
526 e.meter.trk_id
= trk_id
;
529 add_song_ev
(SONG_EV_METER
, cur_time
, &e
);
532 static void add_measure_event
(void)
536 e.measure.trk_id
= -1;
537 e.measure.line
= yyline
;
539 add_song_ev
(SONG_EV_MEASURE
, cur_time
, &e
);
543 static void add_ss_sustain_event
(double sustain
)
547 e.ss_sustain.trk_id
= track
;
548 e.ss_sustain.sustain
= sustain
;
550 add_song_ev
(SONG_EV_SS_SUSTAIN
, cur_time
, &e
);
554 static void add_ss_vibrato_event
(double depth
, double freq
)
558 e.ss_vibrato.trk_id
= track
;
559 e.ss_vibrato.vib_depth
= depth
;
560 e.ss_vibrato.vib_freq
= freq
;
562 add_song_ev
(SONG_EV_SS_VIBRATO
, cur_time
, &e
);
566 static void add_ss_portamento_event
(double portamento
)
570 e.ss_portamento.trk_id
= track
;
571 e.ss_portamento.portamento
= portamento
;
573 add_song_ev
(SONG_EV_SS_PORTAMENTO
, cur_time
, &e
);
577 static void add_ss_channel_event
(int channel
, float vol
)
581 e.ss_channel.trk_id
= track
;
582 e.ss_channel.channel
= channel
;
583 e.ss_channel.vol
= vol
;
585 add_song_ev
(SONG_EV_SS_CHANNEL
, cur_time
, &e
);
589 static void add_ss_wav_event
(char * wav_file
, int base
, int min
, int max
,
590 double loop_start
, double loop_end
, int first_channel
, int skip_channels
)
594 e.ss_wav.trk_id
= track
;
595 e.ss_wav.file
= wav_file
;
596 e.ss_wav.base
= base
;
599 e.ss_wav.loop_start
= loop_start
;
600 e.ss_wav.loop_end
= loop_end
;
601 e.ss_wav.first_channel
= first_channel
;
602 e.ss_wav.skip_channels
= skip_channels
;
604 add_song_ev
(SONG_EV_SS_WAV
, cur_time
, &e
);
608 static void add_ss_pat_event
(char * pat_file
)
612 e.ss_pat.trk_id
= track
;
613 e.ss_pat.file
= pat_file
;
615 add_song_ev
(SONG_EV_SS_PAT
, cur_time
, &e
);
619 static void add_ss_eff_event
(int type
, int channel
, double size
, float gain
,
620 double depth
, double freq
, double phase
, float initial
, float final
)
624 e.ss_eff.trk_id
= track
;
625 e.ss_eff.channel
= channel
;
626 e.ss_eff.size
= size
;
627 e.ss_eff.gain
= gain
;
628 e.ss_eff.depth
= depth
;
629 e.ss_eff.freq
= freq
;
630 e.ss_eff.phase
= phase
;
631 e.ss_eff.initial
= initial
;
632 e.ss_eff.final
= final
;
634 add_song_ev
(type
, cur_time
, &e
);
638 static void add_ss_pitch_stretch
(int note
, double len
, float vol
)
642 e.ss_pitch_stretch.trk_id
= track
;
643 e.ss_pitch_stretch.note
= note
;
644 e.ss_pitch_stretch.len
= len
;
645 e.ss_pitch_stretch.vol
= vol
;
647 add_song_ev
(SONG_EV_SS_PITCH_STRETCH
, cur_time
, &e
);
651 static void add_ss_print_wave_tempo
(int note
, double len
)
655 e.ss_print_wave_tempo.trk_id
= track
;
656 e.ss_print_wave_tempo.note
= note
;
657 e.ss_print_wave_tempo.len
= len
;
659 add_song_ev
(SONG_EV_SS_PRINT_WAVE_TEMPO
, cur_time
, &e
);
663 static void add_midi_channel_event
(int channel
)
667 e.midi_channel.trk_id
= track
;
668 e.midi_channel.channel
= channel
- 1;
670 add_song_ev
(SONG_EV_MIDI_CHANNEL
, cur_time
, &e
);
674 static void add_midi_program_event
(int program
)
678 e.midi_program.trk_id
= track
;
679 e.midi_program.program
= program
;
681 add_song_ev
(SONG_EV_MIDI_PROGRAM
, cur_time
, &e
);
685 static void add_song_info_event
(char * author
, char * name
)
689 e.song_info.trk_id
= track
;
690 e.song_info.author
= author
;
691 e.song_info.name
= name
;
693 add_song_ev
(SONG_EV_SONG_INFO
, cur_time
, &e
);
697 static void init_track
(void)
698 /* sets the default values for a new track */
710 /* groups should not cross track boundaries */
713 /* reset arpeggiator */
717 /* reset alterations */
718 for
(n
= 0;n
< 12;n
++)
731 %token
<i
> P_INTEGER S_INTEGER
732 %token
<d
> P_REAL S_REAL
733 %token
<i
> NOTE_P NOTE_T3 NOTE_T5
734 %token
<p
> NEW_MARK GOTO_MARK ASSERT_MARK
736 %token
<p
> BLOCK BLK_ASSIGN BLK_INSERT FILE_INSERT
741 %token
<i
> XC_ABSNOTE
744 %token SS_SEP SS_WAV SS_LOOP_WAV SS_PAT
745 %token SS_SUSTAIN SS_VIBRATO SS_PORTAMENTO SS_CHANNEL SS_VOL
747 %token SS_EFF_DELAY SS_EFF_ECHO SS_EFF_COMB SS_EFF_ALLPASS SS_EFF_FLANGER
748 %token SS_EFF_WOBBLE SS_EFF_SQWOBBLE SS_EFF_HFWOBBLE
749 %token SS_EFF_FADER SS_EFF_REVERB SS_EFF_FOLDBACK SS_EFF_OFF
751 %token SS_PITCH_STRETCH SS_TIME_STRETCH SS_PRINT_WAVE_TEMPO
755 %token MIDI_CHANNEL MIDI_PROGRAM MIDI_GENERIC
757 %type
<i
> integer note note_pitch rest back
758 %type
<d
> real p_number number note_length
760 %type
<d
> arp_list arp_note
789 /* absolute octave */
793 /* relative octave */
797 /* absolute volume */
801 /* absolute volume */
805 /* relative volume */
822 /* group delimiter */
835 /* measure mark event */
860 add_tempo_event
(-1, $2);
862 |
'M' P_INTEGER
'/' P_INTEGER
{
863 /* meter (time signature) setting */
864 add_meter_event
(-1, $2, $4);
867 /* alteration string */
871 | BLOCK
'*' P_INTEGER
{
875 /* store the block as <TMP> */
876 set_block
("<TMP>", $1);
878 for
(n
= 0;n
< $3;n
++)
879 insert_block
("<TMP>");
901 P_INTEGER
{ $$
= $1; }
902 | S_INTEGER
{ $$
= $1; }
907 | S_REAL
{ $$
= $1; }
911 P_INTEGER
{ $$
= (double) $1; }
912 | P_REAL
{ $$
= $1; }
916 integer
{ $$
= (double) $1; }
921 note_pitch
{ $$
= $1; }
922 | note note_length
{ $$
= $1; length
=$2; }
923 | note
'~' number
{ $$
= $1; DEBUGF
("imm volume: %lf\n", $3); }
928 | note_pitch
'&' { $$
= $1 - 1; }
929 | note_pitch
'#' { $$
= $1 + 1; }
930 | note_pitch
'\'' { $$
= $1 + 12; }
931 | note_pitch
',' { $$
= $1 - 12; }
935 P_INTEGER
{ $$
= 1 / (double) $1; }
936 | note_length NOTE_T3
{ $$
= $1 * 2.0 / 3.0; }
937 | note_length NOTE_T5
{ $$
= $1 * 4.0 / 5.0; }
938 | note_length
'*' p_number
{ $$
= $1 * $3; }
939 | note_length
'.' { $$
= $1 * 1.5; }
945 /* rest with length */
953 /* back with length */
960 /* empty arpeggiator */
969 /* first arpeggiator note */
973 | arp_list
',' arp_note
{
974 /* rest of arpeggiator notes */
981 /* arpeggiator delay */
984 | arp_note
'~' number
{
985 /* arpeggiator volume */
986 arp_volume
= (float)$3;
988 | arp_note S_INTEGER
{
989 /* arpeggiator transpose */
992 | arp_note
'/' P_INTEGER
{
993 /* arpeggiator track offset */
1000 | arp_note NOTE_T5
{
1007 P_INTEGER
{ $$
= $1; }
1008 | XC_ABSNOTE
{ $$
= $1; }
1012 SS_WAV XC_STR xc_absnote
{
1013 /* load .wav file with just one note */
1014 add_ss_wav_event
($2, $3, $3, $3, -1.0, -1.0, 0, 0);
1016 | SS_WAV XC_STR xc_absnote xc_absnote xc_absnote
{
1017 /* load .wav file */
1018 add_ss_wav_event
($2, $3, $4, $5, -1.0, -1.0, 0, 0);
1020 | SS_WAV XC_STR xc_absnote xc_absnote xc_absnote number number
{
1021 /* load .wav file, with loop boundaries */
1022 add_ss_wav_event
($2, $3, $4, $5, $6, $7, 0, 0);
1024 | SS_WAV XC_STR xc_absnote xc_absnote xc_absnote number number number number
{
1025 /* load .wav file, with loop boundaries,
1026 first channel and skip channels */
1027 add_ss_wav_event
($2, $3, $4, $5, $6, $7, $8, $9);
1030 /* load .pat file */
1031 add_ss_pat_event
($2);
1033 | SS_SUSTAIN XC_MSECS
{
1035 add_ss_sustain_event
($2);
1037 | SS_VIBRATO XC_MSECS number
{
1039 add_ss_vibrato_event
($2, $3);
1041 | SS_PORTAMENTO number
{
1042 /* sets portamento */
1043 add_ss_portamento_event
($2);
1045 | SS_CHANNEL integer number
{
1046 /* sets volume for a channel */
1047 add_ss_channel_event
($2, $3);
1049 | SS_VOL number number
{
1050 /* set vol for 2 channels */
1051 add_ss_channel_event
(0, $2);
1052 add_ss_channel_event
(1, $3);
1054 | SS_VOL number number number
{
1055 /* set vol for 3 channels */
1056 add_ss_channel_event
(0, $2);
1057 add_ss_channel_event
(1, $3);
1058 add_ss_channel_event
(2, $4);
1060 | SS_VOL number number number number
{
1061 /* set vol for 4 channels */
1062 add_ss_channel_event
(0, $2);
1063 add_ss_channel_event
(1, $3);
1064 add_ss_channel_event
(2, $4);
1065 add_ss_channel_event
(3, $5);
1067 | SS_VOL number number number number number number
{
1068 /* set vol for 6 channels */
1069 add_ss_channel_event
(0, $2);
1070 add_ss_channel_event
(1, $3);
1071 add_ss_channel_event
(2, $4);
1072 add_ss_channel_event
(3, $5);
1073 add_ss_channel_event
(4, $6);
1074 add_ss_channel_event
(5, $7);
1076 | SS_EFF_DELAY integer XC_MSECS
{
1078 add_ss_eff_event
(SONG_EV_SS_EFF_DELAY
,
1079 $2, $3, 0, 0, 0, 0, 0, 0);
1082 | SS_EFF_ECHO integer XC_MSECS number
{
1084 add_ss_eff_event
(SONG_EV_SS_EFF_ECHO
,
1085 $2, $3, $4, 0, 0, 0, 0, 0);
1088 | SS_EFF_COMB integer XC_MSECS number
{
1090 add_ss_eff_event
(SONG_EV_SS_EFF_COMB
,
1091 $2, $3, $4, 0, 0, 0, 0, 0);
1094 | SS_EFF_ALLPASS integer XC_MSECS number
{
1095 /* allpass effect */
1096 add_ss_eff_event
(SONG_EV_SS_EFF_ALLPASS
,
1097 $2, $3, $4, 0, 0, 0, 0, 0);
1100 | SS_EFF_FLANGER integer XC_MSECS number XC_MSECS number number
{
1101 /* flanger effect */
1102 add_ss_eff_event
(SONG_EV_SS_EFF_FLANGER
,
1103 $2, $3, $4, $5, $6, $7, 0, 0);
1106 | SS_EFF_WOBBLE integer number number
{
1108 add_ss_eff_event
(SONG_EV_SS_EFF_WOBBLE
,
1109 $2, 0, 0.8, 0, $3, $4, 0, 0);
1112 | SS_EFF_WOBBLE integer number number number
{
1113 /* wobble effect, with gain */
1114 add_ss_eff_event
(SONG_EV_SS_EFF_WOBBLE
,
1115 $2, 0, $5, 0, $3, $4, 0, 0);
1118 | SS_EFF_SQWOBBLE integer number number
{
1119 /* square wobble effect */
1120 add_ss_eff_event
(SONG_EV_SS_EFF_SQWOBBLE
,
1121 $2, 0, 0, 0, $3, $4, 0, 0);
1124 | SS_EFF_HFWOBBLE integer number number
{
1125 /* half wobble effect */
1126 add_ss_eff_event
(SONG_EV_SS_EFF_HFWOBBLE
,
1127 $2, 0, 0, 0, $3, $4, 0, 0);
1130 | SS_EFF_FADER integer XC_MSECS number number
{
1132 add_ss_eff_event
(SONG_EV_SS_EFF_FADER
,
1133 $2, $3, 0, 0, 0, 0, $4, $5);
1136 | SS_EFF_REVERB integer
{
1138 add_ss_eff_event
(SONG_EV_SS_EFF_REVERB
,
1139 $2, 0, 0, 0, 0, 0, 0, 0);
1142 | SS_EFF_FOLDBACK integer number
{
1143 /* foldback distortion effect */
1144 add_ss_eff_event
(SONG_EV_SS_EFF_FOLDBACK
,
1145 $2, 0, $3, 0, 0, 0, 0, 0);
1147 | SS_EFF_OFF integer
{
1149 add_ss_eff_event
(SONG_EV_SS_EFF_OFF
,
1150 $2, 0, 0, 0, 0, 0, 0, 0);
1153 | SS_PITCH_STRETCH xc_absnote number number
{
1154 /* pitch stretch note */
1155 add_ss_pitch_stretch
($2, $3, $4);
1160 | SS_PRINT_WAVE_TEMPO xc_absnote number
{
1161 /* print tempo from wave */
1162 add_ss_print_wave_tempo
($2, $3);
1165 | SONG_INFO XC_STR XC_STR
{
1167 add_song_info_event
($2, $3);
1170 | MIDI_CHANNEL integer
{
1172 add_midi_channel_event
($2);
1175 | MIDI_PROGRAM integer
{
1177 add_midi_program_event
($2);
1183 void yyerror(char * s
)
1185 c_err
(s
, NULL
, NULL
);
1189 #define set_block_d(n,b) set_block(n,strdup(b))
1191 static void compile_startup
(void)
1197 /* default settings */
1198 add_tempo_event
(-2, 120.0);
1199 add_meter_event
(-2, 4, 4);
1202 /* standard tonalities */
1203 set_block_d
("CM", "A"); set_block_d
("Am", "$CM");
1204 set_block_d
("C#M", "A#######"); set_block_d
("A#m", "$C#M");
1205 set_block_d
("DM", "A#--#---"); set_block_d
("Bm", "$DM");
1206 set_block_d
("E&M", "A--&--&&"); set_block_d
("Cm", "$E&M");
1207 set_block_d
("EM", "A##-##--"); set_block_d
("C#m", "$EM");
1208 set_block_d
("FM", "A------&"); set_block_d
("Dm", "$FM");
1209 set_block_d
("F#M", "A######-"); set_block_d
("D#m", "$F#M");
1210 set_block_d
("GM", "A---#---"); set_block_d
("Em", "$GM");
1211 set_block_d
("A&M", "A-&&--&&"); set_block_d
("Fm", "$A&M");
1212 set_block_d
("AM", "A#--##--"); set_block_d
("F#m", "$AM");
1213 set_block_d
("B&M", "A--&---&"); set_block_d
("Gm", "$B&M");
1214 set_block_d
("BM", "A##-###-"); set_block_d
("G#m", "$BM");
1218 static int do_parse
(void)
1222 return
(r
+ compiler_error
);
1226 int compile_ahs_string
(char * code
)
1230 push_code
(strdup
(code
));
1236 int compile_ahs
(char * file
)
1240 if
(insert_file
(file
))