]> git.donarmstrong.com Git - lilypond.git/blob - lily/audio-item.cc
0ed413bf6796a858656a9ff318e14c4c9e99584a
[lilypond.git] / lily / audio-item.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 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 {
43   audio_column_ = 0;
44 }
45
46 Audio_note::Audio_note (Pitch p, Moment m, bool tie_event, Pitch transposing)
47 {
48   pitch_ = p;
49   length_mom_ = m;
50   tied_ = 0;
51   transposing_ = transposing;
52   tie_event_ = tie_event;
53 }
54
55 void
56 Audio_note::tie_to (Audio_note *t, Moment skip)
57 {
58   tied_ = t;
59   Audio_note *first = t;
60   while (first->tied_)
61     first = first->tied_;
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_key::Audio_key (int acc, bool major)
69 {
70   accidentals_ = acc;
71   major_ = major;
72 }
73
74 Audio_dynamic::Audio_dynamic ()
75 {
76   volume_ = -1;
77 }
78
79 Audio_span_dynamic::Audio_span_dynamic ()
80 {
81   grow_dir_ = CENTER;
82 }
83
84 void
85 Audio_span_dynamic::add_absolute (Audio_dynamic *d)
86 {
87   assert (d);
88   dynamics_.push_back (d);
89 }
90
91 Moment
92 remap_grace_duration (Moment m)
93 {
94   return Moment (m.main_part_ + Rational (9,40) * m.grace_part_,
95                  Rational (0));
96 }
97
98 Real
99 moment_to_real (Moment m)
100 {
101   return remap_grace_duration (m).main_part_.to_double ();
102 }
103
104 int
105 moment_to_ticks (Moment m)
106 {
107   return int (moment_to_real (m) * 384 * 4);
108 }
109
110 void
111 Audio_span_dynamic::render ()
112 {
113   if (dynamics_.size () <= 1)
114     return ;
115
116   assert (dynamics_[0]->volume_ >= 0);
117
118   while  (dynamics_.back ()->volume_ > 0
119           && dynamics_.size () > 1
120           && sign (dynamics_.back ()->volume_ - dynamics_[0]->volume_) != grow_dir_)
121     {
122       dynamics_.erase (dynamics_.end () - 1);
123     }
124
125   if (dynamics_.size () <= 1)
126     {
127       programming_error ("Impossible or ambiguous (de)crescendo in MIDI.");
128       return ;
129     }
130
131   Real delta_v = grow_dir_ * 0.1;
132
133   Real start_v = dynamics_[0]->volume_;
134   if (dynamics_.back ()->volume_ < 0)
135     dynamics_.back ()->volume_ = max (min (start_v + grow_dir_ * 0.25, 1.0), 0.1);
136
137   delta_v = dynamics_.back ()->volume_ - dynamics_[0]->volume_;
138
139   Moment start = dynamics_[0]->get_column ()->when ();
140
141   Real total_t = moment_to_real (dynamics_.back ()->get_column ()->when () - start);
142
143   for (vsize i = 1; i < dynamics_.size (); i ++)
144     {
145       Moment dt_moment = dynamics_[i]->get_column ()->when ()
146         - start;
147
148       Real dt =  moment_to_real (dt_moment);
149
150       Real v = start_v + delta_v *  (dt / total_t);
151
152       dynamics_[i]->volume_ = v;
153     }
154 }
155
156
157
158 Audio_tempo::Audio_tempo (int per_minute_4)
159 {
160   per_minute_4_ = per_minute_4;
161 }
162
163 Audio_time_signature::Audio_time_signature (int beats, int one_beat)
164 {
165   beats_ = beats;
166   one_beat_ = one_beat;
167 }
168
169 Audio_text::Audio_text (Audio_text::Type type, string text_string)
170 {
171   text_string_ = text_string;
172   type_ = type;
173 }
174