]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-walker.cc
release: 1.1.53
[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--1999 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   last_mom_ = 0;
35 }
36
37 Midi_walker::~Midi_walker()
38
39   // ugh
40   do_stop_notes (last_mom_ + Moment (10, 1));
41 }
42
43 /**
44   Find out if start_note event is needed, and do it if needed.
45  */
46 void 
47 Midi_walker::do_start_note (Midi_note* note_p)
48 {
49   Audio_item* ptr = (*item_l_arr_l_)[index_];
50   Moment stop_mom = note_p->length_mom () + ptr->audio_column_l_->at_mom ();
51   for (int i=0; i < stop_note_queue.size(); i++) 
52     {
53       if (stop_note_queue[i].val->pitch_i() == note_p->pitch_i ()) 
54         {
55           if (stop_note_queue[i].key < stop_mom)
56             stop_note_queue[i].ignore_b_ = true;
57           else {
58             // skip the stopnote
59             delete note_p;
60             return;
61           }
62         }
63     }
64
65   Midi_note_event e;
66   e.val = new Midi_note_off (note_p);
67   e.key = stop_mom;
68   stop_note_queue.insert (e);
69   
70   output_event (ptr->audio_column_l_->at_mom (), note_p);
71 }
72
73 /**
74   Output note events for all notes which end before #max_mom#
75  */
76 void
77 Midi_walker::do_stop_notes (Moment max_mom)
78 {
79   while (stop_note_queue.size() && stop_note_queue.front ().key <= max_mom) 
80     {
81       Midi_note_event e = stop_note_queue.get();
82       if (e.ignore_b_)
83         {
84           delete e.val;
85           continue;
86         }
87       
88       Moment stop_mom = e.key;
89       Midi_note_off* note_p = e.val;
90         
91       output_event (stop_mom, note_p);
92     }
93 }
94
95 /** 
96   Advance the track to #now#, output the item, and adjust current "moment". 
97  */
98 void
99 Midi_walker::output_event (Moment now_mom, Midi_item* l)
100 {
101   Moment delta_t = now_mom - last_mom_ ;
102   last_mom_ += delta_t;
103   track_l_->add (delta_t, l);
104 }
105
106 void
107 Midi_walker::process()
108 {
109   Audio_item* audio_p = (*item_l_arr_l_)[index_];
110   do_stop_notes (audio_p->audio_column_l_->at_mom ());
111
112   /*
113     THIS IS A MEMORY LEAK. FIXME.
114    */
115   //Midi_item* p = ptr->midi_item_p ();
116   if (Midi_item* midi_p = Midi_item::midi_p (audio_p))
117     {
118       midi_p->channel_i_ = track_l_->number_i_;
119       if (Midi_note* note_p = dynamic_cast<Midi_note*>(midi_p))
120         {
121           if (note_p->length_mom ())
122             do_start_note (note_p);
123         }
124       else
125         output_event (audio_p->audio_column_l_->at_mom (), midi_p);
126     }
127 }
128
129 bool
130 Midi_walker::ok () const
131 {
132   return index_ <item_l_arr_l_->size ();
133 }
134
135 void
136 Midi_walker::operator ++(int)
137 {
138   assert (ok());
139   index_++;
140 }