]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-heads-engraver.cc
* lily/translator.cc, lily/context.cc:, lily/translator-group.cc:
[lilypond.git] / lily / note-heads-engraver.cc
1 /*
2   note-heads-engraver.cc -- part of GNU LilyPond
3
4   (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
5 */
6
7 #include "engraver.hh"
8
9 #include <cctype>
10 using namespace std;
11
12 #include "dots.hh"
13 #include "dot-column.hh"
14 #include "duration.hh"
15 #include "item.hh"
16 #include "output-def.hh"
17 #include "pitch.hh"
18 #include "rhythmic-head.hh"
19 #include "staff-symbol-referencer.hh"
20 #include "stream-event.hh"
21 #include "warn.hh"
22
23 #include "translator.icc"
24
25 class Note_heads_engraver : public Engraver
26 {
27   vector<Item*> notes_;
28   vector<Item*> dots_;
29   vector<Stream_event*> note_evs_;
30
31 public:
32   TRANSLATOR_DECLARATIONS (Note_heads_engraver);
33
34 protected:
35   DECLARE_TRANSLATOR_LISTENER (note);
36   void process_music ();
37   void stop_translation_timestep ();
38 };
39
40 Note_heads_engraver::Note_heads_engraver ()
41 {
42 }
43
44 IMPLEMENT_TRANSLATOR_LISTENER (Note_heads_engraver, note);
45 void
46 Note_heads_engraver::listen_note (Stream_event *ev)
47 {
48   note_evs_.push_back (ev);
49 }
50
51 void
52 Note_heads_engraver::process_music ()
53 {
54   for (vsize i = 0; i < note_evs_.size (); i++)
55     {
56       Stream_event *ev = note_evs_[i];
57       Item *note = make_item ("NoteHead", ev->self_scm ());
58
59       Duration dur = *unsmob_duration (ev->get_property ("duration"));
60
61       note->set_property ("duration-log", scm_from_int (dur.duration_log ()));
62       if (dur.dot_count ())
63         {
64           Item *d = make_item ("Dots", note->self_scm ());
65           Rhythmic_head::set_dots (note, d);
66
67           if (dur.dot_count ()
68               != robust_scm2int (d->get_property ("dot-count"), 0))
69             d->set_property ("dot-count", scm_from_int (dur.dot_count ()));
70
71           d->set_parent (note, Y_AXIS);
72
73           dots_.push_back (d);
74         }
75
76       Pitch *pit = unsmob_pitch (ev->get_property ("pitch"));
77
78 #if 0 /* TODO: should have a mechanism to switch off these warnings. */
79
80       if (!pit)
81         ev->origin ()->warning (_ ("NoteEvent without pitch"));
82 #endif
83
84       int pos = pit ? pit->steps () : 0;
85       SCM c0 = get_property ("middleCPosition");
86       if (scm_is_number (c0))
87         pos += scm_to_int (c0);
88
89       note->set_property ("staff-position", scm_from_int (pos));
90
91       /*
92         Shape note heads change on step of the scale.
93       */
94       SCM shape_vector = get_property ("shapeNoteStyles");
95       if (scm_is_vector (shape_vector))
96         {
97           SCM scm_tonic = get_property ("tonic");
98           Pitch tonic (0, 0, 0);
99           if (unsmob_pitch (scm_tonic))
100             tonic = *unsmob_pitch (scm_tonic);
101
102           unsigned int delta = (pit->get_notename () - tonic.get_notename () + 7) % 7;
103
104           SCM style = SCM_EOL;
105           if (scm_c_vector_length (shape_vector) > delta
106               && scm_is_symbol (scm_vector_ref (shape_vector, scm_from_int (delta))))
107             style = scm_vector_ref (shape_vector, scm_from_int (delta));
108           if (scm_is_symbol (style))
109             note->set_property ("style", style);
110         }
111
112       notes_.push_back (note);
113     }
114 }
115
116 void
117 Note_heads_engraver::stop_translation_timestep ()
118 {
119   notes_.clear ();
120   dots_.clear ();
121   note_evs_.clear ();
122 }
123
124 ADD_TRANSLATOR (Note_heads_engraver,
125                 /* doc */ "Generate noteheads.",
126                 /* create */
127                 "NoteHead "
128                 "Dots",
129                 /* accept */
130                 "note-event",
131                 /* read */ "middleCPosition",
132                 /* write */ "");