2 Copyright (C) 2000-2003 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 #include "pbd/error.h"
33 #include "pbd/basename.h"
34 #include <glibmm/thread.h>
35 #include "pbd/xml++.h"
36 #include "pbd/memento_command.h"
37 #include "pbd/enumwriter.h"
38 #include "pbd/stateful_diff_command.h"
40 #include "ardour/ardour.h"
41 #include "ardour/audioengine.h"
42 #include "ardour/butler.h"
43 #include "ardour/configuration.h"
44 #include "ardour/cycle_timer.h"
45 #include "ardour/debug.h"
46 #include "ardour/io.h"
47 #include "ardour/midi_diskstream.h"
48 #include "ardour/midi_playlist.h"
49 #include "ardour/midi_port.h"
50 #include "ardour/midi_region.h"
51 #include "ardour/playlist_factory.h"
52 #include "ardour/region_factory.h"
53 #include "ardour/send.h"
54 #include "ardour/session.h"
55 #include "ardour/smf_source.h"
56 #include "ardour/utils.h"
57 #include "ardour/session_playlists.h"
59 #include "midi++/types.h"
65 using namespace ARDOUR;
68 nframes_t MidiDiskstream::midi_readahead = 4096;
70 MidiDiskstream::MidiDiskstream (Session &sess, const string &name, Diskstream::Flag flag)
71 : Diskstream(sess, name, flag)
75 , _last_flush_frame(0)
76 , _note_mode(Sustained)
77 , _frames_written_to_ringbuffer(0)
78 , _frames_read_from_ringbuffer(0)
80 /* prevent any write sources from being created */
89 assert(!destructive());
92 MidiDiskstream::MidiDiskstream (Session& sess, const XMLNode& node)
93 : Diskstream(sess, node)
97 , _last_flush_frame(0)
98 , _note_mode(Sustained)
99 , _frames_written_to_ringbuffer(0)
100 , _frames_read_from_ringbuffer(0)
105 if (set_state (node, Stateful::loading_state_version)) {
106 in_set_state = false;
107 throw failed_constructor();
110 in_set_state = false;
113 use_destructive_playlist ();
118 MidiDiskstream::init (Diskstream::Flag f)
122 /* there are no channels at this point, so these
123 two calls just get speed_buffer_size and wrap_buffer
124 size setup without duplicating their code.
127 set_block_size (_session.get_block_size());
128 allocate_temporary_buffers ();
130 const size_t size = _session.butler()->midi_diskstream_buffer_size();
131 _playback_buf = new MidiRingBuffer<nframes_t>(size);
132 _capture_buf = new MidiRingBuffer<nframes_t>(size);
134 _n_channels = ChanCount(DataType::MIDI, 1);
136 assert(recordable());
139 MidiDiskstream::~MidiDiskstream ()
141 Glib::Mutex::Lock lm (state_lock);
146 MidiDiskstream::non_realtime_locate (nframes_t position)
149 _write_source->set_timeline_position (position);
151 seek(position, false);
156 MidiDiskstream::non_realtime_input_change ()
159 Glib::Mutex::Lock lm (state_lock);
161 if (input_change_pending == NoChange) {
165 if (input_change_pending & ConfigurationChanged) {
166 if (_io->n_ports().n_midi() != _n_channels.n_midi()) {
167 error << "Can not feed IO " << _io->n_ports()
168 << " with diskstream " << _n_channels << endl;
172 get_input_sources ();
173 set_capture_offset ();
175 if (first_input_change) {
176 set_align_style (_persistent_alignment_style);
177 first_input_change = false;
179 set_align_style_from_io ();
182 input_change_pending = NoChange;
184 /* implicit unlock */
187 /* reset capture files */
189 reset_write_sources (false);
191 /* now refill channel buffers */
193 if (speed() != 1.0f || speed() != -1.0f) {
194 seek ((nframes_t) (_session.transport_frame() * (double) speed()));
197 seek (_session.transport_frame());
200 _last_flush_frame = _session.transport_frame();
204 MidiDiskstream::get_input_sources ()
206 uint32_t ni = _io->n_ports().n_midi();
212 // This is all we do for now at least
215 _source_port = _io->midi(0);
221 MidiDiskstream::find_and_use_playlist (const string& name)
223 boost::shared_ptr<MidiPlaylist> playlist;
225 if ((playlist = boost::dynamic_pointer_cast<MidiPlaylist> (_session.playlists->by_name (name))) == 0) {
226 playlist = boost::dynamic_pointer_cast<MidiPlaylist> (PlaylistFactory::create (DataType::MIDI, _session, name));
230 error << string_compose(_("MidiDiskstream: Playlist \"%1\" isn't an midi playlist"), name) << endmsg;
234 return use_playlist (playlist);
238 MidiDiskstream::use_playlist (boost::shared_ptr<Playlist> playlist)
240 assert(boost::dynamic_pointer_cast<MidiPlaylist>(playlist));
242 Diskstream::use_playlist(playlist);
248 MidiDiskstream::use_new_playlist ()
251 boost::shared_ptr<MidiPlaylist> playlist;
253 if (!in_set_state && destructive()) {
258 newname = Playlist::bump_name (_playlist->name(), _session);
260 newname = Playlist::bump_name (_name, _session);
263 if ((playlist = boost::dynamic_pointer_cast<MidiPlaylist> (PlaylistFactory::create (
264 DataType::MIDI, _session, newname, hidden()))) != 0) {
266 playlist->set_orig_diskstream_id (id());
267 return use_playlist (playlist);
275 MidiDiskstream::use_copy_playlist ()
277 assert(midi_playlist());
283 if (_playlist == 0) {
284 error << string_compose(_("MidiDiskstream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
289 boost::shared_ptr<MidiPlaylist> playlist;
291 newname = Playlist::bump_name (_playlist->name(), _session);
293 if ((playlist = boost::dynamic_pointer_cast<MidiPlaylist>(PlaylistFactory::create (midi_playlist(), newname))) != 0) {
294 playlist->set_orig_diskstream_id (id());
295 return use_playlist (playlist);
301 /** Overloaded from parent to die horribly
304 MidiDiskstream::set_destructive (bool yn)
306 assert( ! destructive());
312 MidiDiskstream::set_note_mode (NoteMode m)
315 midi_playlist()->set_note_mode(m);
316 if (_write_source && _write_source->model())
317 _write_source->model()->set_note_mode(m);
322 trace_midi (ostream& o, MIDI::byte *msg, size_t len)
324 using namespace MIDI;
326 const char trace_prefix = ':';
328 type = (eventType) (msg[0]&0xF0);
335 << " NoteOff NoteNum "
346 << " NoteOn NoteNum "
362 case MIDI::controller:
377 << " Program PropertyChange ProgNum "
386 << " Channel Pressure "
391 case MIDI::pitchbend:
396 << ((msg[2]<<7)|msg[1])
435 << "System Exclusive (1 byte : " << hex << (int) *msg << dec << ')'
441 << "System Exclusive (" << len << ") = [ " << hex;
442 for (unsigned int i = 0; i < len; ++i) {
443 o << (int) msg[i] << ' ';
445 o << dec << ']' << endl;
451 o << trace_prefix << "Song" << endl;
455 o << trace_prefix << "Tune" << endl;
459 o << trace_prefix << "End-of-System Exclusive" << endl;
463 o << trace_prefix << "Timing" << endl;
467 o << trace_prefix << "Start" << endl;
471 o << trace_prefix << "Stop" << endl;
475 o << trace_prefix << "Continue" << endl;
479 o << trace_prefix << "Active Sense" << endl;
483 o << trace_prefix << "Unrecognized MIDI message" << endl;
490 MidiDiskstream::process (nframes_t transport_frame, nframes_t nframes, bool can_record, bool rec_monitors_input)
493 nframes_t rec_offset = 0;
494 nframes_t rec_nframes = 0;
495 bool nominally_recording;
496 bool re = record_enabled ();
498 /* if we've already processed the frames corresponding to this call,
499 just return. this allows multiple routes that are taking input
500 from this diskstream to call our ::process() method, but have
501 this stuff only happen once. more commonly, it allows both
502 the AudioTrack that is using this AudioDiskstream *and* the Session
503 to call process() without problems.
510 commit_should_unlock = false;
512 check_record_status (transport_frame, nframes, can_record);
514 nominally_recording = (can_record && re);
521 /* This lock is held until the end of ::commit, so these two functions
522 must always be called as a pair. The only exception is if this function
523 returns a non-zero value, in which case, ::commit should not be called.
526 // If we can't take the state lock return.
527 if (!state_lock.trylock()) {
530 commit_should_unlock = true;
531 adjust_capture_position = 0;
533 if (nominally_recording || (_session.get_record_enabled() && _session.config.get_punch_in())) {
534 OverlapType ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
536 calculate_record_range(ot, transport_frame, nframes, rec_nframes, rec_offset);
538 if (rec_nframes && !was_recording) {
539 capture_captured = 0;
540 was_recording = true;
545 if (can_record && !_last_capture_regions.empty()) {
546 _last_capture_regions.clear ();
549 if (nominally_recording || rec_nframes) {
551 // Pump entire port buffer into the ring buffer (FIXME: split cycles?)
552 MidiBuffer& buf = _source_port->get_midi_buffer(nframes);
553 for (MidiBuffer::iterator i = buf.begin(); i != buf.end(); ++i) {
554 const Evoral::MIDIEvent<MidiBuffer::TimeType> ev(*i, false);
556 _capture_buf->write(ev.time() + transport_frame, ev.type(), ev.size(), ev.buffer());
562 finish_capture (rec_monitors_input);
569 /* data will be written to disk */
571 if (rec_nframes == nframes && rec_offset == 0) {
572 playback_distance = nframes;
575 adjust_capture_position = rec_nframes;
577 } else if (nominally_recording) {
579 /* XXXX do this for MIDI !!!
580 can't do actual capture yet - waiting for latency effects to finish before we start
583 playback_distance = nframes;
593 /* we're exiting with failure, so ::commit will not
594 be called. unlock the state lock.
597 commit_should_unlock = false;
605 MidiDiskstream::commit (nframes_t nframes)
607 bool need_butler = false;
609 if (_actual_speed < 0.0) {
610 playback_sample -= playback_distance;
612 playback_sample += playback_distance;
615 if (adjust_capture_position != 0) {
616 capture_captured += adjust_capture_position;
617 adjust_capture_position = 0;
620 uint32_t frames_read = g_atomic_int_get(&_frames_read_from_ringbuffer);
621 uint32_t frames_written = g_atomic_int_get(&_frames_written_to_ringbuffer);
622 if ((frames_written - frames_read) + nframes < midi_readahead) {
626 /*cerr << "MDS written: " << frames_written << " - read: " << frames_read <<
627 " = " << frames_written - frames_read
628 << " + " << nframes << " < " << midi_readahead << " = " << need_butler << ")" << endl;*/
630 if (commit_should_unlock) {
640 MidiDiskstream::set_pending_overwrite (bool yn)
642 /* called from audio thread, so we can use the read ptr and playback sample as we wish */
644 pending_overwrite = yn;
646 overwrite_frame = playback_sample;
650 MidiDiskstream::overwrite_existing_buffers ()
652 //read(overwrite_frame, disk_io_chunk_frames, false);
653 overwrite_queued = false;
654 pending_overwrite = false;
660 MidiDiskstream::seek (nframes_t frame, bool complete_refill)
662 Glib::Mutex::Lock lm (state_lock);
665 _playback_buf->reset();
666 _capture_buf->reset();
667 g_atomic_int_set(&_frames_read_from_ringbuffer, 0);
668 g_atomic_int_set(&_frames_written_to_ringbuffer, 0);
670 playback_sample = frame;
673 if (complete_refill) {
674 while ((ret = do_refill_with_alloc ()) > 0) ;
676 ret = do_refill_with_alloc ();
683 MidiDiskstream::can_internal_playback_seek (nframes_t distance)
685 uint32_t frames_read = g_atomic_int_get(&_frames_read_from_ringbuffer);
686 uint32_t frames_written = g_atomic_int_get(&_frames_written_to_ringbuffer);
687 return ((frames_written - frames_read) < distance);
691 MidiDiskstream::internal_playback_seek (nframes_t distance)
693 first_recordable_frame += distance;
694 playback_sample += distance;
699 /** @a start is set to the new frame position (TIME) read up to */
701 MidiDiskstream::read (nframes_t& start, nframes_t dur, bool reversed)
703 nframes_t this_read = 0;
705 nframes_t loop_end = 0;
706 nframes_t loop_start = 0;
711 nframes_t loop_length = 0;
713 /* Make the use of a Location atomic for this read operation.
715 Note: Locations don't get deleted, so all we care about
716 when I say "atomic" is that we are always pointing to
717 the same one and using a start/length values obtained
721 if ((loc = loop_location) != 0) {
722 loop_start = loc->start();
723 loop_end = loc->end();
724 loop_length = loop_end - loop_start;
727 /* if we are looping, ensure that the first frame we read is at the correct
728 position within the loop.
731 if (loc && (start >= loop_end)) {
732 //cerr << "start adjusted from " << start;
733 start = loop_start + ((start - loop_start) % loop_length);
734 //cerr << "to " << start << endl;
736 //cerr << "start is " << start << " loopstart: " << loop_start << " loopend: " << loop_end << endl;
741 /* take any loop into account. we can't read past the end of the loop. */
743 if (loc && (loop_end - start < dur)) {
744 this_read = loop_end - start;
745 //cerr << "reloop true: thisread: " << this_read << " dur: " << dur << endl;
752 if (this_read == 0) {
756 this_read = min(dur,this_read);
758 if (midi_playlist()->read (*_playback_buf, start, this_read) != this_read) {
759 error << string_compose(
760 _("MidiDiskstream %1: cannot read %2 from playlist at frame %3"),
761 _id, this_read, start) << endmsg;
765 g_atomic_int_add(&_frames_written_to_ringbuffer, this_read);
767 _read_data_count = _playlist->read_data_count();
771 // Swap note ons with note offs here. etc?
772 // Fully reversing MIDI requires look-ahead (well, behind) to find previous
773 // CC values etc. hard.
777 /* if we read to the end of the loop, go back to the beginning */
780 // Synthesize LoopEvent here, because the next events
781 // written will have non-monotonic timestamps.
782 _playback_buf->write(loop_end - 1, LoopEventType, 0, 0);
783 cout << "Pushing LoopEvent ts=" << loop_end-1
784 << " start+this_read " << start+this_read << endl;
793 //offset += this_read;
800 MidiDiskstream::do_refill_with_alloc ()
806 MidiDiskstream::do_refill ()
809 size_t write_space = _playback_buf->write_space();
810 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
812 if (write_space == 0) {
820 /* at end: nothing to do */
821 if (file_frame == max_frames) {
825 // At this point we...
826 assert(_playback_buf->write_space() > 0); // ... have something to write to, and
827 assert(file_frame <= max_frames); // ... something to write
829 // now calculate how much time is in the ringbuffer.
830 // and lets write as much as we need to get this to be midi_readahead;
831 uint32_t frames_read = g_atomic_int_get(&_frames_read_from_ringbuffer);
832 uint32_t frames_written = g_atomic_int_get(&_frames_written_to_ringbuffer);
833 if ((frames_written - frames_read) >= midi_readahead) {
834 //cout << "MDS Nothing to do. all fine" << endl;
838 nframes_t to_read = midi_readahead - (frames_written - frames_read);
840 //cout << "MDS read for midi_readahead " << to_read << " rb_contains: "
841 // << frames_written - frames_read << endl;
843 to_read = min(to_read, (max_frames - file_frame));
845 if (read (file_frame, to_read, reversed)) {
852 /** Flush pending data to disk.
854 * Important note: this function will write *AT MOST* disk_io_chunk_frames
855 * of data to disk. it will never write more than that. If it writes that
856 * much and there is more than that waiting to be written, it will return 1,
857 * otherwise 0 on success or -1 on failure.
859 * If there is less than disk_io_chunk_frames to be written, no data will be
860 * written at all unless @a force_flush is true.
863 MidiDiskstream::do_flush (RunContext /*context*/, bool force_flush)
869 _write_data_count = 0;
871 total = _session.transport_frame() - _last_flush_frame;
873 if (_last_flush_frame > _session.transport_frame()
874 || _last_flush_frame < capture_start_frame) {
875 _last_flush_frame = _session.transport_frame();
878 if (total == 0 || _capture_buf->read_space() == 0
879 || (!force_flush && (total < disk_io_chunk_frames && was_recording))) {
883 /* if there are 2+ chunks of disk i/o possible for
884 this track, let the caller know so that it can arrange
885 for us to be called again, ASAP.
887 if we are forcing a flush, then if there is* any* extra
888 work, let the caller know.
890 if we are no longer recording and there is any extra work,
891 let the caller know too.
894 if (total >= 2 * disk_io_chunk_frames || ((force_flush || !was_recording) && total > disk_io_chunk_frames)) {
898 to_write = disk_io_chunk_frames;
900 assert(!destructive());
903 && ( (_session.transport_frame() - _last_flush_frame > disk_io_chunk_frames)
905 if ((!_write_source) || _write_source->midi_write (*_capture_buf, capture_start_frame, to_write) != to_write) {
906 error << string_compose(_("MidiDiskstream %1: cannot write to disk"), _id) << endmsg;
909 _last_flush_frame = _session.transport_frame();
918 MidiDiskstream::transport_stopped (struct tm& /*when*/, time_t /*twhen*/, bool abort_capture)
920 bool more_work = true;
922 boost::shared_ptr<MidiRegion> region;
923 MidiRegion::SourceList srcs;
924 MidiRegion::SourceList::iterator src;
925 vector<CaptureInfo*>::iterator ci;
926 bool mark_write_completed = false;
928 finish_capture (true);
930 /* butler is already stopped, but there may be work to do
931 to flush remaining data to disk.
934 while (more_work && !err) {
935 switch (do_flush (TransportContext, true)) {
942 error << string_compose(_("MidiDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
947 /* XXX is there anything we can do if err != 0 ? */
948 Glib::Mutex::Lock lm (capture_info_lock);
950 if (capture_info.empty()) {
958 _write_source->mark_for_remove ();
959 _write_source->drop_references ();
960 _write_source.reset();
963 /* new source set up in "out" below */
967 assert(_write_source);
969 nframes_t total_capture = 0;
970 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
971 total_capture += (*ci)->frames;
974 /* figure out the name for this take */
976 srcs.push_back (_write_source);
977 _write_source->set_timeline_position (capture_info.front()->start);
978 _write_source->set_captured_for (_name);
980 string whole_file_region_name;
981 whole_file_region_name = region_name_from_path (_write_source->name(), true);
983 /* Register a new region with the Session that
984 describes the entire source. Do this first
985 so that any sub-regions will obviously be
986 children of this one (later!)
992 plist.add (Properties::name, whole_file_region_name);
993 plist.add (Properties::whole_file, true);
994 plist.add (Properties::automatic, true);
995 plist.add (Properties::start, 0);
996 plist.add (Properties::length, total_capture);
997 plist.add (Properties::layer, 0);
999 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
1001 region = boost::dynamic_pointer_cast<MidiRegion> (rx);
1002 region->special_set_position (capture_info.front()->start);
1006 catch (failed_constructor& err) {
1007 error << string_compose(_("%1: could not create region for complete midi file"), _name) << endmsg;
1011 _last_capture_regions.push_back (region);
1013 // cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
1015 _playlist->clear_history ();
1016 _playlist->freeze ();
1018 uint32_t buffer_position = 0;
1019 for (buffer_position = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1023 _session.region_name (region_name, _write_source->name(), false);
1025 // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add a region\n";
1030 plist.add (Properties::start, buffer_position);
1031 plist.add (Properties::length, (*ci)->frames);
1032 plist.add (Properties::name, region_name);
1034 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
1035 region = boost::dynamic_pointer_cast<MidiRegion> (rx);
1038 catch (failed_constructor& err) {
1039 error << _("MidiDiskstream: could not create region for captured midi!") << endmsg;
1040 continue; /* XXX is this OK? */
1043 region->DropReferences.connect_same_thread (*this, boost::bind (&Diskstream::remove_region_from_last_capture, this, boost::weak_ptr<Region>(region)));
1045 _last_capture_regions.push_back (region);
1047 // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
1049 i_am_the_modifier++;
1050 _playlist->add_region (region, (*ci)->start);
1051 i_am_the_modifier--;
1053 buffer_position += (*ci)->frames;
1057 _session.add_command (new StatefulDiffCommand(_playlist));
1061 mark_write_completed = true;
1063 reset_write_sources (mark_write_completed);
1065 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1070 midi_playlist()->clear_note_trackers ();
1073 capture_info.clear ();
1074 capture_start_frame = 0;
1078 MidiDiskstream::transport_looped (nframes_t transport_frame)
1080 if (was_recording) {
1082 // adjust the capture length knowing that the data will be recorded to disk
1083 // only necessary after the first loop where we're recording
1084 if (capture_info.size() == 0) {
1085 capture_captured += _capture_offset;
1087 if (_alignment_style == ExistingMaterial) {
1088 capture_captured += _session.worst_output_latency();
1090 capture_captured += _roll_delay;
1094 finish_capture (true);
1096 // the next region will start recording via the normal mechanism
1097 // we'll set the start position to the current transport pos
1098 // no latency adjustment or capture offset needs to be made, as that already happened the first time
1099 capture_start_frame = transport_frame;
1100 first_recordable_frame = transport_frame; // mild lie
1101 last_recordable_frame = max_frames;
1102 was_recording = true;
1107 MidiDiskstream::finish_capture (bool /*rec_monitors_input*/)
1109 was_recording = false;
1111 if (capture_captured == 0) {
1115 // Why must we destroy?
1116 assert(!destructive());
1118 CaptureInfo* ci = new CaptureInfo;
1120 ci->start = capture_start_frame;
1121 ci->frames = capture_captured;
1123 /* XXX theoretical race condition here. Need atomic exchange ?
1124 However, the circumstances when this is called right
1125 now (either on record-disable or transport_stopped)
1126 mean that no actual race exists. I think ...
1127 We now have a capture_info_lock, but it is only to be used
1128 to synchronize in the transport_stop and the capture info
1129 accessors, so that invalidation will not occur (both non-realtime).
1132 // cerr << "Finish capture, add new CI, " << ci->start << '+' << ci->frames << endl;
1134 capture_info.push_back (ci);
1135 capture_captured = 0;
1139 MidiDiskstream::set_record_enabled (bool yn)
1141 if (!recordable() || !_session.record_enabling_legal()) {
1145 assert(!destructive());
1147 /* yes, i know that this not proof against race conditions, but its
1148 good enough. i think.
1151 if (record_enabled() != yn) {
1153 engage_record_enable ();
1155 disengage_record_enable ();
1161 MidiDiskstream::engage_record_enable ()
1163 bool rolling = _session.transport_speed() != 0.0f;
1165 g_atomic_int_set (&_record_enabled, 1);
1167 if (_source_port && Config->get_monitoring_model() == HardwareMonitoring) {
1168 _source_port->request_monitor_input (!(_session.config.get_auto_input() && rolling));
1171 // FIXME: Why is this necessary? Isn't needed for AudioDiskstream...
1173 use_new_write_source();
1175 _write_source->mark_streaming_midi_write_started (_note_mode, _session.transport_frame());
1177 RecordEnableChanged (); /* EMIT SIGNAL */
1181 MidiDiskstream::disengage_record_enable ()
1183 g_atomic_int_set (&_record_enabled, 0);
1184 if (_source_port && Config->get_monitoring_model() == HardwareMonitoring) {
1186 _source_port->request_monitor_input (false);
1190 RecordEnableChanged (); /* EMIT SIGNAL */
1194 MidiDiskstream::get_state ()
1196 XMLNode* node = new XMLNode ("MidiDiskstream");
1198 LocaleGuard lg (X_("POSIX"));
1200 snprintf (buf, sizeof(buf), "0x%x", _flags);
1201 node->add_property ("flags", buf);
1203 node->add_property("channel-mode", enum_2_string(get_channel_mode()));
1205 snprintf (buf, sizeof(buf), "0x%x", get_channel_mask());
1206 node->add_property("channel-mask", buf);
1208 node->add_property ("playlist", _playlist->name());
1210 snprintf (buf, sizeof(buf), "%f", _visible_speed);
1211 node->add_property ("speed", buf);
1213 node->add_property("name", _name);
1214 id().print(buf, sizeof(buf));
1215 node->add_property("id", buf);
1217 if (_write_source && _session.get_record_enabled()) {
1219 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1220 XMLNode* cs_grandchild;
1222 cs_grandchild = new XMLNode (X_("file"));
1223 cs_grandchild->add_property (X_("path"), _write_source->path());
1224 cs_child->add_child_nocopy (*cs_grandchild);
1226 /* store the location where capture will start */
1230 if (_session.config.get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1231 snprintf (buf, sizeof (buf), "%" PRId64, pi->start());
1233 snprintf (buf, sizeof (buf), "%" PRId64, _session.transport_frame());
1236 cs_child->add_property (X_("at"), buf);
1237 node->add_child_nocopy (*cs_child);
1241 node->add_child_copy (*_extra_xml);
1248 MidiDiskstream::set_state (const XMLNode& node, int /*version*/)
1250 const XMLProperty* prop;
1251 XMLNodeList nlist = node.children();
1252 XMLNodeIterator niter;
1253 XMLNode* capture_pending_node = 0;
1254 LocaleGuard lg (X_("POSIX"));
1256 in_set_state = true;
1258 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1259 /*if ((*niter)->name() == IO::state_node_name) {
1260 deprecated_io_node = new XMLNode (**niter);
1262 assert ((*niter)->name() != IO::state_node_name);
1264 if ((*niter)->name() == X_("CapturingSources")) {
1265 capture_pending_node = *niter;
1269 /* prevent write sources from being created */
1271 in_set_state = true;
1273 if ((prop = node.property ("name")) != 0) {
1274 _name = prop->value();
1277 if ((prop = node.property ("id")) != 0) {
1278 _id = prop->value ();
1281 if ((prop = node.property ("flags")) != 0) {
1282 _flags = Flag (string_2_enum (prop->value(), _flags));
1285 ChannelMode channel_mode = AllChannels;
1286 if ((prop = node.property ("channel-mode")) != 0) {
1287 channel_mode = ChannelMode (string_2_enum(prop->value(), channel_mode));
1290 unsigned int channel_mask = 0xFFFF;
1291 if ((prop = node.property ("channel-mask")) != 0) {
1292 sscanf (prop->value().c_str(), "0x%x", &channel_mask);
1293 if (channel_mask & (~0xFFFF)) {
1294 warning << _("MidiDiskstream: XML property channel-mask out of range") << endmsg;
1298 set_channel_mode(channel_mode, channel_mask);
1300 if ((prop = node.property ("playlist")) == 0) {
1305 bool had_playlist = (_playlist != 0);
1307 if (find_and_use_playlist (prop->value())) {
1311 if (!had_playlist) {
1312 _playlist->set_orig_diskstream_id (_id);
1315 if (capture_pending_node) {
1316 use_pending_capture_data (*capture_pending_node);
1321 if ((prop = node.property ("speed")) != 0) {
1322 double sp = atof (prop->value().c_str());
1324 if (realtime_set_speed (sp, false)) {
1325 non_realtime_set_speed ();
1329 in_set_state = false;
1331 /* make sure this is clear before we do anything else */
1334 //_capturing_source = 0;
1336 /* write sources are handled when we handle the input set
1337 up of the IO that owns this DS (::non_realtime_input_change())
1340 in_set_state = false;
1346 MidiDiskstream::use_new_write_source (uint32_t n)
1348 if (!recordable()) {
1354 if (_write_source) {
1356 if (_write_source->is_empty ()) {
1357 _write_source->mark_for_remove ();
1358 _write_source.reset();
1360 _write_source.reset();
1365 _write_source = boost::dynamic_pointer_cast<SMFSource>(_session.create_midi_source_for_session (*this));
1366 if (!_write_source) {
1367 throw failed_constructor();
1371 catch (failed_constructor &err) {
1372 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1373 _write_source.reset();
1377 _write_source->set_allow_remove_if_empty (true);
1378 _write_source->mark_streaming_midi_write_started (_note_mode, _session.transport_frame());
1384 MidiDiskstream::reset_write_sources (bool mark_write_complete, bool /*force*/)
1386 if (!_session.writable() || !recordable()) {
1390 if (_write_source && mark_write_complete) {
1391 _write_source->mark_streaming_write_completed ();
1394 use_new_write_source (0);
1398 MidiDiskstream::rename_write_sources ()
1400 if (_write_source != 0) {
1401 _write_source->set_source_name (_name.val(), destructive());
1402 /* XXX what to do if this fails ? */
1408 MidiDiskstream::set_block_size (nframes_t /*nframes*/)
1413 MidiDiskstream::allocate_temporary_buffers ()
1418 MidiDiskstream::monitor_input (bool yn)
1421 _source_port->ensure_monitor_input (yn);
1425 MidiDiskstream::set_align_style_from_io ()
1427 bool have_physical = false;
1433 get_input_sources ();
1435 if (_source_port && _source_port->flags() & JackPortIsPhysical) {
1436 have_physical = true;
1439 if (have_physical) {
1440 set_align_style (ExistingMaterial);
1442 set_align_style (CaptureTime);
1448 MidiDiskstream::playback_buffer_load () const
1450 return (float) ((double) _playback_buf->read_space()/
1451 (double) _playback_buf->capacity());
1455 MidiDiskstream::capture_buffer_load () const
1457 return (float) ((double) _capture_buf->write_space()/
1458 (double) _capture_buf->capacity());
1462 MidiDiskstream::use_pending_capture_data (XMLNode& /*node*/)
1467 /** Writes playback events in the given range to \a dst, translating time stamps
1468 * so that an event at \a start has time = 0
1471 MidiDiskstream::get_playback (MidiBuffer& dst, nframes_t start, nframes_t end)
1474 assert(dst.size() == 0);
1476 // Reverse. ... We just don't do reverse, ok? Back off.
1481 // Translates stamps to be relative to start
1485 const size_t events_read = _playback_buf->read(dst, start, end);
1486 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("%1 MDS events read %2 range %3 .. %4 rspace %5 wspace %6\n", _name, events_read, start, end,
1487 _playback_buf->read_space(), _playback_buf->write_space()));
1489 _playback_buf->read(dst, start, end);
1492 gint32 frames_read = end - start;
1493 g_atomic_int_add(&_frames_read_from_ringbuffer, frames_read);