]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-walker.cc
also delete note stop events.
[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_ = 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 bool
37 audio_item_less (Audio_item * const a,
38                  Audio_item * const b)
39 {
40   return a->get_column ()->when_ <  b->get_column ()->when_;
41 }
42
43 Midi_walker::Midi_walker (Audio_staff *audio_staff, Midi_track *track,
44                           int channel)
45 {
46   channel_ = channel;
47   track_ = track;
48   index_ = 0;
49   items_ = audio_staff->audio_items_;
50   vector_sort (items_, audio_item_less);
51   last_tick_ = 0;
52 }
53
54 Midi_walker::~Midi_walker ()
55 {
56   junk_pointers (midi_events_);
57 }
58
59 void
60 Midi_walker::finalize ()
61 {
62   do_stop_notes (INT_MAX);
63 }
64
65 /**
66    Find out if start_note event is needed, and do it if needed.
67 */
68 void
69 Midi_walker::do_start_note (Midi_note *note)
70 {
71   Audio_item *ptr = items_[index_];
72   int stop_ticks = int (moment_to_real (note->audio_->length_mom_) * Real (384 * 4))
73     + ptr->audio_column_->ticks ();
74
75   bool play_start = true;
76   for (vsize i = 0; i < stop_note_queue.size (); i++)
77     {
78       /* if this pith already in queue */
79       if (stop_note_queue[i].val->get_semitone_pitch ()
80           == note->get_semitone_pitch ())
81         {
82           if (stop_note_queue[i].key < stop_ticks)
83             {
84               /* let stopnote in queue be ignored,
85                  new stop note wins */
86               stop_note_queue[i].ignore_ = true;
87
88               /* don't replay start note, */
89               play_start = false;
90               break;
91             }
92           else
93             {
94               /* skip this stopnote,
95                  don't play the start note */
96               note = 0;
97               break;
98             }
99         }
100     }
101
102   if (note)
103     {
104       Midi_note_event e;
105       e.val = new Midi_note_off (note);
106
107       midi_events_.push_back (e.val);
108       e.key = int (stop_ticks);
109       stop_note_queue.insert (e);
110
111       if (play_start)
112         output_event (ptr->audio_column_->ticks (), note);
113     }
114 }
115
116 /**
117    Output note events for all notes which end before #max_mom#
118 */
119 void
120 Midi_walker::do_stop_notes (int max_ticks)
121 {
122   while (stop_note_queue.size () && stop_note_queue.front ().key <= max_ticks)
123     {
124       Midi_note_event e = stop_note_queue.get ();
125       if (e.ignore_)
126         {
127           continue;
128         }
129
130       int stop_ticks = e.key;
131       Midi_note *note = e.val;
132
133       output_event (stop_ticks, note);
134     }
135 }
136
137 void
138 Midi_walker::output_event (int now_ticks, Midi_item *l)
139 {
140   int delta_ticks = now_ticks - last_tick_;
141   last_tick_ = now_ticks;
142
143   /*
144     this is not correct, but at least it doesn't crash when you
145     start with graces
146   */
147   if (delta_ticks < 0)
148     {
149       programming_error ("Going back in MIDI time.");
150       delta_ticks = 0;
151     }
152
153   track_->add (delta_ticks, l);
154 }
155
156 void
157 Midi_walker::process ()
158 {
159   Audio_item *audio = items_[index_];
160   do_stop_notes (audio->audio_column_->ticks ());
161
162   if (Midi_item *midi = get_midi (audio))
163     {
164       if (Midi_channel_item *mci = dynamic_cast<Midi_channel_item*> (midi))
165         mci->channel_ = channel_;
166       
167       if (Midi_note *note = dynamic_cast<Midi_note *> (midi))
168         {
169           if (note->audio_->length_mom_.to_bool ())
170             do_start_note (note);
171         }
172       else
173         output_event (audio->audio_column_->ticks (), midi);
174     }
175 }
176
177 Midi_item*
178 Midi_walker::get_midi (Audio_item *i)
179 {
180   Midi_item *mi = Midi_item::get_midi (i);
181   midi_events_.push_back (mi);
182   return mi;
183 }
184
185 bool
186 Midi_walker::ok () const
187 {
188   return index_ < items_.size ();
189 }
190
191 void
192 Midi_walker::operator ++ (int)
193 {
194   assert (ok ());
195   index_++;
196 }