]> git.donarmstrong.com Git - lilypond.git/blob - lily/tab-note-heads-engraver.cc
51ea30da01e421219ba32f6ea2309176b4a1945a
[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--2015 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 "articulations.hh"
28 #include "duration.hh"
29 #include "item.hh"
30 #include "output-def.hh"
31 #include "pitch.hh"
32 #include "rhythmic-head.hh"
33 #include "stream-event.hh"
34 #include "warn.hh"
35 #include "context.hh"
36
37 #include "translator.icc"
38
39 using std::vector;
40
41 /**
42    make (guitar-like) tablature note
43 */
44 class Tab_note_heads_engraver : public Engraver
45 {
46   vector<Stream_event *> note_events_;
47   vector<Stream_event *> tabstring_events_;
48   vector<Stream_event *> fingering_events_;
49
50 public:
51   TRANSLATOR_DECLARATIONS (Tab_note_heads_engraver);
52
53 protected:
54   DECLARE_TRANSLATOR_LISTENER (note);
55   DECLARE_TRANSLATOR_LISTENER (string_number);
56   DECLARE_TRANSLATOR_LISTENER (fingering);
57   void process_music ();
58
59   void stop_translation_timestep ();
60 };
61
62 Tab_note_heads_engraver::Tab_note_heads_engraver ()
63 {
64 }
65
66 IMPLEMENT_TRANSLATOR_LISTENER (Tab_note_heads_engraver, note);
67 void
68 Tab_note_heads_engraver::listen_note (Stream_event *ev)
69 {
70   note_events_.push_back (ev);
71 }
72
73 IMPLEMENT_TRANSLATOR_LISTENER (Tab_note_heads_engraver, string_number);
74 void
75 Tab_note_heads_engraver::listen_string_number (Stream_event *ev)
76 {
77   tabstring_events_.push_back (ev);
78 }
79
80 IMPLEMENT_TRANSLATOR_LISTENER (Tab_note_heads_engraver, fingering);
81 void
82 Tab_note_heads_engraver::listen_fingering (Stream_event *ev)
83 {
84   fingering_events_.push_back (ev);
85 }
86
87 void
88 Tab_note_heads_engraver::process_music ()
89 {
90   SCM tab_strings = articulation_list (note_events_,
91                                        tabstring_events_,
92                                        "string-number-event");
93   SCM defined_fingers = articulation_list (note_events_,
94                                            fingering_events_,
95                                            "fingering-event");
96   SCM tab_notes = ly_cxx_vector_to_list (note_events_);
97   SCM proc = get_property ("noteToFretFunction");
98   SCM string_fret_finger = SCM_EOL;
99   if (ly_is_procedure (proc))
100     string_fret_finger = scm_call_3 (proc,
101                                      context ()->self_scm (),
102                                      tab_notes,
103                                      scm_list_2 (tab_strings,
104                                                  defined_fingers));
105   SCM note_entry = SCM_EOL;
106   SCM string_number = SCM_EOL;
107   SCM fret = SCM_EOL;
108   SCM fret_label = SCM_EOL;
109   SCM fret_procedure = get_property ("tablatureFormat");
110   SCM staff_line_procedure = get_property ("tabStaffLineLayoutFunction");
111   SCM staff_position = SCM_EOL;
112   vsize fret_count = (vsize) scm_ilength (string_fret_finger);
113   bool length_changed = (note_events_.size () != fret_count);
114   vsize index;
115
116   if (!scm_is_null (string_fret_finger))
117     for (vsize i = 0; i < fret_count; i++)
118       {
119         note_entry = scm_list_ref (string_fret_finger, scm_from_int (i));
120         string_number = scm_car (note_entry);
121         if (scm_is_true (string_number))
122           {
123             fret = scm_cadr (note_entry);
124             fret_label = scm_call_3 (fret_procedure,
125                                      context ()->self_scm (),
126                                      string_number,
127                                      fret);
128             index = length_changed ? 0 : i;
129             Item *note = make_item ("TabNoteHead", note_events_[index]->self_scm ());
130             note->set_property ("text", fret_label);
131             staff_position = scm_call_2 (staff_line_procedure,
132                                          context ()->self_scm (),
133                                          string_number);
134             note->set_property ("staff-position", staff_position);
135           }
136       }
137 }
138
139 void
140 Tab_note_heads_engraver::stop_translation_timestep ()
141 {
142   note_events_.clear ();
143   tabstring_events_.clear ();
144   fingering_events_.clear ();
145 }
146
147 ADD_TRANSLATOR (Tab_note_heads_engraver,
148                 /* doc */
149                 "Generate one or more tablature note heads from event of type"
150                 " @code{NoteEvent}.",
151
152                 /* create */
153                 "TabNoteHead ",
154
155                 /* read */
156                 "defaultStrings "
157                 "fretLabels "
158                 "highStringOne "
159                 "middleCPosition "
160                 "minimumFret "
161                 "noteToFretFunction "
162                 "stringOneTopmost "
163                 "stringTunings "
164                 "tablatureFormat "
165                 "tabStaffLineLayoutFunction ",
166
167                 /* write */
168                 ""
169                );
170