]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-performer.cc
unsmob_pitch -> Pitch::unsmob and related
[lilypond.git] / lily / note-performer.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1996--2014 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   DECLARE_TRANSLATOR_LISTENER (breathing);
40 private:
41   vector<Stream_event *> note_evs_;
42   vector<Audio_note *> notes_;
43
44   vector<Audio_note *> last_notes_;
45   Moment last_start_;
46
47 };
48
49 void
50 Note_performer::process_music ()
51 {
52   if (!note_evs_.size ())
53     return;
54
55   Pitch transposing;
56   SCM prop = get_property ("instrumentTransposition");
57   if (Pitch::unsmob (prop))
58     transposing = *Pitch::unsmob (prop);
59
60   for (vsize i = 0; i < note_evs_.size (); i++)
61     {
62       Stream_event *n = note_evs_[i];
63       SCM pit = n->get_property ("pitch");
64
65       if (Pitch *pitp = Pitch::unsmob (pit))
66         {
67           SCM articulations = n->get_property ("articulations");
68           Stream_event *tie_event = 0;
69           Moment len = get_event_length (n, now_mom ());
70           int velocity = 0;
71           for (SCM s = articulations; scm_is_pair (s); s = scm_cdr (s))
72             {
73               Stream_event *ev = unsmob_stream_event (scm_car (s));
74               if (!ev)
75                 continue;
76
77               if (ev->in_event_class ("tie-event"))
78                 tie_event = ev;
79               SCM f = ev->get_property ("midi-length");
80               if (ly_is_procedure (f))
81                 len = robust_scm2moment (scm_call_2 (f, len.smobbed_copy (),
82                                                      context ()->self_scm ()),
83                                          len);
84               velocity += robust_scm2int (ev->get_property ("midi-extra-velocity"), 0);
85             }
86
87           Audio_note *p = new Audio_note (*pitp, len,
88                                           tie_event, transposing, velocity);
89           Audio_element_info info (p, n);
90           announce_element (info);
91           notes_.push_back (p);
92
93           /*
94             Grace notes shorten the previous non-grace note. If it was
95             part of a tie, shorten the first note in the tie.
96            */
97           if (now_mom ().grace_part_)
98             {
99               if (last_start_.grace_part_ == Rational (0))
100                 {
101                   for (vsize i = 0; i < last_notes_.size (); i++)
102                     {
103                       Audio_note *tie_head = last_notes_[i]->tie_head ();
104                       Moment start = tie_head->audio_column_->when ();
105                       //Shorten the note if it would overlap. It might
106                       //not if there's a rest in between.
107                       if (start + tie_head->length_mom_ > now_mom ())
108                         tie_head->length_mom_ = now_mom () - start;
109                     }
110                 }
111             }
112         }
113     }
114 }
115
116 void
117 Note_performer::stop_translation_timestep ()
118 {
119   if (note_evs_.size ())
120     {
121       last_notes_ = notes_;
122       last_start_ = now_mom ();
123     }
124
125   notes_.clear ();
126   note_evs_.clear ();
127 }
128
129 IMPLEMENT_TRANSLATOR_LISTENER (Note_performer, note)
130 void
131 Note_performer::listen_note (Stream_event *ev)
132 {
133   note_evs_.push_back (ev);
134 }
135
136 IMPLEMENT_TRANSLATOR_LISTENER (Note_performer, breathing)
137 void
138 Note_performer::listen_breathing (Stream_event *ev)
139 {
140   //Shorten previous note if needed
141   SCM f = ev->get_property ("midi-length");
142   if (ly_is_procedure (f))
143     for (vsize i = 0; i < last_notes_.size (); i++)
144       {
145         //Pass midi-length the available time since the last note started,
146         //including any intervening rests. It returns how much is left for the
147         //note.
148         Moment start = last_notes_[i]->audio_column_->when ();
149         Moment available = now_mom () - start;
150         Moment len = robust_scm2moment (scm_call_2 (f, available.smobbed_copy (),
151                                                     context ()->self_scm ()), available);
152         //Take time from the first note of the tie, since it has all the length.
153         Audio_note *tie_head = last_notes_[i]->tie_head ();
154         len += start - tie_head->audio_column_->when ();
155         if (len < tie_head->length_mom_)
156           tie_head->length_mom_ = len;
157       }
158 }
159
160 ADD_TRANSLATOR (Note_performer,
161                 /* doc */
162                 "",
163
164                 /* create */
165                 "",
166
167                 /* read */
168                 "",
169
170                 /* write */
171                 ""
172                );
173
174 Note_performer::Note_performer ()
175 {
176 }