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