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