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