]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-walker.cc
Run grand-replace (issue 3765)
[lilypond.git] / lily / midi-walker.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2014 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   //Pieces that begin with grace notes start at negative times. This
62   //is OK - MIDI output doesn't use absolute ticks, only differences.
63   last_tick_ = items_.empty () ? 0 : items_[0]->audio_column_->ticks ();
64   percussion_ = audio_staff->percussion_;
65   merge_unisons_ = audio_staff->merge_unisons_;
66 }
67
68 Midi_walker::~Midi_walker ()
69 {
70   junk_pointers (midi_events_);
71 }
72
73 void
74 Midi_walker::finalize ()
75 {
76   do_stop_notes (INT_MAX);
77 }
78
79 /**
80    Find out if start_note event is needed, and do it if needed.
81 */
82 void
83 Midi_walker::do_start_note (Midi_note *note)
84 {
85   Audio_item *ptr = items_[index_];
86   assert (note->audio_ == ptr);
87   int now_ticks = ptr->audio_column_->ticks ();
88   int stop_ticks = int (moment_to_real (note->audio_->length_mom_) *
89                         Real (384 * 4)) + now_ticks;
90   for (vsize i = 0; i < stop_note_queue.size (); i++)
91     {
92       /* if this pitch already in queue */
93       if (stop_note_queue[i].val->get_semitone_pitch ()
94           == note->get_semitone_pitch ())
95         {
96           int queued_ticks
97             = stop_note_queue[i].val->audio_->audio_column_->ticks ();
98           // If the two notes started at the same time, or option is set,
99           if (now_ticks == queued_ticks || merge_unisons_)
100             {
101               // merge them.
102               if (stop_note_queue[i].key < stop_ticks)
103                 {
104                   Midi_note_event e;
105                   e.val = stop_note_queue[i].val;
106                   e.key = stop_ticks;
107                   stop_note_queue[i].ignore_ = true;
108                   stop_note_queue.insert (e);
109                 }
110               note = 0;
111               break;
112             }
113           else
114             {
115               // A note was played that interruped a played note.
116               // Stop the old note, and continue to the greatest moment
117               // between the two.
118               if (stop_note_queue[i].key > stop_ticks)
119                 {
120                   stop_ticks = stop_note_queue[i].key;
121                 }
122               output_event (now_ticks, stop_note_queue[i].val);
123               stop_note_queue[i].ignore_ = true;
124               break;
125             }
126         }
127     }
128
129   if (note)
130     {
131       Midi_note_event e;
132       e.val = new Midi_note_off (note);
133
134       midi_events_.push_back (e.val);
135       e.key = stop_ticks;
136       stop_note_queue.insert (e);
137
138       output_event (now_ticks, note);
139     }
140 }
141
142 void
143 Midi_walker::do_stop_notes (int max_ticks)
144 {
145   while (stop_note_queue.size () && stop_note_queue.front ().key <= max_ticks)
146     {
147       Midi_note_event e = stop_note_queue.get ();
148       if (e.ignore_)
149         {
150           continue;
151         }
152
153       int stop_ticks = e.key;
154       Midi_note *note = e.val;
155
156       output_event (stop_ticks, note);
157     }
158 }
159
160 void
161 Midi_walker::output_event (int now_ticks, Midi_item *l)
162 {
163   int delta_ticks = now_ticks - last_tick_;
164   last_tick_ = now_ticks;
165
166   /*
167     this is not correct, but at least it doesn't crash when you
168     start with graces
169   */
170   if (delta_ticks < 0)
171     {
172       programming_error ("Going back in MIDI time.");
173       delta_ticks = 0;
174     }
175
176   track_->add (delta_ticks, l);
177 }
178
179 void
180 Midi_walker::process ()
181 {
182   Audio_item *audio = items_[index_];
183   Audio_column *col = audio->get_column ();
184   do_stop_notes (col->ticks ());
185
186   if (Midi_item *midi = get_midi (audio))
187     {
188       if (Midi_note *note = dynamic_cast<Midi_note *> (midi))
189         {
190           if (note->audio_->length_mom_.to_bool ())
191             do_start_note (note);
192         }
193       else
194         output_event (audio->audio_column_->ticks (), midi);
195     }
196 }
197
198 Midi_item *
199 Midi_walker::get_midi (Audio_item *i)
200 {
201   Midi_item *mi = Midi_item::get_midi (i);
202
203   if (percussion_)
204     if (Midi_channel_item *mci = dynamic_cast<Midi_channel_item *> (mi))
205       mci->channel_ = 9;
206
207   midi_events_.push_back (mi);
208   return mi;
209 }
210
211 bool
212 Midi_walker::ok () const
213 {
214   return index_ < items_.size ();
215 }
216
217 void
218 Midi_walker::operator ++(int)
219 {
220   assert (ok ());
221   index_++;
222 }