lilypond-1.1.9
[lilypond.git] / lily / midi-walker.cc
blob1af44cd1fb65ee70d441b6c628aa1b97f1cd8ef2
1 /*
2 midi-walker.cc -- implement Midi_walker
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--1998 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 return sign (left.key - right.key);
29 Midi_walker::Midi_walker (Audio_staff* audio_staff_l, Midi_track* track_l)
30 : PCursor<Audio_item*>(audio_staff_l->audio_item_l_list_)
32 track_l_ = track_l;
33 last_mom_ = 0;
36 Midi_walker::~Midi_walker()
38 // ugh
39 do_stop_notes (last_mom_ + Moment (10, 1));
42 /**
43 Find out if start_note event is needed, and do it if needed.
45 void
46 Midi_walker::do_start_note (Midi_note* note_p)
48 Moment stop_mom = note_p->duration() + ptr ()->audio_column_l_->at_mom ();
49 for (int i=0; i < stop_note_queue.size(); i++)
51 if (stop_note_queue[i].val->pitch_i() == note_p->pitch_i ())
53 if (stop_note_queue[i].key < stop_mom)
54 stop_note_queue[i].ignore_b_ = true;
55 else {
56 // skip the stopnote
57 delete note_p;
58 return;
63 Midi_note_event e;
64 e.val = new Midi_note_off (note_p);
65 e.key = stop_mom;
66 stop_note_queue.insert (e);
68 output_event (ptr()->audio_column_l_->at_mom (), note_p);
71 /**
72 Output note events for all notes which end before #max_mom#
74 void
75 Midi_walker::do_stop_notes (Moment max_mom)
77 while (stop_note_queue.size() && stop_note_queue.front ().key <= max_mom)
79 Midi_note_event e = stop_note_queue.get();
80 if (e.ignore_b_)
82 delete e.val;
83 continue;
86 Moment stop_mom = e.key;
87 Midi_note_off* note_p = e.val;
89 output_event (stop_mom, note_p);
93 /**
94 Advance the track to #now#, output the item, and adjust current "moment".
96 void
97 Midi_walker::output_event (Moment now_mom, Midi_item* l)
99 Moment delta_t = now_mom - last_mom_ ;
100 last_mom_ += delta_t;
101 track_l_->add (delta_t, l);
104 void
105 Midi_walker::process()
107 do_stop_notes (ptr()->audio_column_l_->at_mom ());
109 Midi_item* p = ptr()->midi_item_p ();
110 if (!p)
111 return;
112 p->channel_i_ = track_l_->number_i_;
114 if (Midi_note *mi = dynamic_cast<Midi_note*>(p))
115 do_start_note (mi);
116 else
117 output_event (ptr()->audio_column_l_->at_mom (), p);