2 midi-walker.cc -- implement Midi_walker
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
10 #include "midi-walker.hh"
12 #include "audio-column.hh"
13 #include "audio-staff.hh"
14 #include "midi-item.hh"
15 #include "midi-stream.hh"
18 Midi_note_event::Midi_note_event ()
24 compare (Midi_note_event const &left, Midi_note_event const &right)
26 Moment m = (left.key - right.key);
37 audio_item_less (Audio_item * const a,
40 return a->get_column ()->when_ < b->get_column ()->when_;
43 Midi_walker::Midi_walker (Audio_staff *audio_staff, Midi_track *track,
49 items_ = audio_staff->audio_items_;
50 vector_sort (items_, audio_item_less);
54 Midi_walker::~Midi_walker ()
56 do_stop_notes (last_tick_ + 384);
58 for (vsize i = 0; i < midi_items_.size (); i++)
59 delete midi_items_[i];
63 Find out if start_note event is needed, and do it if needed.
66 Midi_walker::do_start_note (Midi_note *note)
68 Audio_item *ptr = items_[index_];
69 int stop_ticks = int (moment_to_real (note->audio_->length_mom_) * Real (384 * 4))
70 + ptr->audio_column_->ticks ();
72 bool play_start = true;
73 for (vsize i = 0; i < stop_note_queue.size (); i++)
75 /* if this pith already in queue */
76 if (stop_note_queue[i].val->get_semitone_pitch ()
77 == note->get_semitone_pitch ())
79 if (stop_note_queue[i].key < stop_ticks)
81 /* let stopnote in queue be ignored,
83 stop_note_queue[i].ignore_b_ = true;
84 /* don't replay start note, */
90 /* skip this stopnote,
91 don't play the start note */
102 e.val = new Midi_note_off (note);
103 e.key = int (stop_ticks);
104 stop_note_queue.insert (e);
107 output_event (ptr->audio_column_->ticks (), note);
109 midi_items_.push_back (e.val);
114 Output note events for all notes which end before #max_mom#
117 Midi_walker::do_stop_notes (int max_ticks)
119 while (stop_note_queue.size () && stop_note_queue.front ().key <= max_ticks)
121 Midi_note_event e = stop_note_queue.get ();
128 int stop_ticks = e.key;
129 Midi_note *note = e.val;
131 output_event (stop_ticks, note);
136 Advance the track to #now#, output the item, and adjust current "moment".
139 Midi_walker::output_event (int now_ticks, Midi_item *l)
141 int delta_ticks = now_ticks - last_tick_;
142 last_tick_ = now_ticks;
145 this is not correct, but at least it doesn't crash when you
150 programming_error ("Going back in MIDI time.");
154 track_->add (delta_ticks, l);
158 Midi_walker::process ()
160 Audio_item *audio = items_[index_];
161 do_stop_notes (audio->audio_column_->ticks ());
163 if (Midi_item *midi = Midi_item::get_midi (audio))
165 if (Midi_channel_item *mci = dynamic_cast<Midi_channel_item*> (midi))
166 mci->channel_ = channel_;
168 //midi->channel_ = track_->number_;
169 if (Midi_note *note = dynamic_cast<Midi_note *> (midi))
171 if (note->audio_->length_mom_.to_bool ())
172 do_start_note (note);
175 output_event (audio->audio_column_->ticks (), midi);
178 midi_items_.push_back (midi);
183 Midi_walker::ok () const
185 return index_ < items_.size ();
189 Midi_walker::operator ++ (int)