Svg with woff fonts: also extract and define pango-fonts from paper.
[lilypond/patrick.git] / lily / midi-walker.cc
blob3214b3202442169c6f5e55c603dfbb46c05b673e
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
5 Jan Nieuwenhuizen <janneke@gnu.org>
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.
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.
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/>.
21 #include "midi-walker.hh"
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"
30 Midi_note_event::Midi_note_event ()
32 ignore_ = false;
35 int
36 compare (Midi_note_event const &left, Midi_note_event const &right)
38 Moment m = (left.key - right.key);
40 if (m < 0)
41 return -1;
42 else if (m > 0)
43 return 1;
44 else
45 return 0;
48 bool
49 audio_item_less (Audio_item * const a,
50 Audio_item * const b)
52 return a->get_column ()->when_ < b->get_column ()->when_;
55 Midi_walker::Midi_walker (Audio_staff *audio_staff, Midi_track *track,
56 int channel)
58 channel_ = channel;
59 track_ = track;
60 index_ = 0;
61 items_ = audio_staff->audio_items_;
62 vector_sort (items_, audio_item_less);
63 last_tick_ = 0;
66 Midi_walker::~Midi_walker ()
68 junk_pointers (midi_events_);
71 void
72 Midi_walker::finalize ()
74 do_stop_notes (INT_MAX);
77 /**
78 Find out if start_note event is needed, and do it if needed.
80 void
81 Midi_walker::do_start_note (Midi_note *note)
83 Audio_item *ptr = items_[index_];
84 assert (note->audio_ == ptr);
85 int stop_ticks = int (moment_to_real (note->audio_->length_mom_) * Real (384 * 4))
86 + ptr->audio_column_->ticks ();
88 bool play_start = true;
89 for (vsize i = 0; i < stop_note_queue.size (); i++)
91 /* if this pith already in queue */
92 if (stop_note_queue[i].val->get_semitone_pitch ()
93 == note->get_semitone_pitch ())
95 if (stop_note_queue[i].key < stop_ticks)
97 /* let stopnote in queue be ignored,
98 new stop note wins */
99 stop_note_queue[i].ignore_ = true;
101 /* don't replay start note, */
102 play_start = false;
103 break;
105 else
107 /* skip this stopnote,
108 don't play the start note */
109 note = 0;
110 break;
115 if (note)
117 Midi_note_event e;
118 e.val = new Midi_note_off (note);
120 midi_events_.push_back (e.val);
121 e.key = int (stop_ticks);
122 stop_note_queue.insert (e);
124 if (play_start)
125 output_event (ptr->audio_column_->ticks (), note);
129 void
130 Midi_walker::do_stop_notes (int max_ticks)
132 while (stop_note_queue.size () && stop_note_queue.front ().key <= max_ticks)
134 Midi_note_event e = stop_note_queue.get ();
135 if (e.ignore_)
137 continue;
140 int stop_ticks = e.key;
141 Midi_note *note = e.val;
143 output_event (stop_ticks, note);
147 void
148 Midi_walker::output_event (int now_ticks, Midi_item *l)
150 int delta_ticks = now_ticks - last_tick_;
151 last_tick_ = now_ticks;
154 this is not correct, but at least it doesn't crash when you
155 start with graces
157 if (delta_ticks < 0)
159 programming_error ("Going back in MIDI time.");
160 delta_ticks = 0;
163 track_->add (delta_ticks, l);
166 void
167 Midi_walker::process ()
169 Audio_item *audio = items_[index_];
170 do_stop_notes (audio->audio_column_->ticks ());
172 if (Midi_item *midi = get_midi (audio))
174 if (Midi_channel_item *mci = dynamic_cast<Midi_channel_item*> (midi))
175 mci->channel_ = channel_;
177 if (Midi_note *note = dynamic_cast<Midi_note *> (midi))
179 if (note->audio_->length_mom_.to_bool ())
180 do_start_note (note);
182 else
183 output_event (audio->audio_column_->ticks (), midi);
187 Midi_item*
188 Midi_walker::get_midi (Audio_item *i)
190 Midi_item *mi = Midi_item::get_midi (i);
191 midi_events_.push_back (mi);
192 return mi;
195 bool
196 Midi_walker::ok () const
198 return index_ < items_.size ();
201 void
202 Midi_walker::operator ++ (int)
204 assert (ok ());
205 index_++;