]> git.donarmstrong.com Git - lilypond.git/blob - lily/tab-note-heads-engraver.cc
new file, move from
[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 #include <ctype.h>
7 #include <stdio.h>
8
9 #include "rhythmic-head.hh"
10 #include "output-def.hh"
11 #include "event.hh"
12 #include "dots.hh"
13 #include "dot-column.hh"
14 #include "staff-symbol-referencer.hh"
15 #include "item.hh"
16 #include "score-engraver.hh"
17 #include "warn.hh"
18
19 /**
20    make (guitar-like) tablature note
21 */
22 class Tab_note_heads_engraver : public Engraver
23 {
24   Link_array<Item> notes_;
25   
26   Link_array<Item> dots_;
27   Link_array<Music> note_events_;
28   Link_array<Music> tabstring_events_;
29 public:
30   TRANSLATOR_DECLARATIONS (Tab_note_heads_engraver);
31
32 protected:
33   virtual bool try_music (Music *event) ;
34   virtual void process_music ();
35
36   virtual void stop_translation_timestep ();
37 };
38
39
40 Tab_note_heads_engraver::Tab_note_heads_engraver ()
41 {
42 }
43
44 bool
45 Tab_note_heads_engraver::try_music (Music *m) 
46 {
47   if (m->is_mus_type ("note-event"))
48     {
49       note_events_.push (m);
50       return true;
51     }
52   else if (m->is_mus_type ("string-number-event"))
53     {
54       tabstring_events_.push (m);
55       return true;
56     }
57   else if (m->is_mus_type ("busy-playing-event"))
58     {
59       return note_events_.size ();
60     }
61   
62   return false;
63 }
64
65
66 void
67 Tab_note_heads_engraver::process_music ()
68 {
69   int j = 0; 
70   for (int i=0; i < note_events_.size (); i++)
71     {
72       SCM stringTunings = get_property ("stringTunings");
73       int number_of_strings = ((int) ly_length (stringTunings));
74       bool high_string_one = to_boolean (get_property ("highStringOne"));
75
76       
77       Music * event = note_events_[i];
78       Item * note  = make_item ("TabNoteHead", event->self_scm ());
79
80       
81       Music * tabstring_event=0;
82
83       for (SCM s =event->get_property ("articulations");
84            !tabstring_event && ly_c_pair_p (s); s = ly_cdr (s))
85         {
86           Music * art = unsmob_music (ly_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 = ly_scm2int (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_int2num (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               != ly_scm2int (d->get_property ("dot-count")))
123             d->set_property ("dot-count", scm_int2num (dur.dot_count ()));
124
125           d->set_parent (note, Y_AXIS);
126           
127           dots_.push (d);
128         }
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 = ly_c_number_p (min_fret_scm) ? ly_scm2int (min_fret_scm) : 0;
135
136       while (!string_found)
137         {
138           int fret = unsmob_pitch (scm_pitch)->semitone_pitch ()
139             - ly_scm2int (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
169 ENTER_DESCRIPTION (Tab_note_heads_engraver,
170 /* descr */       "Generate one or more tablature noteheads from Music of type NoteEvent.",
171 /* creats*/       "TabNoteHead Dots",
172 /* accepts */     "note-event string-number-event busy-playing-event",
173 /* acks  */      "",
174 /* reads */       "middleCPosition stringTunings minimumFret tablatureFormat highStringOne stringOneTopmost",
175 /* write */       "");
176