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.
28 #include "interval-set.hh"
29 #include "directional-element-interface.hh"
32 #include "least-squares.hh"
34 #include "output-def.hh"
36 #include "pointer-group-interface.hh"
37 #include "staff-symbol-referencer.hh"
43 #include "text-interface.hh" // debug output.
44 #include "font-interface.hh" // debug output.
48 Beam::add_stem (Grob *me, Grob *s)
50 if (Stem::get_beam (s))
52 programming_error ("Stem already has beam");
56 Pointer_group_interface::add_grob (me, ly_symbol2scm ("stems"), s);
57 s->set_object ("beam", me->self_scm ());
58 add_bound_item (dynamic_cast<Spanner *> (me), dynamic_cast<Item *> (s));
62 Beam::get_thickness (Grob *me)
64 return robust_scm2double (me->get_property ("thickness"), 0)
65 * Staff_symbol_referencer::staff_space (me);
68 /* Return the translation between 2 adjoining beams. */
70 Beam::get_beam_translation (Grob *me)
72 int beam_count = get_beam_count (me);
73 Real staff_space = Staff_symbol_referencer::staff_space (me);
74 Real line = Staff_symbol_referencer::line_thickness (me);
75 Real thickness = get_thickness (me);
76 Real fract = robust_scm2double (me->get_property ("length-fraction"), 1.0);
78 Real beam_translation = beam_count < 4
79 ? (2 * staff_space + line - thickness) / 2.0
80 : (3 * staff_space + line - thickness) / 3.0;
82 return fract * beam_translation;
85 /* Maximum beam_count. */
87 Beam::get_beam_count (Grob *me)
91 extract_grob_set (me, "stems", stems);
92 for (int i = 0; i < stems.size (); i++)
94 Grob *stem = stems[i];
95 m = max (m, (Stem::beam_multiplicity (stem).length () + 1));
101 /* After pre-processing all directions should be set.
102 Several post-processing routines (stem, slur, script) need stem/beam
104 Currenly, this means that beam has set all stem's directions.
105 [Alternatively, stems could set its own directions, according to
106 their beam, during 'final-pre-processing'.] */
107 MAKE_SCHEME_CALLBACK (Beam, calc_direction, 1);
109 Beam::calc_direction (SCM smob)
111 Grob *me = unsmob_grob (smob);
113 /* Beams with less than 2 two stems don't make much sense, but could happen
118 For a beam that only has one stem, we try to do some disappearance magic:
119 we revert the flag, and move on to The Eternal Engraving Fields. */
121 int count = visible_stem_count (me);
124 extract_grob_set (me, "stems", stems);
125 if (stems.size () == 1)
127 me->warning (_ ("removing beam with less than two stems"));
129 stems[0]->set_object ("beam", SCM_EOL);
132 return SCM_UNSPECIFIED;
134 else if (stems.size () == 0)
137 return SCM_UNSPECIFIED;
141 Direction d = CENTER;
145 d = get_default_dir (me);
146 consider_auto_knees (me);
147 set_stem_directions (me, d);
151 set_stem_shorten (me);
154 return scm_from_int (d);
157 /* We want a maximal number of shared beams, but if there is choice, we
158 * take the one that is closest to the end of the stem. This is for
170 position_with_maximal_common_beams (SCM left_beaming, SCM right_beaming,
174 Slice lslice = int_list_to_slice (scm_cdr (left_beaming));
178 for (int i = lslice[-left_dir];
179 (i - lslice[left_dir]) * left_dir <= 0; i += left_dir)
182 for (SCM s = scm_car (right_beaming); scm_is_pair (s); s = scm_cdr (s))
184 int k = -right_dir * scm_to_int (scm_car (s)) + i;
185 if (scm_c_memq (scm_from_int (k), left_beaming) != SCM_BOOL_F)
189 if (count >= best_count)
200 Beam::connect_beams (Grob *me)
202 extract_grob_set (me, "stems", stems);
205 last_int.set_empty ();
207 // SCM last_beaming = SCM_EOL;
208 SCM last_beaming = scm_cons (SCM_EOL, scm_list_1 (scm_from_int (0)));
209 Direction last_dir = CENTER;
210 for (int i = 0; i < stems.size (); i++)
212 Grob *this_stem = stems[i];
213 SCM this_beaming = this_stem->get_property ("beaming");
215 Direction this_dir = get_grob_direction (this_stem);
216 if (scm_is_pair (last_beaming) && scm_is_pair (this_beaming))
218 int start_point = position_with_maximal_common_beams
219 (last_beaming, this_beaming,
220 last_dir ? last_dir : this_dir,
227 new_slice.set_empty ();
228 SCM s = index_get_cell (this_beaming, d);
229 for (; scm_is_pair (s); s = scm_cdr (s))
232 = start_point - this_dir * scm_to_int (scm_car (s));
234 new_slice.add_point (new_beam_pos);
235 scm_set_car_x (s, scm_from_int (new_beam_pos));
238 while (flip (&d) != LEFT);
240 if (!new_slice.is_empty ())
241 last_int = new_slice;
245 SCM s = scm_cdr (this_beaming);
246 for (; scm_is_pair (s); s = scm_cdr (s))
248 int np = -this_dir * scm_to_int (scm_car (s));
249 scm_set_car_x (s, scm_from_int (np));
250 last_int.add_point (np);
254 if (scm_ilength (scm_cdr (this_beaming)) > 0)
256 last_beaming = this_beaming;
263 I really enjoy spaghetti, but spaghetti should be kept on a plate
264 with a little garlic and olive oil. This is too much.
268 MAKE_SCHEME_CALLBACK (Beam, print, 1);
270 Beam::print (SCM grob)
272 Spanner *me = unsmob_spanner (grob);
274 extract_grob_set (me, "stems", stems);
275 Grob *xcommon = common_refpoint_of_array (stems, me, X_AXIS);
277 xcommon = me->get_bound (LEFT)->common_refpoint (xcommon, X_AXIS);
278 xcommon = me->get_bound (RIGHT)->common_refpoint (xcommon, X_AXIS);
281 if (visible_stem_count (me))
283 // ugh -> use commonx
284 x0 = first_visible_stem (me)->relative_coordinate (xcommon, X_AXIS);
285 dx = last_visible_stem (me)->relative_coordinate (xcommon, X_AXIS) - x0;
289 x0 = stems[0]->relative_coordinate (xcommon, X_AXIS);
290 dx = stems.top ()->relative_coordinate (xcommon, X_AXIS) - x0;
293 SCM posns = me->get_property ("positions");
294 Drul_array<Real> pos;
295 if (!is_number_pair (posns))
297 programming_error ("no beam positions?");
298 pos = Interval (0, 0);
301 pos = ly_scm2realdrul (posns);
303 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
305 Real dy = pos[RIGHT] - pos[LEFT];
306 Real slope = (dy && dx) ? dy / dx : 0;
308 Real thick = get_thickness (me);
309 Real bdy = get_beam_translation (me);
311 SCM last_beaming = SCM_EOL;
312 Real last_xposn = -1;
313 Real last_stem_width = -1;
315 Real gap_length = robust_scm2double (me->get_property ("gap"), 0.0);
318 Real lt = me->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
320 for (int i = 0; i <= stems.size (); i++)
322 Grob *stem = (i < stems.size ()) ? stems[i] : 0;
324 SCM this_beaming = stem ? stem->get_property ("beaming") : SCM_EOL;
325 Real xposn = stem ? stem->relative_coordinate (xcommon, X_AXIS) : 0.0;
326 Real stem_width = stem ? robust_scm2double (stem->get_property ("thickness"), 1.0) * lt : 0;
327 Direction stem_dir = stem ? to_dir (stem->get_property ("direction")) : CENTER;
329 We do the space left of ST, with lfliebertjes pointing to the
330 right from the left stem, and rfliebertjes pointing left from
333 SCM left = (i > 0) ? scm_cdr (last_beaming) : SCM_EOL;
334 SCM right = stem ? scm_car (this_beaming) : SCM_EOL;
336 Array<int> full_beams;
337 Array<int> lfliebertjes;
338 Array<int> rfliebertjes;
341 scm_is_pair (s); s = scm_cdr (s))
343 int b = scm_to_int (scm_car (s));
344 if (scm_c_memq (scm_car (s), right) != SCM_BOOL_F)
347 lfliebertjes.push (b);
350 scm_is_pair (s); s = scm_cdr (s))
352 int b = scm_to_int (scm_car (s));
353 if (scm_c_memq (scm_car (s), left) == SCM_BOOL_F)
354 rfliebertjes.push (b);
357 Drul_array<Real> break_overshoot
358 = robust_scm2drul (me->get_property ("break-overshoot"),
359 Drul_array<Real> (-0.5, 0.0));
361 Real w = (i > 0 && stem)
362 ? (xposn - last_xposn)
363 : break_overshoot[ (i == 0) ? LEFT : RIGHT];
365 Real stem_offset = 0.0;
368 w += last_stem_width / 2;
369 stem_offset = -last_stem_width / 2;
375 Real blot = me->get_layout ()->get_dimension (ly_symbol2scm ("blotdiameter"));
376 Stencil whole = Lookup::beam (slope, w, thick, blot);
380 if (scm_is_number (me->get_property ("gap-count")))
382 gap_count = scm_to_int (me->get_property ("gap-count"));
383 gapped = Lookup::beam (slope, w - 2 * gap_length, thick, blot);
385 full_beams.sort (default_compare);
387 full_beams.reverse ();
391 for (int j = full_beams.size (); j--;)
398 b.translate_axis (gap_length, X_AXIS);
400 b.translate_axis (last_xposn - x0 + stem_offset, X_AXIS);
401 b.translate_axis (slope * (last_xposn - x0) + bdy * full_beams[j], Y_AXIS);
403 the_beam.add_stencil (b);
406 if (lfliebertjes.size () || rfliebertjes.size ())
412 int t = Stem::duration_log (stem);
420 nw_f = break_overshoot[RIGHT] / 2;
422 /* Half beam should be one note-width,
423 but let's make sure two half-beams never touch */
427 rw = min (nw_f, ((xposn - last_xposn) / 2));
430 if (me->get_bound (LEFT)->break_status_dir ())
431 rw = xposn - me->get_bound (LEFT)->extent (xcommon, X_AXIS)[RIGHT]
432 + break_overshoot[LEFT];
438 lw = min (nw_f, ((xposn - last_xposn) / 2));
441 lw = me->get_bound (RIGHT)->relative_coordinate (xcommon, X_AXIS)
443 + break_overshoot[RIGHT];
445 rw += stem_width / 2;
446 lw += last_stem_width / 2;
448 Stencil rhalf = Lookup::beam (slope, rw, thick, blot);
449 Stencil lhalf = Lookup::beam (slope, lw, thick, blot);
450 for (int j = lfliebertjes.size (); j--;)
453 b.translate_axis (last_xposn - x0 - last_stem_width /2,
455 b.translate_axis (slope * (last_xposn - x0)
456 + bdy * lfliebertjes[j],
458 the_beam.add_stencil (b);
460 for (int j = rfliebertjes.size (); j--;)
463 b.translate_axis (xposn - x0 - rw + stem_width / 2, X_AXIS);
464 b.translate_axis (slope * (xposn - x0 - rw)
465 + bdy * rfliebertjes[j], Y_AXIS);
466 the_beam.add_stencil (b);
471 last_stem_width = stem_width;
472 last_beaming = this_beaming;
475 the_beam.translate_axis (x0 - me->relative_coordinate (xcommon, X_AXIS),
477 the_beam.translate_axis (pos[LEFT], Y_AXIS);
480 SCM quant_score = me->get_property ("quant-score");
481 SCM debug = me->get_layout ()->lookup_variable (ly_symbol2scm ("debug-beam-quanting"));
482 if (to_boolean (debug) && scm_is_string (quant_score))
486 This code prints the demerits for each beam. Perhaps this
487 should be switchable for those who want to twiddle with the
491 SCM properties = Font_interface::text_font_alist_chain (me);
493 Direction stem_dir = stems.size () ? to_dir (stems[0]->get_property ("direction")) : UP;
495 Stencil tm = *unsmob_stencil (Text_interface::interpret_markup
496 (me->get_layout ()->self_scm (), properties, quant_score));
497 the_beam.add_at_edge (Y_AXIS, stem_dir, tm, 1.0, 0);
501 return the_beam.smobbed_copy ();
505 Beam::get_default_dir (Grob *me)
507 Drul_array<int> total;
508 total[UP] = total[DOWN] = 0;
509 Drul_array<int> count;
510 count[UP] = count[DOWN] = 0;
512 extract_grob_set (me, "stems", stems);
514 for (int i = 0; i < stems.size (); i++)
517 Direction stem_dir = CENTER;
518 SCM stem_dir_scm = s->get_property_data (ly_symbol2scm ("direction"));
519 if (is_direction (stem_dir_scm))
520 stem_dir = to_dir (stem_dir_scm);
522 stem_dir = Stem::get_default_dir (s);
527 total[stem_dir] += max (int (- stem_dir * Stem::head_positions (s) [-stem_dir]), 0);
531 Direction dir = CENTER;
533 if (Direction d = (Direction) sign (count[UP] - count[DOWN]))
535 else if (Direction d = (Direction) sign (total[UP] / count[UP] - total[DOWN]/count[DOWN]))
537 else if (Direction d = (Direction) sign (total[UP] - total[DOWN]))
540 dir = to_dir (me->get_property ("neutral-direction"));
545 /* Set all stems with non-forced direction to beam direction.
546 Urg: non-forced should become `without/with unforced' direction,
547 once stem gets cleaned-up. */
549 Beam::set_stem_directions (Grob *me, Direction d)
551 extract_grob_set (me, "stems", stems);
553 for (int i = 0; i < stems.size (); i++)
557 SCM forcedir = s->get_property_data (ly_symbol2scm ("direction"));
558 if (!to_dir (forcedir))
559 set_grob_direction (s, d);
564 Only try horizontal beams for knees. No reliable detection of
565 anything else is possible here, since we don't know funky-beaming
566 settings, or X-distances (slopes!) People that want sloped
567 knee-beams, should set the directions manually.
572 this routine should take into account the stemlength scoring
573 of a possible knee/nonknee beam.
576 Beam::consider_auto_knees (Grob *me)
578 SCM scm = me->get_property ("auto-knee-gap");
579 if (!scm_is_number (scm))
586 extract_grob_set (me, "stems", stems);
588 Grob *common = common_refpoint_of_array (stems, me, Y_AXIS);
589 Real staff_space = Staff_symbol_referencer::staff_space (me);
591 Array<Interval> head_extents_array;
592 for (int i = 0; i < stems.size (); i++)
594 Grob *stem = stems[i];
595 if (Stem::is_invisible (stem))
598 Interval head_extents = Stem::head_positions (stem);
599 if (!head_extents.is_empty ())
601 head_extents[LEFT] += -1;
602 head_extents[RIGHT] += 1;
603 head_extents *= staff_space * 0.5;
606 We could subtract beam Y position, but this routine only
607 sets stem directions, a constant shift does not have an
610 head_extents += stem->relative_coordinate (common, Y_AXIS);
612 if (to_dir (stem->get_property_data (ly_symbol2scm ("direction"))))
614 Direction stemdir = to_dir (stem->get_property ("direction"));
615 head_extents[-stemdir] = -stemdir * infinity_f;
618 head_extents_array.push (head_extents);
620 gaps.remove_interval (head_extents);
624 Real max_gap_len = 0.0;
626 for (int i = gaps.allowed_regions_.size () -1; i >= 0; i--)
628 Interval gap = gaps.allowed_regions_[i];
631 the outer gaps are not knees.
633 if (isinf (gap[LEFT]) || isinf (gap[RIGHT]))
636 if (gap.length () >= max_gap_len)
638 max_gap_len = gap.length ();
643 Real beam_translation = get_beam_translation (me);
644 Real beam_thickness = Beam::get_thickness (me);
645 int beam_count = Beam::get_beam_count (me);
646 Real height_of_beams = beam_thickness / 2
647 + (beam_count - 1) * beam_translation;
648 Real threshold = scm_to_double (scm) + height_of_beams;
650 if (max_gap_len > threshold)
653 for (int i = 0; i < stems.size (); i++)
655 Grob *stem = stems[i];
656 if (Stem::is_invisible (stem))
659 Interval head_extents = head_extents_array[j++];
661 Direction d = (head_extents.center () < max_gap.center ())
664 stem->set_property ("direction", scm_from_int (d));
666 head_extents.intersect (max_gap);
667 assert (head_extents.is_empty () || head_extents.length () < 1e-6);
672 /* Set stem's shorten property if unset.
675 take some y-position (chord/beam/nearest?) into account
676 scmify forced-fraction
678 This is done in beam because the shorten has to be uniform over the
682 Beam::set_stem_shorten (Grob *me)
685 shortening looks silly for x staff beams
690 Real forced_fraction = 1.0 * forced_stem_count (me)
691 / visible_stem_count (me);
693 int beam_count = get_beam_count (me);
695 SCM shorten_list = me->get_property ("beamed-stem-shorten");
696 if (shorten_list == SCM_EOL)
699 Real staff_space = Staff_symbol_referencer::staff_space (me);
702 = robust_list_ref (beam_count -1, shorten_list);
703 Real shorten = scm_to_double (shorten_elt) * staff_space;
705 shorten *= forced_fraction;
708 me->set_property ("shorten", scm_from_double (shorten));
711 MAKE_SCHEME_CALLBACK (Beam, calc_positions, 1);
713 Beam::calc_positions (SCM smob)
715 Grob *me = unsmob_grob (smob);
719 (void) me->get_property ("direction");
721 SCM posns = scm_cons (SCM_BOOL_F, SCM_BOOL_F);
722 me->set_property ("positions", posns);
724 SCM callbacks = me->get_property ("position-callbacks");
725 for (SCM i = callbacks; scm_is_pair (i); i = scm_cdr (i))
726 scm_call_1 (scm_car (i), me->self_scm ());
729 TODO: move this in separate calc function.
731 set_stem_lengths (me);
732 return SCM_UNSPECIFIED;
737 set_minimum_dy (Grob *me, Real *dy)
742 If dy is smaller than the smallest quant, we
743 get absurd direction-sign penalties.
746 Real ss = Staff_symbol_referencer::staff_space (me);
747 Real thickness = Beam::get_thickness (me) / ss;
748 Real slt = Staff_symbol_referencer::line_thickness (me) / ss;
749 Real sit = (thickness - slt) / 2;
751 Real hang = 1.0 - (thickness - slt) / 2;
753 *dy = sign (*dy) * max (fabs (*dy),
754 min (min (sit, inter), hang));
759 Compute a first approximation to the beam slope.
761 MAKE_SCHEME_CALLBACK (Beam, least_squares, 1);
763 Beam::least_squares (SCM smob)
765 Grob *me = unsmob_grob (smob);
767 int count = visible_stem_count (me);
772 me->set_property ("positions", ly_interval2scm (pos));
773 return SCM_UNSPECIFIED;
777 extract_grob_set (me, "stems", stems);
778 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
779 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
781 Real my_y = me->relative_coordinate (commony, Y_AXIS);
783 Grob *fvs = first_visible_stem (me);
784 Grob *lvs = last_visible_stem (me);
786 Interval ideal (Stem::get_stem_info (fvs).ideal_y_
787 + fvs->relative_coordinate (commony, Y_AXIS) -my_y,
788 Stem::get_stem_info (lvs).ideal_y_
789 + lvs->relative_coordinate (commony, Y_AXIS) - my_y);
791 Real x0 = first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
792 for (int i = 0; i < stems.size (); i++)
796 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
799 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS) - x0;
807 Interval chord (Stem::chord_start_y (first_visible_stem (me)),
808 Stem::chord_start_y (last_visible_stem (me)));
810 /* Simple beams (2 stems) on middle line should be allowed to be
813 However, if both stems reach middle line,
814 ideal[LEFT] == ideal[RIGHT] and ideal.delta () == 0.
816 For that case, we apply artificial slope */
817 if (!ideal[LEFT] && chord.delta () && count == 2)
820 Direction d = (Direction) (sign (chord.delta ()) * UP);
821 pos[d] = get_thickness (me) / 2;
828 For broken beams this doesn't work well. In this case, the
829 slope esp. of the first part of a broken beam should predict
830 where the second part goes.
832 me->set_property ("least-squares-dy",
833 scm_from_double (pos[RIGHT] - pos[LEFT]));
837 Array<Offset> ideals;
838 for (int i = 0; i < stems.size (); i++)
841 if (Stem::is_invisible (s))
843 ideals.push (Offset (x_posns[i],
844 Stem::get_stem_info (s).ideal_y_
845 + s->relative_coordinate (commony, Y_AXIS)
849 minimise_least_squares (&slope, &y, ideals);
853 set_minimum_dy (me, &dy);
854 me->set_property ("least-squares-dy", scm_from_double (dy));
855 pos = Interval (y, (y + dy));
859 "position" is relative to the staff.
861 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
863 me->set_property ("positions", ly_interval2scm (pos));
865 return SCM_UNSPECIFIED;
869 We can't combine with previous function, since check concave and
870 slope damping comes first.
872 TODO: we should use the concaveness to control the amount of damping
875 MAKE_SCHEME_CALLBACK (Beam, shift_region_to_valid, 1);
877 Beam::shift_region_to_valid (SCM grob)
879 Grob *me = unsmob_grob (grob);
884 extract_grob_set (me, "stems", stems);
885 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
886 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
888 Grob *fvs = first_visible_stem (me);
891 return SCM_UNSPECIFIED;
893 Real x0 = fvs->relative_coordinate (commonx, X_AXIS);
894 for (int i = 0; i < stems.size (); i++)
898 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
902 Grob *lvs = last_visible_stem (me);
904 return SCM_UNSPECIFIED;
906 Real dx = lvs->relative_coordinate (commonx, X_AXIS) - x0;
908 Drul_array<Real> pos = ly_scm2interval (me->get_property ("positions"));
910 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
912 Real dy = pos[RIGHT] - pos[LEFT];
914 Real slope = dx ? (dy / dx) : 0.0;
917 Shift the positions so that we have a chance of finding good
918 quants (i.e. no short stem failures.)
920 Interval feasible_left_point;
921 feasible_left_point.set_full ();
922 for (int i = 0; i < stems.size (); i++)
925 if (Stem::is_invisible (s))
928 Direction d = get_grob_direction (s);
931 = Stem::get_stem_info (s).shortest_y_
932 - slope * x_posns [i];
935 left_y is now relative to the stem S. We want relative to
936 ourselves, so translate:
939 += + s->relative_coordinate (commony, Y_AXIS)
940 - me->relative_coordinate (commony, Y_AXIS);
946 feasible_left_point.intersect (flp);
949 if (feasible_left_point.is_empty ())
950 warning (_ ("no viable initial configuration found: may not find good beam slope"));
951 else if (!feasible_left_point.contains (y))
953 const int REGION_SIZE = 2; // UGH UGH
954 if (isinf (feasible_left_point[DOWN]))
955 y = feasible_left_point[UP] - REGION_SIZE;
956 else if (isinf (feasible_left_point[UP]))
957 y = feasible_left_point[DOWN]+ REGION_SIZE;
959 y = feasible_left_point.center ();
962 pos = Drul_array<Real> (y, (y + dy));
963 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
965 me->set_property ("positions", ly_interval2scm (pos));
966 return SCM_UNSPECIFIED;
969 /* This neat trick is by Werner Lemberg,
970 damped = tanh (slope)
971 corresponds with some tables in [Wanske] CHECKME */
972 MAKE_SCHEME_CALLBACK (Beam, slope_damping, 1);
974 Beam::slope_damping (SCM smob)
976 Grob *me = unsmob_grob (smob);
978 if (visible_stem_count (me) <= 1)
979 return SCM_UNSPECIFIED;
981 SCM s = me->get_property ("damping");
982 Real damping = scm_to_double (s);
986 Drul_array<Real> pos = ly_scm2interval (me->get_property ("positions"));
987 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
989 Real dy = pos[RIGHT] - pos[LEFT];
991 Grob *fvs = first_visible_stem (me);
992 Grob *lvs = last_visible_stem (me);
994 Grob *commonx = fvs->common_refpoint (lvs, X_AXIS);
996 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS)
997 - first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
999 Real slope = dy && dx ? dy / dx : 0;
1001 Real concaveness = robust_scm2double (me->get_property ("concaveness"), 0.0);
1003 slope = 0.6 * tanh (slope) / (damping + concaveness);
1005 Real damped_dy = slope * dx;
1007 set_minimum_dy (me, &damped_dy);
1009 pos[LEFT] += (dy - damped_dy) / 2;
1010 pos[RIGHT] -= (dy - damped_dy) / 2;
1012 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1014 me->set_property ("positions", ly_interval2scm (pos));
1016 return SCM_UNSPECIFIED;
1020 Report slice containing the numbers that are both in (car BEAMING)
1024 where_are_the_whole_beams (SCM beaming)
1028 for (SCM s = scm_car (beaming); scm_is_pair (s); s = scm_cdr (s))
1030 if (scm_c_memq (scm_car (s), scm_cdr (beaming)) != SCM_BOOL_F)
1032 l.add_point (scm_to_int (scm_car (s)));
1038 /* Return the Y position of the stem-end, given the Y-left, Y-right
1039 in POS for stem S. This Y position is relative to S. */
1041 Beam::calc_stem_y (Grob *me, Grob *s, Grob ** common,
1043 Drul_array<Real> pos, bool french)
1045 Real beam_translation = get_beam_translation (me);
1047 Real r = s->relative_coordinate (common[X_AXIS], X_AXIS) - xl;
1048 Real dy = pos[RIGHT] - pos[LEFT];
1050 Real stem_y_beam0 = (dy && dx
1055 Direction my_dir = get_grob_direction (s);
1056 SCM beaming = s->get_property ("beaming");
1058 Real stem_y = stem_y_beam0;
1061 Slice bm = where_are_the_whole_beams (beaming);
1062 if (!bm.is_empty ())
1063 stem_y += beam_translation * bm[-my_dir];
1067 Slice bm = Stem::beam_multiplicity (s);
1068 if (!bm.is_empty ())
1069 stem_y += bm[my_dir] * beam_translation;
1072 Real id = me->relative_coordinate (common[Y_AXIS], Y_AXIS)
1073 - s->relative_coordinate (common[Y_AXIS], Y_AXIS);
1079 Hmm. At this time, beam position and slope are determined. Maybe,
1080 stem directions and length should set to relative to the chord's
1081 position of the beam. */
1083 Beam::set_stem_lengths (Grob *me)
1085 extract_grob_set (me, "stems", stems);
1090 for (int a = 2; a--;)
1091 common[a] = common_refpoint_of_array (stems, me, Axis (a));
1093 Drul_array<Real> pos = ly_scm2realdrul (me->get_property ("positions"));
1094 Real staff_space = Staff_symbol_referencer::staff_space (me);
1095 scale_drul (&pos, staff_space);
1099 if (scm_is_number (me->get_property ("gap-count"))
1100 &&scm_to_int (me->get_property ("gap-count")))
1103 thick = get_thickness (me);
1106 // ugh -> use commonx
1107 Grob *fvs = first_visible_stem (me);
1108 Grob *lvs = last_visible_stem (me);
1110 Real xl = fvs ? fvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1111 Real xr = lvs ? lvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1113 for (int i = 0; i < stems.size (); i++)
1116 if (Stem::is_invisible (s))
1119 bool french = to_boolean (s->get_property ("french-beaming"));
1120 Real stem_y = calc_stem_y (me, s, common,
1122 pos, french && s != lvs && s!= fvs);
1125 Make the stems go up to the end of the beam. This doesn't matter
1126 for normal beams, but for tremolo beams it looks silly otherwise.
1129 stem_y += thick * 0.5 * get_grob_direction (s);
1131 Stem::set_stemend (s, 2 * stem_y / staff_space);
1136 Beam::set_beaming (Grob *me, Beaming_info_list const *beaming)
1138 extract_grob_set (me, "stems", stems);
1141 for (int i = 0; i < stems.size (); i++)
1144 Don't overwrite user settings.
1149 Grob *stem = stems[i];
1150 SCM beaming_prop = stem->get_property ("beaming");
1151 if (beaming_prop == SCM_EOL
1152 || index_get_cell (beaming_prop, d) == SCM_EOL)
1154 int b = beaming->infos_.elem (i).beams_i_drul_[d];
1156 && i < stems.size () -1
1157 && Stem::is_invisible (stem))
1158 b = min (b, beaming->infos_.elem (i).beams_i_drul_[-d]);
1160 Stem::set_beaming (stem, b, d);
1163 while (flip (&d) != LEFT);
1168 Beam::forced_stem_count (Grob *me)
1170 extract_grob_set (me, "stems", stems);
1173 for (int i = 0; i < stems.size (); i++)
1177 if (Stem::is_invisible (s))
1180 /* I can imagine counting those boundaries as a half forced stem,
1181 but let's count them full for now. */
1182 if (abs (Stem::chord_start_y (s)) > 0.1
1183 && (get_grob_direction (s) != Stem::get_default_dir (s)))
1190 Beam::visible_stem_count (Grob *me)
1192 extract_grob_set (me, "stems", stems);
1194 for (int i = stems.size (); i--;)
1196 if (!Stem::is_invisible (stems[i]))
1203 Beam::first_visible_stem (Grob *me)
1205 extract_grob_set (me, "stems", stems);
1207 for (int i = 0; i < stems.size (); i++)
1209 if (!Stem::is_invisible (stems[i]))
1216 Beam::last_visible_stem (Grob *me)
1218 extract_grob_set (me, "stems", stems);
1220 for (int i = stems.size (); i--;)
1222 if (!Stem::is_invisible (stems[i]))
1231 handle rest under beam (do_post: beams are calculated now)
1232 what about combination of collisions and rest under beam.
1236 rest -> stem -> beam -> interpolate_y_position ()
1238 MAKE_SCHEME_CALLBACK (Beam, rest_collision_callback, 2);
1240 Beam::rest_collision_callback (SCM element_smob, SCM axis)
1242 Grob *rest = unsmob_grob (element_smob);
1245 if (scm_is_number (rest->get_property ("staff-position")))
1246 return scm_from_int (0);
1248 assert (scm_to_int (axis) == Y_AXIS);
1250 Grob *st = unsmob_grob (rest->get_object ("stem"));
1253 return scm_from_double (0.0);
1254 Grob *beam = unsmob_grob (stem->get_object ("beam"));
1256 || !Beam::has_interface (beam)
1257 || !Beam::visible_stem_count (beam))
1258 return scm_from_double (0.0);
1260 Drul_array<Real> pos (0, 0);
1261 SCM s = beam->get_property ("positions");
1262 if (scm_is_pair (s) && scm_is_number (scm_car (s)))
1263 pos = ly_scm2interval (s);
1265 programming_error ("positions property should always be pair of numbers.");
1267 Real staff_space = Staff_symbol_referencer::staff_space (rest);
1269 scale_drul (&pos, staff_space);
1271 Real dy = pos[RIGHT] - pos[LEFT];
1273 // ugh -> use commonx
1274 Real x0 = first_visible_stem (beam)->relative_coordinate (0, X_AXIS);
1275 Real dx = last_visible_stem (beam)->relative_coordinate (0, X_AXIS) - x0;
1276 Real slope = dy && dx ? dy / dx : 0;
1278 Direction d = get_grob_direction (stem);
1279 Real stem_y = pos[LEFT] + (stem->relative_coordinate (0, X_AXIS) - x0) * slope;
1281 Real beam_translation = get_beam_translation (beam);
1282 Real beam_thickness = Beam::get_thickness (beam);
1285 TODO: this is not strictly correct for 16th knee beams.
1288 = Stem::beam_multiplicity (stem).length () + 1;
1290 Real height_of_my_beams = beam_thickness / 2
1291 + (beam_count - 1) * beam_translation;
1292 Real beam_y = stem_y - d * height_of_my_beams;
1294 Grob *common_y = rest->common_refpoint (beam, Y_AXIS);
1296 Real rest_dim = rest->extent (common_y, Y_AXIS)[d];
1297 Real minimum_distance
1298 = + staff_space * (robust_scm2double (stem->get_property ("stemlet-length"), 0.0)
1299 + robust_scm2double (rest->get_property ("minimum-distance"), 0.0));
1301 Real shift = d * min (((beam_y - d * minimum_distance) - rest_dim) * d, 0.0);
1303 shift /= staff_space;
1304 Real rad = Staff_symbol_referencer::line_count (rest) * staff_space / 2;
1306 /* Always move discretely by half spaces */
1307 shift = ceil (fabs (shift * 2.0)) / 2.0 * sign (shift);
1309 /* Inside staff, move by whole spaces*/
1310 if ((rest->extent (common_y, Y_AXIS)[d] + staff_space * shift) * d
1312 || (rest->extent (common_y, Y_AXIS)[-d] + staff_space * shift) * -d
1314 shift = ceil (fabs (shift)) * sign (shift);
1316 return scm_from_double (staff_space * shift);
1320 Beam::is_knee (Grob *me)
1322 SCM k = me->get_property ("knee");
1323 if (scm_is_bool (k))
1324 return ly_scm2bool (k);
1328 extract_grob_set (me, "stems", stems);
1329 for (int i = stems.size (); i--;)
1331 Direction dir = get_grob_direction (stems[i]);
1340 me->set_property ("knee", ly_bool2scm (knee));
1346 Beam::get_direction_beam_count (Grob *me, Direction d)
1348 extract_grob_set (me, "stems", stems);
1351 for (int i = stems.size (); i--;)
1354 Should we take invisible stems into account?
1356 if (get_grob_direction (stems[i]) == d)
1357 bc = max (bc, (Stem::beam_multiplicity (stems[i]).length () + 1));
1363 ADD_INTERFACE (Beam,
1367 "The @code{thickness} property is the weight of beams, "
1368 "measured in staffspace. The @code{direction} property is not user-serviceable. Use "
1369 "the @code{direction} property of @code{Stem} instead. "
1375 "beamed-stem-shorten "
1388 "neutral-direction "
1389 "position-callbacks "