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