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