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/midi_diskstream.h>
42 #include <ardour/utils.h>
43 #include <ardour/configuration.h>
44 #include <ardour/smf_source.h>
45 #include <ardour/send.h>
46 #include <ardour/region_factory.h>
47 #include <ardour/midi_playlist.h>
48 #include <ardour/playlist_factory.h>
49 #include <ardour/cycle_timer.h>
50 #include <ardour/midi_region.h>
51 #include <ardour/midi_port.h>
57 using namespace ARDOUR;
60 MidiDiskstream::MidiDiskstream (Session &sess, const string &name, Diskstream::Flag flag)
61 : Diskstream(sess, name, flag)
64 //, _current_playback_buffer(0)
65 //, _current_capture_buffer(0)
66 //, _playback_wrap_buffer(0)
67 //, _capture_wrap_buffer(0)
69 , _capture_transition_buf(0)
70 , _last_flush_frame(0)
71 , _note_mode(Sustained)
73 /* prevent any write sources from being created */
82 assert(!destructive());
85 MidiDiskstream::MidiDiskstream (Session& sess, const XMLNode& node)
86 : Diskstream(sess, node)
89 //, _current_playback_buffer(0)
90 //, _current_capture_buffer(0)
91 //, _playback_wrap_buffer(0)
92 //, _capture_wrap_buffer(0)
94 , _capture_transition_buf(0)
95 , _last_flush_frame(0)
96 , _note_mode(Sustained)
101 if (set_state (node)) {
102 in_set_state = false;
103 throw failed_constructor();
106 in_set_state = false;
109 use_destructive_playlist ();
114 MidiDiskstream::init (Diskstream::Flag f)
118 /* there are no channels at this point, so these
119 two calls just get speed_buffer_size and wrap_buffer
120 size setup without duplicating their code.
123 set_block_size (_session.get_block_size());
124 allocate_temporary_buffers ();
126 _playback_buf = new MidiRingBuffer (_session.diskstream_buffer_size());
127 _capture_buf = new MidiRingBuffer (_session.diskstream_buffer_size());
128 _capture_transition_buf = new RingBufferNPT<CaptureTransition> (128);
130 _n_channels = ChanCount(DataType::MIDI, 1);
132 assert(recordable());
135 MidiDiskstream::~MidiDiskstream ()
137 Glib::Mutex::Lock lm (state_lock);
142 MidiDiskstream::non_realtime_locate (nframes_t position)
144 //cerr << "MDS: non_realtime_locate: " << position << endl;
145 assert(_write_source);
146 _write_source->set_timeline_position (position);
147 seek(position, true); // correct?
152 MidiDiskstream::non_realtime_input_change ()
155 Glib::Mutex::Lock lm (state_lock);
157 if (input_change_pending == NoChange) {
161 if (input_change_pending & ConfigurationChanged) {
162 assert(_io->n_inputs() == _n_channels);
165 get_input_sources ();
166 set_capture_offset ();
168 if (first_input_change) {
169 set_align_style (_persistent_alignment_style);
170 first_input_change = false;
172 set_align_style_from_io ();
175 input_change_pending = NoChange;
177 /* implicit unlock */
180 /* reset capture files */
182 reset_write_sources (false);
184 /* now refill channel buffers */
186 if (speed() != 1.0f || speed() != -1.0f) {
187 seek ((nframes_t) (_session.transport_frame() * (double) speed()));
190 seek (_session.transport_frame());
193 _last_flush_frame = _session.transport_frame();
197 MidiDiskstream::get_input_sources ()
199 uint32_t ni = _io->n_inputs().n_midi();
205 // This is all we do for now at least
208 _source_port = _io->midi_input(0);
214 MidiDiskstream::find_and_use_playlist (const string& name)
216 boost::shared_ptr<MidiPlaylist> playlist;
218 if ((playlist = boost::dynamic_pointer_cast<MidiPlaylist> (_session.playlist_by_name (name))) == 0) {
219 playlist = boost::dynamic_pointer_cast<MidiPlaylist> (PlaylistFactory::create (DataType::MIDI, _session, name));
223 error << string_compose(_("MidiDiskstream: Playlist \"%1\" isn't an midi playlist"), name) << endmsg;
227 return use_playlist (playlist);
231 MidiDiskstream::use_playlist (boost::shared_ptr<Playlist> playlist)
233 assert(boost::dynamic_pointer_cast<MidiPlaylist>(playlist));
235 Diskstream::use_playlist(playlist);
241 MidiDiskstream::use_new_playlist ()
244 boost::shared_ptr<MidiPlaylist> playlist;
246 if (!in_set_state && destructive()) {
251 newname = Playlist::bump_name (_playlist->name(), _session);
253 newname = Playlist::bump_name (_name, _session);
256 if ((playlist = boost::dynamic_pointer_cast<MidiPlaylist> (PlaylistFactory::create (
257 DataType::MIDI, _session, newname, hidden()))) != 0) {
259 playlist->set_orig_diskstream_id (id());
260 return use_playlist (playlist);
268 MidiDiskstream::use_copy_playlist ()
270 assert(midi_playlist());
276 if (_playlist == 0) {
277 error << string_compose(_("MidiDiskstream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
282 boost::shared_ptr<MidiPlaylist> playlist;
284 newname = Playlist::bump_name (_playlist->name(), _session);
286 if ((playlist = boost::dynamic_pointer_cast<MidiPlaylist>(PlaylistFactory::create (midi_playlist(), newname))) != 0) {
287 playlist->set_orig_diskstream_id (id());
288 return use_playlist (playlist);
294 /** Overloaded from parent to die horribly
297 MidiDiskstream::set_destructive (bool yn)
299 assert( ! destructive());
305 MidiDiskstream::set_note_mode (NoteMode m)
307 cout << "MDS: SET NOTE MODE: " << m << endl;
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 if (_flags & Recordable) {
407 RingBufferNPT<CaptureTransition>::rw_vector transvec;
408 _capture_transition_buf->get_write_vector(&transvec);
410 if (transvec.len[0] > 0) {
411 transvec.buf[0]->type = CaptureStart;
412 transvec.buf[0]->capture_val = capture_start_frame;
413 _capture_transition_buf->increment_write_ptr(1);
416 fatal << X_("programming error: capture_transition_buf is full on rec start! inconceivable!")
421 } else if (!record_enabled() || !can_record) {
425 last_recordable_frame = transport_frame + _capture_offset;
427 if (_alignment_style == ExistingMaterial) {
428 last_recordable_frame += _session.worst_output_latency();
430 last_recordable_frame += _roll_delay;
434 last_possibly_recording = possibly_recording;
438 MidiDiskstream::process (nframes_t transport_frame, nframes_t nframes, nframes_t offset, bool can_record, bool rec_monitors_input)
440 // FIXME: waay too much code to duplicate (AudioDiskstream::process)
442 nframes_t rec_offset = 0;
443 nframes_t rec_nframes = 0;
444 bool nominally_recording;
445 bool re = record_enabled ();
446 bool collect_playback = false;
448 /* if we've already processed the frames corresponding to this call,
449 just return. this allows multiple routes that are taking input
450 from this diskstream to call our ::process() method, but have
451 this stuff only happen once. more commonly, it allows both
452 the AudioTrack that is using this AudioDiskstream *and* the Session
453 to call process() without problems.
460 commit_should_unlock = false;
462 check_record_status (transport_frame, nframes, can_record);
464 nominally_recording = (can_record && re);
471 /* This lock is held until the end of AudioDiskstream::commit, so these two functions
472 must always be called as a pair. The only exception is if this function
473 returns a non-zero value, in which case, ::commit should not be called.
476 // If we can't take the state lock return.
477 if (!state_lock.trylock()) {
480 commit_should_unlock = true;
481 adjust_capture_position = 0;
483 if (nominally_recording || (_session.get_record_enabled() && Config->get_punch_in())) {
486 ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
493 case OverlapInternal:
494 /* ---------- recrange
497 rec_nframes = nframes;
502 /* |--------| recrange
505 rec_nframes = transport_frame + nframes - first_recordable_frame;
507 rec_offset = first_recordable_frame - transport_frame;
512 /* |--------| recrange
515 rec_nframes = last_recordable_frame - transport_frame;
519 case OverlapExternal:
520 /* |--------| recrange
521 -------------- transrange
523 rec_nframes = last_recordable_frame - last_recordable_frame;
524 rec_offset = first_recordable_frame - transport_frame;
528 if (rec_nframes && !was_recording) {
529 capture_captured = 0;
530 was_recording = true;
535 if (can_record && !_last_capture_regions.empty()) {
536 _last_capture_regions.clear ();
539 if (nominally_recording || rec_nframes) {
541 assert(_source_port);
543 // Pump entire port buffer into the ring buffer (FIXME: split cycles?)
544 //_capture_buf->write(_source_port->get_midi_buffer(), transport_frame);
545 size_t num_events = _source_port->get_midi_buffer().size();
546 size_t to_write = std::min(_capture_buf->write_space(), num_events);
548 MidiBuffer::iterator port_iter = _source_port->get_midi_buffer().begin();
550 for (size_t i=0; i < to_write; ++i) {
551 const MIDI::Event& ev = *port_iter;
553 _capture_buf->write(ev.time() + transport_frame, ev.size(), ev.buffer());
560 finish_capture (rec_monitors_input);
567 /* XXX XXX XXX XXX XXX XXX XXX XXX */
569 /* data will be written to disk */
571 if (rec_nframes == nframes && rec_offset == 0) {
573 playback_distance = nframes;
576 collect_playback = true;
579 adjust_capture_position = rec_nframes;
581 } else if (nominally_recording) {
583 /* can't do actual capture yet - waiting for latency effects to finish before we start*/
585 playback_distance = nframes;
589 collect_playback = true;
592 if (collect_playback) {
594 /* we're doing playback */
596 nframes_t necessary_samples;
598 /* no varispeed playback if we're recording, because the output .... TBD */
600 if (rec_nframes == 0 && _actual_speed != 1.0f) {
601 necessary_samples = (nframes_t) floor ((nframes * fabs (_actual_speed))) + 1;
603 necessary_samples = nframes;
606 // XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX
607 // Write into playback buffer here, and whatnot?
608 //cerr << "MDS FIXME: collect playback" << endl;
618 /* we're exiting with failure, so ::commit will not
619 be called. unlock the state lock.
622 commit_should_unlock = false;
630 MidiDiskstream::commit (nframes_t nframes)
632 bool need_butler = false;
634 if (_actual_speed < 0.0) {
635 playback_sample -= playback_distance;
637 playback_sample += playback_distance;
640 if (adjust_capture_position != 0) {
641 capture_captured += adjust_capture_position;
642 adjust_capture_position = 0;
646 need_butler = _playback_buf->write_space() >= _playback_buf->capacity() / 2;
648 need_butler = _playback_buf->write_space() >= disk_io_chunk_frames
649 || _capture_buf->read_space() >= disk_io_chunk_frames;
652 if (commit_should_unlock) {
662 MidiDiskstream::set_pending_overwrite (bool yn)
664 /* called from audio thread, so we can use the read ptr and playback sample as we wish */
666 pending_overwrite = yn;
668 overwrite_frame = playback_sample;
669 //overwrite_offset = channels.front().playback_buf->get_read_ptr();
673 MidiDiskstream::overwrite_existing_buffers ()
675 //cerr << "MDS: overwrite_existing_buffers() (does nothing)" << endl;
680 MidiDiskstream::seek (nframes_t frame, bool complete_refill)
682 Glib::Mutex::Lock lm (state_lock);
685 //cerr << "MDS: seek: " << frame << endl;
687 _playback_buf->reset();
688 _capture_buf->reset();
690 playback_sample = frame;
693 if (complete_refill) {
694 while ((ret = do_refill_with_alloc ()) > 0) ;
696 ret = do_refill_with_alloc ();
703 MidiDiskstream::can_internal_playback_seek (nframes_t distance)
705 if (_playback_buf->read_space() < distance) {
713 MidiDiskstream::internal_playback_seek (nframes_t distance)
715 cerr << "MDS: internal_playback_seek " << distance << endl;
717 first_recordable_frame += distance;
718 playback_sample += distance;
723 /** @a start is set to the new frame position (TIME) read up to */
725 MidiDiskstream::read (nframes_t& start, nframes_t dur, bool reversed)
727 nframes_t this_read = 0;
729 nframes_t loop_end = 0;
730 nframes_t loop_start = 0;
731 nframes_t loop_length = 0;
735 /* Make the use of a Location atomic for this read operation.
737 Note: Locations don't get deleted, so all we care about
738 when I say "atomic" is that we are always pointing to
739 the same one and using a start/length values obtained
743 if ((loc = loop_location) != 0) {
744 loop_start = loc->start();
745 loop_end = loc->end();
746 loop_length = loop_end - loop_start;
749 /* if we are looping, ensure that the first frame we read is at the correct
750 position within the loop.
753 if (loc && start >= loop_end) {
754 //cerr << "start adjusted from " << start;
755 start = loop_start + ((start - loop_start) % loop_length);
756 //cerr << "to " << start << endl;
758 //cerr << "start is " << start << " loopstart: " << loop_start << " loopend: " << loop_end << endl;
763 /* take any loop into account. we can't read past the end of the loop. */
765 if (loc && (loop_end - start < dur)) {
766 this_read = loop_end - start;
767 //cerr << "reloop true: thisread: " << this_read << " dur: " << dur << endl;
774 if (this_read == 0) {
778 this_read = min(dur,this_read);
780 if (midi_playlist()->read (*_playback_buf, start, this_read) != this_read) {
781 error << string_compose(_("MidiDiskstream %1: cannot read %2 from playlist at frame %3"), _id, this_read,
786 _read_data_count = _playlist->read_data_count();
790 // Swap note ons with note offs here. etc?
791 // Fully reversing MIDI required look-ahead (well, behind) to find previous
792 // CC values etc. hard.
796 /* if we read to the end of the loop, go back to the beginning */
806 //offset += this_read;
813 MidiDiskstream::do_refill_with_alloc ()
819 MidiDiskstream::do_refill ()
822 size_t write_space = _playback_buf->write_space();
823 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
825 if (write_space == 0) {
829 /* if there are 2+ chunks of disk i/o possible for
830 this track, let the caller know so that it can arrange
831 for us to be called again, ASAP.
834 if (write_space >= (_slaved?3:2) * disk_io_chunk_frames) {
838 /* if we're running close to normal speed and there isn't enough
839 space to do disk_io_chunk_frames of I/O, then don't bother.
841 at higher speeds, just do it because the sync between butler
842 and audio thread may not be good enough.
845 if ((write_space < disk_io_chunk_frames) && fabs (_actual_speed) < 2.0f) {
849 /* when slaved, don't try to get too close to the read pointer. this
850 leaves space for the buffer reversal to have something useful to
854 if (_slaved && write_space < (_playback_buf->capacity() / 2)) {
862 /* at end: nothing to do */
863 if (file_frame == max_frames) {
867 // At this point we...
868 assert(_playback_buf->write_space() > 0); // ... have something to write to, and
869 assert(file_frame <= max_frames); // ... something to write
871 nframes_t to_read = min(disk_io_chunk_frames, (max_frames - file_frame));
873 if (read (file_frame, to_read, reversed)) {
880 /** Flush pending data to disk.
882 * Important note: this function will write *AT MOST* disk_io_chunk_frames
883 * of data to disk. it will never write more than that. If it writes that
884 * much and there is more than that waiting to be written, it will return 1,
885 * otherwise 0 on success or -1 on failure.
887 * If there is less than disk_io_chunk_frames to be written, no data will be
888 * written at all unless @a force_flush is true.
891 MidiDiskstream::do_flush (Session::RunContext context, bool force_flush)
897 _write_data_count = 0;
899 if (_last_flush_frame > _session.transport_frame()
900 || _last_flush_frame < capture_start_frame) {
901 _last_flush_frame = _session.transport_frame();
904 total = _session.transport_frame() - _last_flush_frame;
906 if (total == 0 || (_capture_buf->read_space() == 0 && _session.transport_speed() == 0) || (total < disk_io_chunk_frames && !force_flush && was_recording)) {
910 /* if there are 2+ chunks of disk i/o possible for
911 this track, let the caller know so that it can arrange
912 for us to be called again, ASAP.
914 if we are forcing a flush, then if there is* any* extra
915 work, let the caller know.
917 if we are no longer recording and there is any extra work,
918 let the caller know too.
921 if (total >= 2 * disk_io_chunk_frames || ((force_flush || !was_recording) && total > disk_io_chunk_frames)) {
925 to_write = disk_io_chunk_frames;
927 assert(!destructive());
929 if (record_enabled() && _session.transport_frame() - _last_flush_frame > disk_io_chunk_frames) {
930 if ((!_write_source) || _write_source->midi_write (*_capture_buf, to_write) != to_write) {
931 error << string_compose(_("MidiDiskstream %1: cannot write to disk"), _id) << endmsg;
934 _last_flush_frame = _session.transport_frame();
943 MidiDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_capture)
945 uint32_t buffer_position;
946 bool more_work = true;
948 boost::shared_ptr<MidiRegion> region;
949 nframes_t total_capture;
950 MidiRegion::SourceList srcs;
951 MidiRegion::SourceList::iterator src;
952 vector<CaptureInfo*>::iterator ci;
953 bool mark_write_completed = false;
955 finish_capture (true);
957 /* butler is already stopped, but there may be work to do
958 to flush remaining data to disk.
961 while (more_work && !err) {
962 switch (do_flush (Session::TransportContext, true)) {
969 error << string_compose(_("MidiDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
974 /* XXX is there anything we can do if err != 0 ? */
975 Glib::Mutex::Lock lm (capture_info_lock);
977 if (capture_info.empty()) {
985 _write_source->mark_for_remove ();
986 _write_source->drop_references ();
987 _write_source.reset();
990 /* new source set up in "out" below */
994 assert(_write_source);
996 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
997 total_capture += (*ci)->frames;
1000 /* figure out the name for this take */
1002 srcs.push_back (_write_source);
1003 _write_source->set_timeline_position (capture_info.front()->start);
1004 _write_source->set_captured_for (_name);
1006 string whole_file_region_name;
1007 whole_file_region_name = region_name_from_path (_write_source->name(), true);
1009 /* Register a new region with the Session that
1010 describes the entire source. Do this first
1011 so that any sub-regions will obviously be
1012 children of this one (later!)
1016 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, _write_source->last_capture_start_frame(), total_capture,
1017 whole_file_region_name,
1018 0, Region::Flag (Region::DefaultFlags|Region::Automatic|Region::WholeFile)));
1020 region = boost::dynamic_pointer_cast<MidiRegion> (rx);
1021 region->special_set_position (capture_info.front()->start);
1025 catch (failed_constructor& err) {
1026 error << string_compose(_("%1: could not create region for complete midi file"), _name) << endmsg;
1030 _last_capture_regions.push_back (region);
1032 // cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
1034 XMLNode &before = _playlist->get_state();
1035 _playlist->freeze ();
1037 for (buffer_position = _write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1041 _session.region_name (region_name, _write_source->name(), false);
1043 // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add a region\n";
1046 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, buffer_position, (*ci)->frames, region_name));
1047 region = boost::dynamic_pointer_cast<MidiRegion> (rx);
1050 catch (failed_constructor& err) {
1051 error << _("MidiDiskstream: could not create region for captured midi!") << endmsg;
1052 continue; /* XXX is this OK? */
1055 region->GoingAway.connect (bind (mem_fun (*this, &Diskstream::remove_region_from_last_capture), boost::weak_ptr<Region>(region)));
1057 _last_capture_regions.push_back (region);
1059 // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
1061 i_am_the_modifier++;
1062 _playlist->add_region (region, (*ci)->start);
1063 i_am_the_modifier--;
1065 buffer_position += (*ci)->frames;
1069 XMLNode &after = _playlist->get_state();
1070 _session.add_command (new MementoCommand<Playlist>(*_playlist, &before, &after));
1074 mark_write_completed = true;
1076 reset_write_sources (mark_write_completed);
1078 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1082 capture_info.clear ();
1083 capture_start_frame = 0;
1087 MidiDiskstream::transport_looped (nframes_t transport_frame)
1089 if (was_recording) {
1091 // adjust the capture length knowing that the data will be recorded to disk
1092 // only necessary after the first loop where we're recording
1093 if (capture_info.size() == 0) {
1094 capture_captured += _capture_offset;
1096 if (_alignment_style == ExistingMaterial) {
1097 capture_captured += _session.worst_output_latency();
1099 capture_captured += _roll_delay;
1103 finish_capture (true);
1105 // the next region will start recording via the normal mechanism
1106 // we'll set the start position to the current transport pos
1107 // no latency adjustment or capture offset needs to be made, as that already happened the first time
1108 capture_start_frame = transport_frame;
1109 first_recordable_frame = transport_frame; // mild lie
1110 last_recordable_frame = max_frames;
1111 was_recording = true;
1116 MidiDiskstream::finish_capture (bool rec_monitors_input)
1118 was_recording = false;
1120 if (capture_captured == 0) {
1124 // Why must we destroy?
1125 assert(!destructive());
1127 CaptureInfo* ci = new CaptureInfo;
1129 ci->start = capture_start_frame;
1130 ci->frames = capture_captured;
1132 /* XXX theoretical race condition here. Need atomic exchange ?
1133 However, the circumstances when this is called right
1134 now (either on record-disable or transport_stopped)
1135 mean that no actual race exists. I think ...
1136 We now have a capture_info_lock, but it is only to be used
1137 to synchronize in the transport_stop and the capture info
1138 accessors, so that invalidation will not occur (both non-realtime).
1141 // cerr << "Finish capture, add new CI, " << ci->start << '+' << ci->frames << endl;
1143 capture_info.push_back (ci);
1144 capture_captured = 0;
1148 MidiDiskstream::set_record_enabled (bool yn)
1150 if (!recordable() || !_session.record_enabling_legal()) {
1154 assert(!destructive());
1156 if (yn && _source_port == 0) {
1158 /* pick up connections not initiated *from* the IO object
1159 we're associated with.
1162 get_input_sources ();
1165 /* yes, i know that this not proof against race conditions, but its
1166 good enough. i think.
1169 if (record_enabled() != yn) {
1171 engage_record_enable ();
1173 disengage_record_enable ();
1179 MidiDiskstream::engage_record_enable ()
1181 bool rolling = _session.transport_speed() != 0.0f;
1183 g_atomic_int_set (&_record_enabled, 1);
1185 if (_source_port && Config->get_monitoring_model() == HardwareMonitoring) {
1186 _source_port->request_monitor_input (!(Config->get_auto_input() && rolling));
1189 // FIXME: Why is this necessary? Isn't needed for AudioDiskstream...
1191 use_new_write_source();
1193 _write_source->mark_streaming_midi_write_started (_note_mode, _session.transport_frame());
1195 RecordEnableChanged (); /* EMIT SIGNAL */
1199 MidiDiskstream::disengage_record_enable ()
1201 g_atomic_int_set (&_record_enabled, 0);
1202 if (_source_port && Config->get_monitoring_model() == HardwareMonitoring) {
1204 _source_port->request_monitor_input (false);
1208 RecordEnableChanged (); /* EMIT SIGNAL */
1212 MidiDiskstream::get_state ()
1214 XMLNode* node = new XMLNode ("MidiDiskstream");
1216 LocaleGuard lg (X_("POSIX"));
1218 snprintf (buf, sizeof(buf), "0x%x", _flags);
1219 node->add_property ("flags", buf);
1221 node->add_property("channel-mode", enum_2_string(get_channel_mode()));
1223 snprintf (buf, sizeof(buf), "0x%x", get_channel_mask());
1224 node->add_property("channel-mask", buf);
1226 node->add_property ("playlist", _playlist->name());
1228 snprintf (buf, sizeof(buf), "%f", _visible_speed);
1229 node->add_property ("speed", buf);
1231 node->add_property("name", _name);
1232 id().print(buf, sizeof(buf));
1233 node->add_property("id", buf);
1235 if (_write_source && _session.get_record_enabled()) {
1237 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1238 XMLNode* cs_grandchild;
1240 cs_grandchild = new XMLNode (X_("file"));
1241 cs_grandchild->add_property (X_("path"), _write_source->path());
1242 cs_child->add_child_nocopy (*cs_grandchild);
1244 /* store the location where capture will start */
1248 if (Config->get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1249 snprintf (buf, sizeof (buf), "%" PRIu32, pi->start());
1251 snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
1254 cs_child->add_property (X_("at"), buf);
1255 node->add_child_nocopy (*cs_child);
1259 node->add_child_copy (*_extra_xml);
1266 MidiDiskstream::set_state (const XMLNode& node)
1268 const XMLProperty* prop;
1269 XMLNodeList nlist = node.children();
1270 XMLNodeIterator niter;
1271 uint32_t nchans = 1;
1272 XMLNode* capture_pending_node = 0;
1273 LocaleGuard lg (X_("POSIX"));
1275 in_set_state = true;
1277 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1278 /*if ((*niter)->name() == IO::state_node_name) {
1279 deprecated_io_node = new XMLNode (**niter);
1281 assert ((*niter)->name() != IO::state_node_name);
1283 if ((*niter)->name() == X_("CapturingSources")) {
1284 capture_pending_node = *niter;
1288 /* prevent write sources from being created */
1290 in_set_state = true;
1292 if ((prop = node.property ("name")) != 0) {
1293 _name = prop->value();
1296 if ((prop = node.property ("id")) != 0) {
1297 _id = prop->value ();
1300 if ((prop = node.property ("flags")) != 0) {
1301 _flags = Flag (string_2_enum (prop->value(), _flags));
1304 ChannelMode channel_mode = AllChannels;
1305 if ((prop = node.property ("channel-mode")) != 0) {
1306 channel_mode = ChannelMode (string_2_enum(prop->value(), channel_mode));
1309 unsigned int channel_mask = 0xFFFF;
1310 if ((prop = node.property ("channel-mask")) != 0) {
1311 sscanf (prop->value().c_str(), "0x%x", &channel_mask);
1312 if (channel_mask & (~0xFFFF)) {
1313 warning << _("MidiDiskstream: XML property channel-mask out of range") << endmsg;
1317 set_channel_mode(channel_mode, channel_mask);
1319 if ((prop = node.property ("channels")) != 0) {
1320 nchans = atoi (prop->value().c_str());
1323 if ((prop = node.property ("playlist")) == 0) {
1328 bool had_playlist = (_playlist != 0);
1330 if (find_and_use_playlist (prop->value())) {
1334 if (!had_playlist) {
1335 _playlist->set_orig_diskstream_id (_id);
1338 if (capture_pending_node) {
1339 use_pending_capture_data (*capture_pending_node);
1344 if ((prop = node.property ("speed")) != 0) {
1345 double sp = atof (prop->value().c_str());
1347 if (realtime_set_speed (sp, false)) {
1348 non_realtime_set_speed ();
1352 in_set_state = false;
1354 /* make sure this is clear before we do anything else */
1357 //_capturing_source = 0;
1359 /* write sources are handled when we handle the input set
1360 up of the IO that owns this DS (::non_realtime_input_change())
1363 in_set_state = false;
1369 MidiDiskstream::use_new_write_source (uint32_t n)
1371 if (!recordable()) {
1377 if (_write_source) {
1379 if (_write_source->is_empty ()) {
1380 _write_source->mark_for_remove ();
1381 _write_source.reset();
1383 _write_source.reset();
1388 _write_source = boost::dynamic_pointer_cast<SMFSource>(_session.create_midi_source_for_session (*this));
1389 if (!_write_source) {
1390 throw failed_constructor();
1394 catch (failed_constructor &err) {
1395 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1396 _write_source.reset();
1400 _write_source->set_allow_remove_if_empty (true);
1406 MidiDiskstream::reset_write_sources (bool mark_write_complete, bool force)
1408 if (!recordable()) {
1412 if (_write_source && mark_write_complete) {
1413 _write_source->mark_streaming_write_completed ();
1416 use_new_write_source (0);
1418 if (record_enabled()) {
1419 //_capturing_sources.push_back (_write_source);
1424 MidiDiskstream::rename_write_sources ()
1426 if (_write_source != 0) {
1427 _write_source->set_source_name (_name, destructive());
1428 /* XXX what to do if this fails ? */
1434 MidiDiskstream::set_block_size (nframes_t nframes)
1439 MidiDiskstream::allocate_temporary_buffers ()
1444 MidiDiskstream::monitor_input (bool yn)
1447 _source_port->request_monitor_input (yn);
1449 cerr << "MidiDiskstream NO SOURCE PORT TO MONITOR\n";
1453 MidiDiskstream::set_align_style_from_io ()
1455 bool have_physical = false;
1461 get_input_sources ();
1463 if (_source_port && _source_port->flags() & JackPortIsPhysical) {
1464 have_physical = true;
1467 if (have_physical) {
1468 set_align_style (ExistingMaterial);
1470 set_align_style (CaptureTime);
1476 MidiDiskstream::playback_buffer_load () const
1478 return (float) ((double) _playback_buf->read_space()/
1479 (double) _playback_buf->capacity());
1483 MidiDiskstream::capture_buffer_load () const
1485 return (float) ((double) _capture_buf->write_space()/
1486 (double) _capture_buf->capacity());
1491 MidiDiskstream::use_pending_capture_data (XMLNode& node)
1496 /** Writes playback events in the given range to \a dst, translating time stamps
1497 * so that an event at \a start has time = 0
1500 MidiDiskstream::get_playback(MidiBuffer& dst, nframes_t start, nframes_t end)
1503 assert(dst.size() == 0);
1505 // Reverse. ... We just don't do reverse, ok? Back off.
1510 // Translates stamps to be relative to start
1511 _playback_buf->read(dst, start, end);