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
, int times
, int dyn
);
45 int push_code_from_file
(char * file
);
57 static double staccato
;
61 /* parser debugging */
63 #define DEBUGF if(verbose >= 2)printf
73 static struct named_block
* named_blocks
= NULL
;
74 static int n_named_blocks
= 0;
75 static int named_blocks_size
= 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;
98 static int marks_size
= 0;
109 static struct arp
* arps
= NULL
;
110 static int n_arps
= 0;
111 static int arps_size
= 0;
113 static double arp_delay
;
114 static int arp_transpose
;
115 static float arp_volume
;
116 static int arp_track
;
118 int compiler_error
= 0;
124 ********************/
126 void c_err
(char * e1
, char * e2
, char * e3
)
127 /* reports an error from the compiler */
130 if
(e1
!= NULL
) printf
(" %s", e1
);
131 if
(e2
!= NULL
) printf
(" %s", e2
);
132 if
(e3
!= NULL
) printf
(" %s", e3
);
133 printf
(" in line %d\n", yyline
);
139 static void forward
(double step
)
140 /* moves forward current time */
145 /* quantizations could be done here */
151 static int set_named_block
(char * name
, char * block
)
152 /* sets a named block */
156 /* find first if block is previously defined */
157 for
(n
= 0;n
< n_named_blocks
;n
++)
159 if
(strcmp
(name
, named_blocks
[n
].name
) == 0)
161 free
(named_blocks
[n
].block
);
166 if
(n
== n_named_blocks
)
168 /* need to expand? */
169 if
(n_named_blocks
== named_blocks_size
)
171 named_blocks_size
+= 4;
173 named_blocks
= (struct named_block
*)
174 realloc
(named_blocks
,
176 sizeof
(struct named_block
));
182 strncpy
(named_blocks
[n
].name
, name
, sizeof
(named_blocks
[n
].name
));
183 named_blocks
[n
].block
= block
;
189 static void insert_named_block
(char * name
)
190 /* inserts a named block */
194 for
(n
= 0;n
< n_named_blocks
;n
++)
196 if
(strcmp
(name
, named_blocks
[n
].name
) == 0)
198 push_code
(named_blocks
[n
].block
, 1, 0);
203 c_err
("block-not-found", name
, NULL
);
207 static int insert_file
(char * filename
)
209 if
(!push_code_from_file
(filename
))
211 c_err
("script-not-found", filename
, NULL
);
221 static int push_group
(void)
222 /* starts a new group of notes */
224 if
(n_groups
== groups_size
)
228 groups
= (struct group
*) realloc
(groups
,
229 groups_size
* sizeof
(struct group
));
232 groups
[n_groups
].start
= cur_time
;
233 groups
[n_groups
].gliss
= 0.0;
234 groups
[n_groups
].max
= 0.0;
242 static int next_group_part
(void)
247 c_err
("missing-start-of-group", NULL
, NULL
);
251 /* store maximum frame */
252 if
(groups
[n_groups
- 1].max
< cur_time
)
253 groups
[n_groups
- 1].max
= cur_time
;
255 /* add glissando delay */
256 groups
[n_groups
- 1].gliss
+= gliss
;
259 cur_time
= groups
[n_groups
- 1].start
+ groups
[n_groups
- 1].gliss
;
265 static int pop_group
(void)
266 /* finishes a group, moving the frame to the end of the longer part */
270 c_err
("missing-start-of-group", NULL
, NULL
);
276 /* if other parts of group were longer than the last one,
277 move pointer there */
278 if
(groups
[n_groups
].max
> cur_time
)
279 cur_time
= groups
[n_groups
].max
;
287 static void add_mark
(char * name
)
288 /* creates a new mark */
290 if
(n_marks
== marks_size
)
294 marks
= (struct mark
*) realloc
(marks
,
295 marks_size
* sizeof
(struct mark
));
298 strncpy
(marks
[n_marks
].name
, name
, sizeof
(marks
[n_marks
].name
));
299 marks
[n_marks
].time
= cur_time
;
305 static void find_mark
(char * name
, int set
)
306 /* finds a mark by name, optionally moving time cursor there */
310 for
(n
= 0;n
< n_marks
;n
++)
312 if
(strcmp
(marks
[n
].name
, name
) == 0)
316 if
(cur_time
> marks
[n
].time
)
317 c_err
("mark-overpassed", name
, NULL
);
319 cur_time
= marks
[n
].time
;
322 if
(cur_time
!= marks
[n
].time
)
323 c_err
("mark-mismatch", name
, NULL
);
329 c_err
("mark-not-found", name
, NULL
);
335 static void arp_default
(void)
336 /* resets arpeggiator values to the default ones */
345 static void add_arp
(void)
346 /* adds an arpeggiator note */
348 /* if the note is exactly the same, do nothing */
349 if
(arp_delay
== 0.0 && arp_transpose
== 0 &&
350 arp_volume
== 1.0 && arp_track
== track
)
353 if
(n_arps
== arps_size
)
357 arps
= (struct arp
*) realloc
(arps
,
358 arps_size
* sizeof
(struct arp
));
361 arps
[n_arps
].delay
= arp_delay
;
362 arps
[n_arps
].transpose
= arp_transpose
;
363 arps
[n_arps
].volume
= arp_volume
;
364 arps
[n_arps
].track
= arp_track
;
373 static void add_note_event
(int note
)
374 /* adds a note event */
380 /* calculate the note */
381 np
= note
+ transpose
+ (octave
* 12);
383 /* is note out of range? */
384 if
(np
< 0 || np
> 127)
386 c_err
("note-out-of-range", NULL
, NULL
);
390 e.note.type
= SONG_EV_NOTE
;
391 e.note.time
= cur_time
;
392 e.note.trk_id
= track
;
394 e.note.len
= length
* staccato
;
399 /* add arpeggiator repetitions */
400 for
(n
= 0;n
< n_arps
;n
++)
402 e.note.time
= cur_time
+ arps
[n
].delay
;
403 e.note.trk_id
= arps
[n
].track
;
404 e.note.note
= np
+ arps
[n
].transpose
;
405 e.note.vol
= volume
* arps
[n
].volume
;
412 static void add_back_event
(void)
416 e.back.type
= SONG_EV_BACK
;
417 e.back.time
= cur_time
;
418 e.back.trk_id
= track
;
424 static void add_tempo_event
(int trk_id
, double tempo
)
428 e.tempo.type
= SONG_EV_TEMPO
;
429 e.tempo.time
= cur_time
;
430 e.tempo.trk_id
= trk_id
;
431 e.tempo.tempo
= tempo
;
436 static void add_meter_event
(int trk_id
, int num
, int den
)
440 e.meter.type
= SONG_EV_METER
;
441 e.meter.time
= cur_time
;
442 e.meter.trk_id
= trk_id
;
448 static void add_measure_event
(void)
452 e.measure.type
= SONG_EV_MEASURE
;
453 e.measure.time
= cur_time
;
454 e.measure.trk_id
= -1;
455 e.measure.line
= yyline
;
461 static void add_ss_sustain_event
(double sustain
)
465 e.ss_sustain.type
= SONG_EV_SS_SUSTAIN
;
466 e.ss_sustain.time
= cur_time
;
467 e.ss_sustain.trk_id
= track
;
468 e.ss_sustain.sustain
= sustain
;
474 static void add_ss_vibrato_event
(double depth
, double freq
)
478 e.ss_vibrato.type
= SONG_EV_SS_VIBRATO
;
479 e.ss_vibrato.time
= cur_time
;
480 e.ss_vibrato.trk_id
= track
;
481 e.ss_vibrato.vib_depth
= depth
;
482 e.ss_vibrato.vib_freq
= freq
;
488 static void add_ss_channel_event
(int channel
, float vol
)
492 e.ss_channel.type
= SONG_EV_SS_CHANNEL
;
493 e.ss_channel.time
= cur_time
;
494 e.ss_channel.trk_id
= track
;
495 e.ss_channel.channel
= channel
;
496 e.ss_channel.vol
= vol
;
502 static void add_ss_wav_event
(char * wav_file
, int base
, int min
, int max
,
503 double loop_start
, double loop_end
)
507 e.ss_wav.type
= SONG_EV_SS_WAV
;
508 e.ss_wav.time
= cur_time
;
509 e.ss_wav.trk_id
= track
;
510 e.ss_wav.file
= wav_file
;
511 e.ss_wav.base
= base
;
514 e.ss_wav.loop_start
= loop_start
;
515 e.ss_wav.loop_end
= loop_end
;
521 static void add_ss_pat_event
(char * pat_file
)
525 e.ss_pat.type
= SONG_EV_SS_PAT
;
526 e.ss_pat.time
= cur_time
;
527 e.ss_pat.trk_id
= track
;
528 e.ss_pat.file
= pat_file
;
534 static void add_ss_eff_event
(int type
, int channel
, double size
, float gain
,
535 double depth
, double freq
, double phase
, float initial
, float final
)
539 e.ss_eff.type
= type
;
540 e.ss_eff.time
= cur_time
;
541 e.ss_eff.trk_id
= track
;
542 e.ss_eff.channel
= channel
;
543 e.ss_eff.size
= size
;
544 e.ss_eff.gain
= gain
;
545 e.ss_eff.depth
= depth
;
546 e.ss_eff.freq
= freq
;
547 e.ss_eff.phase
= phase
;
548 e.ss_eff.initial
= initial
;
549 e.ss_eff.final
= final
;
555 static void add_ss_pitch_stretch
(int note
, double len
, float vol
)
559 e.ss_pitch_stretch.type
= SONG_EV_SS_PITCH_STRETCH
;
560 e.ss_pitch_stretch.time
= cur_time
;
561 e.ss_pitch_stretch.trk_id
= track
;
562 e.ss_pitch_stretch.note
= note
;
563 e.ss_pitch_stretch.len
= len
;
564 e.ss_pitch_stretch.vol
= vol
;
570 static void add_ss_print_wave_tempo
(int note
, double len
)
574 e.ss_print_wave_tempo.type
= SONG_EV_SS_PRINT_WAVE_TEMPO
;
575 e.ss_print_wave_tempo.time
= cur_time
;
576 e.ss_print_wave_tempo.trk_id
= track
;
577 e.ss_print_wave_tempo.note
= note
;
578 e.ss_print_wave_tempo.len
= len
;
584 static void add_midi_channel_event
(int channel
)
588 /* 1st channel is 0 */
591 e.midi_channel.type
= SONG_EV_MIDI_CHANNEL
;
592 e.midi_channel.time
= cur_time
;
593 e.midi_channel.trk_id
= track
;
594 e.midi_channel.channel
= channel
;
600 static void add_midi_program_event
(int program
)
604 e.midi_program.type
= SONG_EV_MIDI_PROGRAM
;
605 e.midi_program.time
= cur_time
;
606 e.midi_program.trk_id
= track
;
607 e.midi_program.program
= program
;
613 static void add_track_info_event
(char * author
, char * name
)
617 e.track_info.type
= SONG_EV_TRACK_INFO
;
618 e.track_info.time
= cur_time
;
619 e.track_info.trk_id
= track
;
620 e.track_info.author
= author
;
621 e.track_info.name
= name
;
627 static void init_track
(void)
628 /* sets the default values for a new track */
638 /* groups should not cross track boundaries */
641 /* reset arpeggiator */
655 %token
<i
> P_INTEGER S_INTEGER
656 %token
<d
> P_REAL S_REAL
657 %token
<i
> NOTE_P NOTE_T3 NOTE_T5
658 %token
<p
> NEW_MARK GOTO_MARK ASSERT_MARK
660 %token
<p
> BLOCK BLK_ASSIGN BLK_INSERT FILE_INSERT
663 %token
<i
> XC_ABSNOTE
666 %token SS_SEP SS_WAV SS_LOOP_WAV SS_PAT SS_SUSTAIN SS_VIBRATO SS_CHANNEL SS_VOL
668 %token SS_EFF_DELAY SS_EFF_ECHO SS_EFF_COMB SS_EFF_ALLPASS SS_EFF_FLANGER
669 %token SS_EFF_WOBBLE SS_EFF_SQWOBBLE SS_EFF_FADER SS_EFF_REVERB SS_EFF_OFF
671 %token SS_PITCH_STRETCH SS_TIME_STRETCH SS_PRINT_WAVE_TEMPO
675 %token MIDI_CHANNEL MIDI_PROGRAM MIDI_GENERIC
677 %type
<i
> integer note note_pitch rest back
678 %type
<d
> real p_number number note_length
680 %type
<d
> arp_list arp_note
709 /* absolute octave */
713 /* relative octave */
717 /* absolute volume */
721 /* absolute volume */
725 /* relative volume */
742 /* group delimiter */
755 /* measure mark event */
780 add_tempo_event
(-1, $2);
782 |
'M' P_INTEGER
'/' P_INTEGER
{
783 /* meter (time signature) setting */
784 add_meter_event
(-1, $2, $4);
787 | BLOCK
'*' P_INTEGER
{
789 push_code
($1, $3, 1);
793 set_named_block
($2, $1);
796 /* insert named block */
797 insert_named_block
($1);
811 P_INTEGER
{ $$
= $1; }
812 | S_INTEGER
{ $$
= $1; }
817 | S_REAL
{ $$
= $1; }
821 P_INTEGER
{ $$
= (double) $1; }
822 | P_REAL
{ $$
= $1; }
826 integer
{ $$
= (double) $1; }
831 note_pitch
{ $$
= $1; }
832 | note note_length
{ $$
= $1; length
=$2; }
833 | note
'~' number
{ $$
= $1; DEBUGF
("imm volume: %lf\n", $3); }
838 | note_pitch
'&' { $$
= $1 - 1; }
839 | note_pitch
'#' { $$
= $1 + 1; }
840 | note_pitch
'\'' { $$
= $1 + 12; }
841 | note_pitch
',' { $$
= $1 - 12; }
845 P_INTEGER
{ $$
= 1 / (double) $1; }
846 | note_length NOTE_T3
{ $$
= $1 * 2.0 / 3.0; }
847 | note_length NOTE_T5
{ $$
= $1 * 4.0 / 5.0; }
848 | note_length
'*' p_number
{ $$
= $1 * $3; }
849 | note_length
'.' { $$
= $1 * 1.5; }
855 /* rest with length */
863 /* back with length */
870 /* empty arpeggiator */
879 /* first arpeggiator note */
883 | arp_list
',' arp_note
{
884 /* rest of arpeggiator notes */
891 /* arpeggiator delay */
894 | arp_note
'~' number
{
895 /* arpeggiator volume */
896 arp_volume
= (float)$3;
898 | arp_note S_INTEGER
{
899 /* arpeggiator transpose */
902 | arp_note
'@' P_INTEGER
{
903 /* arpeggiator track */
909 P_INTEGER
{ $$
= $1; }
910 | XC_ABSNOTE
{ $$
= $1; }
914 SS_WAV XC_STR xc_absnote
{
915 /* load .wav file with just one note */
916 add_ss_wav_event
($2, $3, $3, $3, -1.0, -1.0);
918 | SS_WAV XC_STR xc_absnote xc_absnote xc_absnote
{
920 add_ss_wav_event
($2, $3, $4, $5, -1.0, -1.0);
922 | SS_WAV XC_STR xc_absnote xc_absnote xc_absnote number number
{
923 /* load .wav file, with loop boundaries */
924 add_ss_wav_event
($2, $3, $4, $5, $6, $7);
928 add_ss_pat_event
($2);
930 | SS_SUSTAIN XC_MSECS
{
932 add_ss_sustain_event
($2);
934 | SS_VIBRATO XC_MSECS number
{
936 add_ss_vibrato_event
($2, $3);
938 | SS_CHANNEL integer number
{
939 /* sets volume for a channel */
940 add_ss_channel_event
($2, $3);
942 | SS_VOL number number
{
943 /* set vol for 2 channels */
944 add_ss_channel_event
(0, $2);
945 add_ss_channel_event
(1, $3);
947 | SS_VOL number number number
{
948 /* set vol for 3 channels */
949 add_ss_channel_event
(0, $2);
950 add_ss_channel_event
(1, $3);
951 add_ss_channel_event
(2, $4);
953 | SS_VOL number number number number
{
954 /* set vol for 4 channels */
955 add_ss_channel_event
(0, $2);
956 add_ss_channel_event
(1, $3);
957 add_ss_channel_event
(2, $4);
958 add_ss_channel_event
(3, $5);
960 | SS_VOL number number number number number number
{
961 /* set vol for 6 channels */
962 add_ss_channel_event
(0, $2);
963 add_ss_channel_event
(1, $3);
964 add_ss_channel_event
(2, $4);
965 add_ss_channel_event
(3, $5);
966 add_ss_channel_event
(4, $6);
967 add_ss_channel_event
(5, $7);
969 | SS_EFF_DELAY integer XC_MSECS
{
971 add_ss_eff_event
(SONG_EV_SS_EFF_DELAY
,
972 $2, $3, 0, 0, 0, 0, 0, 0);
975 | SS_EFF_ECHO integer XC_MSECS number
{
977 add_ss_eff_event
(SONG_EV_SS_EFF_ECHO
,
978 $2, $3, $4, 0, 0, 0, 0, 0);
981 | SS_EFF_COMB integer XC_MSECS number
{
983 add_ss_eff_event
(SONG_EV_SS_EFF_COMB
,
984 $2, $3, $4, 0, 0, 0, 0, 0);
987 | SS_EFF_ALLPASS integer XC_MSECS number
{
989 add_ss_eff_event
(SONG_EV_SS_EFF_ALLPASS
,
990 $2, $3, $4, 0, 0, 0, 0, 0);
993 | SS_EFF_FLANGER integer XC_MSECS number XC_MSECS number number
{
995 add_ss_eff_event
(SONG_EV_SS_EFF_FLANGER
,
996 $2, $3, $4, $5, $6, $7, 0, 0);
999 | SS_EFF_WOBBLE integer number number
{
1001 add_ss_eff_event
(SONG_EV_SS_EFF_WOBBLE
,
1002 $2, 0, 0, 0, $3, $4, 0, 0);
1005 | SS_EFF_SQWOBBLE integer number number
{
1006 /* square wobble effect */
1007 add_ss_eff_event
(SONG_EV_SS_EFF_SQWOBBLE
,
1008 $2, 0, 0, 0, $3, $4, 0, 0);
1011 | SS_EFF_FADER integer XC_MSECS number number
{
1013 add_ss_eff_event
(SONG_EV_SS_EFF_FADER
,
1014 $2, $3, 0, 0, 0, 0, $4, $5);
1017 | SS_EFF_REVERB integer
{
1019 add_ss_eff_event
(SONG_EV_SS_EFF_REVERB
,
1020 $2, 0, 0, 0, 0, 0, 0, 0);
1023 | SS_EFF_OFF integer
{
1025 add_ss_eff_event
(SONG_EV_SS_EFF_OFF
,
1026 $2, 0, 0, 0, 0, 0, 0, 0);
1029 | SS_PITCH_STRETCH xc_absnote number number
{
1030 /* pitch stretch note */
1031 add_ss_pitch_stretch
($2, $3, $4);
1036 | SS_PRINT_WAVE_TEMPO xc_absnote number
{
1037 /* print tempo from wave */
1038 add_ss_print_wave_tempo
($2, $3);
1041 | TRACK_INFO XC_STR XC_STR
{
1042 /* add track info */
1043 add_track_info_event
($2, $3);
1046 | MIDI_CHANNEL integer
{
1048 add_midi_channel_event
($2);
1051 | MIDI_PROGRAM integer
{
1053 add_midi_program_event
($2);
1059 void yyerror(char * s
)
1061 c_err
(s
, NULL
, NULL
);
1065 static void compile_startup
(void)
1071 /* default settings */
1072 add_tempo_event
(-2, 120.0);
1073 add_meter_event
(-2, 4, 4);
1078 int compile_ahs_string
(char * code
)
1082 push_code
(code
, 1, 0);
1084 return
(yyparse() + compiler_error
);
1088 int compile_ahs
(char * file
)
1092 if
(insert_file
(file
))
1095 return
(yyparse() + compiler_error
);