]> git.donarmstrong.com Git - lilypond.git/blob - lily/fretboard-engraver.cc
Rationalize string number handling for notes and chords
[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 "articulations.hh"
25 #include "context.hh"
26 #include "item.hh"
27 #include "engraver.hh"
28 #include "pitch.hh"
29 #include "stream-event.hh"
30 #include "warn.hh"
31
32 #include "translator.icc"
33
34 /**
35    make (guitar-like) tablature note
36 */
37 class Fretboard_engraver : public Engraver
38 {
39   Item *fret_board_;
40
41   vector<Stream_event *> note_events_;
42   vector<Stream_event *> tabstring_events_;
43 public:
44   TRANSLATOR_DECLARATIONS (Fretboard_engraver);
45
46 protected:
47   void stop_translation_timestep ();
48   void process_music ();
49   virtual void derived_mark () const;
50   DECLARE_TRANSLATOR_LISTENER (note);
51   DECLARE_TRANSLATOR_LISTENER (string_number);
52
53 private:
54   SCM last_fret_notes_;
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   SCM tab_strings = articulation_list (note_events_,
90                                        tabstring_events_,
91                                        "string-number-event");
92   fret_board_ = make_item ("FretBoard", note_events_[0]->self_scm ());
93   SCM fret_notes = ly_cxx_vector_to_list (note_events_);
94   SCM proc = get_property ("noteToFretFunction");
95   if (ly_is_procedure (proc))
96     scm_call_4 (proc,
97                 context ()->self_scm (),
98                 fret_notes,
99                 tab_strings,
100                 fret_board_->self_scm ());
101   SCM changes = get_property ("chordChanges");
102   if (to_boolean (changes) && scm_is_pair (last_fret_notes_)
103       && ly_is_equal (last_fret_notes_, fret_notes))
104     fret_board_->set_property ("begin-of-line-visible", SCM_BOOL_T);
105
106   last_fret_notes_ = fret_notes;
107 }
108
109 void
110 Fretboard_engraver::stop_translation_timestep ()
111 {
112   fret_board_ = 0;
113   note_events_.clear ();
114   tabstring_events_.clear ();
115 }
116
117 ADD_TRANSLATOR (Fretboard_engraver,
118                 /* doc */
119                 "Generate fret diagram from one or more events of type"
120                 " @code{NoteEvent}.",
121
122                 /* create */
123                 "FretBoard ",
124
125                 /* read */
126                 "chordChanges "
127                 "highStringOne "
128                 "maximumFretStretch "
129                 "minimumFret "
130                 "noteToFretFunction "
131                 "predefinedDiagramTable "
132                 "stringTunings "
133                 "tablatureFormat ",
134
135                 /* write */
136                 ""
137                 );
138