]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-walker.cc
Map voices to channels in MIDI output.
[lilypond.git] / lily / midi-walker.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
5   Jan Nieuwenhuizen <janneke@gnu.org>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "midi-walker.hh"
22
23 #include "audio-column.hh"
24 #include "audio-staff.hh"
25 #include "midi-item.hh"
26 #include "midi-chunk.hh"
27 #include "midi-stream.hh"
28 #include "warn.hh"
29
30 Midi_note_event::Midi_note_event ()
31 {
32   ignore_ = false;
33 }
34
35 int
36 compare (Midi_note_event const &left, Midi_note_event const &right)
37 {
38   Moment m = (left.key - right.key);
39
40   if (m < 0)
41     return -1;
42   else if (m > 0)
43     return 1;
44   else
45     return 0;
46 }
47
48 bool
49 audio_item_less (Audio_item * const a,
50                  Audio_item * const b)
51 {
52   return a->get_column ()->when_ <  b->get_column ()->when_;
53 }
54
55 Midi_walker::Midi_walker (Audio_staff *audio_staff, Midi_track *track)
56 {
57   track_ = track;
58   index_ = 0;
59   items_ = audio_staff->audio_items_;
60   vector_sort (items_, audio_item_less);
61   last_tick_ = 0;
62   percussion_ = audio_staff->percussion_;
63 }
64
65 Midi_walker::~Midi_walker ()
66 {
67   junk_pointers (midi_events_);
68 }
69
70 void
71 Midi_walker::finalize ()
72 {
73   do_stop_notes (INT_MAX);
74 }
75
76 /**
77    Find out if start_note event is needed, and do it if needed.
78 */
79 void
80 Midi_walker::do_start_note (Midi_note *note)
81 {
82   Audio_item *ptr = items_[index_];
83   assert (note->audio_ == ptr);
84   int stop_ticks = int (moment_to_real (note->audio_->length_mom_) * Real (384 * 4))
85     + ptr->audio_column_->ticks ();
86
87   bool play_start = true;
88   for (vsize i = 0; i < stop_note_queue.size (); i++)
89     {
90       /* if this pith already in queue */
91       if (stop_note_queue[i].val->get_semitone_pitch ()
92           == note->get_semitone_pitch ())
93         {
94           if (stop_note_queue[i].key < stop_ticks)
95             {
96               /* let stopnote in queue be ignored,
97                  new stop note wins */
98               stop_note_queue[i].ignore_ = true;
99
100               /* don't replay start note, */
101               play_start = false;
102               break;
103             }
104           else
105             {
106               /* skip this stopnote,
107                  don't play the start note */
108               note = 0;
109               break;
110             }
111         }
112     }
113
114   if (note)
115     {
116       Midi_note_event e;
117       e.val = new Midi_note_off (note);
118
119       midi_events_.push_back (e.val);
120       e.key = int (stop_ticks);
121       stop_note_queue.insert (e);
122
123       if (play_start)
124         output_event (ptr->audio_column_->ticks (), note);
125     }
126 }
127
128 void
129 Midi_walker::do_stop_notes (int max_ticks)
130 {
131   while (stop_note_queue.size () && stop_note_queue.front ().key <= max_ticks)
132     {
133       Midi_note_event e = stop_note_queue.get ();
134       if (e.ignore_)
135         {
136           continue;
137         }
138
139       int stop_ticks = e.key;
140       Midi_note *note = e.val;
141
142       output_event (stop_ticks, note);
143     }
144 }
145
146 void
147 Midi_walker::output_event (int now_ticks, Midi_item *l)
148 {
149   int delta_ticks = now_ticks - last_tick_;
150   last_tick_ = now_ticks;
151
152   /*
153     this is not correct, but at least it doesn't crash when you
154     start with graces
155   */
156   if (delta_ticks < 0)
157     {
158       programming_error ("Going back in MIDI time.");
159       delta_ticks = 0;
160     }
161
162   track_->add (delta_ticks, l);
163 }
164
165 void
166 Midi_walker::process ()
167 {
168   Audio_item *audio = items_[index_];
169   do_stop_notes (audio->audio_column_->ticks ());
170
171   if (Midi_item *midi = get_midi (audio))
172     {
173       if (Midi_note *note = dynamic_cast<Midi_note *> (midi))
174         {
175           if (note->audio_->length_mom_.to_bool ())
176             do_start_note (note);
177         }
178       else
179         output_event (audio->audio_column_->ticks (), midi);
180     }
181 }
182
183 Midi_item*
184 Midi_walker::get_midi (Audio_item *i)
185 {
186   Midi_item *mi = Midi_item::get_midi (i);
187
188   if (percussion_)
189     if (Midi_channel_item *mci = dynamic_cast<Midi_channel_item*> (mi))
190       mci->channel_ = 9;
191
192   midi_events_.push_back (mi);
193   return mi;
194 }
195
196 bool
197 Midi_walker::ok () const
198 {
199   return index_ < items_.size ();
200 }
201
202 void
203 Midi_walker::operator ++ (int)
204 {
205   assert (ok ());
206   index_++;
207 }