]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-performer.cc
Grace notes: only shorten previous note if overlapping
[lilypond.git] / lily / note-performer.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1996--2012 Jan Nieuwenhuizen <janneke@gnu.org>
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 "performer.hh"
21 #include "audio-item.hh"
22 #include "audio-column.hh"
23 #include "global-context.hh"
24 #include "stream-event.hh"
25 #include "warn.hh"
26
27 #include "translator.icc"
28
29 class Note_performer : public Performer
30 {
31 public:
32   TRANSLATOR_DECLARATIONS (Note_performer);
33
34 protected:
35   void stop_translation_timestep ();
36   void process_music ();
37
38   DECLARE_TRANSLATOR_LISTENER (note);
39 private:
40   vector<Stream_event *> note_evs_;
41   vector<Audio_note *> notes_;
42
43   vector<Audio_note *> last_notes_;
44   Moment last_start_;
45
46 };
47
48 void
49 Note_performer::process_music ()
50 {
51   if (!note_evs_.size ())
52     return;
53
54   Pitch transposing;
55   SCM prop = get_property ("instrumentTransposition");
56   if (unsmob_pitch (prop))
57     transposing = *unsmob_pitch (prop);
58
59   for (vsize i = 0; i < note_evs_.size (); i++)
60     {
61       Stream_event *n = note_evs_[i];
62       SCM pit = n->get_property ("pitch");
63
64       if (Pitch *pitp = unsmob_pitch (pit))
65         {
66           SCM articulations = n->get_property ("articulations");
67           Stream_event *tie_event = 0;
68           for (SCM s = articulations;
69                !tie_event && scm_is_pair (s);
70                s = scm_cdr (s))
71             {
72               Stream_event *ev = unsmob_stream_event (scm_car (s));
73               if (!ev)
74                 continue;
75
76               if (ev->in_event_class ("tie-event"))
77                 tie_event = ev;
78             }
79
80           Moment len = get_event_length (n, now_mom ());
81
82           Audio_note *p = new Audio_note (*pitp, len,
83                                           tie_event, transposing);
84           Audio_element_info info (p, n);
85           announce_element (info);
86           notes_.push_back (p);
87
88           /*
89             Grace notes shorten the previous non-grace note. If it was
90             part of a tie, shorten the first note in the tie.
91            */
92           if (now_mom ().grace_part_)
93             {
94               if (last_start_.grace_part_ == Rational (0))
95                 {
96                   for (vsize i = 0; i < last_notes_.size (); i++)
97                     {
98                       Audio_note *tie_head = last_notes_[i]->tie_head ();
99                       Moment start = tie_head->audio_column_->when ();
100                       //Shorten the note if it would overlap. It might
101                       //not if there's a rest in between.
102                       if (start + tie_head->length_mom_ > now_mom ())
103                         tie_head->length_mom_ = now_mom () - start;
104                     }
105                 }
106             }
107         }
108     }
109 }
110
111 void
112 Note_performer::stop_translation_timestep ()
113 {
114   if (note_evs_.size ())
115     {
116       last_notes_ = notes_;
117       last_start_ = now_mom ();
118     }
119
120   notes_.clear ();
121   note_evs_.clear ();
122 }
123
124 IMPLEMENT_TRANSLATOR_LISTENER (Note_performer, note)
125 void
126 Note_performer::listen_note (Stream_event *ev)
127 {
128   note_evs_.push_back (ev);
129 }
130
131 ADD_TRANSLATOR (Note_performer,
132                 /* doc */
133                 "",
134
135                 /* create */
136                 "",
137
138                 /* read */
139                 "",
140
141                 /* write */
142                 ""
143                );
144
145 Note_performer::Note_performer ()
146 {
147 }