]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-walker.cc
release: 1.5.1
[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--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7          Jan Nieuwenhuizen <janneke@gnu.org>
8  */
9
10 #include "midi-walker.hh"
11 #include "audio-column.hh"
12 #include "audio-item.hh"
13 #include "audio-staff.hh"
14 #include "midi-item.hh"
15 #include "midi-stream.hh"
16 #include "debug.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   return sign (left.key - right.key);
27 }
28
29 Midi_walker::Midi_walker (Audio_staff* audio_staff_l, Midi_track* track_l)
30 {
31   track_l_ = track_l;
32   index_= 0;
33   item_l_arr_l_ = &audio_staff_l->audio_item_l_arr_;
34
35   last_mom_ = 0;
36 }
37
38 Midi_walker::~Midi_walker ()
39
40   // ugh
41   do_stop_notes (last_mom_ + Moment (10, 1));
42 }
43
44 /**
45   Find out if start_note event is needed, and do it if needed.
46  */
47 void 
48 Midi_walker::do_start_note (Midi_note* note_p)
49 {
50   Audio_item* ptr = (*item_l_arr_l_)[index_];
51   Moment stop_mom = note_p->length_mom () + ptr->audio_column_l_->at_mom ();
52
53   bool play_start = true;
54   for (int i=0; i < stop_note_queue.size (); i++) 
55     {
56       /* if this pith already in queue */
57       if (stop_note_queue[i].val->pitch_i () == note_p->pitch_i ()) 
58         {
59           if (stop_note_queue[i].key < stop_mom)
60             {
61               /* let stopnote in queue be ignored,
62                new stop note wins */
63               stop_note_queue[i].ignore_b_ = true;
64               /* don't replay start note, */
65               play_start = false;
66               break;
67             }
68           else
69             {
70               /* skip this stopnote,
71                  don't play the start note */
72               delete note_p;
73               note_p = 0;
74               break;
75           }
76         }
77     }
78
79   if (note_p)
80     {
81       Midi_note_event e;
82       e.val = new Midi_note_off (note_p);
83       e.key = stop_mom;
84       stop_note_queue.insert (e);
85
86       if (play_start)
87         output_event (ptr->audio_column_l_->at_mom (), note_p);
88     }
89 }
90
91 /**
92   Output note events for all notes which end before #max_mom#
93  */
94 void
95 Midi_walker::do_stop_notes (Moment max_mom)
96 {
97   while (stop_note_queue.size () && stop_note_queue.front ().key <= max_mom) 
98     {
99       Midi_note_event e = stop_note_queue.get ();
100       if (e.ignore_b_)
101         {
102           delete e.val;
103           continue;
104         }
105       
106       Moment stop_mom = e.key;
107       Midi_note* note_p = e.val;
108         
109       output_event (stop_mom, note_p);
110     }
111 }
112
113 /** 
114   Advance the track to #now#, output the item, and adjust current "moment". 
115  */
116 void
117 Midi_walker::output_event (Moment now_mom, Midi_item* l)
118 {
119   Moment delta_t = now_mom - last_mom_ ;
120   last_mom_ = now_mom;
121
122   /*
123     this is not correct, but at least it doesn't crash when you
124     start with graces
125    */
126   if (delta_t < Moment(0))
127     {
128       delta_t = Moment (0);
129     }
130
131   
132   track_l_->add (delta_t, l);
133 }
134
135 void
136 Midi_walker::process ()
137 {
138   Audio_item* audio_p = (*item_l_arr_l_)[index_];
139   do_stop_notes (audio_p->audio_column_l_->at_mom ());
140
141   if (Midi_item* midi_p = Midi_item::midi_p (audio_p))
142     {
143       midi_p->channel_i_ = track_l_->channel_i_;
144       //midi_p->channel_i_ = track_l_->number_i_;
145       if (Midi_note* note_p = dynamic_cast<Midi_note*> (midi_p))
146         {
147           if (note_p->length_mom ())
148             do_start_note (note_p);
149         }
150       else
151         output_event (audio_p->audio_column_l_->at_mom (), midi_p);
152     }
153 }
154
155 bool
156 Midi_walker::ok () const
157 {
158   return index_ <item_l_arr_l_->size ();
159 }
160
161 void
162 Midi_walker::operator ++ (int)
163 {
164   assert (ok ());
165   index_++;
166 }