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