]> git.donarmstrong.com Git - lilypond.git/blob - lily/tab-note-heads-engraver.cc
Merge branch 'lilypond/translation' of ssh://git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / lily / tab-note-heads-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2002--2009 Han-Wen Nienhuys, Jean-Baptiste Lamy <jiba@tuxfamily.org>,
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <cctype>
21 #include <cstdio>
22
23 #include "engraver.hh"
24
25 using namespace std;
26
27 #include "duration.hh"
28 #include "item.hh"
29 #include "output-def.hh"
30 #include "pitch.hh"
31 #include "rhythmic-head.hh"
32 #include "stream-event.hh"
33 #include "warn.hh"
34 #include "context.hh"
35
36 #include "translator.icc"
37
38 /**
39    make (guitar-like) tablature note
40 */
41 class Tab_note_heads_engraver : public Engraver
42 {
43   vector<Item*> notes_;
44
45   vector<Stream_event*> note_events_;
46   vector<Stream_event*> tabstring_events_;
47 public:
48   TRANSLATOR_DECLARATIONS (Tab_note_heads_engraver);
49
50 protected:
51   DECLARE_TRANSLATOR_LISTENER (note);
52   DECLARE_TRANSLATOR_LISTENER (string_number);
53   void process_music ();
54
55   void stop_translation_timestep ();
56 };
57
58 Tab_note_heads_engraver::Tab_note_heads_engraver ()
59 {
60 }
61
62 IMPLEMENT_TRANSLATOR_LISTENER (Tab_note_heads_engraver, note);
63 void
64 Tab_note_heads_engraver::listen_note (Stream_event *ev)
65 {
66   note_events_.push_back (ev);
67 }
68
69 IMPLEMENT_TRANSLATOR_LISTENER (Tab_note_heads_engraver, string_number);
70 void
71 Tab_note_heads_engraver::listen_string_number (Stream_event *ev)
72 {
73   tabstring_events_.push_back (ev);
74 }
75
76 void
77 Tab_note_heads_engraver::process_music ()
78 {
79   vsize j = 0;
80   for (vsize i = 0; i < note_events_.size (); i++)
81     {
82       SCM string_tunings = get_property ("stringTunings");
83       int string_count = scm_ilength (string_tunings);
84       bool high_string_one = to_boolean (get_property ("highStringOne"));
85
86       Stream_event *event = note_events_[i];
87
88       Stream_event *tabstring_event = 0;
89
90       for (SCM s = event->get_property ("articulations");
91            !tabstring_event && scm_is_pair (s); s = scm_cdr (s))
92         {
93           Stream_event *art = unsmob_stream_event (scm_car (s));
94
95           if (art->in_event_class ("string-number-event"))
96             tabstring_event = art;
97         }
98
99       if (!tabstring_event && j < tabstring_events_.size ())
100         {
101           tabstring_event = tabstring_events_[j];
102           if (j + 1 < tabstring_events_.size ())
103             j++;
104         }
105
106       int string_number = 0;
107       if (tabstring_event)
108         string_number = scm_to_int (tabstring_event->get_property ("string-number"));
109
110       if (!string_number)
111         {
112           SCM scm_pitch = event->get_property ("pitch");
113           int min_fret = robust_scm2int (get_property ("minimumFret"), 0);
114           int start = (high_string_one) ? 1 : string_count;
115           int end = (high_string_one) ? string_count+1 : 0;
116
117           int i = start;
118           do
119             {
120               int fret = unsmob_pitch (scm_pitch)->rounded_semitone_pitch ()
121                 - scm_to_int (robust_list_ref (i - 1, string_tunings));
122           
123               if (fret >= min_fret)
124                 {
125                   string_number = i;
126                   break;
127                 }
128               i += high_string_one ? 1 : -1;
129             }
130           while (i != end);
131         }
132       
133       if (string_number)
134         {
135           SCM proc = get_property ("tablatureFormat");
136           SCM text = scm_call_3 (proc, scm_from_int (string_number),
137                                  context ()->self_scm (),
138                                  event->self_scm ());
139           Item *note = make_item ("TabNoteHead", event->self_scm ());
140           note->set_property ("text", text);
141
142
143           int pos = 2 * string_number - string_count - 1; // No tab-note between the string !!!
144           if (to_boolean (get_property ("stringOneTopmost")))
145             pos = - pos;
146
147           note->set_property ("staff-position", scm_from_int (pos));
148       
149           notes_.push_back (note);
150         }
151       else
152         event->origin ()->warning ("could not calculate a string number.");
153     }
154 }
155 void
156 Tab_note_heads_engraver::stop_translation_timestep ()
157 {
158   notes_.clear ();
159   note_events_.clear ();
160   tabstring_events_.clear ();
161 }
162
163 ADD_TRANSLATOR (Tab_note_heads_engraver,
164                 /* doc */
165                 "Generate one or more tablature noteheads from event of type"
166                 " @code{NoteEvent}.",
167
168                 /* create */
169                 "TabNoteHead ",
170
171                 /* read */
172                 "fretLabels "
173                 "highStringOne "
174                 "middleCPosition "
175                 "minimumFret "
176                 "stringOneTopmost "
177                 "stringTunings "
178                 "tablatureFormat ",
179
180                 /* write */ ""
181                 );
182