2 Copyright (C) 2006 Paul Davis
3 By Dave Robillard, 2006
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <pbd/error.h>
20 #include <sigc++/retype.h>
21 #include <sigc++/retype_return.h>
22 #include <sigc++/bind.h>
24 #include <ardour/midi_track.h>
25 #include <ardour/midi_diskstream.h>
26 #include <ardour/session.h>
27 #include <ardour/redirect.h>
28 #include <ardour/midi_region.h>
29 #include <ardour/midi_source.h>
30 #include <ardour/route_group_specialized.h>
31 #include <ardour/insert.h>
32 #include <ardour/midi_playlist.h>
33 #include <ardour/panner.h>
34 #include <ardour/utils.h>
35 #include <ardour/buffer_set.h>
40 using namespace ARDOUR;
43 MidiTrack::MidiTrack (Session& sess, string name, Route::Flag flag, TrackMode mode)
44 : Track (sess, name, flag, mode, DataType::MIDI)
46 MidiDiskstream::Flag dflags = MidiDiskstream::Flag (0);
48 if (_flags & Hidden) {
49 dflags = MidiDiskstream::Flag (dflags | MidiDiskstream::Hidden);
51 dflags = MidiDiskstream::Flag (dflags | MidiDiskstream::Recordable);
54 if (mode == Destructive) {
55 dflags = MidiDiskstream::Flag (dflags | MidiDiskstream::Destructive);
58 MidiDiskstream* ds = new MidiDiskstream (_session, name, dflags);
61 _freeze_record.state = NoFreeze;
62 _saved_meter_point = _meter_point;
68 MidiTrack::MidiTrack (Session& sess, const XMLNode& node)
71 _freeze_record.state = NoFreeze;
74 _saved_meter_point = _meter_point;
77 MidiTrack::~MidiTrack ()
86 MidiTrack::set_diskstream (MidiDiskstream& ds)
92 _diskstream = &ds.ref();
93 _diskstream->set_io (*this);
94 _diskstream->set_destructive (_mode == Destructive);
96 _diskstream->set_record_enabled (false);
97 //_diskstream->monitor_input (false);
99 ic_connection.disconnect();
100 ic_connection = input_changed.connect (mem_fun (*_diskstream, &MidiDiskstream::handle_input_change));
102 DiskstreamChanged (); /* EMIT SIGNAL */
108 MidiTrack::use_diskstream (string name)
110 MidiDiskstream *dstream;
112 if ((dstream = dynamic_cast<MidiDiskstream*>(_session.diskstream_by_name (name))) == 0) {
113 error << string_compose(_("MidiTrack: midi diskstream \"%1\" not known by session"), name) << endmsg;
117 return set_diskstream (*dstream);
121 MidiTrack::use_diskstream (const PBD::ID& id)
123 MidiDiskstream *dstream;
125 if ((dstream = dynamic_cast<MidiDiskstream*>(_session.diskstream_by_id (id))) == 0) {
126 error << string_compose(_("MidiTrack: midi diskstream \"%1\" not known by session"), id) << endmsg;
130 return set_diskstream (*dstream);
134 MidiTrack::midi_diskstream() const
136 return *dynamic_cast<MidiDiskstream*>(_diskstream);
140 MidiTrack::set_state (const XMLNode& node)
142 const XMLProperty *prop;
143 XMLNodeConstIterator iter;
145 if (Route::set_state (node)) {
149 if ((prop = node.property (X_("mode"))) != 0) {
150 if (prop->value() == X_("normal")) {
152 } else if (prop->value() == X_("destructive")) {
155 warning << string_compose ("unknown midi track mode \"%1\" seen and ignored", prop->value()) << endmsg;
162 if ((prop = node.property ("diskstream-id")) == 0) {
164 /* some old sessions use the diskstream name rather than the ID */
166 if ((prop = node.property ("diskstream")) == 0) {
167 fatal << _("programming error: MidiTrack given state without diskstream!") << endmsg;
172 if (use_diskstream (prop->value())) {
178 PBD::ID id (prop->value());
180 if (use_diskstream (id)) {
187 XMLNodeConstIterator niter;
190 nlist = node.children();
191 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
194 if (child->name() == X_("remote_control")) {
195 if ((prop = child->property (X_("id"))) != 0) {
197 sscanf (prop->value().c_str(), "%d", &x);
198 set_remote_control_id (x);
203 pending_state = const_cast<XMLNode*> (&node);
205 _session.StateReady.connect (mem_fun (*this, &MidiTrack::set_state_part_two));
211 MidiTrack::state(bool full_state)
213 XMLNode& root (Route::state(full_state));
214 XMLNode* freeze_node;
217 if (_freeze_record.playlist) {
220 freeze_node = new XMLNode (X_("freeze-info"));
221 freeze_node->add_property ("playlist", _freeze_record.playlist->name());
222 snprintf (buf, sizeof (buf), "%d", (int) _freeze_record.state);
223 freeze_node->add_property ("state", buf);
225 for (vector<FreezeRecordInsertInfo*>::iterator i = _freeze_record.insert_info.begin(); i != _freeze_record.insert_info.end(); ++i) {
226 inode = new XMLNode (X_("insert"));
227 (*i)->id.print (buf);
228 inode->add_property (X_("id"), buf);
229 inode->add_child_copy ((*i)->state);
231 freeze_node->add_child_nocopy (*inode);
234 root.add_child_nocopy (*freeze_node);
237 /* Alignment: act as a proxy for the diskstream */
239 XMLNode* align_node = new XMLNode (X_("alignment"));
240 switch (_diskstream->alignment_style()) {
241 case ExistingMaterial:
242 snprintf (buf, sizeof (buf), X_("existing"));
245 snprintf (buf, sizeof (buf), X_("capture"));
248 align_node->add_property (X_("style"), buf);
249 root.add_child_nocopy (*align_node);
251 XMLNode* remote_control_node = new XMLNode (X_("remote_control"));
252 snprintf (buf, sizeof (buf), "%d", _remote_control_id);
253 remote_control_node->add_property (X_("id"), buf);
254 root.add_child_nocopy (*remote_control_node);
258 root.add_property (X_("mode"), X_("normal"));
261 root.add_property (X_("mode"), X_("destructive"));
265 /* we don't return diskstream state because we don't
266 own the diskstream exclusively. control of the diskstream
267 state is ceded to the Session, even if we create the
271 _diskstream->id().print (buf);
272 root.add_property ("diskstream-id", buf);
278 MidiTrack::set_state_part_two ()
282 LocaleGuard lg (X_("POSIX"));
284 /* This is called after all session state has been restored but before
285 have been made ports and connections are established.
288 if (pending_state == 0) {
292 if ((fnode = find_named_node (*pending_state, X_("freeze-info"))) != 0) {
295 _freeze_record.have_mementos = false;
296 _freeze_record.state = Frozen;
298 for (vector<FreezeRecordInsertInfo*>::iterator i = _freeze_record.insert_info.begin(); i != _freeze_record.insert_info.end(); ++i) {
301 _freeze_record.insert_info.clear ();
303 if ((prop = fnode->property (X_("playlist"))) != 0) {
304 Playlist* pl = _session.playlist_by_name (prop->value());
306 _freeze_record.playlist = dynamic_cast<MidiPlaylist*> (pl);
308 _freeze_record.playlist = 0;
309 _freeze_record.state = NoFreeze;
314 if ((prop = fnode->property (X_("state"))) != 0) {
315 _freeze_record.state = (FreezeState) atoi (prop->value().c_str());
318 XMLNodeConstIterator citer;
319 XMLNodeList clist = fnode->children();
321 for (citer = clist.begin(); citer != clist.end(); ++citer) {
322 if ((*citer)->name() != X_("insert")) {
326 if ((prop = (*citer)->property (X_("id"))) == 0) {
330 FreezeRecordInsertInfo* frii = new FreezeRecordInsertInfo (*((*citer)->children().front()),
331 boost::shared_ptr<Insert>());
332 frii->id = prop->value ();
333 _freeze_record.insert_info.push_back (frii);
337 /* Alignment: act as a proxy for the diskstream */
339 if ((fnode = find_named_node (*pending_state, X_("alignment"))) != 0) {
341 if ((prop = fnode->property (X_("style"))) != 0) {
342 if (prop->value() == "existing") {
343 _diskstream->set_persistent_align_style (ExistingMaterial);
344 } else if (prop->value() == "capture") {
345 _diskstream->set_persistent_align_style (CaptureTime);
353 MidiTrack::n_process_buffers ()
355 return max (_diskstream->n_channels(), redirect_max_outs);
359 MidiTrack::no_roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset,
360 bool session_state_changing, bool can_record, bool rec_monitors_input)
362 if (n_outputs().get(DataType::MIDI) == 0) {
368 silence (nframes, offset);
372 if (session_state_changing) {
374 /* XXX is this safe to do against transport state changes? */
376 passthru_silence (start_frame, end_frame, nframes, offset, 0, false);
380 midi_diskstream().check_record_status (start_frame, nframes, can_record);
384 if (_have_internal_generator) {
385 /* since the instrument has no input streams,
386 there is no reason to send any signal
392 if (_session.get_auto_input()) {
393 if (Config->get_use_sw_monitoring()) {
394 send_silence = false;
399 if (_diskstream->record_enabled()) {
400 if (Config->get_use_sw_monitoring()) {
401 send_silence = false;
411 apply_gain_automation = false;
415 /* if we're sending silence, but we want the meters to show levels for the signal,
419 if (_have_internal_generator) {
420 passthru_silence (start_frame, end_frame, nframes, offset, 0, true);
422 if (_meter_point == MeterInput) {
423 just_meter_input (start_frame, end_frame, nframes, offset);
425 passthru_silence (start_frame, end_frame, nframes, offset, 0, false);
430 /* we're sending signal, but we may still want to meter the input.
433 passthru (start_frame, end_frame, nframes, offset, 0, (_meter_point == MeterInput));
440 MidiTrack::roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset, int declick,
441 bool can_record, bool rec_monitors_input)
444 MidiDiskstream& diskstream = midi_diskstream();
447 Glib::RWLock::ReaderLock lm (redirect_lock, Glib::TRY_LOCK);
449 // automation snapshot can also be called from the non-rt context
450 // and it uses the redirect list, so we take the lock out here
451 automation_snapshot (start_frame);
455 if (n_outputs().get_total() == 0 && _redirects.empty()) {
460 silence (nframes, offset);
464 jack_nframes_t transport_frame = _session.transport_frame();
466 if ((nframes = check_initial_delay (nframes, offset, transport_frame)) == 0) {
467 /* need to do this so that the diskstream sets its
468 playback distance to zero, thus causing diskstream::commit
471 return diskstream.process (transport_frame, 0, 0, can_record, rec_monitors_input);
476 if ((dret = diskstream.process (transport_frame, nframes, offset, can_record, rec_monitors_input)) != 0) {
478 silence (nframes, offset);
483 /* special condition applies */
485 if (_meter_point == MeterInput) {
486 just_meter_input (start_frame, end_frame, nframes, offset);
489 if (diskstream.record_enabled() && !can_record && !_session.get_auto_input()) {
491 /* not actually recording, but we want to hear the input material anyway,
492 at least potentially (depending on monitoring options)
495 passthru (start_frame, end_frame, nframes, offset, 0, true);
499 XXX is it true that the earlier test on n_outputs()
500 means that we can avoid checking it again here? i think
501 so, because changing the i/o configuration of an IO
502 requires holding the AudioEngine lock, which we hold
503 while in the process() tree.
507 /* copy the diskstream data to all output buffers */
509 //const size_t limit = n_process_buffers().get(DataType::AUDIO);
510 BufferSet& bufs = _session.get_scratch_buffers (n_process_buffers());
512 diskstream.get_playback(bufs.get_midi(0), start_frame, end_frame);
514 process_output_buffers (bufs, start_frame, end_frame, nframes, offset,
515 (!_session.get_record_enabled() || !_session.get_do_not_record_plugins()), declick, (_meter_point != MeterInput));
523 MidiTrack::silent_roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset,
524 bool can_record, bool rec_monitors_input)
526 if (n_outputs().get(DataType::MIDI) == 0 && _redirects.empty()) {
531 silence (nframes, offset);
536 apply_gain_automation = false;
538 silence (nframes, offset);
540 return midi_diskstream().process (_session.transport_frame() + offset, nframes, offset, can_record, rec_monitors_input);
544 MidiTrack::process_output_buffers (BufferSet& bufs,
545 jack_nframes_t start_frame, jack_nframes_t end_frame,
546 jack_nframes_t nframes, jack_nframes_t offset, bool with_redirects, int declick,
549 // There's no such thing as a MIDI bus for the time being, to avoid diverging from trunk
550 // too much until the SoC settles down. We'll do all the MIDI route work here for now
552 // Main output stage is the only stage we've got.
553 // I think it's a pretty good stage though, wouldn't you say?
557 IO::silence(nframes, offset);
561 deliver_output(bufs, start_frame, end_frame, nframes, offset);
568 MidiTrack::set_name (string str, void *src)
572 if (record_enabled() && _session.actively_recording()) {
573 /* this messes things up if done while recording */
577 if (_diskstream->set_name (str)) {
581 /* save state so that the statefile fully reflects any filename changes */
583 if ((ret = IO::set_name (str, src)) == 0) {
584 _session.save_state ("");
590 MidiTrack::export_stuff (BufferSet& bufs, jack_nframes_t nframes, jack_nframes_t end_frame)
596 MidiTrack::set_latency_delay (jack_nframes_t longest_session_latency)
598 Route::set_latency_delay (longest_session_latency);
599 _diskstream->set_roll_delay (_roll_delay);
603 MidiTrack::bounce (InterThreadInfo& itt)
606 //vector<MidiSource*> srcs;
607 //_session.write_one_midi_track (*this, 0, _session.current_end_frame(), false, srcs, itt);
612 MidiTrack::bounce_range (jack_nframes_t start, jack_nframes_t end, InterThreadInfo& itt)
615 //vector<MidiSource*> srcs;
616 //_session.write_one_midi_track (*this, start, end, false, srcs, itt);
620 MidiTrack::freeze (InterThreadInfo& itt)
625 MidiTrack::unfreeze ()
627 _freeze_record.state = UnFrozen;
628 FreezeChange (); /* EMIT SIGNAL */
632 MidiTrack::set_mode (TrackMode m)
637 _diskstream->set_destructive (m == Destructive);