]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-walker.cc
Performance::remap_grace_durations(): robust way of handling grace
[lilypond.git] / lily / midi-walker.cc
1 /*
2   midi-walker.cc -- implement Midi_walker
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7   Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 #include "midi-walker.hh"
11
12 #include "audio-column.hh"
13 #include "audio-staff.hh"
14 #include "midi-item.hh"
15 #include "midi-stream.hh"
16 #include "warn.hh"
17
18 Midi_note_event::Midi_note_event ()
19 {
20   ignore_b_ = false;
21 }
22
23 int
24 compare (Midi_note_event const &left, Midi_note_event const &right)
25 {
26   Moment m = (left.key - right.key);
27
28   if (m < 0)
29     return -1;
30   else if (m > 0)
31     return 1;
32   else
33     return 0;
34 }
35
36 Midi_walker::Midi_walker (Audio_staff *audio_staff, Midi_track *track,
37                           int channel)
38 {
39   channel_ = channel;
40   track_ = track;
41   index_ = 0;
42   items_ = &audio_staff->audio_items_;
43
44   last_mom_ = 0;
45 }
46
47 Midi_walker::~Midi_walker ()
48 {
49   // ugh
50   do_stop_notes (last_mom_ + Moment (Rational (10, 1)));
51 }
52
53 /**
54    Find out if start_note event is needed, and do it if needed.
55 */
56 void
57 Midi_walker::do_start_note (Midi_note *note)
58 {
59   Audio_item *ptr = (*items_)[index_];
60   Moment stop_mom = note->get_length () + ptr->audio_column_->when ();
61
62   bool play_start = true;
63   for (vsize i = 0; i < stop_note_queue.size (); i++)
64     {
65       /* if this pith already in queue */
66       if (stop_note_queue[i].val->get_semitone_pitch ()
67           == note->get_semitone_pitch ())
68         {
69           if (stop_note_queue[i].key < stop_mom)
70             {
71               /* let stopnote in queue be ignored,
72                  new stop note wins */
73               stop_note_queue[i].ignore_b_ = true;
74               /* don't replay start note, */
75               play_start = false;
76               break;
77             }
78           else
79             {
80               /* skip this stopnote,
81                  don't play the start note */
82               delete note;
83               note = 0;
84               break;
85             }
86         }
87     }
88
89   if (note)
90     {
91       Midi_note_event e;
92       e.val = new Midi_note_off (note);
93       e.key = stop_mom;
94       stop_note_queue.insert (e);
95
96       if (play_start)
97         output_event (ptr->audio_column_->when (), note);
98     }
99 }
100
101 /**
102    Output note events for all notes which end before #max_mom#
103 */
104 void
105 Midi_walker::do_stop_notes (Moment max_mom)
106 {
107   while (stop_note_queue.size () && stop_note_queue.front ().key <= max_mom)
108     {
109       Midi_note_event e = stop_note_queue.get ();
110       if (e.ignore_b_)
111         {
112           delete e.val;
113           continue;
114         }
115
116       Moment stop_mom = e.key;
117       Midi_note *note = e.val;
118
119       output_event (stop_mom, note);
120     }
121 }
122
123 /**
124    Advance the track to #now#, output the item, and adjust current "moment".
125 */
126 void
127 Midi_walker::output_event (Moment now_mom, Midi_item *l)
128 {
129   Moment delta_t = now_mom - last_mom_;
130   last_mom_ = now_mom;
131
132   /*
133     this is not correct, but at least it doesn't crash when you
134     start with graces
135   */
136   if (delta_t < Moment (0))
137     delta_t = Moment (0);
138
139   track_->add (delta_t, l);
140 }
141
142 void
143 Midi_walker::process ()
144 {
145   Audio_item *audio = (*items_)[index_];
146   do_stop_notes (audio->audio_column_->when ());
147
148   if (Midi_item *midi = Midi_item::get_midi (audio))
149     {
150       if (Midi_channel_item *mci = dynamic_cast<Midi_channel_item*> (midi))
151         mci->channel_ = channel_;
152       
153       //midi->channel_ = track_->number_;
154       if (Midi_note *note = dynamic_cast<Midi_note *> (midi))
155         {
156           if (note->get_length ().to_bool ())
157             do_start_note (note);
158         }
159       else
160         output_event (audio->audio_column_->when (), midi);
161     }
162 }
163
164 bool
165 Midi_walker::ok () const
166 {
167   return index_ < items_->size ();
168 }
169
170 void
171 Midi_walker::operator ++ (int)
172 {
173   assert (ok ());
174   index_++;
175 }