4 Ann Hell Ex Machina - Music Software
5 Copyright (C) 2003/2004 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
42 void yyerror(char * s
);
44 /* injecting code functions (defined in compiler.l) */
45 int push_code
(char * code
, int times
, int dyn
);
46 int push_code_from_file
(char * file
);
55 static double _length
;
57 static int _transpose
;
58 static double _staccato
;
62 /* parser debugging */
64 #define DEBUGF if(_debug)printf
74 static struct _named_block
* _named_blocks
=NULL
;
75 static int _n_named_blocks
=0;
76 static int _named_blocks_size
=0;
86 static struct _group
* _groups
=NULL
;
87 static int _n_groups
=0;
88 static int _groups_size
=0;
97 static struct _mark
* _marks
=NULL
;
98 static int _n_marks
=0;
99 static int _marks_size
=0;
110 static struct _arp
* _arps
=NULL
;
111 static int _n_arps
=0;
112 static int _arps_size
=0;
114 static double _arp_delay
;
115 static int _arp_transpose
;
116 static float _arp_volume
;
117 static int _arp_track
;
121 ********************/
123 void _c_err
(char * e1
, char * e2
, char * e3
)
124 /* reports an error from the compiler */
126 printf
("Compiler error:");
127 if
(e1
!= NULL
) printf
(" %s", e1
);
128 if
(e2
!= NULL
) printf
(" %s", e2
);
129 if
(e3
!= NULL
) printf
(" %s", e3
);
134 static void _forward
(void)
135 /* moves forward current time by current length */
137 /* just add current length to time cursor */
138 _cur_time
+= _length
;
140 /* quantizations could be done here */
146 static int _set_named_block
(char * name
, char * block
)
147 /* sets a named block */
151 /* find first if block is previously defined */
152 for
(n
=0;n
< _n_named_blocks
;n
++)
154 if
(strcmp
(name
, _named_blocks
[n
].name
) == 0)
156 free
(_named_blocks
[n
].block
);
161 if
(n
== _n_named_blocks
)
163 /* need to expand? */
164 if
(_n_named_blocks
== _named_blocks_size
)
166 _named_blocks_size
+= 4;
168 _named_blocks
=(struct _named_block
*)
169 realloc
(_named_blocks
,
171 sizeof
(struct _named_block
));
177 strncpy
(_named_blocks
[n
].name
, name
, sizeof
(_named_blocks
[n
].name
));
178 _named_blocks
[n
].block
=block
;
184 static void _insert_named_block
(char * name
)
185 /* inserts a named block */
189 for
(n
=0;n
< _n_named_blocks
;n
++)
191 if
(strcmp
(name
, _named_blocks
[n
].name
) == 0)
193 push_code
(_named_blocks
[n
].block
, 1, 0);
198 _c_err
("block-not-found", name
, NULL
);
202 static void _insert_file
(char * filename
)
204 if
(!push_code_from_file
(filename
))
205 _c_err
("script-not-found", filename
, NULL
);
211 static int _push_group
(void)
212 /* starts a new group of notes */
214 if
(_n_groups
== _groups_size
)
218 _groups
=(struct _group
*) realloc
(_groups
,
219 _groups_size
* sizeof
(struct _group
));
222 _groups
[_n_groups
].start
=_cur_time
;
223 _groups
[_n_groups
].gliss
=0.0;
224 _groups
[_n_groups
].max
=0.0;
232 static int _next_group_part
(void)
237 _c_err
("missing-start-of-group", NULL
, NULL
);
241 /* store maximum frame */
242 if
(_groups
[_n_groups
- 1].max
< _cur_time
)
243 _groups
[_n_groups
- 1].max
=_cur_time
;
245 /* add glissando delay */
246 _groups
[_n_groups
- 1].gliss
+= _gliss
;
249 _cur_time
=_groups
[_n_groups
- 1].start
+ _groups
[_n_groups
- 1].gliss
;
255 static int _pop_group
(void)
256 /* finishes a group, moving the frame to the end of the longer part */
260 _c_err
("missing-start-of-group", NULL
, NULL
);
266 _cur_time
=_groups
[_n_groups
].max
;
274 static void _add_mark
(char * name
)
275 /* creates a new mark */
277 if
(_n_marks
== _marks_size
)
281 _marks
=(struct _mark
*) realloc
(_marks
,
282 _marks_size
* sizeof
(struct _mark
));
285 strncpy
(_marks
[_n_marks
].name
, name
, sizeof
(_marks
[_n_marks
].name
));
286 _marks
[_n_marks
].time
=_cur_time
;
292 static void _find_mark
(char * name
, int set
)
293 /* finds a mark by name, optionally moving time cursor there */
297 for
(n
=0;n
< _n_marks
;n
++)
299 if
(strcmp
(_marks
[n
].name
, name
) == 0)
303 if
(_cur_time
> _marks
[n
].time
)
304 _c_err
("mark-overpassed", name
, NULL
);
306 _cur_time
=_marks
[n
].time
;
309 if
(_cur_time
!= _marks
[n
].time
)
310 _c_err
("mark-mismatch", name
, NULL
);
316 _c_err
("mark-not-found", name
, NULL
);
322 static void _arp_default
(void)
323 /* resets arpeggiator values to the default ones */
332 static void _add_arp
(void)
333 /* adds an arpeggiator note */
335 /* if the note is exactly the same, do nothing */
336 if
(_arp_delay
== 0.0 && _arp_transpose
== 0 &&
337 _arp_volume
== 1.0 && _arp_track
== _track
)
340 if
(_n_arps
== _arps_size
)
344 _arps
=(struct _arp
*) realloc
(_arps
,
345 _arps_size
* sizeof
(struct _arp
));
348 _arps
[_n_arps
].delay
=_arp_delay
;
349 _arps
[_n_arps
].transpose
=_arp_transpose
;
350 _arps
[_n_arps
].volume
=_arp_volume
;
351 _arps
[_n_arps
].track
=_arp_track
;
360 static void _add_note_event
(int note
)
361 /* adds a note event */
367 /* calculate the note */
368 np
=note
+ _transpose
+ (_octave
* 12);
370 /* is note out of range? */
371 if
(np
< 0 || np
> 127)
373 _c_err
("note-out-of-range", NULL
, NULL
);
377 e1.note.type
=EV1_NOTE
;
378 e1.note.time
=_cur_time
;
379 e1.note.trk_id
=_track
;
381 e1.note.len
=_length
* _staccato
;
386 printf
("Note(%d): %lf %d %lf %lf\n",
387 _track
, _cur_time
, np
, _length
, _volume
);
389 /* add arpeggiator repetitions */
390 for
(n
=0;n
< _n_arps
;n
++)
392 e1.note.time
=_cur_time
+ _arps
[n
].delay
;
393 e1.note.trk_id
=_arps
[n
].track
;
394 e1.note.note
=np
+ _arps
[n
].transpose
;
395 e1.note.vol
=_volume
* _arps
[n
].volume
;
402 static void _add_tempo_event
(int trk_id
, double tempo
)
406 e1.tempo.type
=EV1_TEMPO
;
407 e1.tempo.time
=_cur_time
;
408 e1.tempo.trk_id
=trk_id
;
409 e1.tempo.tempo
=tempo
;
414 static void _add_meter_event
(int trk_id
, int num
, int den
)
418 e1.meter.type
=EV1_METER
;
419 e1.meter.time
=_cur_time
;
420 e1.meter.trk_id
=trk_id
;
426 static void _add_measure_event
(void)
430 e1.generic.type
=EV1_MEASURE
;
431 e1.generic.time
=_cur_time
;
432 e1.generic.trk_id
=-1;
437 static void _init_track
(void)
438 /* sets the default values for a new track */
448 /* groups should not cross track boundaries */
451 /* reset arpeggiator */
465 %token
<i
> P_INTEGER S_INTEGER
466 %token
<d
> P_REAL S_REAL
467 %token
<i
> NOTE_P NOTE_T3 NOTE_T5
468 %token
<p
> NEW_MARK GOTO_MARK ASSERT_MARK
470 %token
<p
> BLOCK BLK_ASSIGN BLK_INSERT FILE_INSERT
475 %token SS_SEP SS_WAV SS_PAT SS_SUSTAIN
476 %token SS_EFF_DELAY SS_EFF_ECHO SS_EFF_COMB SS_EFF_ALLPASS
477 %token SS_EFF_FLANGER SS_EFF_WOBBLE SS_EFF_SQWOBBLE SS_EFF_FADER
479 %type
<i
> integer note note_pitch rest
480 %type
<d
> real p_number number note_length
482 %type
<d
> arp_list arp_note
506 /* absolute octave */
510 /* relative octave */
514 /* absolute volume */
518 /* relative volume */
519 _volume
+= (float)$2;
535 /* group delimiter */
548 /* measure mark event */
549 _add_measure_event
();
573 _add_tempo_event
(-1, $2);
575 |
'M' P_INTEGER
'/' P_INTEGER
{
576 /* meter (time signature) setting */
577 _add_meter_event
(-1, $2, $4);
580 | BLOCK
'*' P_INTEGER
{
582 push_code
($1, $3, 1);
586 _set_named_block
($2, $1);
589 /* insert named block */
590 _insert_named_block
($1);
599 | ss_cmd
{ DEBUGF
("ss_cmd\n"); }
604 P_INTEGER
{ $$
= $1; }
605 | S_INTEGER
{ $$
= $1; }
610 | S_REAL
{ $$
= $1; }
614 P_INTEGER
{ $$
= (double) $1; }
615 | P_REAL
{ $$
= $1; }
619 integer
{ $$
= (double) $1; }
624 note_pitch
{ $$
= $1; }
625 | note note_length
{ $$
= $1; _length
=$2; }
626 | note
'~' number
{ $$
= $1; DEBUGF
("imm volume: %lf\n", $3); }
631 | note_pitch
'&' { $$
= $1 - 1; }
632 | note_pitch
'#' { $$
= $1 + 1; }
633 | note_pitch
'\'' { $$
= $1 + 12; }
634 | note_pitch
',' { $$
= $1 - 12; }
638 P_INTEGER
{ $$
= 1 / (double) $1; }
639 | note_length NOTE_T3
{ $$
= $1 * 2.0 / 3.0; }
640 | note_length NOTE_T5
{ $$
= $1 * 4.0 / 5.0; }
641 | note_length
'*' p_number
{ $$
= $1 * $3; }
642 | note_length
'.' { $$
= $1 * 1.5; }
648 /* rest with length */
655 /* empty arpeggiator */
664 /* first arpeggiator note */
668 | arp_list
',' arp_note
{
669 /* rest of arpeggiator notes */
676 /* arpeggiator delay */
679 | arp_note
'~' number
{
680 /* arpeggiator volume */
681 _arp_volume
=(float)$3;
683 | arp_note S_INTEGER
{
684 /* arpeggiator transpose */
687 | arp_note
'@' P_INTEGER
{
688 /* arpeggiator track */
694 SS_WAV SS_STR
{ DEBUGF
("SS_WAV: %s\n", $2); }
695 | SS_PAT SS_STR
{ DEBUGF
("SS_PAT: %s\n", $2); }
696 | SS_SUSTAIN SS_FRAMES
{ DEBUGF
("SS_SUSTAIN: %d\n", $2); }
697 | SS_EFF_DELAY S_INTEGER SS_FRAMES
{ DEBUGF
("delay chan: %d frames: %d\n", $2, $3); }
698 | SS_EFF_ECHO S_INTEGER SS_FRAMES number
{ DEBUGF
("echo chan: %d frames: %d gain: %lf\n", $2, $3, $4); }
699 | SS_EFF_FADER S_INTEGER SS_FRAMES number number
{ DEBUGF
("fader chan: %d frames: %d %lf .. %lf\n", $2, $3, $4, $5); }
704 void yyerror(char * s
)
706 printf
("yyerror: %s\n", s
);
714 push_code
("!this-is-a-mark", 1, 0);
715 push_code
("< c1 ; e& ; g>", 1, 0);
716 push_code
("^this-is-a-mark", 1, 0);
717 push_code
("x4,2-1,2.+4", 1, 0);
718 push_code
("a b'' c,, ", 1, 0);
719 push_code
("v0.1 (d8 v+0.1)*10", 1, 0);
720 push_code
("t1 t-2 t+3", 1, 0);
721 push_code
("{ fader -1 100ms 0 1 } r r4. z2 `room_kit.ahs` o4 o+1 o-2 ", 1, 0);
722 push_code
("{ sustain 100ms }", 1, 0);
723 push_code
("T120.0 M4/4 z8 abcde T80 (edcba)=drum1 $drum1 $drum1 ((a&b)*3 (cd)*2)*10", 2, 0);
727 printf
("Exiting main...\n");
732 int compile
(char * code
)
737 /* default settings */
738 _add_tempo_event
(-2, 120.0);
739 _add_meter_event
(-2, 4, 4);
742 push_code
(code
, 1, 0);