From 03c03bdcde77a7884f1aa6eb70b360ac71b647d1 Mon Sep 17 00:00:00 2001 From: Han-Wen Nienhuys Date: Sun, 20 May 2007 21:58:29 -0300 Subject: [PATCH] Fix #349. Refactor automatic tab strings: don't crash on out-of-range pitches. Conflicts: lily/tab-note-heads-engraver.cc --- lily/tab-note-heads-engraver.cc | 83 +++++++++++++++++---------------- 1 file changed, 44 insertions(+), 39 deletions(-) diff --git a/lily/tab-note-heads-engraver.cc b/lily/tab-note-heads-engraver.cc index 7870c0218a..8871ba4b2d 100644 --- a/lily/tab-note-heads-engraver.cc +++ b/lily/tab-note-heads-engraver.cc @@ -13,6 +13,7 @@ using namespace std; +#include "context.hh" #include "duration.hh" #include "item.hh" #include "output-def.hh" @@ -67,12 +68,11 @@ Tab_note_heads_engraver::process_music () vsize j = 0; for (vsize i = 0; i < note_events_.size (); i++) { - SCM stringTunings = get_property ("stringTunings"); - int number_of_strings = scm_ilength (stringTunings); + SCM string_tunings = get_property ("stringTunings"); + int string_count = scm_ilength (string_tunings); bool high_string_one = to_boolean (get_property ("highStringOne")); Stream_event *event = note_events_[i]; - Item *note = make_item ("TabNoteHead", event->self_scm ()); Stream_event *tabstring_event = 0; @@ -92,49 +92,54 @@ Tab_note_heads_engraver::process_music () j++; } - int tab_string; - bool string_found; + int string_number = 0; if (tabstring_event) + string_number = scm_to_int (tabstring_event->get_property ("string-number")); + + if (!string_number) { - tab_string = scm_to_int (tabstring_event->get_property ("string-number")); - string_found = true; - } - else - { - tab_string = high_string_one ? 1 : number_of_strings; - string_found = false; + SCM scm_pitch = event->get_property ("pitch"); + int min_fret = robust_scm2int (get_property ("minimumFret"), 0); + int start = (high_string_one) ? 1 : string_count; + int end = (high_string_one) ? string_count : 1; + + int i = start; + do + { + int fret = unsmob_pitch (scm_pitch)->semitone_pitch () + - scm_to_int (robust_list_ref (i - 1, string_tunings)); + + if (fret >= min_fret) + { + string_number = i; + break; + } + i += high_string_one ? 1 : -1; + } + while (i != end); } - - Duration dur = *unsmob_duration (event->get_property ("duration")); - - SCM scm_pitch = event->get_property ("pitch"); - SCM proc = get_property ("tablatureFormat"); - SCM min_fret_scm = get_property ("minimumFret"); - int min_fret = scm_is_number (min_fret_scm) ? scm_to_int (min_fret_scm) : 0; - - while (!string_found) + + if (string_number) { - int fret = unsmob_pitch (scm_pitch)->semitone_pitch () - - scm_to_int (scm_list_ref (stringTunings, scm_from_int (tab_string - 1))); - if (fret < min_fret) - tab_string += high_string_one ? 1 : -1; - else - string_found = true; + SCM proc = get_property ("tablatureFormat"); + SCM text = scm_call_3 (proc, scm_from_int (string_number), + context ()->self_scm (), + event->self_scm ()); + Item *note = make_item ("TabNoteHead", event->self_scm ()); + note->set_property ("text", text); + + int pos = 2 * string_number - string_count - 1; // No tab-note between the string !!! + if (to_boolean (get_property ("stringOneTopmost"))) + pos = - pos; + + note->set_property ("staff-position", scm_from_int (pos)); + + notes_.push_back (note); } - - SCM text = scm_call_3 (proc, scm_from_int (tab_string), stringTunings, scm_pitch); - - int pos = 2 * tab_string - number_of_strings - 1; // No tab-note between the string !!! - if (to_boolean (get_property ("stringOneTopmost"))) - pos = -pos; - - note->set_property ("text", text); - - note->set_property ("staff-position", scm_from_int (pos)); - notes_.push_back (note); + else + event->origin ()->warning ("could not calculate a string number."); } } - void Tab_note_heads_engraver::stop_translation_timestep () { -- 2.39.5