]> git.donarmstrong.com Git - lilypond.git/blob - lily/tab-note-heads-engraver.cc
(DECLARE_EVENT_SWALLOWER): ENTER_DESCRIPTION -> ADD_TRANSLATOR
[lilypond.git] / lily / tab-note-heads-engraver.cc
1 /*
2   head-grav.cc -- part of GNU LilyPond
3
4   (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
5 */
6
7 #include <cctype>
8 #include <cstdio>
9
10 #include "rhythmic-head.hh"
11 #include "output-def.hh"
12 #include "event.hh"
13 #include "dots.hh"
14 #include "dot-column.hh"
15 #include "staff-symbol-referencer.hh"
16 #include "item.hh"
17 #include "score-engraver.hh"
18 #include "warn.hh"
19
20 /**
21    make (guitar-like) tablature note
22 */
23 class Tab_note_heads_engraver : public Engraver
24 {
25   Link_array<Item> notes_;
26   
27   Link_array<Item> dots_;
28   Link_array<Music> note_events_;
29   Link_array<Music> tabstring_events_;
30 public:
31   TRANSLATOR_DECLARATIONS (Tab_note_heads_engraver);
32
33 protected:
34   virtual bool try_music (Music *event) ;
35   virtual void process_music ();
36
37   virtual void stop_translation_timestep ();
38 };
39
40
41 Tab_note_heads_engraver::Tab_note_heads_engraver ()
42 {
43 }
44
45 bool
46 Tab_note_heads_engraver::try_music (Music *m) 
47 {
48   if (m->is_mus_type ("note-event"))
49     {
50       note_events_.push (m);
51       return true;
52     }
53   else if (m->is_mus_type ("string-number-event"))
54     {
55       tabstring_events_.push (m);
56       return true;
57     }
58   else if (m->is_mus_type ("busy-playing-event"))
59     {
60       return note_events_.size ();
61     }
62   
63   return false;
64 }
65
66
67 void
68 Tab_note_heads_engraver::process_music ()
69 {
70   int j = 0; 
71   for (int i = 0; i < note_events_.size (); i++)
72     {
73       SCM stringTunings = get_property ("stringTunings");
74       int number_of_strings = ((int) ly_length (stringTunings));
75       bool high_string_one = to_boolean (get_property ("highStringOne"));
76
77       
78       Music * event = note_events_[i];
79       Item * note  = make_item ("TabNoteHead", event->self_scm ());
80
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       
132       SCM scm_pitch = event->get_property ("pitch");
133       SCM proc      = get_property ("tablatureFormat");
134       SCM min_fret_scm = get_property ("minimumFret");
135       int min_fret = scm_is_number (min_fret_scm) ? scm_to_int (min_fret_scm) : 0;
136
137       while (!string_found)
138         {
139           int fret = unsmob_pitch (scm_pitch)->semitone_pitch ()
140             - scm_to_int (scm_list_ref (stringTunings,scm_int2num (tab_string-1)));
141           if (fret<min_fret)
142             tab_string += high_string_one ? 1 : -1;
143           else
144             string_found = true;
145         }
146
147       SCM text = scm_call_3 (proc, scm_int2num (tab_string), stringTunings, scm_pitch);
148
149       int pos = 2 * tab_string - number_of_strings - 1; // No tab-note between the string !!!
150       if (to_boolean (get_property ("stringOneTopmost")))
151         pos = -pos;
152       
153       note->set_property ("text", text);      
154       
155       note->set_property ("staff-position", scm_int2num (pos));
156       notes_.push (note);
157     }
158 }
159
160 void
161 Tab_note_heads_engraver::stop_translation_timestep ()
162 {
163   notes_.clear ();
164   dots_.clear ();
165   note_events_.clear ();
166   tabstring_events_.clear ();
167 }
168
169
170 ADD_TRANSLATOR (Tab_note_heads_engraver,
171 /* descr */       "Generate one or more tablature noteheads from Music of type NoteEvent.",
172 /* creats*/       "TabNoteHead Dots",
173 /* accepts */     "note-event string-number-event busy-playing-event",
174 /* acks  */      "",
175 /* reads */       "middleCPosition stringTunings minimumFret tablatureFormat highStringOne stringOneTopmost",
176 /* write */       "");
177