1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3 
4   Copyright (C) 1997--2020 Jan Nieuwenhuizen <janneke@gnu.org>
5 
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10 
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15 
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "performance.hh"
21 
22 #include "audio-column.hh"
23 #include "audio-item.hh"
24 #include "audio-staff.hh"
25 #include "file-name.hh"
26 #include "international.hh"
27 #include "lily-version.hh"
28 #include "main.hh"
29 #include "midi-chunk.hh"
30 #include "midi-stream.hh"
31 #include "output-def.hh"
32 #include "score.hh"
33 #include "string-convert.hh"
34 #include "warn.hh"
35 
36 #include <ctime>
37 
38 using std::string;
39 
Performance(bool ports)40 Performance::Performance (bool ports)
41   : start_mom_ (Rational::infinity ()),
42     midi_ (0),
43     ports_ (ports),
44     headers_ (SCM_EOL)
45 {
46 }
47 
~Performance()48 Performance::~Performance ()
49 {
50   junk_pointers (audio_elements_);
51 }
52 
53 void
derived_mark() const54 Performance::derived_mark () const
55 {
56   scm_gc_mark (headers_);
57 }
58 
59 void
push_header(SCM header)60 Performance::push_header (SCM header)
61 {
62   assert (ly_is_module (header));
63   headers_ = scm_cons (header, headers_);
64 }
65 
66 void
output(Midi_stream & midi_stream,const string & performance_name) const67 Performance::output (Midi_stream &midi_stream,
68                      const string &performance_name) const
69 {
70   int tracks_ = audio_staffs_.size ();
71 
72   midi_stream.write (Midi_header (1, tracks_, 384));
73   debug_output (_ ("Track...") + " ", false);
74 
75   for (vsize i = 0; i < audio_staffs_.size (); i++)
76     {
77       Audio_staff *s = audio_staffs_[i];
78       if (Audio_control_track_staff * c
79           = dynamic_cast<Audio_control_track_staff *>(s))
80         {
81           // The control track, created by Control_track_performer, should
82           // contain a placeholder for the name of the MIDI sequence as its
83           // initial audio element.  Fill in the name of the sequence to
84           // this element before outputting MIDI.
85           assert (!c->audio_items_.empty ());
86           Audio_text *text
87             = dynamic_cast<Audio_text *>(c->audio_items_.front ());
88           assert (text != 0);
89           assert (text->type_ == Audio_text::TRACK_NAME);
90           assert (text->text_string_ == "control track");
91           text->text_string_ = performance_name;
92         }
93       debug_output ("[" + std::to_string (i), true);
94       s->output (midi_stream, i, ports_, start_mom_);
95       debug_output ("]", false);
96     }
97 }
98 
99 void
add_element(Audio_element * p)100 Performance::add_element (Audio_element *p)
101 {
102   audio_elements_.push_back (p);
103 }
104 
105 void
write_output(string out,const string & performance_name) const106 Performance::write_output (string out, const string &performance_name) const
107 {
108   if (out == "-")
109     out = "lelie.midi";
110 
111   /* Maybe a bit crude, but we had this before */
112   File_name file_name (out);
113   out = file_name.to_string ();
114 
115   Midi_stream midi_stream (out);
116   message (_f ("MIDI output to `%s'...", out));
117 
118   output (midi_stream, performance_name);
119   progress_indication ("\n");
120 
121   SCM after_writing = midi_->c_variable ("after-writing");
122   if (ly_is_procedure (after_writing))
123     scm_call_2 (after_writing, self_scm (), ly_string2scm (out));
124 }
125 
126 void
process()127 Performance::process ()
128 {
129   // Find the first Audio_item in the performance, so all staves start at the
130   // same tick.
131   // TODO: Could this be done on the fly rather than in a separate pass?
132   for (auto elem : audio_elements_)
133     {
134       if (auto item = dynamic_cast<const Audio_item *> (elem))
135         {
136           if (auto col = item->get_column ())
137             {
138               start_mom_ = std::min (start_mom_, col->when ());
139             }
140         }
141     }
142 }
143