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 "pbd/enumwriter.h"
25 #include "midi++/events.h"
26 #include "evoral/midi_util.h"
28 #include "ardour/amp.h"
29 #include "ardour/buffer_set.h"
30 #include "ardour/delivery.h"
31 #include "ardour/io_processor.h"
32 #include "ardour/meter.h"
33 #include "ardour/midi_diskstream.h"
34 #include "ardour/midi_playlist.h"
35 #include "ardour/midi_region.h"
36 #include "ardour/midi_source.h"
37 #include "ardour/midi_track.h"
38 #include "ardour/panner.h"
39 #include "ardour/processor.h"
40 #include "ardour/route_group_specialized.h"
41 #include "ardour/session.h"
42 #include "ardour/utils.h"
47 using namespace ARDOUR;
50 MidiTrack::MidiTrack (Session& sess, string name, Route::Flag flag, TrackMode mode)
51 : Track (sess, name, flag, mode, DataType::MIDI)
52 , _immediate_events(1024) // FIXME: size?
53 , _note_mode(Sustained)
55 use_new_diskstream ();
58 _freeze_record.state = NoFreeze;
59 _saved_meter_point = _meter_point;
63 MidiTrack::MidiTrack (Session& sess, const XMLNode& node)
64 : Track (sess, node, DataType::MIDI )
65 , _immediate_events(1024) // FIXME: size?
66 , _note_mode(Sustained)
68 _set_state(node, false);
71 MidiTrack::~MidiTrack ()
76 MidiTrack::use_new_diskstream ()
78 MidiDiskstream::Flag dflags = MidiDiskstream::Flag (0);
80 if (_flags & Hidden) {
81 dflags = MidiDiskstream::Flag (dflags | MidiDiskstream::Hidden);
83 dflags = MidiDiskstream::Flag (dflags | MidiDiskstream::Recordable);
86 assert(_mode != Destructive);
88 boost::shared_ptr<MidiDiskstream> ds (new MidiDiskstream (_session, name(), dflags));
89 _session.add_diskstream (ds);
91 set_diskstream (boost::dynamic_pointer_cast<MidiDiskstream> (ds));
95 MidiTrack::set_diskstream (boost::shared_ptr<MidiDiskstream> ds)
98 _diskstream->set_io (*(_input.get()));
99 _diskstream->set_destructive (_mode == Destructive);
101 _diskstream->set_record_enabled (false);
102 //_diskstream->monitor_input (false);
104 ic_connection.disconnect();
105 ic_connection = _input->changed.connect (mem_fun (*_diskstream, &MidiDiskstream::handle_input_change));
107 DiskstreamChanged (); /* EMIT SIGNAL */
113 MidiTrack::use_diskstream (string name)
115 boost::shared_ptr<MidiDiskstream> dstream;
117 cerr << "\n\n\nMIDI use diskstream\n";
119 if ((dstream = boost::dynamic_pointer_cast<MidiDiskstream>(_session.diskstream_by_name (name))) == 0) {
120 error << string_compose(_("MidiTrack: midi diskstream \"%1\" not known by session"), name) << endmsg;
124 cerr << "\n\n\nMIDI found DS\n";
125 return set_diskstream (dstream);
129 MidiTrack::use_diskstream (const PBD::ID& id)
131 boost::shared_ptr<MidiDiskstream> dstream;
133 if ((dstream = boost::dynamic_pointer_cast<MidiDiskstream> (_session.diskstream_by_id (id))) == 0) {
134 error << string_compose(_("MidiTrack: midi diskstream \"%1\" not known by session"), id) << endmsg;
138 return set_diskstream (dstream);
141 boost::shared_ptr<MidiDiskstream>
142 MidiTrack::midi_diskstream() const
144 return boost::dynamic_pointer_cast<MidiDiskstream>(_diskstream);
148 MidiTrack::set_state (const XMLNode& node)
150 return _set_state (node, true);
154 MidiTrack::_set_state (const XMLNode& node, bool call_base)
156 const XMLProperty *prop;
157 XMLNodeConstIterator iter;
159 if (Route::_set_state (node, call_base)) {
163 // No destructive MIDI tracks (yet?)
166 if ((prop = node.property (X_("note-mode"))) != 0) {
167 _note_mode = NoteMode (string_2_enum (prop->value(), _note_mode));
169 _note_mode = Sustained;
172 if ((prop = node.property ("diskstream-id")) == 0) {
174 /* some old sessions use the diskstream name rather than the ID */
176 if ((prop = node.property ("diskstream")) == 0) {
177 fatal << _("programming error: MidiTrack given state without diskstream!") << endmsg;
182 if (use_diskstream (prop->value())) {
188 PBD::ID id (prop->value());
191 /* this wierd hack is used when creating tracks from a template. there isn't
192 a particularly good time to interpose between setting the first part of
193 the track state (notably Route::set_state() and the track mode), and the
194 second part (diskstream stuff). So, we have a special ID for the diskstream
195 that means "you should create a new diskstream here, not look for
199 cerr << "\n\n\n\n MIDI track " << name() << " found DS id " << id << endl;
202 use_new_diskstream ();
203 } else if (use_diskstream (id)) {
209 XMLNodeConstIterator niter;
212 nlist = node.children();
213 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
216 if (child->name() == X_("recenable")) {
217 _rec_enable_control->set_state (*child);
218 _session.add_controllable (_rec_enable_control);
222 pending_state = const_cast<XMLNode*> (&node);
224 if (_session.state_of_the_state() & Session::Loading) {
225 _session.StateReady.connect (mem_fun (*this, &MidiTrack::set_state_part_two));
227 set_state_part_two ();
234 MidiTrack::state(bool full_state)
236 XMLNode& root (Route::state(full_state));
237 XMLNode* freeze_node;
240 if (_freeze_record.playlist) {
243 freeze_node = new XMLNode (X_("freeze-info"));
244 freeze_node->add_property ("playlist", _freeze_record.playlist->name());
245 freeze_node->add_property ("state", enum_2_string (_freeze_record.state));
247 for (vector<FreezeRecordProcessorInfo*>::iterator i = _freeze_record.processor_info.begin(); i != _freeze_record.processor_info.end(); ++i) {
248 inode = new XMLNode (X_("processor"));
249 (*i)->id.print (buf, sizeof(buf));
250 inode->add_property (X_("id"), buf);
251 inode->add_child_copy ((*i)->state);
253 freeze_node->add_child_nocopy (*inode);
256 root.add_child_nocopy (*freeze_node);
259 /* Alignment: act as a proxy for the diskstream */
261 XMLNode* align_node = new XMLNode (X_("Alignment"));
262 AlignStyle as = _diskstream->alignment_style ();
263 align_node->add_property (X_("style"), enum_2_string (as));
264 root.add_child_nocopy (*align_node);
266 root.add_property (X_("note-mode"), enum_2_string (_note_mode));
268 /* we don't return diskstream state because we don't
269 own the diskstream exclusively. control of the diskstream
270 state is ceded to the Session, even if we create the
274 _diskstream->id().print (buf, sizeof(buf));
275 root.add_property ("diskstream-id", buf);
277 root.add_child_nocopy (_rec_enable_control->get_state());
283 MidiTrack::set_state_part_two ()
287 LocaleGuard lg (X_("POSIX"));
289 /* This is called after all session state has been restored but before
290 have been made ports and connections are established.
293 if (pending_state == 0) {
297 if ((fnode = find_named_node (*pending_state, X_("freeze-info"))) != 0) {
299 _freeze_record.state = Frozen;
301 for (vector<FreezeRecordProcessorInfo*>::iterator i = _freeze_record.processor_info.begin(); i != _freeze_record.processor_info.end(); ++i) {
304 _freeze_record.processor_info.clear ();
306 if ((prop = fnode->property (X_("playlist"))) != 0) {
307 boost::shared_ptr<Playlist> pl = _session.playlist_by_name (prop->value());
309 _freeze_record.playlist = boost::dynamic_pointer_cast<MidiPlaylist> (pl);
311 _freeze_record.playlist.reset();
312 _freeze_record.state = NoFreeze;
317 if ((prop = fnode->property (X_("state"))) != 0) {
318 _freeze_record.state = FreezeState (string_2_enum (prop->value(), _freeze_record.state));
321 XMLNodeConstIterator citer;
322 XMLNodeList clist = fnode->children();
324 for (citer = clist.begin(); citer != clist.end(); ++citer) {
325 if ((*citer)->name() != X_("processor")) {
329 if ((prop = (*citer)->property (X_("id"))) == 0) {
333 FreezeRecordProcessorInfo* frii = new FreezeRecordProcessorInfo (*((*citer)->children().front()),
334 boost::shared_ptr<Processor>());
335 frii->id = prop->value ();
336 _freeze_record.processor_info.push_back (frii);
340 /* Alignment: act as a proxy for the diskstream */
342 if ((fnode = find_named_node (*pending_state, X_("Alignment"))) != 0) {
344 if ((prop = fnode->property (X_("style"))) != 0) {
346 /* fix for older sessions from before EnumWriter */
350 if (prop->value() == "capture") {
351 pstr = "CaptureTime";
352 } else if (prop->value() == "existing") {
353 pstr = "ExistingMaterial";
355 pstr = prop->value();
358 AlignStyle as = AlignStyle (string_2_enum (pstr, as));
359 _diskstream->set_persistent_align_style (as);
366 MidiTrack::roll (nframes_t nframes, sframes_t start_frame, sframes_t end_frame, int declick,
367 bool can_record, bool rec_monitors_input)
370 boost::shared_ptr<MidiDiskstream> diskstream = midi_diskstream();
373 Glib::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
375 // automation snapshot can also be called from the non-rt context
376 // and it uses the redirect list, so we take the lock out here
377 automation_snapshot (start_frame);
381 if (n_outputs().n_total() == 0 && _processors.empty()) {
390 nframes_t transport_frame = _session.transport_frame();
393 if ((nframes = check_initial_delay (nframes, transport_frame)) == 0) {
394 /* need to do this so that the diskstream sets its
395 playback distance to zero, thus causing diskstream::commit
398 return diskstream->process (transport_frame, 0, can_record, rec_monitors_input);
403 if ((dret = diskstream->process (transport_frame, nframes, can_record, rec_monitors_input)) != 0) {
409 /* special condition applies */
411 if (_meter_point == MeterInput) {
412 _input->process_input (_meter, start_frame, end_frame, nframes);
415 if (diskstream->record_enabled() && !can_record && !_session.config.get_auto_input()) {
417 /* not actually recording, but we want to hear the input material anyway,
418 at least potentially (depending on monitoring options)
421 passthru (start_frame, end_frame, nframes, 0);
425 XXX is it true that the earlier test on n_outputs()
426 means that we can avoid checking it again here? i think
427 so, because changing the i/o configuration of an IO
428 requires holding the AudioEngine lock, which we hold
429 while in the process() tree.
433 /* copy the diskstream data to all output buffers */
435 //const size_t limit = n_process_buffers().n_audio();
436 BufferSet& bufs = _session.get_scratch_buffers (n_process_buffers());
438 diskstream->get_playback(bufs.get_midi(0), start_frame, end_frame);
440 process_output_buffers (bufs, start_frame, end_frame, nframes,
441 (!_session.get_record_enabled() || !Config->get_do_not_record_plugins()), declick);
445 _main_outs->flush (nframes);
451 MidiTrack::write_controller_messages(MidiBuffer& output_buf, sframes_t start, sframes_t end, nframes_t nframes)
453 // Append immediate events (UI controls)
455 // XXX TAKE _port_offset in Port into account???
457 _immediate_events.read (output_buf, 0, 0, nframes - 1); // all stamps = 0
461 MidiTrack::export_stuff (BufferSet& bufs, nframes_t nframes, sframes_t end_frame)
467 MidiTrack::set_latency_delay (nframes_t longest_session_latency)
469 Route::set_latency_delay (longest_session_latency);
470 _diskstream->set_roll_delay (_roll_delay);
473 boost::shared_ptr<Region>
474 MidiTrack::bounce (InterThreadInfo& itt)
477 // vector<MidiSource*> srcs;
478 // return _session.write_one_track (*this, 0, _session.current_end_frame(), false, srcs, itt);
479 return boost::shared_ptr<Region> ();
483 boost::shared_ptr<Region>
484 MidiTrack::bounce_range (nframes_t start, nframes_t end, InterThreadInfo& itt, bool enable_processing)
487 //vector<MidiSource*> srcs;
488 //return _session.write_one_track (*this, start, end, false, srcs, itt);
489 return boost::shared_ptr<Region> ();
493 MidiTrack::freeze (InterThreadInfo& itt)
498 MidiTrack::unfreeze ()
500 _freeze_record.state = UnFrozen;
501 FreezeChange (); /* EMIT SIGNAL */
505 MidiTrack::set_note_mode (NoteMode m)
508 midi_diskstream()->set_note_mode(m);
512 MidiTrack::midi_panic()
514 for (uint8_t channel = 0; channel <= 0xF; channel++) {
515 uint8_t ev[3] = { MIDI_CMD_CONTROL | channel, MIDI_CTL_SUSTAIN, 0 };
516 write_immediate_event(3, ev);
517 ev[1] = MIDI_CTL_ALL_NOTES_OFF;
518 write_immediate_event(3, ev);
519 ev[1] = MIDI_CTL_RESET_CONTROLLERS;
520 write_immediate_event(3, ev);
524 /** \return true on success, false on failure (no buffer space left)
527 MidiTrack::write_immediate_event(size_t size, const uint8_t* buf)
529 if (!Evoral::midi_event_is_valid(buf, size)) {
530 cerr << "WARNING: Ignoring illegal immediate MIDI event" << endl;
533 const uint32_t type = EventTypeMap::instance().midi_event_type(buf[0]);
534 return (_immediate_events.write(0, type, size, buf) == size);
538 MidiTrack::MidiControl::set_value(float val)
542 cerr << "MIDIControl value is infinity" << endl;
543 } else if (isnan(val)) {
544 cerr << "MIDIControl value is NaN" << endl;
545 } else if (val < _list->parameter().min()) {
546 cerr << "MIDIControl value is < " << _list->parameter().min() << endl;
547 } else if (val > _list->parameter().max()) {
548 cerr << "MIDIControl value is > " << _list->parameter().max() << endl;
557 assert(val <= _list->parameter().max());
560 if ( ! automation_playback()) {
561 uint8_t ev[3] = { _list->parameter().channel(), int(val), 0 };
562 switch(_list->parameter().type()) {
563 case MidiCCAutomation:
564 ev[0] += MIDI_CMD_CONTROL;
565 ev[1] = _list->parameter().id();
569 case MidiPgmChangeAutomation:
571 ev[0] += MIDI_CMD_PGM_CHANGE;
575 case MidiChannelPressureAutomation:
577 ev[0] += MIDI_CMD_CHANNEL_PRESSURE;
581 case MidiPitchBenderAutomation:
582 ev[0] += MIDI_CMD_BENDER;
583 ev[1] = 0x7F & int(val);
584 ev[2] = 0x7F & (int(val) >> 7);
590 _route->write_immediate_event(size, ev);
593 AutomationControl::set_value(val);