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