lilypond-1.5.10
[lilypond.git] / lily / midi-walker.cc
blob6a568b43fce3a393d1edd0ac72e0c7011355dcc4
1 /*
2 midi-walker.cc -- implement Midi_walker
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
8 */
10 #include "midi-walker.hh"
11 #include "audio-column.hh"
12 #include "audio-item.hh"
13 #include "audio-staff.hh"
14 #include "midi-item.hh"
15 #include "midi-stream.hh"
16 #include "debug.hh"
18 Midi_note_event::Midi_note_event ()
20 ignore_b_ = false;
23 int
24 compare (Midi_note_event const& left, Midi_note_event const& right)
26 Moment m = (left.key - right.key);
28 if (m<0)
29 return -1;
30 else if (m > 0)
31 return 1;
32 else
33 return 0;
36 Midi_walker::Midi_walker (Audio_staff* audio_staff_l, Midi_track* track_l)
38 track_l_ = track_l;
39 index_= 0;
40 item_l_arr_l_ = &audio_staff_l->audio_item_l_arr_;
42 last_mom_ = 0;
45 Midi_walker::~Midi_walker ()
47 // ugh
48 do_stop_notes (last_mom_ + Moment (Rational (10, 1)));
51 /**
52 Find out if start_note event is needed, and do it if needed.
54 void
55 Midi_walker::do_start_note (Midi_note* note_p)
57 Audio_item* ptr = (*item_l_arr_l_)[index_];
58 Moment stop_mom = note_p->length_mom () + ptr->audio_column_l_->at_mom ();
60 bool play_start = true;
61 for (int i=0; i < stop_note_queue.size (); i++)
63 /* if this pith already in queue */
64 if (stop_note_queue[i].val->pitch_i () == note_p->pitch_i ())
66 if (stop_note_queue[i].key < stop_mom)
68 /* let stopnote in queue be ignored,
69 new stop note wins */
70 stop_note_queue[i].ignore_b_ = true;
71 /* don't replay start note, */
72 play_start = false;
73 break;
75 else
77 /* skip this stopnote,
78 don't play the start note */
79 delete note_p;
80 note_p = 0;
81 break;
86 if (note_p)
88 Midi_note_event e;
89 e.val = new Midi_note_off (note_p);
90 e.key = stop_mom;
91 stop_note_queue.insert (e);
93 if (play_start)
94 output_event (ptr->audio_column_l_->at_mom (), note_p);
98 /**
99 Output note events for all notes which end before #max_mom#
101 void
102 Midi_walker::do_stop_notes (Moment max_mom)
104 while (stop_note_queue.size () && stop_note_queue.front ().key <= max_mom)
106 Midi_note_event e = stop_note_queue.get ();
107 if (e.ignore_b_)
109 delete e.val;
110 continue;
113 Moment stop_mom = e.key;
114 Midi_note* note_p = e.val;
116 output_event (stop_mom, note_p);
120 /**
121 Advance the track to #now#, output the item, and adjust current "moment".
123 void
124 Midi_walker::output_event (Moment now_mom, Midi_item* l)
126 Moment delta_t = now_mom - last_mom_ ;
127 last_mom_ = now_mom;
130 this is not correct, but at least it doesn't crash when you
131 start with graces
133 if (delta_t < Moment(0))
135 delta_t = Moment (0);
139 track_l_->add (delta_t, l);
142 void
143 Midi_walker::process ()
145 Audio_item* audio_p = (*item_l_arr_l_)[index_];
146 do_stop_notes (audio_p->audio_column_l_->at_mom ());
148 if (Midi_item* midi_p = Midi_item::midi_p (audio_p))
150 midi_p->channel_i_ = track_l_->channel_i_;
151 //midi_p->channel_i_ = track_l_->number_i_;
152 if (Midi_note* note_p = dynamic_cast<Midi_note*> (midi_p))
154 if (note_p->length_mom ().to_bool ())
155 do_start_note (note_p);
157 else
158 output_event (audio_p->audio_column_l_->at_mom (), midi_p);
162 bool
163 Midi_walker::ok () const
165 return index_ <item_l_arr_l_->size ();
168 void
169 Midi_walker::operator ++ (int)
171 assert (ok ());
172 index_++;