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