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