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