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