* lily/performance.cc (output): remap modulo 16.
[lilypond.git] / lily / performance.cc
blob3c9e700f0484f880f1feb2c21d920226cb920eb2
1 /*
2 performance.cc -- implement Performance
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
9 #include <time.h>
10 #include "warn.hh"
11 #include "string.hh"
12 #include "string-convert.hh"
13 #include "main.hh"
14 #include "midi-def.hh"
15 #include "midi-item.hh"
16 #include "midi-stream.hh"
17 #include "audio-column.hh"
18 #include "audio-item.hh"
19 #include "audio-staff.hh"
20 #include "performance.hh"
21 #include "score.hh"
22 #include "input-file-results.hh"
23 #include "file-path.hh"
24 #include "lily-version.hh"
26 #include "killing-cons.tcc"
28 Performance::Performance ()
30 midi_ =0;
31 audio_elem_p_list_ = 0;
35 Performance::~Performance ()
37 delete audio_elem_p_list_;
40 void
41 Performance::output (Midi_stream& midi_stream)
43 int tracks_i = audio_staffs_.size () + 1;
45 // ugh
46 int clocks_per_4_i = 384;
48 midi_stream << Midi_header (1, tracks_i, clocks_per_4_i);
49 output_header_track (midi_stream);
50 progress_indication ("\n");
51 progress_indication (_ ("Track ... "));
52 int channel = 0;
53 for (int i =0; i < audio_staffs_.size (); i++)
55 Audio_staff *s = audio_staffs_[i];
56 if (verbose_global_b)
57 progress_indication ("[" + to_string (i)) ;
60 MIDI players tend to ignore instrument settings on
61 channel 10, the percussion channel by default.
63 if (channel % 16 == 9)
64 channel++;
68 Huh? Why does each staff also have a separate channel? We
69 should map channels to voices, not staves. --hwn.
71 if (s->channel_ < 0)
73 s->channel_ = channel % 16;
74 if (channel > 15)
75 warning ("MIDI channel wrapped around. Remapping modulo 16.");
78 s->output (midi_stream, channel++);
79 if (verbose_global_b)
80 progress_indication ("]");
85 void
86 Performance::output_header_track (Midi_stream& midi_stream)
88 Midi_track midi_track;
90 midi_track.channel_ = 9;
92 // perhaps multiple text events?
93 String id_string;
94 String str = String (_ ("Creator: "));
95 id_string = String_convert::pad_to (gnu_lilypond_version_string (), 30);
96 str += id_string;
99 This seems silly, but in fact the audio elements should
100 be generated elsewhere: not midi-specific.
102 Audio_text creator_a (Audio_text::TEXT, str);
103 Midi_text creator (&creator_a);
104 midi_track.add (Moment (0), &creator);
106 /* Better not translate this */
107 str = "Generated automatically by: ";
108 str += id_string;
110 Audio_text generate_a (Audio_text::TEXT, str);
111 Midi_text generate (&generate_a);
112 midi_track.add (Moment (0), &generate);
114 str = _ ("at ");
115 time_t t (time (0));
116 str += ctime (&t);
117 str = str.left_string (str.length () - 1);
118 str = String_convert::pad_to (str, 60);
120 Audio_text at_a (Audio_text::TEXT, str);
121 Midi_text at (&at_a);
122 midi_track.add (Moment (0), &at);
125 // TODO:
126 // str = _f ("from musical definition: %s", origin_string_);
128 Audio_text from_a (Audio_text::TEXT, str);
129 Midi_text from (&from_a);
130 midi_track.add (Moment (0), &from);
132 Audio_text track_name_a (Audio_text::TRACK_NAME, "Track "
133 + String_convert::int2dec (0, 0, '0'));
134 Midi_text track_name (&track_name_a);
136 midi_track.add (Moment (0), &track_name);
138 // Some sequencers read track 0 last.
139 // Audio_tempo tempo_a (midi_->get_tempo (Moment (Rational (1, 4))));
140 // Midi_tempo tempo (&tempo_a);
141 // midi_track.add (Moment (0), &tempo);
143 midi_stream << midi_track;
146 void
147 Performance::add_element (Audio_element *p)
149 if (Audio_staff*s=dynamic_cast<Audio_staff *> (p))
151 audio_staffs_.push (s);
153 else if (Audio_column *c = dynamic_cast<Audio_column*> (p))
155 c->performance_ = this;
157 audio_elem_p_list_ = new Killing_cons<Audio_element> (p, audio_elem_p_list_);
161 Performance::process (String out)
163 if (out == "-")
164 out = "lelie.midi";
166 /* Maybe a bit crude, but we had this before */
167 Path p = split_path (out);
168 p.ext = "midi";
169 out = p.to_string ();
171 Midi_stream midi_stream (out);
172 progress_indication (_f ("MIDI output to `%s'...", out));
174 global_input_file->target_strings_.push (out);
176 output (midi_stream);
177 progress_indication ("\n");
178 return SCM_UNDEFINED;