]> git.donarmstrong.com Git - lilypond.git/blob - lily/performance.cc
985901c01cac1466af0dbfde49288548b131f614
[lilypond.git] / lily / performance.cc
1 /*
2   performance.cc -- implement Performance
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "performance.hh"
10
11 #include <ctime>
12 using namespace std;
13
14 #include "audio-column.hh"
15 #include "audio-staff.hh"
16 #include "file-name.hh"
17 #include "lily-version.hh"
18 #include "main.hh"
19 #include "midi-item.hh"
20 #include "midi-stream.hh"
21 #include "score.hh"
22 #include "string-convert.hh"
23 #include "warn.hh"
24
25 #include "killing-cons.tcc"
26
27 Performance::Performance ()
28 {
29   midi_ = 0;
30   audio_element_list_ = 0;
31 }
32
33 Performance::~Performance ()
34 {
35   delete audio_element_list_;
36 }
37
38 void
39 Performance::output (Midi_stream &midi_stream)
40 {
41   int tracks_i = audio_staffs_.size () + 1;
42
43   // ugh
44   int clocks_per_4_i = 384;
45
46   midi_stream << Midi_header (1, tracks_i, clocks_per_4_i);
47   output_header_track (midi_stream);
48   message (_ ("Track...") + " ");
49   int channel = 0;
50   for (int i = 0; i < audio_staffs_.size (); i++)
51     {
52       Audio_staff *s = audio_staffs_[i];
53       if (be_verbose_global)
54         progress_indication ("[" + to_string (i));
55
56       /*
57         MIDI players tend to ignore instrument settings on
58         channel 10, the percussion channel by default.
59       */
60       if (channel % 16 == 9)
61         channel++;
62
63       /*
64         Huh? Why does each staff also have a separate channel? We
65         should map channels to voices, not staves. --hwn.
66       */
67       if (s->channel_ < 0)
68         {
69           s->channel_ = channel % 16;
70           if (channel > 15)
71             {
72               warning (_ ("MIDI channel wrapped around"));
73               warning (_ ("remapping modulo 16"));
74             }
75         }
76
77       s->output (midi_stream, channel++);
78       if (be_verbose_global)
79         progress_indication ("]");
80     }
81 }
82
83 void
84 Performance::output_header_track (Midi_stream &midi_stream)
85 {
86   Midi_track midi_track;
87
88   midi_track.channel_ = 9;
89
90   // perhaps multiple text events?
91   String id_string;
92   String str = String (_ ("Creator: "));
93   id_string = String_convert::pad_to (gnu_lilypond_version_string (), 30);
94   str += id_string;
95
96   /*
97     This seems silly, but in fact the audio elements should
98     be generated elsewhere: not midi-specific.
99   */
100   Audio_text creator_a (Audio_text::TEXT, str);
101   Midi_text creator (&creator_a);
102   midi_track.add (Moment (0), &creator);
103
104   /* Better not translate this */
105   str = "Generated automatically by: ";
106   str += id_string;
107
108   Audio_text generate_a (Audio_text::TEXT, str);
109   Midi_text generate (&generate_a);
110   midi_track.add (Moment (0), &generate);
111
112   str = _ ("at ");
113   time_t t (time (0));
114   str += ctime (&t);
115   str = str.left_string (str.length () - 1);
116   str = String_convert::pad_to (str, 60);
117
118   Audio_text at_a (Audio_text::TEXT, str);
119   Midi_text at (&at_a);
120   midi_track.add (Moment (0), &at);
121
122   // TODO:
123   //  str = _f ("from musical definition: %s", origin_string_);
124
125   Audio_text from_a (Audio_text::TEXT, str);
126   Midi_text from (&from_a);
127   midi_track.add (Moment (0), &from);
128
129   Audio_text track_name_a (Audio_text::TRACK_NAME, "Track "
130                            + String_convert::int2dec (0, 0, '0'));
131   Midi_text track_name (&track_name_a);
132
133   midi_track.add (Moment (0), &track_name);
134
135   // Some sequencers read track 0 last.
136   //  Audio_tempo tempo_a (midi_->get_tempo (Moment (Rational (1, 4))));
137   //  Midi_tempo tempo (&tempo_a);
138   //  midi_track.add (Moment (0), &tempo);
139
140   midi_stream << midi_track;
141 }
142
143 void
144 Performance::add_element (Audio_element *p)
145 {
146   if (Audio_staff *s = dynamic_cast<Audio_staff *> (p))
147     audio_staffs_.push (s);
148   audio_element_list_ = new Killing_cons<Audio_element> (p, audio_element_list_);
149 }
150
151 void
152 Performance::write_output (String out)
153 {
154   if (out == "-")
155     out = "lelie.midi";
156
157   /* Maybe a bit crude, but we had this before */
158   File_name file_name (out);
159   file_name.ext_ = "midi";
160   out = file_name.to_string ();
161
162   Midi_stream midi_stream (out);
163   message (_f ("MIDI output to `%s'...", out));
164
165   output (midi_stream);
166   progress_indication ("\n");
167 }
168