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