4 Ann Hell Ex Machina - Music Software
5 Copyright (C) 2003/2005 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(ss_debug)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
;
98 static int marks_size
=0;
109 static struct arp
* arps
=NULL
;
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 cur_time
=groups
[n_groups
].max
;
284 static void add_mark
(char * name
)
285 /* creates a new mark */
287 if
(n_marks
== marks_size
)
291 marks
=(struct mark
*) realloc
(marks
,
292 marks_size
* sizeof
(struct mark
));
295 strncpy
(marks
[n_marks
].name
, name
, sizeof
(marks
[n_marks
].name
));
296 marks
[n_marks
].time
=cur_time
;
302 static void find_mark
(char * name
, int set
)
303 /* finds a mark by name, optionally moving time cursor there */
307 for
(n
=0;n
< n_marks
;n
++)
309 if
(strcmp
(marks
[n
].name
, name
) == 0)
313 if
(cur_time
> marks
[n
].time
)
314 c_err
("mark-overpassed", name
, NULL
);
316 cur_time
=marks
[n
].time
;
319 if
(cur_time
!= marks
[n
].time
)
320 c_err
("mark-mismatch", name
, NULL
);
326 c_err
("mark-not-found", name
, NULL
);
332 static void arp_default
(void)
333 /* resets arpeggiator values to the default ones */
342 static void add_arp
(void)
343 /* adds an arpeggiator note */
345 /* if the note is exactly the same, do nothing */
346 if
(arp_delay
== 0.0 && arp_transpose
== 0 &&
347 arp_volume
== 1.0 && arp_track
== track
)
350 if
(n_arps
== arps_size
)
354 arps
=(struct arp
*) realloc
(arps
,
355 arps_size
* sizeof
(struct arp
));
358 arps
[n_arps
].delay
=arp_delay
;
359 arps
[n_arps
].transpose
=arp_transpose
;
360 arps
[n_arps
].volume
=arp_volume
;
361 arps
[n_arps
].track
=arp_track
;
370 static void add_note_event
(int note
)
371 /* adds a note event */
377 /* calculate the note */
378 np
=note
+ transpose
+ (octave
* 12);
380 /* is note out of range? */
381 if
(np
< 0 || np
> 127)
383 c_err
("note-out-of-range", NULL
, NULL
);
387 e.note.type
=SONG_EV_NOTE
;
388 e.note.time
=cur_time
;
391 e.note.len
=length
* staccato
;
396 /* add arpeggiator repetitions */
397 for
(n
=0;n
< n_arps
;n
++)
399 e.note.time
=cur_time
+ arps
[n
].delay
;
400 e.note.trk_id
=arps
[n
].track
;
401 e.note.note
=np
+ arps
[n
].transpose
;
402 e.note.vol
=volume
* arps
[n
].volume
;
409 static void add_tempo_event
(int trk_id
, double tempo
)
413 e.tempo.type
=SONG_EV_TEMPO
;
414 e.tempo.time
=cur_time
;
415 e.tempo.trk_id
=trk_id
;
421 static void add_meter_event
(int trk_id
, int num
, int den
)
425 e.meter.type
=SONG_EV_METER
;
426 e.meter.time
=cur_time
;
427 e.meter.trk_id
=trk_id
;
433 static void add_measure_event
(void)
437 e.measure.type
=SONG_EV_MEASURE
;
438 e.measure.time
=cur_time
;
440 e.measure.line
=yyline
;
446 static void add_ss_sustain_event
(double sustain
)
450 e.ss_sustain.type
=SONG_EV_SS_SUSTAIN
;
451 e.ss_sustain.time
=cur_time
;
452 e.ss_sustain.trk_id
=track
;
453 e.ss_sustain.sustain
=sustain
;
459 static void add_ss_vibrato_event
(double depth
, double freq
)
463 e.ss_vibrato.type
=SONG_EV_SS_VIBRATO
;
464 e.ss_vibrato.time
=cur_time
;
465 e.ss_vibrato.trk_id
=track
;
466 e.ss_vibrato.vib_depth
=depth
;
467 e.ss_vibrato.vib_freq
=freq
;
473 static void add_ss_channel_event
(int channel
, float vol
)
477 e.ss_channel.type
=SONG_EV_SS_CHANNEL
;
478 e.ss_channel.time
=cur_time
;
479 e.ss_channel.trk_id
=track
;
480 e.ss_channel.channel
=channel
;
481 e.ss_channel.vol
=vol
;
487 static void add_ss_wav_event
(char * wav_file
, int base
, int min
, int max
,
488 double loop_start
, double loop_end
)
492 e.ss_wav.type
=SONG_EV_SS_WAV
;
493 e.ss_wav.time
=cur_time
;
494 e.ss_wav.trk_id
=track
;
495 e.ss_wav.file
=wav_file
;
499 e.ss_wav.loop_start
=loop_start
;
500 e.ss_wav.loop_end
=loop_end
;
505 static void add_ss_pat_event
(char * pat_file
)
509 e.ss_pat.type
=SONG_EV_SS_PAT
;
510 e.ss_pat.time
=cur_time
;
511 e.ss_pat.trk_id
=track
;
512 e.ss_pat.file
=pat_file
;
518 static void add_ss_eff_event
(int type
, int channel
, double size
, float gain
,
519 double depth
, double freq
, double phase
, float initial
, float final
)
524 e.ss_eff.time
=cur_time
;
525 e.ss_eff.trk_id
=track
;
526 e.ss_eff.channel
=channel
;
529 e.ss_eff.depth
=depth
;
531 e.ss_eff.phase
=phase
;
532 e.ss_eff.initial
=initial
;
533 e.ss_eff.final
=final
;
539 static void add_ss_pitch_stretch
(int note
, double len
, float vol
)
543 e.ss_pitch_stretch.type
=SONG_EV_SS_PITCH_STRETCH
;
544 e.ss_pitch_stretch.time
=cur_time
;
545 e.ss_pitch_stretch.trk_id
=track
;
546 e.ss_pitch_stretch.note
=note
;
547 e.ss_pitch_stretch.len
=len
;
548 e.ss_pitch_stretch.vol
=vol
;
554 static void add_midi_channel_event
(int channel
)
558 e.midi_channel.type
=SONG_EV_MIDI_CHANNEL
;
559 e.midi_channel.time
=cur_time
;
560 e.midi_channel.trk_id
=track
;
561 e.midi_channel.channel
=channel
;
567 static void add_midi_program_event
(int program
)
571 e.midi_program.type
=SONG_EV_MIDI_PROGRAM
;
572 e.midi_program.time
=cur_time
;
573 e.midi_program.trk_id
=track
;
574 e.midi_program.program
=program
;
580 static void init_track
(void)
581 /* sets the default values for a new track */
591 /* groups should not cross track boundaries */
594 /* reset arpeggiator */
608 %token
<i
> P_INTEGER S_INTEGER
609 %token
<d
> P_REAL S_REAL
610 %token
<i
> NOTE_P NOTE_T3 NOTE_T5
611 %token
<p
> NEW_MARK GOTO_MARK ASSERT_MARK
613 %token
<p
> BLOCK BLK_ASSIGN BLK_INSERT FILE_INSERT
616 %token
<i
> XC_ABSNOTE
619 %token SS_SEP SS_WAV SS_LOOP_WAV SS_PAT SS_SUSTAIN SS_VIBRATO SS_CHANNEL SS_VOL
621 %token SS_EFF_DELAY SS_EFF_ECHO SS_EFF_COMB SS_EFF_ALLPASS SS_EFF_FLANGER
622 %token SS_EFF_WOBBLE SS_EFF_SQWOBBLE SS_EFF_FADER SS_EFF_REVERB SS_EFF_OFF
624 %token SS_PITCH_STRETCH SS_TIME_STRETCH
626 %token MIDI_CHANNEL MIDI_PROGRAM MIDI_GENERIC
628 %type
<i
> integer note note_pitch rest
629 %type
<d
> real p_number number note_length
631 %type
<d
> arp_list arp_note
656 /* absolute octave */
660 /* relative octave */
664 /* absolute volume */
668 /* absolute volume */
672 /* relative volume */
689 /* group delimiter */
702 /* measure mark event */
727 add_tempo_event
(-1, $2);
729 |
'M' P_INTEGER
'/' P_INTEGER
{
730 /* meter (time signature) setting */
731 add_meter_event
(-1, $2, $4);
734 | BLOCK
'*' P_INTEGER
{
736 push_code
($1, $3, 1);
740 set_named_block
($2, $1);
743 /* insert named block */
744 insert_named_block
($1);
758 P_INTEGER
{ $$
= $1; }
759 | S_INTEGER
{ $$
= $1; }
764 | S_REAL
{ $$
= $1; }
768 P_INTEGER
{ $$
= (double) $1; }
769 | P_REAL
{ $$
= $1; }
773 integer
{ $$
= (double) $1; }
778 note_pitch
{ $$
= $1; }
779 | note note_length
{ $$
= $1; length
=$2; }
780 | note
'~' number
{ $$
= $1; DEBUGF
("imm volume: %lf\n", $3); }
785 | note_pitch
'&' { $$
= $1 - 1; }
786 | note_pitch
'#' { $$
= $1 + 1; }
787 | note_pitch
'\'' { $$
= $1 + 12; }
788 | note_pitch
',' { $$
= $1 - 12; }
792 P_INTEGER
{ $$
= 1 / (double) $1; }
793 | note_length NOTE_T3
{ $$
= $1 * 2.0 / 3.0; }
794 | note_length NOTE_T5
{ $$
= $1 * 4.0 / 5.0; }
795 | note_length
'*' p_number
{ $$
= $1 * $3; }
796 | note_length
'.' { $$
= $1 * 1.5; }
802 /* rest with length */
809 /* empty arpeggiator */
818 /* first arpeggiator note */
822 | arp_list
',' arp_note
{
823 /* rest of arpeggiator notes */
830 /* arpeggiator delay */
833 | arp_note
'~' number
{
834 /* arpeggiator volume */
835 arp_volume
=(float)$3;
837 | arp_note S_INTEGER
{
838 /* arpeggiator transpose */
841 | arp_note
'@' P_INTEGER
{
842 /* arpeggiator track */
848 P_INTEGER
{ $$
= $1; }
849 | XC_ABSNOTE
{ $$
= $1; }
853 SS_WAV XC_STR xc_absnote
{
854 /* load .wav file with just one note */
855 add_ss_wav_event
($2, $3, $3, $3, -1.0, -1.0);
857 | SS_WAV XC_STR xc_absnote xc_absnote xc_absnote
{
859 add_ss_wav_event
($2, $3, $4, $5, -1.0, -1.0);
861 | SS_WAV XC_STR xc_absnote xc_absnote xc_absnote number number
{
862 /* load .wav file, with loop boundaries */
863 add_ss_wav_event
($2, $3, $4, $5, $6, $7);
867 add_ss_pat_event
($2);
869 | SS_SUSTAIN XC_MSECS
{
871 add_ss_sustain_event
($2);
873 | SS_VIBRATO XC_MSECS number
{
875 add_ss_vibrato_event
($2, $3);
877 | SS_CHANNEL integer number
{
878 /* sets volume for a channel */
879 add_ss_channel_event
($2, $3);
881 | SS_VOL number number
{
882 /* set vol for 2 channels */
883 add_ss_channel_event
(0, $2);
884 add_ss_channel_event
(1, $3);
886 | SS_VOL number number number
{
887 /* set vol for 3 channels */
888 add_ss_channel_event
(0, $2);
889 add_ss_channel_event
(1, $3);
890 add_ss_channel_event
(2, $4);
892 | SS_VOL number number number number
{
893 /* set vol for 4 channels */
894 add_ss_channel_event
(0, $2);
895 add_ss_channel_event
(1, $3);
896 add_ss_channel_event
(2, $4);
897 add_ss_channel_event
(3, $5);
899 | SS_VOL number number number number number number
{
900 /* set vol for 6 channels */
901 add_ss_channel_event
(0, $2);
902 add_ss_channel_event
(1, $3);
903 add_ss_channel_event
(2, $4);
904 add_ss_channel_event
(3, $5);
905 add_ss_channel_event
(4, $6);
906 add_ss_channel_event
(5, $7);
908 | SS_EFF_DELAY integer XC_MSECS
{
910 add_ss_eff_event
(SONG_EV_SS_EFF_DELAY
,
911 $2, $3, 0, 0, 0, 0, 0, 0);
914 | SS_EFF_ECHO integer XC_MSECS number
{
916 add_ss_eff_event
(SONG_EV_SS_EFF_ECHO
,
917 $2, $3, $4, 0, 0, 0, 0, 0);
920 | SS_EFF_COMB integer XC_MSECS number
{
922 add_ss_eff_event
(SONG_EV_SS_EFF_COMB
,
923 $2, $3, $4, 0, 0, 0, 0, 0);
926 | SS_EFF_ALLPASS integer XC_MSECS number
{
928 add_ss_eff_event
(SONG_EV_SS_EFF_ALLPASS
,
929 $2, $3, $4, 0, 0, 0, 0, 0);
932 | SS_EFF_FLANGER integer XC_MSECS number XC_MSECS number number
{
934 add_ss_eff_event
(SONG_EV_SS_EFF_FLANGER
,
935 $2, $3, $4, $5, $6, $7, 0, 0);
938 | SS_EFF_WOBBLE integer number number
{
940 add_ss_eff_event
(SONG_EV_SS_EFF_WOBBLE
,
941 $2, 0, 0, 0, $3, $4, 0, 0);
944 | SS_EFF_SQWOBBLE integer number number
{
945 /* square wobble effect */
946 add_ss_eff_event
(SONG_EV_SS_EFF_SQWOBBLE
,
947 $2, 0, 0, 0, $3, $4, 0, 0);
950 | SS_EFF_FADER integer XC_MSECS number number
{
952 add_ss_eff_event
(SONG_EV_SS_EFF_FADER
,
953 $2, $3, 0, 0, 0, 0, $4, $5);
956 | SS_EFF_REVERB integer
{
958 add_ss_eff_event
(SONG_EV_SS_EFF_REVERB
,
959 $2, 0, 0, 0, 0, 0, 0, 0);
962 | SS_EFF_OFF integer
{
964 add_ss_eff_event
(SONG_EV_SS_EFF_OFF
,
965 $2, 0, 0, 0, 0, 0, 0, 0);
968 | SS_PITCH_STRETCH xc_absnote number number
{
969 /* pitch stretch note */
970 add_ss_pitch_stretch
($2, $3, $4);
975 | MIDI_CHANNEL integer
{
977 add_midi_channel_event
($2);
980 | MIDI_PROGRAM integer
{
982 add_midi_program_event
($2);
988 void yyerror(char * s
)
990 c_err
(s
, NULL
, NULL
);
998 push_code
("!this-is-a-mark", 1, 0);
999 push_code
("< c1 ; e& ; g>", 1, 0);
1000 push_code
("^this-is-a-mark", 1, 0);
1001 push_code
("x4,2-1,2.+4", 1, 0);
1002 push_code
("a b'' c,, ", 1, 0);
1003 push_code
("v0.1 (d8 v+0.1)*10", 1, 0);
1004 push_code
("t1 t-2 t+3", 1, 0);
1005 push_code
("{ fader -1 100ms 0 1 } r r4. z2 `room_kit.ahs` o4 o+1 o-2 ", 1, 0);
1006 push_code
("{ sustain 100ms }", 1, 0);
1007 push_code
("T120.0 M4/4 z8 abcde T80 (edcba)=drum1 $drum1 $drum1 ((a&b)*3 (cd)*2)*10", 2, 0);
1011 printf
("Exiting main...\n");
1016 static void compile_startup
(void)
1022 /* default settings */
1023 add_tempo_event
(-2, 120.0);
1024 add_meter_event
(-2, 4, 4);
1029 int compile
(char * code
)
1033 push_code
(code
, 1, 0);
1035 return
(yyparse() + compiler_error
);
1039 int compile_file
(char * file
)
1043 if
(insert_file
(file
))
1046 return
(yyparse() + compiler_error
);