]> git.donarmstrong.com Git - lilypond.git/blob - lily/performance.cc
(LY_DEFINE): move parse-file function
[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 <time.h>
10 #include "warn.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-path.hh"
23 #include "lily-version.hh"
24
25 #include "killing-cons.tcc"
26
27 Performance::Performance ()
28 {
29   midi_ =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_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   progress_indication ("\n");
50   progress_indication (_ ("Track ... "));
51   int channel = 0;
52   for (int i =0; i < audio_staffs_.size (); i++)
53     {
54       Audio_staff *s = audio_staffs_[i];
55       if (verbose_global_b)
56         progress_indication ("[" + to_string (i)) ;
57
58       /*
59         MIDI players tend to ignore instrument settings on
60         channel 10, the percussion channel by default.
61        */
62       if (channel % 16 == 9)
63          channel++;
64
65
66       /*
67         Huh? Why does each staff also have a separate channel? We
68         should map channels to voices, not staves. --hwn.
69         */
70       if (s->channel_ < 0)
71         {
72           s->channel_ = channel % 16;
73           if (channel > 15)
74             warning ("MIDI channel wrapped around. Remapping modulo 16.");
75         }
76       
77       s->output (midi_stream, channel++);
78       if (verbose_global_b)
79         progress_indication ("]");
80     }
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   String id_string;
93   String str = 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.left_string (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
124   // TODO:
125   //  str = _f ("from musical definition: %s", origin_string_);
126
127   Audio_text from_a (Audio_text::TEXT, str);
128   Midi_text from (&from_a);
129   midi_track.add (Moment (0), &from);
130
131   Audio_text track_name_a (Audio_text::TRACK_NAME, "Track "
132                            + String_convert::int2dec (0, 0, '0'));
133   Midi_text track_name (&track_name_a);
134                         
135   midi_track.add (Moment (0), &track_name);
136
137   // Some sequencers read track 0 last.
138   //  Audio_tempo tempo_a (midi_->get_tempo (Moment (Rational (1, 4))));
139   //  Midi_tempo tempo (&tempo_a);
140   //  midi_track.add (Moment (0), &tempo);
141
142   midi_stream << midi_track;
143 }
144
145 void
146 Performance::add_element (Audio_element *p)
147 {
148   if (Audio_staff*s=dynamic_cast<Audio_staff *> (p)) 
149     {
150       audio_staffs_.push (s);
151     }
152   else if (Audio_column *c = dynamic_cast<Audio_column*> (p))
153     {
154       c->performance_ = this;
155     }
156   audio_elem_p_list_ = new Killing_cons<Audio_element> (p, audio_elem_p_list_);
157 }
158
159 SCM
160 Performance::process (String out)
161 {
162   if (out == "-")
163     out = "lelie.midi";
164   
165   /* Maybe a bit crude, but we had this before */
166   Path p = split_path (out);
167   p.ext = "midi";
168   out = p.to_string ();
169   
170   Midi_stream midi_stream (out);
171   progress_indication (_f ("MIDI output to `%s'...", out));
172
173   output (midi_stream);
174   progress_indication ("\n");
175   return SCM_UNDEFINED;
176 }