]> git.donarmstrong.com Git - lilypond.git/blob - lily/tab-note-heads-engraver.cc
8871ba4b2dc9d16b513ae598e93fe23d30686df6
[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
12 #include "engraver.hh"
13
14 using namespace std;
15
16 #include "context.hh"
17 #include "duration.hh"
18 #include "item.hh"
19 #include "output-def.hh"
20 #include "pitch.hh"
21 #include "rhythmic-head.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<Stream_event*> note_events_;
35   vector<Stream_event*> tabstring_events_;
36 public:
37   TRANSLATOR_DECLARATIONS (Tab_note_heads_engraver);
38
39 protected:
40   DECLARE_TRANSLATOR_LISTENER (note);
41   DECLARE_TRANSLATOR_LISTENER (string_number);
42   void process_music ();
43
44   void stop_translation_timestep ();
45 };
46
47 Tab_note_heads_engraver::Tab_note_heads_engraver ()
48 {
49 }
50
51 IMPLEMENT_TRANSLATOR_LISTENER (Tab_note_heads_engraver, note);
52 void
53 Tab_note_heads_engraver::listen_note (Stream_event *ev)
54 {
55   note_events_.push_back (ev);
56 }
57
58 IMPLEMENT_TRANSLATOR_LISTENER (Tab_note_heads_engraver, string_number);
59 void
60 Tab_note_heads_engraver::listen_string_number (Stream_event *ev)
61 {
62   tabstring_events_.push_back (ev);
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 string_tunings = get_property ("stringTunings");
72       int string_count = scm_ilength (string_tunings);
73       bool high_string_one = to_boolean (get_property ("highStringOne"));
74
75       Stream_event *event = note_events_[i];
76
77       Stream_event *tabstring_event = 0;
78
79       for (SCM s = event->get_property ("articulations");
80            !tabstring_event && scm_is_pair (s); s = scm_cdr (s))
81         {
82           Stream_event *art = unsmob_stream_event (scm_car (s));
83
84           if (art->in_event_class ("string-number-event"))
85             tabstring_event = art;
86         }
87
88       if (!tabstring_event && j < tabstring_events_.size ())
89         {
90           tabstring_event = tabstring_events_[j];
91           if (j + 1 < tabstring_events_.size ())
92             j++;
93         }
94
95       int string_number = 0;
96       if (tabstring_event)
97         string_number = scm_to_int (tabstring_event->get_property ("string-number"));
98
99       if (!string_number)
100         {
101           SCM scm_pitch = event->get_property ("pitch");
102           int min_fret = robust_scm2int (get_property ("minimumFret"), 0);
103           int start = (high_string_one) ? 1 : string_count;
104           int end = (high_string_one) ? string_count : 1;
105
106           int i = start;
107           do
108             {
109               int fret = unsmob_pitch (scm_pitch)->semitone_pitch ()
110                 - scm_to_int (robust_list_ref (i - 1, string_tunings));
111           
112               if (fret >= min_fret)
113                 {
114                   string_number = i;
115                   break;
116                 }
117               i += high_string_one ? 1 : -1;
118             }
119           while (i != end);
120         }
121       
122       if (string_number)
123         {
124           SCM proc = get_property ("tablatureFormat");
125           SCM text = scm_call_3 (proc, scm_from_int (string_number),
126                                  context ()->self_scm (),
127                                  event->self_scm ());
128           Item *note = make_item ("TabNoteHead", event->self_scm ());
129           note->set_property ("text", text);
130
131           int pos = 2 * string_number - string_count - 1; // No tab-note between the string !!!
132           if (to_boolean (get_property ("stringOneTopmost")))
133             pos = - pos;
134
135           note->set_property ("staff-position", scm_from_int (pos));
136       
137           notes_.push_back (note);
138         }
139       else
140         event->origin ()->warning ("could not calculate a string number.");
141     }
142 }
143 void
144 Tab_note_heads_engraver::stop_translation_timestep ()
145 {
146   notes_.clear ();
147   note_events_.clear ();
148   tabstring_events_.clear ();
149 }
150
151 ADD_TRANSLATOR (Tab_note_heads_engraver,
152                 /* doc */ "Generate one or more tablature noteheads from event of type NoteEvent.",
153                 /* create */
154                 "TabNoteHead "
155                 ,
156
157                 /* read */
158                 "middleCPosition "
159                 "stringTunings "
160                 "minimumFret "
161                 "tablatureFormat "
162                 "highStringOne "
163                 "stringOneTopmost ",
164
165                 /* write */ "");
166