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