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