]> git.donarmstrong.com Git - lilypond.git/blob - lily/tab-note-heads-engraver.cc
use rational numbers for alteration field of Pitch.
[lilypond.git] / lily / tab-note-heads-engraver.cc
1 /*
2   tab-note-heads-engraver.cc -- part of GNU LilyPond
3
4   (c) 2002--2006 Han-Wen Nienhuys, Jean-Baptiste Lamy <jiba@tuxfamily.org>,
5 */
6
7 #include <cctype>
8 #include <cstdio>
9
10 #include "engraver.hh"
11
12 using namespace std;
13
14 #include "duration.hh"
15 #include "item.hh"
16 #include "output-def.hh"
17 #include "pitch.hh"
18 #include "rhythmic-head.hh"
19 #include "stream-event.hh"
20 #include "warn.hh"
21 #include "context.hh"
22
23 #include "translator.icc"
24
25 /**
26    make (guitar-like) tablature note
27 */
28 class Tab_note_heads_engraver : public Engraver
29 {
30   vector<Item*> notes_;
31
32   vector<Stream_event*> note_events_;
33   vector<Stream_event*> tabstring_events_;
34 public:
35   TRANSLATOR_DECLARATIONS (Tab_note_heads_engraver);
36
37 protected:
38   DECLARE_TRANSLATOR_LISTENER (note);
39   DECLARE_TRANSLATOR_LISTENER (string_number);
40   void process_music ();
41
42   void stop_translation_timestep ();
43 };
44
45 Tab_note_heads_engraver::Tab_note_heads_engraver ()
46 {
47 }
48
49 IMPLEMENT_TRANSLATOR_LISTENER (Tab_note_heads_engraver, note);
50 void
51 Tab_note_heads_engraver::listen_note (Stream_event *ev)
52 {
53   note_events_.push_back (ev);
54 }
55
56 IMPLEMENT_TRANSLATOR_LISTENER (Tab_note_heads_engraver, string_number);
57 void
58 Tab_note_heads_engraver::listen_string_number (Stream_event *ev)
59 {
60   tabstring_events_.push_back (ev);
61 }
62
63 void
64 Tab_note_heads_engraver::process_music ()
65 {
66   vsize j = 0;
67   for (vsize i = 0; i < note_events_.size (); i++)
68     {
69       SCM string_tunings = get_property ("stringTunings");
70       int number_of_strings = scm_ilength (string_tunings);
71       bool high_string_one = to_boolean (get_property ("highStringOne"));
72
73       Stream_event *event = note_events_[i];
74       Item *note = make_item ("TabNoteHead", event->self_scm ());
75
76       Stream_event *tabstring_event = 0;
77
78       for (SCM s = event->get_property ("articulations");
79            !tabstring_event && scm_is_pair (s); s = scm_cdr (s))
80         {
81           Stream_event *art = unsmob_stream_event (scm_car (s));
82
83           if (art->in_event_class ("string-number-event"))
84             tabstring_event = art;
85         }
86
87       if (!tabstring_event && j < tabstring_events_.size ())
88         {
89           tabstring_event = tabstring_events_[j];
90           if (j + 1 < tabstring_events_.size ())
91             j++;
92         }
93
94       int tab_string;
95       bool string_found;
96       if (tabstring_event)
97         {
98           tab_string = scm_to_int (tabstring_event->get_property ("string-number"));
99           string_found = true;
100         }
101       else
102         {
103           tab_string = high_string_one ? 1 : number_of_strings;
104           string_found = false;
105         }
106
107       Duration dur = *unsmob_duration (event->get_property ("duration"));
108
109       SCM scm_pitch = event->get_property ("pitch");
110       SCM proc = get_property ("tablatureFormat");
111       SCM min_fret_scm = get_property ("minimumFret");
112       int min_fret = scm_is_number (min_fret_scm) ? scm_to_int (min_fret_scm) : 0;
113
114       while (!string_found)
115         {
116           int fret = unsmob_pitch (scm_pitch)->rounded_semitone_pitch ()
117             - scm_to_int (scm_list_ref (string_tunings, scm_from_int (tab_string - 1)));
118           if (fret < min_fret)
119             tab_string += high_string_one ? 1 : -1;
120           else
121             string_found = true;
122         }
123
124       SCM text = scm_call_3 (proc, scm_from_int (tab_string),
125                              context ()->self_scm (),
126                              event->self_scm ());
127       note->set_property ("text", text);
128
129
130       int pos = 2 * tab_string - number_of_strings - 1; // No tab-note between the string !!!
131       if (to_boolean (get_property ("stringOneTopmost")))
132         pos = -pos;
133
134       note->set_property ("staff-position", scm_from_int (pos));
135
136       
137       notes_.push_back (note);
138     }
139 }
140
141 void
142 Tab_note_heads_engraver::stop_translation_timestep ()
143 {
144   notes_.clear ();
145   note_events_.clear ();
146   tabstring_events_.clear ();
147 }
148
149 ADD_TRANSLATOR (Tab_note_heads_engraver,
150                 /* doc */ "Generate one or more tablature noteheads from event of type NoteEvent.",
151                 /* create */
152                 "TabNoteHead "
153                 ,
154
155                 /* read */
156                 "middleCPosition "
157                 "stringTunings "
158                 "minimumFret "
159                 "tablatureFormat "
160                 "highStringOne "
161                 "stringOneTopmost ",
162
163                 /* write */ "");
164