]> 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--2004 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_elem_p_list_ = 0;
30 }
31
32
33 Performance::~Performance ()
34 {
35   delete audio_elem_p_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   progress_indication ("\n");
49   progress_indication (_ ("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 (verbose_global_b)
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       /*
66         Huh? Why does each staff also have a separate channel? We
67         should map channels to voices, not staves. --hwn.
68         */
69       if (s->channel_ < 0)
70         {
71           s->channel_ = channel % 16;
72           if (channel > 15)
73             warning ("MIDI channel wrapped around. Remapping modulo 16.");
74         }
75       
76       s->output (midi_stream, channel++);
77       if (verbose_global_b)
78         progress_indication ("]");
79     }
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
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     {
149       audio_staffs_.push (s);
150     }
151   audio_elem_p_list_ = new Killing_cons<Audio_element> (p, audio_elem_p_list_);
152 }
153
154 SCM
155 Performance::process (String out)
156 {
157   if (out == "-")
158     out = "lelie.midi";
159   
160   /* Maybe a bit crude, but we had this before */
161   File_name file_name (out);
162   file_name.ext_ = "midi";
163   out = file_name.to_string ();
164   
165   Midi_stream midi_stream (out);
166   progress_indication (_f ("MIDI output to `%s'...", out));
167
168   output (midi_stream);
169   progress_indication ("\n");
170   return SCM_UNDEFINED;
171 }