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