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"
39 #include "ardour/ardour.h"
40 #include "ardour/audioengine.h"
41 #include "ardour/configuration.h"
42 #include "ardour/cycle_timer.h"
43 #include "ardour/io.h"
44 #include "ardour/midi_diskstream.h"
45 #include "ardour/midi_playlist.h"
46 #include "ardour/midi_port.h"
47 #include "ardour/midi_region.h"
48 #include "ardour/playlist_factory.h"
49 #include "ardour/region_factory.h"
50 #include "ardour/send.h"
51 #include "ardour/session.h"
52 #include "ardour/smf_source.h"
53 #include "ardour/utils.h"
59 using namespace ARDOUR;
62 nframes_t MidiDiskstream::midi_readahead = 4096;
64 MidiDiskstream::MidiDiskstream (Session &sess, const string &name, Diskstream::Flag flag)
65 : Diskstream(sess, name, flag)
69 , _last_flush_frame(0)
70 , _note_mode(Sustained)
71 , _frames_written_to_ringbuffer(0)
72 , _frames_read_from_ringbuffer(0)
74 /* prevent any write sources from being created */
83 assert(!destructive());
86 MidiDiskstream::MidiDiskstream (Session& sess, const XMLNode& node)
87 : Diskstream(sess, node)
91 , _last_flush_frame(0)
92 , _note_mode(Sustained)
93 , _frames_written_to_ringbuffer(0)
94 , _frames_read_from_ringbuffer(0)
99 if (set_state (node)) {
100 in_set_state = false;
101 throw failed_constructor();
104 in_set_state = false;
107 use_destructive_playlist ();
112 MidiDiskstream::init (Diskstream::Flag f)
116 /* there are no channels at this point, so these
117 two calls just get speed_buffer_size and wrap_buffer
118 size setup without duplicating their code.
121 set_block_size (_session.get_block_size());
122 allocate_temporary_buffers ();
124 const size_t size = _session.midi_diskstream_buffer_size();
125 _playback_buf = new MidiRingBuffer<nframes_t>(size);
126 _capture_buf = new MidiRingBuffer<nframes_t>(size);
128 _n_channels = ChanCount(DataType::MIDI, 1);
130 assert(recordable());
133 MidiDiskstream::~MidiDiskstream ()
135 Glib::Mutex::Lock lm (state_lock);
140 MidiDiskstream::non_realtime_locate (nframes_t position)
143 _write_source->set_timeline_position (position);
145 seek(position, false);
150 MidiDiskstream::non_realtime_input_change ()
153 Glib::Mutex::Lock lm (state_lock);
155 if (input_change_pending == NoChange) {
159 if (input_change_pending & ConfigurationChanged) {
160 if (_io->n_inputs().n_midi() != _n_channels.n_midi()) {
161 error << "Can not feed IO " << _io->n_inputs()
162 << " with diskstream " << _n_channels << endl;
166 get_input_sources ();
167 set_capture_offset ();
169 if (first_input_change) {
170 set_align_style (_persistent_alignment_style);
171 first_input_change = false;
173 set_align_style_from_io ();
176 input_change_pending = NoChange;
178 /* implicit unlock */
181 /* reset capture files */
183 reset_write_sources (false);
185 /* now refill channel buffers */
187 if (speed() != 1.0f || speed() != -1.0f) {
188 seek ((nframes_t) (_session.transport_frame() * (double) speed()));
191 seek (_session.transport_frame());
194 _last_flush_frame = _session.transport_frame();
198 MidiDiskstream::get_input_sources ()
200 uint32_t ni = _io->n_inputs().n_midi();
206 // This is all we do for now at least
209 _source_port = _io->midi_input(0);
215 MidiDiskstream::find_and_use_playlist (const string& name)
217 boost::shared_ptr<MidiPlaylist> playlist;
219 if ((playlist = boost::dynamic_pointer_cast<MidiPlaylist> (_session.playlist_by_name (name))) == 0) {
220 playlist = boost::dynamic_pointer_cast<MidiPlaylist> (PlaylistFactory::create (DataType::MIDI, _session, name));
224 error << string_compose(_("MidiDiskstream: Playlist \"%1\" isn't an midi playlist"), name) << endmsg;
228 return use_playlist (playlist);
232 MidiDiskstream::use_playlist (boost::shared_ptr<Playlist> playlist)
234 assert(boost::dynamic_pointer_cast<MidiPlaylist>(playlist));
236 Diskstream::use_playlist(playlist);
242 MidiDiskstream::use_new_playlist ()
245 boost::shared_ptr<MidiPlaylist> playlist;
247 if (!in_set_state && destructive()) {
252 newname = Playlist::bump_name (_playlist->name(), _session);
254 newname = Playlist::bump_name (_name, _session);
257 if ((playlist = boost::dynamic_pointer_cast<MidiPlaylist> (PlaylistFactory::create (
258 DataType::MIDI, _session, newname, hidden()))) != 0) {
260 playlist->set_orig_diskstream_id (id());
261 return use_playlist (playlist);
269 MidiDiskstream::use_copy_playlist ()
271 assert(midi_playlist());
277 if (_playlist == 0) {
278 error << string_compose(_("MidiDiskstream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
283 boost::shared_ptr<MidiPlaylist> playlist;
285 newname = Playlist::bump_name (_playlist->name(), _session);
287 if ((playlist = boost::dynamic_pointer_cast<MidiPlaylist>(PlaylistFactory::create (midi_playlist(), newname))) != 0) {
288 playlist->set_orig_diskstream_id (id());
289 return use_playlist (playlist);
295 /** Overloaded from parent to die horribly
298 MidiDiskstream::set_destructive (bool yn)
300 assert( ! destructive());
306 MidiDiskstream::set_note_mode (NoteMode m)
309 midi_playlist()->set_note_mode(m);
310 if (_write_source && _write_source->model())
311 _write_source->model()->set_note_mode(m);
315 MidiDiskstream::check_record_status (nframes_t transport_frame, nframes_t nframes, bool can_record)
317 // FIXME: waaay too much code to duplicate (AudioDiskstream)
319 int possibly_recording;
322 const int transport_rolling = 0x4;
323 const int track_rec_enabled = 0x2;
324 const int global_rec_enabled = 0x1;
326 /* merge together the 3 factors that affect record status, and compute
330 rolling = _session.transport_speed() != 0.0f;
331 possibly_recording = (rolling << 2) | (record_enabled() << 1) | can_record;
332 change = possibly_recording ^ last_possibly_recording;
334 if (possibly_recording == last_possibly_recording) {
340 /* if per-track or global rec-enable turned on while the other was already on, we've started recording */
342 if (((change & track_rec_enabled) && record_enabled() && (!(change & global_rec_enabled) && can_record)) ||
343 ((change & global_rec_enabled) && can_record && (!(change & track_rec_enabled) && record_enabled()))) {
345 /* starting to record: compute first+last frames */
347 first_recordable_frame = transport_frame + _capture_offset;
348 last_recordable_frame = max_frames;
349 capture_start_frame = transport_frame;
351 if (!(last_possibly_recording & transport_rolling) && (possibly_recording & transport_rolling)) {
353 /* was stopped, now rolling (and recording) */
355 if (_alignment_style == ExistingMaterial) {
356 first_recordable_frame += _session.worst_output_latency();
358 first_recordable_frame += _roll_delay;
363 /* was rolling, but record state changed */
365 if (_alignment_style == ExistingMaterial) {
368 if (!Config->get_punch_in()) {
370 /* manual punch in happens at the correct transport frame
371 because the user hit a button. but to get alignment correct
372 we have to back up the position of the new region to the
373 appropriate spot given the roll delay.
376 capture_start_frame -= _roll_delay;
378 /* XXX paul notes (august 2005): i don't know why
382 first_recordable_frame += _capture_offset;
386 /* autopunch toggles recording at the precise
387 transport frame, and then the DS waits
388 to start recording for a time that depends
389 on the output latency.
392 first_recordable_frame += _session.worst_output_latency();
397 if (Config->get_punch_in()) {
398 first_recordable_frame += _roll_delay;
400 capture_start_frame -= _roll_delay;
406 } else if (!record_enabled() || !can_record) {
410 last_recordable_frame = transport_frame + _capture_offset;
412 if (_alignment_style == ExistingMaterial) {
413 last_recordable_frame += _session.worst_output_latency();
415 last_recordable_frame += _roll_delay;
419 last_possibly_recording = possibly_recording;
423 MidiDiskstream::process (nframes_t transport_frame, nframes_t nframes, bool can_record, bool rec_monitors_input)
425 // FIXME: waay too much code to duplicate (AudioDiskstream::process)
427 nframes_t rec_offset = 0;
428 nframes_t rec_nframes = 0;
429 bool nominally_recording;
430 bool re = record_enabled ();
431 bool collect_playback = true;
433 /* if we've already processed the frames corresponding to this call,
434 just return. this allows multiple routes that are taking input
435 from this diskstream to call our ::process() method, but have
436 this stuff only happen once. more commonly, it allows both
437 the AudioTrack that is using this AudioDiskstream *and* the Session
438 to call process() without problems.
445 commit_should_unlock = false;
447 check_record_status (transport_frame, nframes, can_record);
449 nominally_recording = (can_record && re);
456 /* This lock is held until the end of ::commit, so these two functions
457 must always be called as a pair. The only exception is if this function
458 returns a non-zero value, in which case, ::commit should not be called.
461 // If we can't take the state lock return.
462 if (!state_lock.trylock()) {
465 commit_should_unlock = true;
466 adjust_capture_position = 0;
468 if (nominally_recording || (_session.get_record_enabled() && Config->get_punch_in())) {
471 ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
478 case OverlapInternal:
479 /* ---------- recrange
482 rec_nframes = nframes;
487 /* |--------| recrange
490 rec_nframes = transport_frame + nframes - first_recordable_frame;
492 rec_offset = first_recordable_frame - transport_frame;
497 /* |--------| recrange
500 rec_nframes = last_recordable_frame - transport_frame;
504 case OverlapExternal:
505 /* |--------| recrange
506 -------------- transrange
508 rec_nframes = last_recordable_frame - last_recordable_frame;
509 rec_offset = first_recordable_frame - transport_frame;
513 if (rec_nframes && !was_recording) {
514 capture_captured = 0;
515 was_recording = true;
520 if (can_record && !_last_capture_regions.empty()) {
521 _last_capture_regions.clear ();
524 if (nominally_recording || rec_nframes) {
526 // Pump entire port buffer into the ring buffer (FIXME: split cycles?)
527 MidiBuffer& buf = _source_port->get_midi_buffer(nframes);
528 for (MidiBuffer::iterator i = buf.begin(); i != buf.end(); ++i) {
529 const Evoral::MIDIEvent<MidiBuffer::TimeType> ev(*i, false);
531 _capture_buf->write(ev.time() + transport_frame, ev.type(), ev.size(), ev.buffer());
537 finish_capture (rec_monitors_input);
544 /* data will be written to disk */
546 if (rec_nframes == nframes && rec_offset == 0) {
547 playback_distance = nframes;
550 adjust_capture_position = rec_nframes;
552 } else if (nominally_recording) {
554 /* can't do actual capture yet - waiting for latency effects to finish before we start*/
556 playback_distance = nframes;
557 collect_playback = false;
561 if (collect_playback) {
563 /* we're doing playback */
565 nframes_t necessary_samples;
567 /* no varispeed playback if we're recording, because the output .... TBD */
569 if (rec_nframes == 0 && _actual_speed != 1.0f) {
570 necessary_samples = (nframes_t) floor ((nframes * fabs (_actual_speed))) + 1;
572 necessary_samples = nframes;
575 // Pump entire port buffer into playback buffer (FIXME: split cycles?)
576 MidiBuffer& buf = _source_port->get_midi_buffer(nframes);
577 for (MidiBuffer::iterator i = buf.begin(); i != buf.end(); ++i) {
578 const Evoral::MIDIEvent<MidiBuffer::TimeType> ev(*i, false);
580 _playback_buf->write(ev.time() + transport_frame, ev.type(), ev.size(), ev.buffer());
590 /* we're exiting with failure, so ::commit will not
591 be called. unlock the state lock.
594 commit_should_unlock = false;
602 MidiDiskstream::commit (nframes_t nframes)
604 bool need_butler = false;
606 if (_actual_speed < 0.0) {
607 playback_sample -= playback_distance;
609 playback_sample += playback_distance;
612 if (adjust_capture_position != 0) {
613 capture_captured += adjust_capture_position;
614 adjust_capture_position = 0;
617 uint32_t frames_read = g_atomic_int_get(&_frames_read_from_ringbuffer);
618 uint32_t frames_written = g_atomic_int_get(&_frames_written_to_ringbuffer);
619 if ((frames_written - frames_read) + nframes < midi_readahead) {
623 /*cerr << "MDS written: " << frames_written << " - read: " << frames_read <<
624 " = " << frames_written - frames_read
625 << " + " << nframes << " < " << midi_readahead << " = " << need_butler << ")" << endl;*/
627 if (commit_should_unlock) {
637 MidiDiskstream::set_pending_overwrite (bool yn)
639 /* called from audio thread, so we can use the read ptr and playback sample as we wish */
641 pending_overwrite = yn;
643 overwrite_frame = playback_sample;
647 MidiDiskstream::overwrite_existing_buffers ()
649 //read(overwrite_frame, disk_io_chunk_frames, false);
650 overwrite_queued = false;
651 pending_overwrite = false;
657 MidiDiskstream::seek (nframes_t frame, bool complete_refill)
659 Glib::Mutex::Lock lm (state_lock);
662 _playback_buf->reset();
663 _capture_buf->reset();
664 g_atomic_int_set(&_frames_read_from_ringbuffer, 0);
665 g_atomic_int_set(&_frames_written_to_ringbuffer, 0);
667 playback_sample = frame;
670 if (complete_refill) {
671 while ((ret = do_refill_with_alloc ()) > 0) ;
673 ret = do_refill_with_alloc ();
680 MidiDiskstream::can_internal_playback_seek (nframes_t distance)
682 uint32_t frames_read = g_atomic_int_get(&_frames_read_from_ringbuffer);
683 uint32_t frames_written = g_atomic_int_get(&_frames_written_to_ringbuffer);
684 return ((frames_written - frames_read) < distance);
688 MidiDiskstream::internal_playback_seek (nframes_t distance)
690 cerr << "MDS: internal_playback_seek " << distance << endl;
692 first_recordable_frame += distance;
693 playback_sample += distance;
698 /** @a start is set to the new frame position (TIME) read up to */
700 MidiDiskstream::read (nframes_t& start, nframes_t dur, bool reversed)
702 nframes_t this_read = 0;
704 nframes_t loop_end = 0;
705 nframes_t loop_start = 0;
706 nframes_t loop_length = 0;
710 /* Make the use of a Location atomic for this read operation.
712 Note: Locations don't get deleted, so all we care about
713 when I say "atomic" is that we are always pointing to
714 the same one and using a start/length values obtained
718 if ((loc = loop_location) != 0) {
719 loop_start = loc->start();
720 loop_end = loc->end();
721 loop_length = loop_end - loop_start;
724 /* if we are looping, ensure that the first frame we read is at the correct
725 position within the loop.
728 if (loc && (start >= loop_end)) {
729 //cerr << "start adjusted from " << start;
730 start = loop_start + ((start - loop_start) % loop_length);
731 //cerr << "to " << start << endl;
733 //cerr << "start is " << start << " loopstart: " << loop_start << " loopend: " << loop_end << endl;
738 /* take any loop into account. we can't read past the end of the loop. */
740 if (loc && (loop_end - start < dur)) {
741 this_read = loop_end - start;
742 //cerr << "reloop true: thisread: " << this_read << " dur: " << dur << endl;
749 if (this_read == 0) {
753 this_read = min(dur,this_read);
755 if (midi_playlist()->read (*_playback_buf, start, this_read) != this_read) {
756 error << string_compose(
757 _("MidiDiskstream %1: cannot read %2 from playlist at frame %3"),
758 _id, this_read, start) << endmsg;
762 //cout << "MDS this read " << this_read << " start = " << start << endl;
763 g_atomic_int_add(&_frames_written_to_ringbuffer, this_read);
765 _read_data_count = _playlist->read_data_count();
769 // Swap note ons with note offs here. etc?
770 // Fully reversing MIDI requires look-ahead (well, behind) to find previous
771 // CC values etc. hard.
775 /* if we read to the end of the loop, go back to the beginning */
778 // Synthesize LoopEvent here, because the next events
779 // written will have non-monotonic timestamps.
780 _playback_buf->write(loop_end - 1, LoopEventType, 0, 0);
781 //cout << "Pushing LoopEvent ts=" << loop_end-1
782 // << " start+this_read " << start+this_read << endl;
791 //offset += this_read;
798 MidiDiskstream::do_refill_with_alloc ()
804 MidiDiskstream::do_refill ()
807 size_t write_space = _playback_buf->write_space();
808 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
810 if (write_space == 0) {
818 /* at end: nothing to do */
819 if (file_frame == max_frames) {
823 // At this point we...
824 assert(_playback_buf->write_space() > 0); // ... have something to write to, and
825 assert(file_frame <= max_frames); // ... something to write
827 // now calculate how much time is in the ringbuffer.
828 // and lets write as much as we need to get this to be midi_readahead;
829 uint32_t frames_read = g_atomic_int_get(&_frames_read_from_ringbuffer);
830 uint32_t frames_written = g_atomic_int_get(&_frames_written_to_ringbuffer);
831 if ((frames_written - frames_read) >= midi_readahead) {
832 //cout << "MDS Nothing to do. all fine" << endl;
836 nframes_t to_read = midi_readahead - (frames_written - frames_read);
838 //cout << "MDS read for midi_readahead " << to_read << " rb_contains: "
839 // << frames_written - frames_read << endl;
841 to_read = min(to_read, (max_frames - file_frame));
843 if (read (file_frame, to_read, reversed)) {
850 /** Flush pending data to disk.
852 * Important note: this function will write *AT MOST* disk_io_chunk_frames
853 * of data to disk. it will never write more than that. If it writes that
854 * much and there is more than that waiting to be written, it will return 1,
855 * otherwise 0 on success or -1 on failure.
857 * If there is less than disk_io_chunk_frames to be written, no data will be
858 * written at all unless @a force_flush is true.
861 MidiDiskstream::do_flush (RunContext context, bool force_flush)
867 _write_data_count = 0;
869 total = _session.transport_frame() - _last_flush_frame;
871 if (_last_flush_frame > _session.transport_frame()
872 || _last_flush_frame < capture_start_frame) {
873 _last_flush_frame = _session.transport_frame();
876 if (total == 0 || _capture_buf->read_space() == 0
877 || (!force_flush && (total < disk_io_chunk_frames && was_recording))) {
881 /* if there are 2+ chunks of disk i/o possible for
882 this track, let the caller know so that it can arrange
883 for us to be called again, ASAP.
885 if we are forcing a flush, then if there is* any* extra
886 work, let the caller know.
888 if we are no longer recording and there is any extra work,
889 let the caller know too.
892 if (total >= 2 * disk_io_chunk_frames || ((force_flush || !was_recording) && total > disk_io_chunk_frames)) {
896 to_write = disk_io_chunk_frames;
898 assert(!destructive());
901 && ( (_session.transport_frame() - _last_flush_frame > disk_io_chunk_frames)
903 if ((!_write_source) || _write_source->midi_write (*_capture_buf, capture_start_frame, to_write) != to_write) {
904 error << string_compose(_("MidiDiskstream %1: cannot write to disk"), _id) << endmsg;
907 _last_flush_frame = _session.transport_frame();
916 MidiDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_capture)
918 uint32_t buffer_position;
919 bool more_work = true;
921 boost::shared_ptr<MidiRegion> region;
922 nframes_t total_capture;
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 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
970 total_capture += (*ci)->frames;
973 /* figure out the name for this take */
975 srcs.push_back (_write_source);
976 _write_source->set_timeline_position (capture_info.front()->start);
977 _write_source->set_captured_for (_name);
979 string whole_file_region_name;
980 whole_file_region_name = region_name_from_path (_write_source->name(), true);
982 /* Register a new region with the Session that
983 describes the entire source. Do this first
984 so that any sub-regions will obviously be
985 children of this one (later!)
989 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, 0,
990 total_capture, whole_file_region_name, 0,
991 Region::Flag (Region::DefaultFlags|Region::Automatic|Region::WholeFile)));
993 region = boost::dynamic_pointer_cast<MidiRegion> (rx);
994 region->special_set_position (capture_info.front()->start);
998 catch (failed_constructor& err) {
999 error << string_compose(_("%1: could not create region for complete midi file"), _name) << endmsg;
1003 _last_capture_regions.push_back (region);
1005 // cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
1007 XMLNode &before = _playlist->get_state();
1008 _playlist->freeze ();
1010 for (buffer_position = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1014 _session.region_name (region_name, _write_source->name(), false);
1016 // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add a region\n";
1019 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, buffer_position, (*ci)->frames, region_name));
1020 region = boost::dynamic_pointer_cast<MidiRegion> (rx);
1023 catch (failed_constructor& err) {
1024 error << _("MidiDiskstream: could not create region for captured midi!") << endmsg;
1025 continue; /* XXX is this OK? */
1028 region->GoingAway.connect (bind (mem_fun (*this, &Diskstream::remove_region_from_last_capture), boost::weak_ptr<Region>(region)));
1030 _last_capture_regions.push_back (region);
1032 // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
1034 i_am_the_modifier++;
1035 _playlist->add_region (region, (*ci)->start);
1036 i_am_the_modifier--;
1038 buffer_position += (*ci)->frames;
1042 XMLNode &after = _playlist->get_state();
1043 _session.add_command (new MementoCommand<Playlist>(*_playlist, &before, &after));
1047 mark_write_completed = true;
1049 reset_write_sources (mark_write_completed);
1051 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1055 capture_info.clear ();
1056 capture_start_frame = 0;
1060 MidiDiskstream::transport_looped (nframes_t transport_frame)
1062 if (was_recording) {
1064 // adjust the capture length knowing that the data will be recorded to disk
1065 // only necessary after the first loop where we're recording
1066 if (capture_info.size() == 0) {
1067 capture_captured += _capture_offset;
1069 if (_alignment_style == ExistingMaterial) {
1070 capture_captured += _session.worst_output_latency();
1072 capture_captured += _roll_delay;
1076 finish_capture (true);
1078 // the next region will start recording via the normal mechanism
1079 // we'll set the start position to the current transport pos
1080 // no latency adjustment or capture offset needs to be made, as that already happened the first time
1081 capture_start_frame = transport_frame;
1082 first_recordable_frame = transport_frame; // mild lie
1083 last_recordable_frame = max_frames;
1084 was_recording = true;
1089 MidiDiskstream::finish_capture (bool rec_monitors_input)
1091 was_recording = false;
1093 if (capture_captured == 0) {
1097 // Why must we destroy?
1098 assert(!destructive());
1100 CaptureInfo* ci = new CaptureInfo;
1102 ci->start = capture_start_frame;
1103 ci->frames = capture_captured;
1105 /* XXX theoretical race condition here. Need atomic exchange ?
1106 However, the circumstances when this is called right
1107 now (either on record-disable or transport_stopped)
1108 mean that no actual race exists. I think ...
1109 We now have a capture_info_lock, but it is only to be used
1110 to synchronize in the transport_stop and the capture info
1111 accessors, so that invalidation will not occur (both non-realtime).
1114 // cerr << "Finish capture, add new CI, " << ci->start << '+' << ci->frames << endl;
1116 capture_info.push_back (ci);
1117 capture_captured = 0;
1121 MidiDiskstream::set_record_enabled (bool yn)
1123 if (!recordable() || !_session.record_enabling_legal()) {
1127 assert(!destructive());
1129 if (yn && _source_port == 0) {
1131 /* pick up connections not initiated *from* the IO object
1132 we're associated with.
1135 get_input_sources ();
1138 /* yes, i know that this not proof against race conditions, but its
1139 good enough. i think.
1142 if (record_enabled() != yn) {
1144 engage_record_enable ();
1146 disengage_record_enable ();
1152 MidiDiskstream::engage_record_enable ()
1154 bool rolling = _session.transport_speed() != 0.0f;
1156 g_atomic_int_set (&_record_enabled, 1);
1158 if (_source_port && Config->get_monitoring_model() == HardwareMonitoring) {
1159 _source_port->request_monitor_input (!(Config->get_auto_input() && rolling));
1162 // FIXME: Why is this necessary? Isn't needed for AudioDiskstream...
1164 use_new_write_source();
1166 _write_source->mark_streaming_midi_write_started (_note_mode, _session.transport_frame());
1168 RecordEnableChanged (); /* EMIT SIGNAL */
1172 MidiDiskstream::disengage_record_enable ()
1174 g_atomic_int_set (&_record_enabled, 0);
1175 if (_source_port && Config->get_monitoring_model() == HardwareMonitoring) {
1177 _source_port->request_monitor_input (false);
1181 RecordEnableChanged (); /* EMIT SIGNAL */
1185 MidiDiskstream::get_state ()
1187 XMLNode* node = new XMLNode ("MidiDiskstream");
1189 LocaleGuard lg (X_("POSIX"));
1191 snprintf (buf, sizeof(buf), "0x%x", _flags);
1192 node->add_property ("flags", buf);
1194 node->add_property("channel-mode", enum_2_string(get_channel_mode()));
1196 snprintf (buf, sizeof(buf), "0x%x", get_channel_mask());
1197 node->add_property("channel-mask", buf);
1199 node->add_property ("playlist", _playlist->name());
1201 snprintf (buf, sizeof(buf), "%f", _visible_speed);
1202 node->add_property ("speed", buf);
1204 node->add_property("name", _name);
1205 id().print(buf, sizeof(buf));
1206 node->add_property("id", buf);
1208 if (_write_source && _session.get_record_enabled()) {
1210 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1211 XMLNode* cs_grandchild;
1213 cs_grandchild = new XMLNode (X_("file"));
1214 cs_grandchild->add_property (X_("path"), _write_source->path());
1215 cs_child->add_child_nocopy (*cs_grandchild);
1217 /* store the location where capture will start */
1221 if (Config->get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1222 snprintf (buf, sizeof (buf), "%" PRIu32, pi->start());
1224 snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
1227 cs_child->add_property (X_("at"), buf);
1228 node->add_child_nocopy (*cs_child);
1232 node->add_child_copy (*_extra_xml);
1239 MidiDiskstream::set_state (const XMLNode& node)
1241 const XMLProperty* prop;
1242 XMLNodeList nlist = node.children();
1243 XMLNodeIterator niter;
1244 uint32_t nchans = 1;
1245 XMLNode* capture_pending_node = 0;
1246 LocaleGuard lg (X_("POSIX"));
1248 in_set_state = true;
1250 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1251 /*if ((*niter)->name() == IO::state_node_name) {
1252 deprecated_io_node = new XMLNode (**niter);
1254 assert ((*niter)->name() != IO::state_node_name);
1256 if ((*niter)->name() == X_("CapturingSources")) {
1257 capture_pending_node = *niter;
1261 /* prevent write sources from being created */
1263 in_set_state = true;
1265 if ((prop = node.property ("name")) != 0) {
1266 _name = prop->value();
1269 if ((prop = node.property ("id")) != 0) {
1270 _id = prop->value ();
1273 if ((prop = node.property ("flags")) != 0) {
1274 _flags = Flag (string_2_enum (prop->value(), _flags));
1277 ChannelMode channel_mode = AllChannels;
1278 if ((prop = node.property ("channel-mode")) != 0) {
1279 channel_mode = ChannelMode (string_2_enum(prop->value(), channel_mode));
1282 unsigned int channel_mask = 0xFFFF;
1283 if ((prop = node.property ("channel-mask")) != 0) {
1284 sscanf (prop->value().c_str(), "0x%x", &channel_mask);
1285 if (channel_mask & (~0xFFFF)) {
1286 warning << _("MidiDiskstream: XML property channel-mask out of range") << endmsg;
1290 set_channel_mode(channel_mode, channel_mask);
1292 if ((prop = node.property ("channels")) != 0) {
1293 nchans = atoi (prop->value().c_str());
1296 if ((prop = node.property ("playlist")) == 0) {
1301 bool had_playlist = (_playlist != 0);
1303 if (find_and_use_playlist (prop->value())) {
1307 if (!had_playlist) {
1308 _playlist->set_orig_diskstream_id (_id);
1311 if (capture_pending_node) {
1312 use_pending_capture_data (*capture_pending_node);
1317 if ((prop = node.property ("speed")) != 0) {
1318 double sp = atof (prop->value().c_str());
1320 if (realtime_set_speed (sp, false)) {
1321 non_realtime_set_speed ();
1325 in_set_state = false;
1327 /* make sure this is clear before we do anything else */
1330 //_capturing_source = 0;
1332 /* write sources are handled when we handle the input set
1333 up of the IO that owns this DS (::non_realtime_input_change())
1336 in_set_state = false;
1342 MidiDiskstream::use_new_write_source (uint32_t n)
1344 if (!recordable()) {
1350 if (_write_source) {
1352 if (_write_source->is_empty ()) {
1353 _write_source->mark_for_remove ();
1354 _write_source.reset();
1356 _write_source.reset();
1361 _write_source = boost::dynamic_pointer_cast<SMFSource>(_session.create_midi_source_for_session (*this));
1362 if (!_write_source) {
1363 throw failed_constructor();
1367 catch (failed_constructor &err) {
1368 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1369 _write_source.reset();
1373 _write_source->set_allow_remove_if_empty (true);
1379 MidiDiskstream::reset_write_sources (bool mark_write_complete, bool force)
1381 if (!recordable()) {
1385 if (_write_source && mark_write_complete) {
1386 _write_source->mark_streaming_write_completed ();
1389 use_new_write_source (0);
1391 if (record_enabled()) {
1392 //_capturing_sources.push_back (_write_source);
1397 MidiDiskstream::rename_write_sources ()
1399 if (_write_source != 0) {
1400 _write_source->set_source_name (_name, destructive());
1401 /* XXX what to do if this fails ? */
1407 MidiDiskstream::set_block_size (nframes_t nframes)
1412 MidiDiskstream::allocate_temporary_buffers ()
1417 MidiDiskstream::monitor_input (bool yn)
1420 _source_port->ensure_monitor_input (yn);
1424 MidiDiskstream::set_align_style_from_io ()
1426 bool have_physical = false;
1432 get_input_sources ();
1434 if (_source_port && _source_port->flags() & JackPortIsPhysical) {
1435 have_physical = true;
1438 if (have_physical) {
1439 set_align_style (ExistingMaterial);
1441 set_align_style (CaptureTime);
1447 MidiDiskstream::playback_buffer_load () const
1449 return (float) ((double) _playback_buf->read_space()/
1450 (double) _playback_buf->capacity());
1454 MidiDiskstream::capture_buffer_load () const
1456 return (float) ((double) _capture_buf->write_space()/
1457 (double) _capture_buf->capacity());
1461 MidiDiskstream::use_pending_capture_data (XMLNode& node)
1466 /** Writes playback events in the given range to \a dst, translating time stamps
1467 * so that an event at \a start has time = 0
1470 MidiDiskstream::get_playback (MidiBuffer& dst, nframes_t start, nframes_t end)
1473 assert(dst.size() == 0);
1475 // Reverse. ... We just don't do reverse, ok? Back off.
1480 // Check only events added this offset cycle
1481 MidiBuffer::iterator this_cycle_start = dst.end();
1483 // Translates stamps to be relative to start
1485 _playback_buf->read(dst, start, end);
1487 const size_t events_read = _playback_buf->read(dst, start, end);
1488 cout << "MDS events read = " << events_read
1489 << " start = " << start << " end = " << end
1490 << " readspace " << _playback_buf->read_space()
1491 << " writespace " << _playback_buf->write_space() << endl;
1494 gint32 frames_read = end - start;
1495 g_atomic_int_add(&_frames_read_from_ringbuffer, frames_read);
1497 // Feed the data through the MidiStateTracker
1498 // If it detects a LoopEvent it will add necessary note offs
1499 if (_midi_state_tracker.track(this_cycle_start, dst.end())) {
1500 _midi_state_tracker.resolve_notes(dst, end-start - 1);