]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-walker.cc
2979c9380ce1127a3d6687180840ca1a70315730
[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--2006 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_->at_mom ();
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_pitch () == note->get_pitch ())
67         {
68           if (stop_note_queue[i].key < stop_mom)
69             {
70               /* let stopnote in queue be ignored,
71                  new stop note wins */
72               stop_note_queue[i].ignore_b_ = true;
73               /* don't replay start note, */
74               play_start = false;
75               break;
76             }
77           else
78             {
79               /* skip this stopnote,
80                  don't play the start note */
81               delete note;
82               note = 0;
83               break;
84             }
85         }
86     }
87
88   if (note)
89     {
90       Midi_note_event e;
91       e.val = new Midi_note_off (note);
92       e.key = stop_mom;
93       stop_note_queue.insert (e);
94
95       if (play_start)
96         output_event (ptr->audio_column_->at_mom (), note);
97     }
98 }
99
100 /**
101    Output note events for all notes which end before #max_mom#
102 */
103 void
104 Midi_walker::do_stop_notes (Moment max_mom)
105 {
106   while (stop_note_queue.size () && stop_note_queue.front ().key <= max_mom)
107     {
108       Midi_note_event e = stop_note_queue.get ();
109       if (e.ignore_b_)
110         {
111           delete e.val;
112           continue;
113         }
114
115       Moment stop_mom = e.key;
116       Midi_note *note = e.val;
117
118       output_event (stop_mom, note);
119     }
120 }
121
122 /**
123    Advance the track to #now#, output the item, and adjust current "moment".
124 */
125 void
126 Midi_walker::output_event (Moment now_mom, Midi_item *l)
127 {
128   Moment delta_t = now_mom - last_mom_;
129   last_mom_ = now_mom;
130
131   /*
132     this is not correct, but at least it doesn't crash when you
133     start with graces
134   */
135   if (delta_t < Moment (0))
136     delta_t = Moment (0);
137
138   track_->add (delta_t, l);
139 }
140
141 void
142 Midi_walker::process ()
143 {
144   Audio_item *audio = (*items_)[index_];
145   do_stop_notes (audio->audio_column_->at_mom ());
146
147   if (Midi_item *midi = Midi_item::get_midi (audio))
148     {
149       if (Midi_channel_item *mci = dynamic_cast<Midi_channel_item*> (midi))
150         mci->channel_ = channel_;
151       
152       //midi->channel_ = track_->number_;
153       if (Midi_note *note = dynamic_cast<Midi_note *> (midi))
154         {
155           if (note->get_length ().to_bool ())
156             do_start_note (note);
157         }
158       else
159         output_event (audio->audio_column_->at_mom (), midi);
160     }
161 }
162
163 bool
164 Midi_walker::ok () const
165 {
166   return index_ < items_->size ();
167 }
168
169 void
170 Midi_walker::operator ++ (int)
171 {
172   assert (ok ());
173   index_++;
174 }