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 void add_mark
(char * name
)
344 /* creates a new mark */
346 GROW
(marks
, n_marks
, struct mark
);
348 marks
[n_marks
].name
= strdup
(name
);
349 marks
[n_marks
].time
= cur_time
;
355 static void find_mark
(char * name
, int set
)
356 /* finds a mark by name, optionally moving time cursor there */
360 for
(n
= 0;n
< n_marks
;n
++)
362 if
(strcmp
(marks
[n
].name
, name
) == 0)
366 if
(cur_time
> marks
[n
].time
)
367 c_err
("mark-overpassed", name
, NULL
);
369 cur_time
= marks
[n
].time
;
372 if
(cur_time
!= marks
[n
].time
)
373 c_err
("mark-mismatch", name
, NULL
);
379 c_err
("mark-not-found", name
, NULL
);
385 static void arp_default
(void)
386 /* resets arpeggiator values to the default ones */
395 static void add_arp
(void)
396 /* adds an arpeggiator note */
400 /* if the note is exactly the same, do nothing */
401 if
(arp_delay
== 0.0 && arp_transpose
== 0 &&
402 arp_volume
== 1.0 && arp_track_off
== 0)
405 if
(n_arps
== arps_size
)
407 GROW
(arps
, arps_size
, struct arp
);
413 a
->delay
= arp_delay
;
414 a
->transpose
= arp_transpose
;
415 a
->volume
= arp_volume
;
416 a
->track_off
= arp_track_off
;
423 static void set_alteration
(char * altstr
)
424 /* sets the alterations from altstr */
426 int n
, steps
[] = { 2, 0, 2, 0, 1, 2, 0, 2, 0, 2, 0, 1 };
428 /* reset alterations */
429 for
(n
= 0;n
< 12;n
++) alterations
[n
] = 0;
431 /* changed according the altstr spec */
432 for
(n
= 0;*altstr
!= '\0' && n
< 12;altstr
++)
436 case
'&': alterations
[n
] = -1; break
;
437 case
'#': alterations
[n
] = 1; break
;
440 /* move to next natural note */
448 static void add_note_event
(int note
)
449 /* adds a note event */
455 /* sum the alteration */
456 if
((n
= note %
12) < 0) n
+= 12;
457 note
+= alterations
[n
];
459 /* calculate the note */
460 np
= note
+ transpose
+ (octave
* 12);
462 /* is note out of range? */
463 if
(np
< 0 || np
> 127)
465 c_err
("note-out-of-range", NULL
, NULL
);
469 e.note.trk_id
= track
;
471 e.note.len
= length
* staccato
;
474 add_song_ev
(SONG_EV_NOTE
, cur_time
, &e
);
476 /* add arpeggiator repetitions */
477 for
(n
= 0;n
< n_arps
;n
++)
479 e.note.trk_id
= track
+ arps
[n
].track_off
;
480 e.note.note
= np
+ arps
[n
].transpose
;
481 e.note.vol
= volume
* arps
[n
].volume
;
483 add_song_ev
(SONG_EV_NOTE
, cur_time
+ arps
[n
].delay
, &e
);
488 static void add_back_event
(void)
492 e.back.trk_id
= track
;
494 add_song_ev
(SONG_EV_BACK
, cur_time
, &e
);
498 static void add_tempo_event
(int trk_id
, double tempo
)
502 e.tempo.trk_id
= trk_id
;
503 e.tempo.tempo
= tempo
;
504 add_song_ev
(SONG_EV_TEMPO
, cur_time
, &e
);
508 static void add_meter_event
(int trk_id
, int num
, int den
)
512 e.meter.trk_id
= trk_id
;
515 add_song_ev
(SONG_EV_METER
, cur_time
, &e
);
518 static void add_measure_event
(void)
522 e.measure.trk_id
= -1;
523 e.measure.line
= yyline
;
525 add_song_ev
(SONG_EV_MEASURE
, cur_time
, &e
);
529 static void add_ss_sustain_event
(double sustain
)
533 e.ss_sustain.trk_id
= track
;
534 e.ss_sustain.sustain
= sustain
;
536 add_song_ev
(SONG_EV_SS_SUSTAIN
, cur_time
, &e
);
540 static void add_ss_vibrato_event
(double depth
, double freq
)
544 e.ss_vibrato.trk_id
= track
;
545 e.ss_vibrato.vib_depth
= depth
;
546 e.ss_vibrato.vib_freq
= freq
;
548 add_song_ev
(SONG_EV_SS_VIBRATO
, cur_time
, &e
);
552 static void add_ss_portamento_event
(double portamento
)
556 e.ss_portamento.trk_id
= track
;
557 e.ss_portamento.portamento
= portamento
;
559 add_song_ev
(SONG_EV_SS_PORTAMENTO
, cur_time
, &e
);
563 static void add_ss_channel_event
(int channel
, float vol
)
567 e.ss_channel.trk_id
= track
;
568 e.ss_channel.channel
= channel
;
569 e.ss_channel.vol
= vol
;
571 add_song_ev
(SONG_EV_SS_CHANNEL
, cur_time
, &e
);
575 static void add_ss_wav_event
(char * wav_file
, int base
, int min
, int max
,
576 double loop_start
, double loop_end
, int first_channel
, int skip_channels
)
580 e.ss_wav.trk_id
= track
;
581 e.ss_wav.file
= wav_file
;
582 e.ss_wav.base
= base
;
585 e.ss_wav.loop_start
= loop_start
;
586 e.ss_wav.loop_end
= loop_end
;
587 e.ss_wav.first_channel
= first_channel
;
588 e.ss_wav.skip_channels
= skip_channels
;
590 add_song_ev
(SONG_EV_SS_WAV
, cur_time
, &e
);
594 static void add_ss_pat_event
(char * pat_file
)
598 e.ss_pat.trk_id
= track
;
599 e.ss_pat.file
= pat_file
;
601 add_song_ev
(SONG_EV_SS_PAT
, cur_time
, &e
);
605 static void add_ss_eff_event
(int type
, int channel
, double size
, float gain
,
606 double depth
, double freq
, double phase
, float initial
, float final
)
610 e.ss_eff.trk_id
= track
;
611 e.ss_eff.channel
= channel
;
612 e.ss_eff.size
= size
;
613 e.ss_eff.gain
= gain
;
614 e.ss_eff.depth
= depth
;
615 e.ss_eff.freq
= freq
;
616 e.ss_eff.phase
= phase
;
617 e.ss_eff.initial
= initial
;
618 e.ss_eff.final
= final
;
620 add_song_ev
(type
, cur_time
, &e
);
624 static void add_ss_pitch_stretch
(int note
, double len
, float vol
)
628 e.ss_pitch_stretch.trk_id
= track
;
629 e.ss_pitch_stretch.note
= note
;
630 e.ss_pitch_stretch.len
= len
;
631 e.ss_pitch_stretch.vol
= vol
;
633 add_song_ev
(SONG_EV_SS_PITCH_STRETCH
, cur_time
, &e
);
637 static void add_ss_print_wave_tempo
(int note
, double len
)
641 e.ss_print_wave_tempo.trk_id
= track
;
642 e.ss_print_wave_tempo.note
= note
;
643 e.ss_print_wave_tempo.len
= len
;
645 add_song_ev
(SONG_EV_SS_PRINT_WAVE_TEMPO
, cur_time
, &e
);
649 static void add_midi_channel_event
(int channel
)
653 e.midi_channel.trk_id
= track
;
654 e.midi_channel.channel
= channel
- 1;
656 add_song_ev
(SONG_EV_MIDI_CHANNEL
, cur_time
, &e
);
660 static void add_midi_program_event
(int program
)
664 e.midi_program.trk_id
= track
;
665 e.midi_program.program
= program
;
667 add_song_ev
(SONG_EV_MIDI_PROGRAM
, cur_time
, &e
);
671 static void add_song_info_event
(char * author
, char * name
)
675 e.song_info.trk_id
= track
;
676 e.song_info.author
= author
;
677 e.song_info.name
= name
;
679 add_song_ev
(SONG_EV_SONG_INFO
, cur_time
, &e
);
683 static void init_track
(void)
684 /* sets the default values for a new track */
696 /* groups should not cross track boundaries */
699 /* reset arpeggiator */
703 /* reset alterations */
704 for
(n
= 0;n
< 12;n
++)
717 %token
<i
> P_INTEGER S_INTEGER
718 %token
<d
> P_REAL S_REAL
719 %token
<i
> NOTE_P NOTE_T3 NOTE_T5
720 %token
<p
> NEW_MARK GOTO_MARK ASSERT_MARK
722 %token
<p
> BLOCK BLK_ASSIGN BLK_INSERT FILE_INSERT
727 %token
<i
> XC_ABSNOTE
730 %token SS_SEP SS_WAV SS_LOOP_WAV SS_PAT
731 %token SS_SUSTAIN SS_VIBRATO SS_PORTAMENTO SS_CHANNEL SS_VOL
733 %token SS_EFF_DELAY SS_EFF_ECHO SS_EFF_COMB SS_EFF_ALLPASS SS_EFF_FLANGER
734 %token SS_EFF_WOBBLE SS_EFF_SQWOBBLE SS_EFF_HFWOBBLE
735 %token SS_EFF_FADER SS_EFF_REVERB SS_EFF_FOLDBACK SS_EFF_OFF
737 %token SS_PITCH_STRETCH SS_TIME_STRETCH SS_PRINT_WAVE_TEMPO
741 %token MIDI_CHANNEL MIDI_PROGRAM MIDI_GENERIC
743 %type
<i
> integer note note_pitch rest back
744 %type
<d
> real p_number number note_length
746 %type
<d
> arp_list arp_note
775 /* absolute octave */
779 /* relative octave */
783 /* absolute volume */
787 /* absolute volume */
791 /* relative volume */
808 /* group delimiter */
821 /* measure mark event */
846 add_tempo_event
(-1, $2);
848 |
'M' P_INTEGER
'/' P_INTEGER
{
849 /* meter (time signature) setting */
850 add_meter_event
(-1, $2, $4);
853 /* alteration string */
857 | BLOCK
'*' P_INTEGER
{
861 /* store the block as <TMP> */
862 set_block
("<TMP>", $1);
864 for
(n
= 0;n
< $3;n
++)
865 insert_block
("<TMP>");
887 P_INTEGER
{ $$
= $1; }
888 | S_INTEGER
{ $$
= $1; }
893 | S_REAL
{ $$
= $1; }
897 P_INTEGER
{ $$
= (double) $1; }
898 | P_REAL
{ $$
= $1; }
902 integer
{ $$
= (double) $1; }
907 note_pitch
{ $$
= $1; }
908 | note note_length
{ $$
= $1; length
=$2; }
909 | note
'~' number
{ $$
= $1; DEBUGF
("imm volume: %lf\n", $3); }
914 | note_pitch
'&' { $$
= $1 - 1; }
915 | note_pitch
'#' { $$
= $1 + 1; }
916 | note_pitch
'\'' { $$
= $1 + 12; }
917 | note_pitch
',' { $$
= $1 - 12; }
921 P_INTEGER
{ $$
= 1 / (double) $1; }
922 | note_length NOTE_T3
{ $$
= $1 * 2.0 / 3.0; }
923 | note_length NOTE_T5
{ $$
= $1 * 4.0 / 5.0; }
924 | note_length
'*' p_number
{ $$
= $1 * $3; }
925 | note_length
'.' { $$
= $1 * 1.5; }
931 /* rest with length */
939 /* back with length */
946 /* empty arpeggiator */
955 /* first arpeggiator note */
959 | arp_list
',' arp_note
{
960 /* rest of arpeggiator notes */
967 /* arpeggiator delay */
970 | arp_note
'~' number
{
971 /* arpeggiator volume */
972 arp_volume
= (float)$3;
974 | arp_note S_INTEGER
{
975 /* arpeggiator transpose */
978 | arp_note
'/' P_INTEGER
{
979 /* arpeggiator track offset */
993 P_INTEGER
{ $$
= $1; }
994 | XC_ABSNOTE
{ $$
= $1; }
998 SS_WAV XC_STR xc_absnote
{
999 /* load .wav file with just one note */
1000 add_ss_wav_event
($2, $3, $3, $3, -1.0, -1.0, 0, 0);
1002 | SS_WAV XC_STR xc_absnote xc_absnote xc_absnote
{
1003 /* load .wav file */
1004 add_ss_wav_event
($2, $3, $4, $5, -1.0, -1.0, 0, 0);
1006 | SS_WAV XC_STR xc_absnote xc_absnote xc_absnote number number
{
1007 /* load .wav file, with loop boundaries */
1008 add_ss_wav_event
($2, $3, $4, $5, $6, $7, 0, 0);
1010 | SS_WAV XC_STR xc_absnote xc_absnote xc_absnote number number number number
{
1011 /* load .wav file, with loop boundaries,
1012 first channel and skip channels */
1013 add_ss_wav_event
($2, $3, $4, $5, $6, $7, $8, $9);
1016 /* load .pat file */
1017 add_ss_pat_event
($2);
1019 | SS_SUSTAIN XC_MSECS
{
1021 add_ss_sustain_event
($2);
1023 | SS_VIBRATO XC_MSECS number
{
1025 add_ss_vibrato_event
($2, $3);
1027 | SS_PORTAMENTO number
{
1028 /* sets portamento */
1029 add_ss_portamento_event
($2);
1031 | SS_CHANNEL integer number
{
1032 /* sets volume for a channel */
1033 add_ss_channel_event
($2, $3);
1035 | SS_VOL number number
{
1036 /* set vol for 2 channels */
1037 add_ss_channel_event
(0, $2);
1038 add_ss_channel_event
(1, $3);
1040 | SS_VOL number number number
{
1041 /* set vol for 3 channels */
1042 add_ss_channel_event
(0, $2);
1043 add_ss_channel_event
(1, $3);
1044 add_ss_channel_event
(2, $4);
1046 | SS_VOL number number number number
{
1047 /* set vol for 4 channels */
1048 add_ss_channel_event
(0, $2);
1049 add_ss_channel_event
(1, $3);
1050 add_ss_channel_event
(2, $4);
1051 add_ss_channel_event
(3, $5);
1053 | SS_VOL number number number number number number
{
1054 /* set vol for 6 channels */
1055 add_ss_channel_event
(0, $2);
1056 add_ss_channel_event
(1, $3);
1057 add_ss_channel_event
(2, $4);
1058 add_ss_channel_event
(3, $5);
1059 add_ss_channel_event
(4, $6);
1060 add_ss_channel_event
(5, $7);
1062 | SS_EFF_DELAY integer XC_MSECS
{
1064 add_ss_eff_event
(SONG_EV_SS_EFF_DELAY
,
1065 $2, $3, 0, 0, 0, 0, 0, 0);
1068 | SS_EFF_ECHO integer XC_MSECS number
{
1070 add_ss_eff_event
(SONG_EV_SS_EFF_ECHO
,
1071 $2, $3, $4, 0, 0, 0, 0, 0);
1074 | SS_EFF_COMB integer XC_MSECS number
{
1076 add_ss_eff_event
(SONG_EV_SS_EFF_COMB
,
1077 $2, $3, $4, 0, 0, 0, 0, 0);
1080 | SS_EFF_ALLPASS integer XC_MSECS number
{
1081 /* allpass effect */
1082 add_ss_eff_event
(SONG_EV_SS_EFF_ALLPASS
,
1083 $2, $3, $4, 0, 0, 0, 0, 0);
1086 | SS_EFF_FLANGER integer XC_MSECS number XC_MSECS number number
{
1087 /* flanger effect */
1088 add_ss_eff_event
(SONG_EV_SS_EFF_FLANGER
,
1089 $2, $3, $4, $5, $6, $7, 0, 0);
1092 | SS_EFF_WOBBLE integer number number
{
1094 add_ss_eff_event
(SONG_EV_SS_EFF_WOBBLE
,
1095 $2, 0, 0.8, 0, $3, $4, 0, 0);
1098 | SS_EFF_WOBBLE integer number number number
{
1099 /* wobble effect, with gain */
1100 add_ss_eff_event
(SONG_EV_SS_EFF_WOBBLE
,
1101 $2, 0, $5, 0, $3, $4, 0, 0);
1104 | SS_EFF_SQWOBBLE integer number number
{
1105 /* square wobble effect */
1106 add_ss_eff_event
(SONG_EV_SS_EFF_SQWOBBLE
,
1107 $2, 0, 0, 0, $3, $4, 0, 0);
1110 | SS_EFF_HFWOBBLE integer number number
{
1111 /* half wobble effect */
1112 add_ss_eff_event
(SONG_EV_SS_EFF_HFWOBBLE
,
1113 $2, 0, 0, 0, $3, $4, 0, 0);
1116 | SS_EFF_FADER integer XC_MSECS number number
{
1118 add_ss_eff_event
(SONG_EV_SS_EFF_FADER
,
1119 $2, $3, 0, 0, 0, 0, $4, $5);
1122 | SS_EFF_REVERB integer
{
1124 add_ss_eff_event
(SONG_EV_SS_EFF_REVERB
,
1125 $2, 0, 0, 0, 0, 0, 0, 0);
1128 | SS_EFF_FOLDBACK integer number
{
1129 /* foldback distortion effect */
1130 add_ss_eff_event
(SONG_EV_SS_EFF_FOLDBACK
,
1131 $2, 0, $3, 0, 0, 0, 0, 0);
1133 | SS_EFF_OFF integer
{
1135 add_ss_eff_event
(SONG_EV_SS_EFF_OFF
,
1136 $2, 0, 0, 0, 0, 0, 0, 0);
1139 | SS_PITCH_STRETCH xc_absnote number number
{
1140 /* pitch stretch note */
1141 add_ss_pitch_stretch
($2, $3, $4);
1146 | SS_PRINT_WAVE_TEMPO xc_absnote number
{
1147 /* print tempo from wave */
1148 add_ss_print_wave_tempo
($2, $3);
1151 | SONG_INFO XC_STR XC_STR
{
1153 add_song_info_event
($2, $3);
1156 | MIDI_CHANNEL integer
{
1158 add_midi_channel_event
($2);
1161 | MIDI_PROGRAM integer
{
1163 add_midi_program_event
($2);
1169 void yyerror(char * s
)
1171 c_err
(s
, NULL
, NULL
);
1175 #define set_block_d(n,b) set_block(n,strdup(b))
1177 static void compile_startup
(void)
1183 /* default settings */
1184 add_tempo_event
(-2, 120.0);
1185 add_meter_event
(-2, 4, 4);
1188 /* standard tonalities */
1189 set_block_d
("CM", "A"); set_block_d
("Am", "$CM");
1190 set_block_d
("C#M", "A#######"); set_block_d
("A#m", "$C#M");
1191 set_block_d
("DM", "A#--#---"); set_block_d
("Bm", "$DM");
1192 set_block_d
("E&M", "A--&--&&"); set_block_d
("Cm", "$E&M");
1193 set_block_d
("EM", "A##-##--"); set_block_d
("C#m", "$EM");
1194 set_block_d
("FM", "A------&"); set_block_d
("Dm", "$FM");
1195 set_block_d
("F#M", "A######-"); set_block_d
("D#m", "$F#M");
1196 set_block_d
("GM", "A---#---"); set_block_d
("Em", "$GM");
1197 set_block_d
("A&M", "A-&&--&&"); set_block_d
("Fm", "$A&M");
1198 set_block_d
("AM", "A#--##--"); set_block_d
("F#m", "$AM");
1199 set_block_d
("B&M", "A--&---&"); set_block_d
("Gm", "$B&M");
1200 set_block_d
("BM", "A##-###-"); set_block_d
("G#m", "$BM");
1204 static int do_parse
(void)
1208 return
(r
+ compiler_error
);
1212 int compile_ahs_string
(char * code
)
1216 push_code
(strdup
(code
));
1222 int compile_ahs
(char * file
)
1226 if
(insert_file
(file
))