]> git.donarmstrong.com Git - lilypond.git/blob - lily/audio-item.cc
Merge branch 'tie-grace-fix' into staging
[lilypond.git] / lily / audio-item.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--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 "audio-item.hh"
21
22 #include "midi-item.hh"
23 #include "audio-column.hh"
24
25 Audio_instrument::Audio_instrument (string instrument_string)
26 {
27   str_ = instrument_string;
28 }
29
30 void
31 Audio_item::render ()
32 {
33 }
34
35 Audio_column *
36 Audio_item::get_column () const
37 {
38   return audio_column_;
39 }
40
41 Audio_item::Audio_item ()
42   : audio_column_ (0),
43     channel_ (0)
44 {
45 }
46
47 Audio_note::Audio_note (Pitch p, Moment m, bool tie_event, Pitch transposing)
48   : pitch_ (p),
49     length_mom_ (m),
50     transposing_ (transposing),
51     dynamic_ (0),
52     tied_ (0),
53     tie_event_ (tie_event)
54 {
55 }
56
57 void
58 Audio_note::tie_to (Audio_note *t, Moment skip)
59 {
60   tied_ = t;
61   Audio_note *first = tie_head();
62   // Add the skip to the tied note and the length of the appended note
63   // to the full duration of the tie...
64   first->length_mom_ += skip + length_mom_;
65   length_mom_ = 0;
66 }
67
68 Audio_note *
69 Audio_note::tie_head ()
70 {
71   Audio_note *first = this;
72   while (first->tied_)
73     first = first->tied_;
74   return first;
75 }
76
77 string
78 Audio_note::to_string () const
79 {
80   string s = "#<Audio_note pitch ";
81   s += pitch_.to_string();
82   s += " len ";
83   s += length_mom_.to_string();
84   if (tied_)
85     {
86       s += " tied to " + tied_->to_string();
87     }
88   if (tie_event_)
89     {
90       s += " tie_event";
91     }
92   s += ">";
93   return s;
94 }
95
96 Audio_key::Audio_key (int acc, bool major)
97 {
98   accidentals_ = acc;
99   major_ = major;
100 }
101
102 Audio_dynamic::Audio_dynamic ()
103   : volume_ (-1),
104     silent_ (false)
105 {
106 }
107
108 Audio_span_dynamic::Audio_span_dynamic (Real min_volume, Real max_volume)
109 {
110   grow_dir_ = CENTER;
111   min_volume_ = min_volume;
112   max_volume_ = max_volume;
113 }
114
115 void
116 Audio_span_dynamic::add_absolute (Audio_dynamic *d)
117 {
118   assert (d);
119   dynamics_.push_back (d);
120 }
121
122 Moment
123 remap_grace_duration (Moment m)
124 {
125   return Moment (m.main_part_ + Rational (9, 40) * m.grace_part_,
126                  Rational (0));
127 }
128
129 Real
130 moment_to_real (Moment m)
131 {
132   return remap_grace_duration (m).main_part_.to_double ();
133 }
134
135 int
136 moment_to_ticks (Moment m)
137 {
138   return int (moment_to_real (m) * 384 * 4);
139 }
140
141 void
142 Audio_span_dynamic::render ()
143 {
144   if (dynamics_.size () <= 1)
145     return;
146
147   assert (dynamics_[0]->volume_ >= 0);
148
149   while (dynamics_.back ()->volume_ > 0
150          && dynamics_.size () > 1
151          && sign (dynamics_.back ()->volume_ - dynamics_[0]->volume_) != grow_dir_)
152     {
153       dynamics_.erase (dynamics_.end () - 1);
154     }
155
156   if (dynamics_.size () <= 1)
157     {
158       programming_error ("Impossible or ambiguous (de)crescendo in MIDI.");
159       return;
160     }
161
162   Real start_v = dynamics_[0]->volume_;
163   if (dynamics_.back ()->volume_ < 0)
164     {
165       // The dynamic spanner does not end with an explicit dynamic script
166       // event.  Adjust the end volume by at most 1/4 of the available
167       // volume range in this case.
168       dynamics_.back ()->volume_ = max (min (start_v + grow_dir_ * (max_volume_ - min_volume_) * 0.25, max_volume_), min_volume_);
169     }
170
171   Real delta_v = dynamics_.back ()->volume_ - dynamics_[0]->volume_;
172
173   Moment start = dynamics_[0]->get_column ()->when ();
174
175   Real total_t = moment_to_real (dynamics_.back ()->get_column ()->when () - start);
176
177   for (vsize i = 1; i < dynamics_.size (); i++)
178     {
179       Moment dt_moment = dynamics_[i]->get_column ()->when ()
180                          - start;
181
182       Real dt = moment_to_real (dt_moment);
183
184       Real v = start_v + delta_v * (dt / total_t);
185
186       dynamics_[i]->volume_ = v;
187     }
188 }
189
190 Audio_tempo::Audio_tempo (int per_minute_4)
191 {
192   per_minute_4_ = per_minute_4;
193 }
194
195 Audio_time_signature::Audio_time_signature (int beats, int one_beat)
196 {
197   beats_ = beats;
198   one_beat_ = one_beat;
199 }
200
201 Audio_text::Audio_text (Audio_text::Type type, string text_string)
202 {
203   text_string_ = text_string;
204   type_ = type;
205 }
206