]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-walker.cc
release: 0.1.8
[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 Han-Wen Nienhuys <hanwen@stack.nl>
7            Jan Nieuwenhuizen <jan@digicash.com>
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     : PCursor<Audio_item*>( audio_staff_l->audio_item_l_list_)
31 {
32     track_l_ = track_l;
33     last_mom_ = 0;
34 }
35
36 Midi_walker::~Midi_walker()
37
38     // ugh
39     do_stop_notes (last_mom_ + Moment (10, 1) );
40 }
41
42 /**
43   Find out if start_note event is needed, and do it if needed.
44  */
45 void 
46 Midi_walker::do_start_note (Midi_note* note_l)
47 {
48     Moment stop_mom = note_l->duration() + ptr ()->audio_column_l_->at_mom ();
49     for ( int i=0; i < stop_note_queue.size(); i++) {
50         if ( stop_note_queue[ i ].val->pitch_i() == note_l->pitch_i ()) {
51             if ( stop_note_queue[ i ].key < stop_mom)
52                 stop_note_queue[ i ].ignore_b_ = true;
53             else // skip the stopnote 
54                 return; 
55         }
56     }
57
58     Midi_note_event e;
59     e.val = new Midi_note_off (note_l);
60     e.key = stop_mom;
61     stop_note_queue.insert (e);
62     
63     output_event (ptr()->audio_column_l_->at_mom (), note_l);
64 }
65
66 /**
67   Output note events for all notes which end before #max_mom#
68  */
69 void
70 Midi_walker::do_stop_notes (Moment max_mom)
71 {
72     while ( stop_note_queue.size() && stop_note_queue.front ().key <= max_mom) {
73         Midi_note_event e = stop_note_queue.get();
74         if ( e.ignore_b_) 
75             continue;
76         
77         Moment stop_mom = e.key;
78         Midi_note_off* note_l = e.val;
79         
80         output_event (stop_mom, note_l);
81     }
82 }
83
84 /** 
85   Advance the track to #now#, output the item, and adjust current "moment". 
86  */
87 void
88 Midi_walker::output_event (Moment now_mom, Midi_item* l)
89 {
90     Moment delta_t = now_mom - last_mom_ ;
91     last_mom_ += delta_t;
92     track_l_->add (delta_t, l);
93 }
94
95 void
96 Midi_walker::process()
97 {
98     do_stop_notes (ptr()->audio_column_l_->at_mom ());
99
100     Midi_item* p = ptr()->midi_item_p ();
101     p->channel_i_ = track_l_->number_i_;
102     
103     if ( p->name() != Midi_note::static_name ())
104         output_event (ptr()->audio_column_l_->at_mom (), p);
105     else
106         do_start_note ((Midi_note*)p);
107         
108     delete p;
109 }
110