]> git.donarmstrong.com Git - lilypond.git/blob - lily/tab-note-heads-engraver.cc
* lily/*-performer.cc: Converted try_music to listen_*
[lilypond.git] / lily / tab-note-heads-engraver.cc
1 /*
2   tab-note-heads-engraver.cc -- part of GNU LilyPond
3
4   based on note-heads-engraver.cc, by Jean-Baptiste Lamy <jiba@tuxfamily.org>,
5
6   (c) 2002--2006
7 */
8
9 #include <cctype>
10 #include <cstdio>
11 using namespace std;
12
13 #include "rhythmic-head.hh"
14 #include "output-def.hh"
15 #include "music.hh"
16 #include "dots.hh"
17 #include "dot-column.hh"
18 #include "staff-symbol-referencer.hh"
19 #include "item.hh"
20 #include "score-engraver.hh"
21 #include "warn.hh"
22 #include "duration.hh"
23
24 /**
25    make (guitar-like) tablature note
26 */
27 class Tab_note_heads_engraver : public Engraver
28 {
29   vector<Item*> notes_;
30
31   vector<Item*> dots_;
32   vector<Music*> note_events_;
33   vector<Music*> tabstring_events_;
34 public:
35   TRANSLATOR_DECLARATIONS (Tab_note_heads_engraver);
36
37 protected:
38   virtual bool try_music (Music *event);
39   void process_music ();
40
41   void stop_translation_timestep ();
42 };
43
44 Tab_note_heads_engraver::Tab_note_heads_engraver ()
45 {
46 }
47
48 bool
49 Tab_note_heads_engraver::try_music (Music *m)
50 {
51   if (m->is_mus_type ("note-event"))
52     {
53       note_events_.push_back (m);
54       return true;
55     }
56   else if (m->is_mus_type ("string-number-event"))
57     {
58       tabstring_events_.push_back (m);
59       return true;
60     }
61
62   return false;
63 }
64
65 void
66 Tab_note_heads_engraver::process_music ()
67 {
68   vsize j = 0;
69   for (vsize i = 0; i < note_events_.size (); i++)
70     {
71       SCM stringTunings = get_property ("stringTunings");
72       int number_of_strings = ((int) ly_length (stringTunings));
73       bool high_string_one = to_boolean (get_property ("highStringOne"));
74
75       Music *event = note_events_[i];
76       Item *note = make_item ("TabNoteHead", event->self_scm ());
77
78       Music *tabstring_event = 0;
79
80       for (SCM s = event->get_property ("articulations");
81            !tabstring_event && scm_is_pair (s); s = scm_cdr (s))
82         {
83           Music *art = unsmob_music (scm_car (s));
84
85           if (art->is_mus_type ("string-number-event"))
86             tabstring_event = art;
87         }
88
89       if (!tabstring_event && j < tabstring_events_.size ())
90         {
91           tabstring_event = tabstring_events_[j];
92           if (j + 1 < tabstring_events_.size ())
93             j++;
94         }
95
96       int tab_string;
97       bool string_found;
98       if (tabstring_event)
99         {
100           tab_string = scm_to_int (tabstring_event->get_property ("string-number"));
101           string_found = true;
102         }
103       else
104         {
105           tab_string = high_string_one ? 1 : number_of_strings;
106           string_found = false;
107         }
108
109       Duration dur = *unsmob_duration (event->get_property ("duration"));
110       note->set_property ("duration-log",
111                           scm_from_int (dur.duration_log ()));
112
113       if (dur.dot_count ())
114         {
115           Item *d = make_item ("Dots", event->self_scm ());
116           Rhythmic_head::set_dots (note, d);
117
118           if (dur.dot_count ()
119               != scm_to_int (d->get_property ("dot-count")))
120             d->set_property ("dot-count", scm_from_int (dur.dot_count ()));
121
122           d->set_parent (note, Y_AXIS);
123
124           dots_.push_back (d);
125         }
126
127       SCM scm_pitch = event->get_property ("pitch");
128       SCM proc = get_property ("tablatureFormat");
129       SCM min_fret_scm = get_property ("minimumFret");
130       int min_fret = scm_is_number (min_fret_scm) ? scm_to_int (min_fret_scm) : 0;
131
132       while (!string_found)
133         {
134           int fret = unsmob_pitch (scm_pitch)->semitone_pitch ()
135             - scm_to_int (scm_list_ref (stringTunings, scm_from_int (tab_string - 1)));
136           if (fret < min_fret)
137             tab_string += high_string_one ? 1 : -1;
138           else
139             string_found = true;
140         }
141
142       SCM text = scm_call_3 (proc, scm_from_int (tab_string), stringTunings, scm_pitch);
143
144       int pos = 2 * tab_string - number_of_strings - 1; // No tab-note between the string !!!
145       if (to_boolean (get_property ("stringOneTopmost")))
146         pos = -pos;
147
148       note->set_property ("text", text);
149
150       note->set_property ("staff-position", scm_from_int (pos));
151       notes_.push_back (note);
152     }
153 }
154
155 void
156 Tab_note_heads_engraver::stop_translation_timestep ()
157 {
158   notes_.clear ();
159   dots_.clear ();
160   note_events_.clear ();
161   tabstring_events_.clear ();
162 }
163
164 #include "translator.icc"
165
166 ADD_TRANSLATOR (Tab_note_heads_engraver,
167                 /* doc */ "Generate one or more tablature noteheads from Music of type NoteEvent.",
168                 /* create */ "TabNoteHead Dots",
169                 /* accept */ "note-event string-number-event",
170                 /* read */ "middleCPosition stringTunings minimumFret tablatureFormat highStringOne stringOneTopmost",
171                 /* write */ "");
172