lilypond-1.5.9
[lilypond.git] / lily / performance.cc
blobf77674fe5508043c84c88003d1c91c3507a1be44
1 /*
2 performance.cc -- implement Performance
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2001 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
9 #include <time.h>
10 #include "debug.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 "file-results.hh"
23 #include "file-path.hh"
24 #include "lily-version.hh"
26 #include "killing-cons.tcc"
28 Performance::Performance ()
30 midi_l_ =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_staff_l_arr_.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_staff_l_arr_.size (); i++)
55 Audio_staff *s = audio_staff_l_arr_[i];
56 if (verbose_global_b)
57 progress_indication ("[" + to_str (i)) ;
60 MIDI players tend to ignore instrument settings on
61 channel 10, the percussion channel by default.
63 if (channel == 9)
64 channel++;
65 if (s->channel_i_ < 0)
66 s->channel_i_ = channel;
67 s->output (midi_stream, channel++);
68 if (verbose_global_b)
69 progress_indication ("]");
74 void
75 Performance::output_header_track (Midi_stream& midi_stream)
77 Midi_track midi_track;
79 midi_track.channel_i_ = 9;
81 // perhaps multiple text events?
82 String id_str;
83 String str = String (_ ("Creator: "));
84 id_str = String_convert::pad_to (gnu_lilypond_version_str (), 40);
85 str += id_str;
86 str += "\n";
89 This seems silly, but in fact the audio elements should
90 be generated elsewhere: not midi-specific.
92 Audio_text creator_a (Audio_text::TEXT, str);
93 Midi_text creator (&creator_a);
94 midi_track.add (Moment (0), &creator);
96 /* Better not translate this */
97 str = "Generated automatically by: ";
98 str += id_str;
99 str += _ (", at ");
100 time_t t (time (0));
101 str += ctime (&t);
102 str = str.left_str (str.length_i () - 1);
105 Pad out time stamps to 120 chars. */
107 str = String_convert::pad_to (str, 120);
109 Audio_text generate_a (Audio_text::TEXT, str);
110 Midi_text generate (&generate_a);
111 midi_track.add (Moment (0), &generate);
113 str = _f ("from musical definition: %s", origin_str_);
115 Audio_text from_a (Audio_text::TEXT, str);
116 Midi_text from (&from_a);
117 midi_track.add (Moment (0), &from);
119 Audio_text track_name_a (Audio_text::TRACK_NAME, "Track "
120 + String_convert::i2dec_str (0, 0, '0'));
121 Midi_text track_name (&track_name_a);
123 midi_track.add (Moment (0), &track_name);
125 // Some sequencers read track 0 last.
126 // Audio_tempo tempo_a (midi_l_->get_tempo_i (Moment (Rational (1, 4))));
127 // Midi_tempo tempo (&tempo_a);
128 // midi_track.add (Moment (0), &tempo);
130 midi_stream << midi_track;
133 void
134 Performance::add_element (Audio_element *p)
136 if (Audio_staff*s=dynamic_cast<Audio_staff *> (p))
138 audio_staff_l_arr_.push (s);
140 else if (Audio_column *c = dynamic_cast<Audio_column*> (p))
142 c->performance_l_ = this;
144 audio_elem_p_list_ = new Killing_cons<Audio_element> (p, audio_elem_p_list_);
148 void
149 Performance::process ()
151 String out = output_name_global;
152 if (out == "-")
153 out = "lelie.midi";
154 int def = midi_l_->get_next_score_count ();
155 if (def)
157 Path p = split_path (out);
158 p.base += "-" + to_str (def);
159 out = p.str ();
162 /* Maybe a bit crude, but we had this before */
163 Path p = split_path (out);
164 p.ext = "midi";
165 out = p.str ();
167 Midi_stream midi_stream (out);
168 progress_indication (_f ("MIDI output to `%s'...", out));
169 target_str_global_array.push (out);
171 output (midi_stream);
172 progress_indication ("\n");