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