]> git.donarmstrong.com Git - lilypond.git/blob - lily/performance.cc
release: 1.3.18
[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--1999 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include <time.h>
10 #include "debug.hh"
11 #include "string.hh"
12 #include "string-convert.hh"
13 #include "main.hh"
14 #include "midi-def.hh"
15 #include "midi-item.hh"
16 #include "midi-stream.hh"
17 #include "audio-column.hh"
18 #include "audio-item.hh"
19 #include "audio-staff.hh"
20 #include "performance.hh"
21 #include "score.hh"
22 #include "file-results.hh"
23 #include "lily-version.hh"
24
25 #include "killing-cons.tcc"
26
27 Performance::Performance ()
28 {
29   midi_l_ =0;
30   audio_elem_p_list_ = 0;
31 }
32
33
34 Performance::~Performance()
35 {
36   delete audio_elem_p_list_;
37 }
38
39 void
40 Performance::output (Midi_stream& midi_stream)
41 {
42   int tracks_i = audio_staff_l_arr_.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   progress_indication ("\n");
50   progress_indication (_ ("Track ... "));
51   int channel = 1;
52   for (int i =0; i < audio_staff_l_arr_.size (); i++)
53     {
54       Audio_staff *s = audio_staff_l_arr_[i];
55       progress_indication ("[" + to_str (i)) ;
56
57       /*
58         Aargh, let's hear it for the MIDI standard.
59         MIDI players tend to ignore instrument settings on
60         channel 10, the percussion channel by default.
61        */
62       if (channel == 9)
63         channel++;
64       s->output (midi_stream, channel++);
65       progress_indication ("]");
66     }
67 }
68
69 void
70 Performance::output_header_track (Midi_stream& midi_stream)
71 {
72   Midi_track midi_track;
73
74   // perhaps multiple text events?
75   String str = String (_("Creator: "));
76   if (no_timestamps_global_b)
77     str += gnu_lilypond_str ();
78   else
79     str += gnu_lilypond_version_str();
80   str += "\n";
81
82   /*
83     This seems silly, but in fact the audio elements should
84     be generated elsewhere: not midi-specific.
85    */
86   Audio_text creator_a (Audio_text::TEXT, str);
87   Midi_text creator (&creator_a);
88   midi_track.add (Moment (0), &creator);
89
90   str = _("Automatically generated");
91   if (no_timestamps_global_b)
92     str += ".\n";
93   else
94     {
95       str += _(", at ");
96       time_t t (time (0));
97       str += ctime (&t);
98       str = str.left_str (str.length_i() - 1);
99     }
100   Audio_text generate_a (Audio_text::TEXT, str);
101   Midi_text generate (&generate_a);
102   midi_track.add (Moment (0), &generate);
103
104   str = _f ("from musical definition: %s", origin_str_);
105
106   Audio_text from_a (Audio_text::TEXT, str);
107   Midi_text from (&from_a);
108   midi_track.add (Moment (0), &from);
109
110   Audio_text track_name_a (Audio_text::TRACK_NAME, "Track "
111                            + String_convert::i2dec_str (0, 0, '0'));
112   Midi_text track_name (&track_name_a);
113                         
114   midi_track.add (Moment (0), &track_name);
115
116   // Some sequencers read track 0 last.
117   //  Audio_tempo tempo_a (midi_l_->get_tempo_i (Moment (1, 4)));
118   //  Midi_tempo tempo (&tempo_a);
119   //  midi_track.add (Moment (0), &tempo);
120
121   midi_stream << midi_track;
122 }
123
124 void
125 Performance::add_element (Audio_element *p)
126 {
127   if (Audio_staff*s=dynamic_cast<Audio_staff *> (p)) 
128     {
129       audio_staff_l_arr_.push (s);
130     }
131   else if (Audio_column *c = dynamic_cast<Audio_column*>(p))
132     {
133       c->performance_l_ = this;
134     }
135   audio_elem_p_list_ = new Killing_cons<Audio_element> (p, audio_elem_p_list_);
136 }
137
138 void
139 Performance::print() const
140 {
141 #ifndef NPRINT
142   DEBUG_OUT << "Performance { ";
143   DEBUG_OUT << "Items: ";
144   for (Cons<Audio_element>* i =audio_elem_p_list_; i; i = i->next_)
145     i->car_->print ();
146   DEBUG_OUT << "}";
147 #endif
148 }
149
150 void
151 Performance::process()
152 {
153   print ();
154
155   String out = midi_l_->get_default_output ();
156   if (out.empty_b ())
157     {
158       
159       out = default_outname_base_global;
160       if (out == "-")
161         out = "lelie";
162       int def = midi_l_->get_next_default_count ();
163       if (def)
164         {
165           out += "-" + to_str (def);
166         }
167
168       out += ".midi";
169     }
170   
171   Midi_stream midi_stream (out);
172   progress_indication ( _f ("MIDI output to %s...", out));
173   target_str_global_array.push (out);
174
175   output (midi_stream);
176   progress_indication("\n");
177 }