2 beam.cc -- implement Beam
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
13 - Determine auto knees based on positions if it's set by the user.
15 - the code is littered with * and / staff_space calls for
16 #'positions. Consider moving to real-world coordinates?
18 Problematic issue is user tweaks (user tweaks are in staff-coordinates.)
22 - Stems run to the Y-center of the beam.
24 - beam_translation is the offset between Y centers of the beam.
27 #include <math.h> // tanh.
30 #include "interval-set.hh"
31 #include "directional-element-interface.hh"
34 #include "least-squares.hh"
36 #include "output-def.hh"
38 #include "pointer-group-interface.hh"
39 #include "staff-symbol-referencer.hh"
45 #include "text-interface.hh" // debug output.
46 #include "font-interface.hh" // debug output.
50 Beam::add_stem (Grob *me, Grob *s)
52 Pointer_group_interface::add_grob (me, ly_symbol2scm ("stems"), s);
54 s->add_dependency (me);
56 assert (!Stem::get_beam (s));
57 s->set_object ("beam", me->self_scm ());
59 add_bound_item (dynamic_cast<Spanner *> (me), dynamic_cast<Item *> (s));
63 Beam::get_thickness (Grob *me)
65 return robust_scm2double (me->get_property ("thickness"), 0)
66 * Staff_symbol_referencer::staff_space (me);
69 /* Return the translation between 2 adjoining beams. */
71 Beam::get_beam_translation (Grob *me)
73 SCM func = me->get_property ("space-function");
75 if (ly_is_procedure (func))
77 SCM s = scm_call_2 (func, me->self_scm (), scm_from_int (get_beam_count (me)));
78 return scm_to_double (s);
86 /* Maximum beam_count. */
88 Beam::get_beam_count (Grob *me)
92 extract_grob_set (me, "stems", stems);
93 for (int i = 0; i < stems.size(); i++)
95 Grob *stem = stems[i];
96 m = max (m, (Stem::beam_multiplicity (stem).length () + 1));
102 Space return space between beams.
104 MAKE_SCHEME_CALLBACK (Beam, space_function, 2);
106 Beam::space_function (SCM smob, SCM beam_count)
108 Grob *me = unsmob_grob (smob);
110 Real staff_space = Staff_symbol_referencer::staff_space (me);
111 Real line = Staff_symbol_referencer::line_thickness (me);
112 Real thickness = get_thickness (me);
114 Real beam_translation = scm_to_int (beam_count) < 4
115 ? (2 * staff_space + line - thickness) / 2.0
116 : (3 * staff_space + line - thickness) / 3.0;
118 return scm_from_double (beam_translation);
121 /* After pre-processing all directions should be set.
122 Several post-processing routines (stem, slur, script) need stem/beam
124 Currenly, this means that beam has set all stem's directions.
125 [Alternatively, stems could set its own directions, according to
126 their beam, during 'final-pre-processing'.] */
127 MAKE_SCHEME_CALLBACK (Beam, before_line_breaking, 1);
129 Beam::before_line_breaking (SCM smob)
131 Grob *me = unsmob_grob (smob);
133 /* Beams with less than 2 two stems don't make much sense, but could happen
138 For a beam that only has one stem, we try to do some disappearance magic:
139 we revert the flag, and move on to The Eternal Engraving Fields. */
141 int count = visible_stem_count (me);
144 extract_grob_set (me, "stems", stems);
145 if (stems.size () == 1)
147 me->warning (_ ("removing beam with less than two stems"));
149 stems[0]->set_object ("beam", SCM_EOL);
152 return SCM_UNSPECIFIED;
154 else if (stems.size () == 0)
157 return SCM_UNSPECIFIED;
162 Direction d = get_default_dir (me);
164 consider_auto_knees (me);
165 set_stem_directions (me, d);
169 set_stem_shorten (me);
175 /* We want a maximal number of shared beams, but if there is choice, we
176 * take the one that is closest to the end of the stem. This is for
188 position_with_maximal_common_beams (SCM left_beaming, SCM right_beaming,
192 Slice lslice = int_list_to_slice (scm_cdr (left_beaming));
196 for (int i = lslice[-left_dir];
197 (i - lslice[left_dir]) * left_dir <= 0; i += left_dir)
200 for (SCM s = scm_car (right_beaming); scm_is_pair (s); s = scm_cdr (s))
202 int k = -right_dir * scm_to_int (scm_car (s)) + i;
203 if (scm_c_memq (scm_from_int (k), left_beaming) != SCM_BOOL_F)
207 if (count >= best_count)
218 Beam::connect_beams (Grob *me)
220 extract_grob_set (me, "stems", stems);
223 last_int.set_empty ();
224 SCM last_beaming = SCM_EOL;
225 Direction last_dir = CENTER;
226 for (int i = 0; i < stems.size (); i++)
228 Grob *this_stem = stems[i];
229 SCM this_beaming = this_stem->get_property ("beaming");
231 Direction this_dir = get_grob_direction (this_stem);
232 if (scm_is_pair (last_beaming) && scm_is_pair (this_beaming))
234 int start_point = position_with_maximal_common_beams
235 (last_beaming, this_beaming,
242 if (d == RIGHT && i == stems.size () - 1)
245 new_slice.set_empty ();
246 SCM s = index_get_cell (this_beaming, d);
247 for (; scm_is_pair (s); s = scm_cdr (s))
250 = start_point - this_dir * scm_to_int (scm_car (s));
252 new_slice.add_point (new_beam_pos);
253 scm_set_car_x (s, scm_from_int (new_beam_pos));
256 while (flip (&d) != LEFT);
258 if (!new_slice.is_empty ())
259 last_int = new_slice;
263 scm_set_car_x (this_beaming, SCM_EOL);
264 SCM s = scm_cdr (this_beaming);
265 for (; scm_is_pair (s); s = scm_cdr (s))
267 int np = -this_dir * scm_to_int (scm_car (s));
268 scm_set_car_x (s, scm_from_int (np));
269 last_int.add_point (np);
273 if (i == stems.size () -1)
275 scm_set_cdr_x (this_beaming, SCM_EOL);
278 if (scm_ilength (scm_cdr (this_beaming)) > 0)
280 last_beaming = this_beaming;
287 TODO: should not make beams per stem, but per Y-level.
289 MAKE_SCHEME_CALLBACK (Beam, print, 1);
291 Beam::print (SCM grob)
293 Spanner *me = unsmob_spanner (grob);
296 extract_grob_set (me, "stems", stems);
297 Grob *xcommon = common_refpoint_of_array (stems, me, X_AXIS);
299 xcommon = me->get_bound (LEFT)->common_refpoint (xcommon, X_AXIS);
300 xcommon = me->get_bound (RIGHT)->common_refpoint (xcommon, X_AXIS);
303 if (visible_stem_count (me))
305 // ugh -> use commonx
306 x0 = first_visible_stem (me)->relative_coordinate (xcommon, X_AXIS);
307 dx = last_visible_stem (me)->relative_coordinate (xcommon, X_AXIS) - x0;
311 x0 = stems[0]->relative_coordinate (xcommon, X_AXIS);
312 dx = stems.top ()->relative_coordinate (xcommon, X_AXIS) - x0;
315 SCM posns = me->get_property ("positions");
316 Drul_array<Real> pos;
317 if (!is_number_pair (posns))
319 programming_error ("no beam positions?");
320 pos = Interval (0, 0);
323 pos = ly_scm2realdrul (posns);
325 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
327 Real dy = pos[RIGHT] - pos[LEFT];
328 Real slope = (dy && dx) ? dy / dx : 0;
330 Real thick = get_thickness (me);
331 Real bdy = get_beam_translation (me);
333 SCM last_beaming = SCM_EOL;
334 Real last_xposn = -1;
335 Real last_stem_width = -1;
337 Real gap_length = robust_scm2double (me->get_property ("gap"), 0.0);
340 Real lt = me->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
342 for (int i = 0; i <= stems.size (); i++)
344 Grob *st = (i < stems.size ()) ? stems[i] : 0;
346 SCM this_beaming = st ? st->get_property ("beaming") : SCM_EOL;
347 Real xposn = st ? st->relative_coordinate (xcommon, X_AXIS) : 0.0;
348 Real stem_width = st ? robust_scm2double (st->get_property ("thickness"), 1.0) * lt : 0;
349 Direction stem_dir = st ? to_dir (st->get_property ("direction")) : CENTER;
351 We do the space left of ST, with lfliebertjes pointing to the
352 right from the left stem, and rfliebertjes pointing left from
355 SCM left = (i > 0) ? scm_cdr (last_beaming) : SCM_EOL;
356 SCM right = st ? scm_car (this_beaming) : SCM_EOL;
358 Array<int> full_beams;
359 Array<int> lfliebertjes;
360 Array<int> rfliebertjes;
363 scm_is_pair (s); s = scm_cdr (s))
365 int b = scm_to_int (scm_car (s));
366 if (scm_c_memq (scm_car (s), right) != SCM_BOOL_F)
372 lfliebertjes.push (b);
376 scm_is_pair (s); s = scm_cdr (s))
378 int b = scm_to_int (scm_car (s));
379 if (scm_c_memq (scm_car (s), left) == SCM_BOOL_F)
381 rfliebertjes.push (b);
386 how much to stick out for beams across linebreaks
388 Real break_overshoot = 3.0;
389 Real w = (i > 0 && st) ? (xposn - last_xposn) : break_overshoot;
391 Real stem_offset = 0.0;
394 w += last_stem_width / 2;
395 stem_offset = -last_stem_width / 2;
401 Real blot = me->get_layout ()->get_dimension (ly_symbol2scm ("blotdiameter"));
402 Stencil whole = Lookup::beam (slope, w, thick, blot);
406 if (scm_is_number (me->get_property ("gap-count")))
408 gap_count = scm_to_int (me->get_property ("gap-count"));
409 gapped = Lookup::beam (slope, w - 2 * gap_length, thick, blot);
411 full_beams.sort (default_compare);
413 full_beams.reverse ();
417 for (int j = full_beams.size (); j--;)
424 b.translate_axis (gap_length, X_AXIS);
426 b.translate_axis (last_xposn - x0 + stem_offset, X_AXIS);
427 b.translate_axis (slope * (last_xposn - x0) + bdy * full_beams[j], Y_AXIS);
429 the_beam.add_stencil (b);
432 if (lfliebertjes.size () || rfliebertjes.size ())
438 int t = Stem::duration_log (st);
440 SCM proc = me->get_property ("flag-width-function");
441 SCM result = scm_call_1 (proc, scm_from_int (t));
442 nw_f = scm_to_double (result);
445 nw_f = break_overshoot / 2;
447 /* Half beam should be one note-width,
448 but let's make sure two half-beams never touch */
452 rw = min (nw_f, ((xposn - last_xposn) / 2));
455 TODO: 0.5 is a guess.
457 rw = xposn - me->get_bound (LEFT)->extent (xcommon, X_AXIS)[RIGHT]
461 lw = min (nw_f, ((xposn - last_xposn) / 2));
463 lw = me->get_bound (RIGHT)->relative_coordinate (xcommon, X_AXIS)
466 Stencil rhalf = Lookup::beam (slope, rw, thick, blot);
467 Stencil lhalf = Lookup::beam (slope, lw, thick, blot);
468 for (int j = lfliebertjes.size (); j--;)
471 b.translate_axis (last_xposn - x0, X_AXIS);
472 b.translate_axis (slope * (last_xposn - x0) + bdy * lfliebertjes[j], Y_AXIS);
473 the_beam.add_stencil (b);
475 for (int j = rfliebertjes.size (); j--;)
478 b.translate_axis (xposn - x0 - rw, X_AXIS);
479 b.translate_axis (slope * (xposn - x0 -rw) + bdy * rfliebertjes[j], Y_AXIS);
480 the_beam.add_stencil (b);
485 last_stem_width = stem_width;
486 last_beaming = this_beaming;
489 the_beam.translate_axis (x0 - me->relative_coordinate (xcommon, X_AXIS), X_AXIS);
490 the_beam.translate_axis (pos[LEFT], Y_AXIS);
493 SCM quant_score = me->get_property ("quant-score");
494 if (to_boolean (me->get_layout ()->lookup_variable (ly_symbol2scm ("debug-beam-quanting")))
495 && scm_is_string (quant_score))
499 This code prints the demerits for each beam. Perhaps this
500 should be switchable for those who want to twiddle with the
504 SCM properties = Font_interface::text_font_alist_chain (me);
506 Direction stem_dir = stems.size () ? to_dir (stems[0]->get_property ("direction")) : UP;
508 Stencil tm = *unsmob_stencil (Text_interface::interpret_markup
509 (me->get_layout ()->self_scm (), properties, quant_score));
510 the_beam.add_at_edge (Y_AXIS, stem_dir, tm, 1.0, 0);
514 return the_beam.smobbed_copy ();
518 Beam::get_default_dir (Grob *me)
520 Drul_array<int> total;
521 total[UP] = total[DOWN] = 0;
522 Drul_array<int> count;
523 count[UP] = count[DOWN] = 0;
526 extract_grob_set (me, "stems", stems);
528 for (int i = 0; i < stems.size (); i++)
532 Direction sd = get_grob_direction (s);
534 int center_distance = max (int (- d * Stem::head_positions (s) [-d]), 0);
535 int current = sd ? (1 + d * sd) / 2 : center_distance;
543 while (flip (&d) != DOWN);
545 SCM func = me->get_property ("dir-function");
546 SCM s = scm_call_2 (func,
547 scm_cons (scm_from_int (count[UP]),
548 scm_from_int (count[DOWN])),
549 scm_cons (scm_from_int (total[UP]),
550 scm_from_int (total[DOWN])));
552 if (scm_is_number (s) && scm_to_int (s))
555 /* If dir is not determined: get default */
556 return to_dir (me->get_property ("neutral-direction"));
559 /* Set all stems with non-forced direction to beam direction.
560 Urg: non-forced should become `without/with unforced' direction,
561 once stem gets cleaned-up. */
563 Beam::set_stem_directions (Grob *me, Direction d)
565 extract_grob_set (me, "stems", stems);
567 for (int i = 0; i < stems.size (); i++)
571 SCM forcedir = s->get_property ("direction");
572 if (!to_dir (forcedir))
573 set_grob_direction (s, d);
578 Only try horizontal beams for knees. No reliable detection of
579 anything else is possible here, since we don't know funky-beaming
580 settings, or X-distances (slopes!) People that want sloped
581 knee-beams, should set the directions manually.
586 this routine should take into account the stemlength scoring
587 of a possible knee/nonknee beam.
590 Beam::consider_auto_knees (Grob *me)
592 SCM scm = me->get_property ("auto-knee-gap");
593 if (!scm_is_number (scm))
600 extract_grob_set (me, "stems", stems);
602 Grob *common = common_refpoint_of_array (stems, me, Y_AXIS);
603 Real staff_space = Staff_symbol_referencer::staff_space (me);
605 Array<Interval> head_extents_array;
606 for (int i = 0; i < stems.size (); i++)
608 Grob *stem = stems[i];
609 if (Stem::is_invisible (stem))
612 Interval head_extents = Stem::head_positions (stem);
613 if (!head_extents.is_empty ())
615 head_extents[LEFT] += -1;
616 head_extents[RIGHT] += 1;
617 head_extents *= staff_space * 0.5;
620 We could subtract beam Y position, but this routine only
621 sets stem directions, a constant shift does not have an
624 head_extents += stem->relative_coordinate (common, Y_AXIS);
626 if (to_dir (stem->get_property ("direction")))
628 Direction stemdir = to_dir (stem->get_property ("direction"));
629 head_extents[-stemdir] = -stemdir * infinity_f;
632 head_extents_array.push (head_extents);
634 gaps.remove_interval (head_extents);
638 Real max_gap_len = 0.0;
640 for (int i = gaps.allowed_regions_.size () -1; i >= 0; i--)
642 Interval gap = gaps.allowed_regions_[i];
645 the outer gaps are not knees.
647 if (isinf (gap[LEFT]) || isinf (gap[RIGHT]))
650 if (gap.length () >= max_gap_len)
652 max_gap_len = gap.length ();
657 Real beam_translation = get_beam_translation (me);
658 Real beam_thickness = Beam::get_thickness (me);
659 int beam_count = Beam::get_beam_count (me);
660 Real height_of_beams = beam_thickness / 2
661 + (beam_count - 1) * beam_translation;
662 Real threshold = scm_to_double (scm) + height_of_beams;
664 if (max_gap_len > threshold)
667 for (int i = 0; i < stems.size (); i++)
669 Grob *stem = stems[i];
670 if (Stem::is_invisible (stem))
673 Interval head_extents = head_extents_array[j++];
675 Direction d = (head_extents.center () < max_gap.center ())
678 stem->set_property ("direction", scm_from_int (d));
680 head_extents.intersect (max_gap);
681 assert (head_extents.is_empty () || head_extents.length () < 1e-6);
686 /* Set stem's shorten property if unset.
689 take some y-position (chord/beam/nearest?) into account
690 scmify forced-fraction
692 This is done in beam because the shorten has to be uniform over the
696 Beam::set_stem_shorten (Grob *me)
699 shortening looks silly for x staff beams
704 Real forced_fraction = 1.0 * forced_stem_count (me)
705 / visible_stem_count (me);
707 int beam_count = get_beam_count (me);
709 SCM shorten_list = me->get_property ("beamed-stem-shorten");
710 if (shorten_list == SCM_EOL)
713 Real staff_space = Staff_symbol_referencer::staff_space (me);
716 = robust_list_ref (beam_count -1, shorten_list);
717 Real shorten_f = scm_to_double (shorten_elt) * staff_space;
719 /* your similar cute comment here */
720 shorten_f *= forced_fraction;
723 me->set_property ("shorten", scm_from_double (shorten_f));
726 /* Call list of y-dy-callbacks, that handle setting of
729 MAKE_SCHEME_CALLBACK (Beam, after_line_breaking, 1);
731 Beam::after_line_breaking (SCM smob)
733 Grob *me = unsmob_grob (smob);
736 return SCM_UNSPECIFIED;
740 Beam::position_beam (Grob *me)
744 if (to_boolean (me->get_property ("positioning-done")))
747 me->set_property ("positioning-done", SCM_BOOL_T);
749 /* Copy to mutable list. */
750 SCM s = ly_deep_copy (me->get_property ("positions"));
751 me->set_property ("positions", s);
753 if (scm_car (s) == SCM_BOOL_F)
755 // one wonders if such genericity is necessary --hwn.
756 SCM callbacks = me->get_property ("position-callbacks");
757 for (SCM i = callbacks; scm_is_pair (i); i = scm_cdr (i))
758 scm_call_1 (scm_car (i), me->self_scm ());
761 set_stem_lengths (me);
765 set_minimum_dy (Grob *me, Real *dy)
770 If dy is smaller than the smallest quant, we
771 get absurd direction-sign penalties.
774 Real ss = Staff_symbol_referencer::staff_space (me);
775 Real thickness = Beam::get_thickness (me) / ss;
776 Real slt = Staff_symbol_referencer::line_thickness (me) / ss;
777 Real sit = (thickness - slt) / 2;
779 Real hang = 1.0 - (thickness - slt) / 2;
781 *dy = sign (*dy) * max (fabs (*dy),
782 min (min (sit, inter), hang));
787 Compute a first approximation to the beam slope.
789 MAKE_SCHEME_CALLBACK (Beam, least_squares, 1);
791 Beam::least_squares (SCM smob)
793 Grob *me = unsmob_grob (smob);
795 int count = visible_stem_count (me);
800 me->set_property ("positions", ly_interval2scm (pos));
801 return SCM_UNSPECIFIED;
805 extract_grob_set (me, "stems", stems);
806 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
807 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
809 Real my_y = me->relative_coordinate (commony, Y_AXIS);
811 Grob *fvs = first_visible_stem (me);
812 Grob *lvs = last_visible_stem (me);
814 Interval ideal (Stem::get_stem_info (fvs).ideal_y_
815 + fvs->relative_coordinate (commony, Y_AXIS) -my_y,
816 Stem::get_stem_info (lvs).ideal_y_
817 + lvs->relative_coordinate (commony, Y_AXIS) - my_y);
819 Real x0 = first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
820 for (int i = 0; i < stems.size (); i++)
824 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
827 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS) - x0;
835 Interval chord (Stem::chord_start_y (first_visible_stem (me)),
836 Stem::chord_start_y (last_visible_stem (me)));
838 /* Simple beams (2 stems) on middle line should be allowed to be
841 However, if both stems reach middle line,
842 ideal[LEFT] == ideal[RIGHT] and ideal.delta () == 0.
844 For that case, we apply artificial slope */
845 if (!ideal[LEFT] && chord.delta () && count == 2)
848 Direction d = (Direction) (sign (chord.delta ()) * UP);
849 pos[d] = get_thickness (me) / 2;
858 For broken beams this doesn't work well. In this case, the
859 slope esp. of the first part of a broken beam should predict
860 where the second part goes.
862 me->set_property ("least-squares-dy",
863 scm_from_double (pos[RIGHT] - pos[LEFT]));
867 Array<Offset> ideals;
868 for (int i = 0; i < stems.size (); i++)
871 if (Stem::is_invisible (s))
873 ideals.push (Offset (x_posns[i],
874 Stem::get_stem_info (s).ideal_y_
875 + s->relative_coordinate (commony, Y_AXIS)
879 minimise_least_squares (&slope, &y, ideals);
883 set_minimum_dy (me, &dy);
884 me->set_property ("least-squares-dy", scm_from_double (dy));
885 pos = Interval (y, (y + dy));
889 "position" is relative to the staff.
891 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
893 me->set_property ("positions", ly_interval2scm (pos));
895 return SCM_UNSPECIFIED;
899 We can't combine with previous function, since check concave and
900 slope damping comes first.
902 TODO: we should use the concaveness to control the amount of damping
905 MAKE_SCHEME_CALLBACK (Beam, shift_region_to_valid, 1);
907 Beam::shift_region_to_valid (SCM grob)
909 Grob *me = unsmob_grob (grob);
914 extract_grob_set (me, "stems", stems);
915 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
916 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
918 Grob *fvs = first_visible_stem (me);
921 return SCM_UNSPECIFIED;
923 Real x0 = fvs->relative_coordinate (commonx, X_AXIS);
924 for (int i = 0; i < stems.size (); i++)
928 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
932 Grob *lvs = last_visible_stem (me);
934 return SCM_UNSPECIFIED;
936 Real dx = lvs->relative_coordinate (commonx, X_AXIS) - x0;
938 Drul_array<Real> pos = ly_scm2interval (me->get_property ("positions"));
940 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
942 Real dy = pos[RIGHT] - pos[LEFT];
944 Real slope = dx ? (dy / dx) : 0.0;
947 Shift the positions so that we have a chance of finding good
948 quants (i.e. no short stem failures.)
950 Interval feasible_left_point;
951 feasible_left_point.set_full ();
952 for (int i = 0; i < stems.size (); i++)
955 if (Stem::is_invisible (s))
958 Direction d = Stem::get_direction (s);
961 = Stem::get_stem_info (s).shortest_y_
962 - slope * x_posns [i];
965 left_y is now relative to the stem S. We want relative to
966 ourselves, so translate:
969 += + s->relative_coordinate (commony, Y_AXIS)
970 - me->relative_coordinate (commony, Y_AXIS);
976 feasible_left_point.intersect (flp);
979 if (feasible_left_point.is_empty ())
980 warning (_ ("no viable initial configuration found: may not find good beam slope"));
981 else if (!feasible_left_point.contains (y))
983 const int REGION_SIZE = 2; // UGH UGH
984 if (isinf (feasible_left_point[DOWN]))
985 y = feasible_left_point[UP] - REGION_SIZE;
986 else if (isinf (feasible_left_point[UP]))
987 y = feasible_left_point[DOWN]+ REGION_SIZE;
989 y = feasible_left_point.center ();
992 pos = Drul_array<Real> (y, (y + dy));
993 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
995 me->set_property ("positions", ly_interval2scm (pos));
996 return SCM_UNSPECIFIED;
999 /* This neat trick is by Werner Lemberg,
1000 damped = tanh (slope)
1001 corresponds with some tables in [Wanske] CHECKME */
1002 MAKE_SCHEME_CALLBACK (Beam, slope_damping, 1);
1004 Beam::slope_damping (SCM smob)
1006 Grob *me = unsmob_grob (smob);
1008 if (visible_stem_count (me) <= 1)
1009 return SCM_UNSPECIFIED;
1011 SCM s = me->get_property ("damping");
1012 Real damping = scm_to_double (s);
1016 Drul_array<Real> pos = ly_scm2interval (me->get_property ("positions"));
1017 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
1019 Real dy = pos[RIGHT] - pos[LEFT];
1021 Grob *fvs = first_visible_stem (me);
1022 Grob *lvs = last_visible_stem (me);
1024 Grob *commonx = fvs->common_refpoint (lvs, X_AXIS);
1026 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS)
1027 - first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
1029 Real slope = dy && dx ? dy / dx : 0;
1031 Real concaveness = robust_scm2double (me->get_property ("concaveness"), 0.0);
1033 slope = 0.6 * tanh (slope) / (damping + concaveness);
1035 Real damped_dy = slope * dx;
1037 set_minimum_dy (me, &damped_dy);
1039 pos[LEFT] += (dy - damped_dy) / 2;
1040 pos[RIGHT] -= (dy - damped_dy) / 2;
1042 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1044 me->set_property ("positions", ly_interval2scm (pos));
1046 return SCM_UNSPECIFIED;
1050 Report slice containing the numbers that are both in (car BEAMING)
1054 where_are_the_whole_beams (SCM beaming)
1058 for (SCM s = scm_car (beaming); scm_is_pair (s); s = scm_cdr (s))
1060 if (scm_c_memq (scm_car (s), scm_cdr (beaming)) != SCM_BOOL_F)
1062 l.add_point (scm_to_int (scm_car (s)));
1068 /* Return the Y position of the stem-end, given the Y-left, Y-right
1069 in POS for stem S. This Y position is relative to S. */
1071 Beam::calc_stem_y (Grob *me, Grob *s, Grob ** common,
1073 Drul_array<Real> pos, bool french)
1075 Real beam_translation = get_beam_translation (me);
1077 Real r = s->relative_coordinate (common[X_AXIS], X_AXIS) - xl;
1078 Real dy = pos[RIGHT] - pos[LEFT];
1080 Real stem_y_beam0 = (dy && dx
1085 Direction my_dir = get_grob_direction (s);
1086 SCM beaming = s->get_property ("beaming");
1088 Real stem_y = stem_y_beam0;
1091 Slice bm = where_are_the_whole_beams (beaming);
1092 if (!bm.is_empty ())
1093 stem_y += beam_translation * bm[-my_dir];
1097 Slice bm = Stem::beam_multiplicity (s);
1098 if (!bm.is_empty ())
1099 stem_y += bm[my_dir] * beam_translation;
1102 Real id = me->relative_coordinate (common[Y_AXIS], Y_AXIS)
1103 - s->relative_coordinate (common[Y_AXIS], Y_AXIS);
1109 Hmm. At this time, beam position and slope are determined. Maybe,
1110 stem directions and length should set to relative to the chord's
1111 position of the beam. */
1113 Beam::set_stem_lengths (Grob *me)
1115 extract_grob_set (me, "stems", stems);
1120 for (int a = 2; a--;)
1121 common[a] = common_refpoint_of_array (stems, me, Axis (a));
1123 Drul_array<Real> pos = ly_scm2realdrul (me->get_property ("positions"));
1124 Real staff_space = Staff_symbol_referencer::staff_space (me);
1125 scale_drul (&pos, staff_space);
1129 if (scm_is_number (me->get_property ("gap-count"))
1130 &&scm_to_int (me->get_property ("gap-count")))
1133 thick = get_thickness (me);
1136 // ugh -> use commonx
1137 Grob *fvs = first_visible_stem (me);
1138 Grob *lvs = last_visible_stem (me);
1140 Real xl = fvs ? fvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1141 Real xr = lvs ? lvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1143 for (int i = 0; i < stems.size (); i++)
1146 if (Stem::is_invisible (s))
1149 bool french = to_boolean (s->get_property ("french-beaming"));
1150 Real stem_y = calc_stem_y (me, s, common,
1152 pos, french && s != lvs && s!= fvs);
1155 Make the stems go up to the end of the beam. This doesn't matter
1156 for normal beams, but for tremolo beams it looks silly otherwise.
1159 stem_y += thick * 0.5 * get_grob_direction (s);
1161 Stem::set_stemend (s, 2 * stem_y / staff_space);
1166 Beam::set_beaming (Grob *me, Beaming_info_list *beaming)
1168 extract_grob_set (me, "stems", stems);
1171 for (int i = 0; i < stems.size (); i++)
1174 Don't overwrite user settings.
1179 /* Don't set beaming for outside of outer stems */
1180 if ((d == LEFT && i == 0)
1181 || (d == RIGHT && i == stems.size () -1))
1184 Grob *st = stems[i];
1185 SCM beaming_prop = st->get_property ("beaming");
1186 if (beaming_prop == SCM_EOL
1187 || index_get_cell (beaming_prop, d) == SCM_EOL)
1189 int b = beaming->infos_.elem (i).beams_i_drul_[d];
1191 && i < stems.size () -1
1192 && Stem::is_invisible (st))
1193 b = min (b, beaming->infos_.elem (i).beams_i_drul_[-d]);
1195 Stem::set_beaming (st, b, d);
1198 while (flip (&d) != LEFT);
1203 Beam::forced_stem_count (Grob *me)
1205 extract_grob_set (me, "stems", stems);
1208 for (int i = 0; i < stems.size (); i++)
1212 if (Stem::is_invisible (s))
1215 /* I can imagine counting those boundaries as a half forced stem,
1216 but let's count them full for now. */
1217 if (abs (Stem::chord_start_y (s)) > 0.1
1218 && (Stem::get_direction (s) != Stem::get_default_dir (s)))
1225 Beam::visible_stem_count (Grob *me)
1227 extract_grob_set (me, "stems", stems);
1229 for (int i = stems.size (); i--;)
1231 if (!Stem::is_invisible (stems[i]))
1238 Beam::first_visible_stem (Grob *me)
1240 extract_grob_set (me, "stems", stems);
1242 for (int i = 0; i < stems.size (); i++)
1244 if (!Stem::is_invisible (stems[i]))
1251 Beam::last_visible_stem (Grob *me)
1253 extract_grob_set (me, "stems", stems);
1255 for (int i = stems.size (); i--;)
1257 if (!Stem::is_invisible (stems[i]))
1266 handle rest under beam (do_post: beams are calculated now)
1267 what about combination of collisions and rest under beam.
1271 rest -> stem -> beam -> interpolate_y_position ()
1273 MAKE_SCHEME_CALLBACK (Beam, rest_collision_callback, 2);
1275 Beam::rest_collision_callback (SCM element_smob, SCM axis)
1277 Grob *rest = unsmob_grob (element_smob);
1280 if (scm_is_number (rest->get_property ("staff-position")))
1281 return scm_from_int (0);
1283 assert (scm_to_int (axis) == Y_AXIS);
1285 Grob *st = unsmob_grob (rest->get_object ("stem"));
1288 return scm_from_double (0.0);
1289 Grob *beam = unsmob_grob (stem->get_object ("beam"));
1291 || !Beam::has_interface (beam)
1292 || !Beam::visible_stem_count (beam))
1293 return scm_from_double (0.0);
1295 Drul_array<Real> pos (0, 0);
1296 SCM s = beam->get_property ("positions");
1297 if (scm_is_pair (s) && scm_is_number (scm_car (s)))
1298 pos = ly_scm2interval (s);
1299 Real staff_space = Staff_symbol_referencer::staff_space (rest);
1301 scale_drul (&pos, staff_space);
1303 Real dy = pos[RIGHT] - pos[LEFT];
1305 // ugh -> use commonx
1306 Real x0 = first_visible_stem (beam)->relative_coordinate (0, X_AXIS);
1307 Real dx = last_visible_stem (beam)->relative_coordinate (0, X_AXIS) - x0;
1308 Real slope = dy && dx ? dy / dx : 0;
1310 Direction d = Stem::get_direction (stem);
1311 Real stem_y = pos[LEFT] + (stem->relative_coordinate (0, X_AXIS) - x0) * slope;
1313 Real beam_translation = get_beam_translation (beam);
1314 Real beam_thickness = Beam::get_thickness (beam);
1317 TODO: this is not strictly correct for 16th knee beams.
1320 = Stem::beam_multiplicity (stem).length () + 1;
1322 Real height_of_my_beams = beam_thickness / 2
1323 + (beam_count - 1) * beam_translation;
1324 Real beam_y = stem_y - d * height_of_my_beams;
1326 Grob *common_y = rest->common_refpoint (beam, Y_AXIS);
1328 Real rest_dim = rest->extent (common_y, Y_AXIS)[d];
1329 Real minimum_distance
1330 = + staff_space * (robust_scm2double (stem->get_property ("stemlet-length"), 0.0)
1331 + robust_scm2double (rest->get_property ("minimum-distance"), 0.0));
1333 Real shift = d * min (((beam_y - d * minimum_distance) - rest_dim) * d, 0.0);
1335 shift /= staff_space;
1336 Real rad = Staff_symbol_referencer::line_count (rest) * staff_space / 2;
1338 /* Always move discretely by half spaces */
1339 shift = ceil (fabs (shift * 2.0)) / 2.0 * sign (shift);
1341 /* Inside staff, move by whole spaces*/
1342 if ((rest->extent (common_y, Y_AXIS)[d] + staff_space * shift) * d
1344 || (rest->extent (common_y, Y_AXIS)[-d] + staff_space * shift) * -d
1346 shift = ceil (fabs (shift)) * sign (shift);
1348 return scm_from_double (staff_space * shift);
1352 Beam::is_knee (Grob *me)
1354 SCM k = me->get_property ("knee");
1355 if (scm_is_bool (k))
1356 return ly_scm2bool (k);
1360 extract_grob_set (me, "stems", stems);
1361 for (int i = stems.size (); i--;)
1363 Direction dir = get_grob_direction (stems[i]);
1372 me->set_property ("knee", ly_bool2scm (knee));
1378 Beam::get_direction_beam_count (Grob *me, Direction d)
1380 extract_grob_set (me, "stems", stems);
1383 for (int i = stems.size (); i--;)
1386 Should we take invisible stems into account?
1388 if (Stem::get_direction (stems[i]) == d)
1389 bc = max (bc, (Stem::beam_multiplicity (stems[i]).length () + 1));
1395 ADD_INTERFACE (Beam, "beam-interface",
1397 "The @code{thickness} property is the weight of beams, and is measured "
1399 "knee positioning-done position-callbacks "
1400 "concaveness dir-function quant-score auto-knee-gap gap "
1401 "gap-count chord-tremolo beamed-stem-shorten shorten least-squares-dy "
1402 "details damping inspect-quants flag-width-function neutral-direction positions space-function "