]> git.donarmstrong.com Git - lilypond.git/blob - lily/fretboard-engraver.cc
Merge branch 'lilypond/translation' of ssh://git.sv.gnu.org/srv/git/lilypond into...
[lilypond.git] / lily / fretboard-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2006--2010 Han-Wen Nienhuys
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 using namespace std;
23
24 #include "context.hh"
25 #include "item.hh"
26 #include "engraver.hh"
27 #include "pitch.hh"
28 #include "stream-event.hh"
29 #include "warn.hh"
30
31 #include "translator.icc"
32
33 /**
34    make (guitar-like) tablature note
35 */
36 class Fretboard_engraver : public Engraver
37 {
38   Item *fret_board_;
39
40   vector<Stream_event*> note_events_;
41   vector<Stream_event*> tabstring_events_;
42 public:
43   TRANSLATOR_DECLARATIONS (Fretboard_engraver);
44
45 protected:
46   void stop_translation_timestep ();
47   void process_music ();
48   virtual void derived_mark() const;
49   DECLARE_TRANSLATOR_LISTENER (note);
50   DECLARE_TRANSLATOR_LISTENER (string_number);
51
52 private:
53   SCM last_fret_notes_;
54 };
55
56
57 void
58 Fretboard_engraver::derived_mark () const
59 {
60   scm_gc_mark (last_fret_notes_);
61 }
62
63 Fretboard_engraver::Fretboard_engraver ()
64 {
65   fret_board_ = 0;
66   last_fret_notes_ = SCM_EOL;
67 }
68
69 IMPLEMENT_TRANSLATOR_LISTENER (Fretboard_engraver, note);
70 void
71 Fretboard_engraver::listen_note (Stream_event *ev)
72 {
73   note_events_.push_back (ev);
74 }
75
76 IMPLEMENT_TRANSLATOR_LISTENER (Fretboard_engraver, string_number);
77 void
78 Fretboard_engraver::listen_string_number (Stream_event *ev)
79 {
80   tabstring_events_.push_back (ev);
81 }
82
83 void
84 Fretboard_engraver::process_music ()
85 {
86   if (!note_events_.size ())
87     return ;
88
89   // Ugh -- copied from tab-note-heads-engraver; need to resolve
90   vsize j = 0;
91
92   vector<Stream_event *> string_events;
93
94   for (vsize i = 0; i < note_events_.size (); i++)
95     {
96
97       Stream_event *event = note_events_[i];
98
99       Stream_event *tabstring_event = 0;
100
101       /*
102          For notes inside a chord construct, string indications are
103          stored as articulations on the note, so we check through
104          the notes
105       */
106       for (SCM s = event->get_property ("articulations");
107            !tabstring_event && scm_is_pair (s); s = scm_cdr (s))
108         {
109           Stream_event *art = unsmob_stream_event (scm_car (s));
110
111           if (art->in_event_class ("string-number-event"))
112             tabstring_event = art;
113         }
114
115       /*
116          For string indications listed outside a chord construct,
117          a string_number_event is generated, so if there was no string
118          in the articulations, we check for string events outside
119          the chord construct
120       */
121       if (!tabstring_event && j < tabstring_events_.size ())
122         {
123           tabstring_event = tabstring_events_[j];
124           if (j + 1 < tabstring_events_.size ())
125             j++;
126         }
127       if (tabstring_event)
128         string_events.push_back (tabstring_event);
129     }
130   // end of copied code
131
132   fret_board_ = make_item ("FretBoard", note_events_[0]->self_scm ());
133   SCM fret_notes = ly_cxx_vector_to_list (note_events_);
134   SCM proc = get_property ("noteToFretFunction");
135   if (ly_is_procedure (proc))
136      scm_call_4 (proc,
137                  context ()->self_scm (),
138                  fret_notes,
139                  ly_cxx_vector_to_list (string_events),
140                  fret_board_->self_scm ());
141   SCM changes = get_property ("chordChanges");
142   if (to_boolean (changes) && scm_is_pair (last_fret_notes_)
143       && ly_is_equal (last_fret_notes_, fret_notes))
144     fret_board_->set_property ("begin-of-line-visible", SCM_BOOL_T);
145
146   last_fret_notes_ = fret_notes;
147 }
148
149 void
150 Fretboard_engraver::stop_translation_timestep ()
151 {
152   fret_board_ = 0;
153   note_events_.clear ();
154   tabstring_events_.clear ();
155 }
156
157 ADD_TRANSLATOR (Fretboard_engraver,
158                 /* doc */
159                 "Generate fret diagram from one or more events of type"
160                 " @code{NoteEvent}.",
161
162                 /* create */
163                 "FretBoard ",
164
165                 /* read */
166                 "chordChanges "
167                 "highStringOne "
168                 "maximumFretStretch "
169                 "minimumFret "
170                 "noteToFretFunction "
171                 "predefinedDiagramTable "
172                 "stringTunings "
173                 "tablatureFormat ",
174
175                 /* write */
176                 ""
177                 );
178