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