]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-walker.cc
Run grand-replace for 2012
[lilypond.git] / lily / midi-walker.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
5   Jan Nieuwenhuizen <janneke@gnu.org>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "midi-walker.hh"
22
23 #include "audio-column.hh"
24 #include "audio-staff.hh"
25 #include "midi-item.hh"
26 #include "midi-chunk.hh"
27 #include "midi-stream.hh"
28 #include "warn.hh"
29
30 Midi_note_event::Midi_note_event ()
31 {
32   ignore_ = false;
33 }
34
35 int
36 compare (Midi_note_event const &left, Midi_note_event const &right)
37 {
38   Moment m = (left.key - right.key);
39
40   if (m < 0)
41     return -1;
42   else if (m > 0)
43     return 1;
44   else
45     return 0;
46 }
47
48 bool
49 audio_item_less (Audio_item *const a,
50                  Audio_item *const b)
51 {
52   return a->get_column ()->when_ < b->get_column ()->when_;
53 }
54
55 Midi_walker::Midi_walker (Audio_staff *audio_staff, Midi_track *track)
56 {
57   track_ = track;
58   index_ = 0;
59   items_ = audio_staff->audio_items_;
60   vector_sort (items_, audio_item_less);
61   last_tick_ = 0;
62   percussion_ = audio_staff->percussion_;
63   merge_unisons_ = audio_staff->merge_unisons_;
64 }
65
66 Midi_walker::~Midi_walker ()
67 {
68   junk_pointers (midi_events_);
69 }
70
71 void
72 Midi_walker::finalize ()
73 {
74   do_stop_notes (INT_MAX);
75 }
76
77 /**
78    Find out if start_note event is needed, and do it if needed.
79 */
80 void
81 Midi_walker::do_start_note (Midi_note *note)
82 {
83   Audio_item *ptr = items_[index_];
84   assert (note->audio_ == ptr);
85   int now_ticks = ptr->audio_column_->ticks ();
86   int stop_ticks = int (moment_to_real (note->audio_->length_mom_) *
87                         Real (384 * 4)) + now_ticks;
88   for (vsize i = 0; i < stop_note_queue.size (); i++)
89     {
90       /* if this pitch already in queue */
91       if (stop_note_queue[i].val->get_semitone_pitch ()
92           == note->get_semitone_pitch ())
93         {
94           int queued_ticks
95             = stop_note_queue[i].val->audio_->audio_column_->ticks ();
96           // If the two notes started at the same time, or option is set,
97           if (now_ticks == queued_ticks || merge_unisons_)
98             {
99               // merge them.
100               if (stop_note_queue[i].key < stop_ticks)
101                 {
102                   Midi_note_event e;
103                   e.val = stop_note_queue[i].val;
104                   e.key = stop_ticks;
105                   stop_note_queue[i].ignore_ = true;
106                   stop_note_queue.insert (e);
107                 }
108               note = 0;
109               break;
110             }
111           else
112             {
113               // A note was played that interruped a played note.
114               // Stop the old note, and continue to the greatest moment
115               // between the two.
116               if (stop_note_queue[i].key > stop_ticks)
117                 {
118                   stop_ticks = stop_note_queue[i].key;
119                 }
120               output_event (now_ticks, stop_note_queue[i].val);
121               stop_note_queue[i].ignore_ = true;
122               break;
123             }
124         }
125     }
126
127   if (note)
128     {
129       Midi_note_event e;
130       e.val = new Midi_note_off (note);
131
132       midi_events_.push_back (e.val);
133       e.key = stop_ticks;
134       stop_note_queue.insert (e);
135
136       output_event (now_ticks, note);
137     }
138 }
139
140 void
141 Midi_walker::do_stop_notes (int max_ticks)
142 {
143   while (stop_note_queue.size () && stop_note_queue.front ().key <= max_ticks)
144     {
145       Midi_note_event e = stop_note_queue.get ();
146       if (e.ignore_)
147         {
148           continue;
149         }
150
151       int stop_ticks = e.key;
152       Midi_note *note = e.val;
153
154       output_event (stop_ticks, note);
155     }
156 }
157
158 void
159 Midi_walker::output_event (int now_ticks, Midi_item *l)
160 {
161   int delta_ticks = now_ticks - last_tick_;
162   last_tick_ = now_ticks;
163
164   /*
165     this is not correct, but at least it doesn't crash when you
166     start with graces
167   */
168   if (delta_ticks < 0)
169     {
170       programming_error ("Going back in MIDI time.");
171       delta_ticks = 0;
172     }
173
174   track_->add (delta_ticks, l);
175 }
176
177 void
178 Midi_walker::process ()
179 {
180   Audio_item *audio = items_[index_];
181   do_stop_notes (audio->audio_column_->ticks ());
182
183   if (Midi_item *midi = get_midi (audio))
184     {
185       if (Midi_note *note = dynamic_cast<Midi_note *> (midi))
186         {
187           if (note->audio_->length_mom_.to_bool ())
188             do_start_note (note);
189         }
190       else
191         output_event (audio->audio_column_->ticks (), midi);
192     }
193 }
194
195 Midi_item *
196 Midi_walker::get_midi (Audio_item *i)
197 {
198   Midi_item *mi = Midi_item::get_midi (i);
199
200   if (percussion_)
201     if (Midi_channel_item *mci = dynamic_cast<Midi_channel_item *> (mi))
202       mci->channel_ = 9;
203
204   midi_events_.push_back (mi);
205   return mi;
206 }
207
208 bool
209 Midi_walker::ok () const
210 {
211   return index_ < items_.size ();
212 }
213
214 void
215 Midi_walker::operator ++(int)
216 {
217   assert (ok ());
218   index_++;
219 }