]> git.donarmstrong.com Git - lilypond.git/blob - lily/audio-item.cc
Run grand-replace (issue 3765)
[lilypond.git] / lily / audio-item.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--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 "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                         int velocity)
49   : pitch_ (p),
50     length_mom_ (m),
51     transposing_ (transposing),
52     dynamic_ (0),
53     extra_velocity_ (velocity),
54     tied_ (0),
55     tie_event_ (tie_event)
56 {
57 }
58
59 void
60 Audio_note::tie_to (Audio_note *t, Moment skip)
61 {
62   tied_ = t;
63   Audio_note *first = tie_head();
64   // Add the skip to the tied note and the length of the appended note
65   // to the full duration of the tie...
66   first->length_mom_ += skip + length_mom_;
67   length_mom_ = 0;
68 }
69
70 Audio_note *
71 Audio_note::tie_head ()
72 {
73   Audio_note *first = this;
74   while (first->tied_)
75     first = first->tied_;
76   return first;
77 }
78
79 string
80 Audio_note::to_string () const
81 {
82   string s = "#<Audio_note pitch ";
83   s += pitch_.to_string();
84   s += " len ";
85   s += length_mom_.to_string();
86   if (tied_)
87     {
88       s += " tied to " + tied_->to_string();
89     }
90   if (tie_event_)
91     {
92       s += " tie_event";
93     }
94   s += ">";
95   return s;
96 }
97
98 Audio_key::Audio_key (int acc, bool major)
99 {
100   accidentals_ = acc;
101   major_ = major;
102 }
103
104 Audio_dynamic::Audio_dynamic ()
105   : volume_ (-1),
106     silent_ (false)
107 {
108 }
109
110 Audio_span_dynamic::Audio_span_dynamic (Real min_volume, Real max_volume)
111 {
112   grow_dir_ = CENTER;
113   min_volume_ = min_volume;
114   max_volume_ = max_volume;
115 }
116
117 void
118 Audio_span_dynamic::add_absolute (Audio_dynamic *d)
119 {
120   assert (d);
121   dynamics_.push_back (d);
122 }
123
124 Moment
125 remap_grace_duration (Moment m)
126 {
127   return Moment (m.main_part_ + Rational (9, 40) * m.grace_part_,
128                  Rational (0));
129 }
130
131 Real
132 moment_to_real (Moment m)
133 {
134   return remap_grace_duration (m).main_part_.to_double ();
135 }
136
137 int
138 moment_to_ticks (Moment m)
139 {
140   return int (moment_to_real (m) * 384 * 4);
141 }
142
143 void
144 Audio_span_dynamic::render ()
145 {
146   if (dynamics_.size () <= 1)
147     return;
148
149   assert (dynamics_[0]->volume_ >= 0);
150
151   while (dynamics_.back ()->volume_ > 0
152          && dynamics_.size () > 1
153          && sign (dynamics_.back ()->volume_ - dynamics_[0]->volume_) != grow_dir_)
154     {
155       dynamics_.erase (dynamics_.end () - 1);
156     }
157
158   if (dynamics_.size () <= 1)
159     {
160       programming_error ("Impossible or ambiguous (de)crescendo in MIDI.");
161       return;
162     }
163
164   Real start_v = dynamics_[0]->volume_;
165   if (dynamics_.back ()->volume_ < 0)
166     {
167       // The dynamic spanner does not end with an explicit dynamic script
168       // event.  Adjust the end volume by at most 1/4 of the available
169       // volume range in this case.
170       dynamics_.back ()->volume_ = max (min (start_v + grow_dir_ * (max_volume_ - min_volume_) * 0.25, max_volume_), min_volume_);
171     }
172
173   Real delta_v = dynamics_.back ()->volume_ - dynamics_[0]->volume_;
174
175   Moment start = dynamics_[0]->get_column ()->when ();
176
177   Real total_t = moment_to_real (dynamics_.back ()->get_column ()->when () - start);
178
179   for (vsize i = 1; i < dynamics_.size (); i++)
180     {
181       Moment dt_moment = dynamics_[i]->get_column ()->when ()
182                          - start;
183
184       Real dt = moment_to_real (dt_moment);
185
186       Real v = start_v + delta_v * (dt / total_t);
187
188       dynamics_[i]->volume_ = v;
189     }
190 }
191
192 Audio_tempo::Audio_tempo (int per_minute_4)
193 {
194   per_minute_4_ = per_minute_4;
195 }
196
197 Audio_time_signature::Audio_time_signature (int beats, int one_beat)
198 {
199   beats_ = beats;
200   one_beat_ = one_beat;
201 }
202
203 Audio_text::Audio_text (Audio_text::Type type, const string &text_string)
204 {
205   text_string_ = text_string;
206   type_ = type;
207 }
208
209 Audio_control_function_value_change
210 ::Audio_control_function_value_change (Control control, Real value)
211   : control_ (control), value_ (value)
212 {
213 }
214
215 const Audio_control_function_value_change::Context_property
216 Audio_control_function_value_change::context_properties_[] = {
217   // property name, enum constant, lower bound for range, upper bound for range
218   { "midiBalance",     BALANCE,      -1.0, 1.0 },
219   { "midiPanPosition", PAN_POSITION, -1.0, 1.0 },
220   { "midiReverbLevel", REVERB_LEVEL,  0.0, 1.0 },
221   { "midiChorusLevel", CHORUS_LEVEL,  0.0, 1.0 },
222   // extra element to signify the end of the mapping, must be kept last
223   { 0,                 NUM_CONTROLS,  0.0, 0.0 }
224 };