X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Fmidi_diskstream.cc;h=69eca996aaeda92cf56c47643f34f4d966d82265;hb=7204702c3ffbd3a1c6747511104d9c6af1b9c93d;hp=f059269b638a85efa54c12c94d2d65fe680e565f;hpb=e6521bb0434d88802aa28e75235bc8c19ebb9262;p=ardour.git diff --git a/libs/ardour/midi_diskstream.cc b/libs/ardour/midi_diskstream.cc index f059269b63..69eca996aa 100644 --- a/libs/ardour/midi_diskstream.cc +++ b/libs/ardour/midi_diskstream.cc @@ -26,11 +26,10 @@ #include #include #include -#include // for ffs(3) #include -#include #include "pbd/error.h" +#include "pbd/ffs.h" #include "pbd/basename.h" #include #include "pbd/xml++.h" @@ -76,6 +75,7 @@ MidiDiskstream::MidiDiskstream (Session &sess, const string &name, Diskstream::F , _frames_read_from_ringbuffer(0) , _frames_pending_write(0) , _num_captured_loops(0) + , _accumulated_capture_offset(0) , _gui_feed_buffer(AudioEngine::instance()->raw_buffer_size (DataType::MIDI)) { in_set_state = true; @@ -100,6 +100,7 @@ MidiDiskstream::MidiDiskstream (Session& sess, const XMLNode& node) , _frames_read_from_ringbuffer(0) , _frames_pending_write(0) , _num_captured_loops(0) + , _accumulated_capture_offset(0) , _gui_feed_buffer(AudioEngine::instance()->raw_buffer_size (DataType::MIDI)) { in_set_state = true; @@ -137,6 +138,8 @@ MidiDiskstream::init () MidiDiskstream::~MidiDiskstream () { Glib::Threads::Mutex::Lock lm (state_lock); + delete _playback_buf; + delete _capture_buf; } @@ -202,8 +205,8 @@ MidiDiskstream::non_realtime_input_change () seek (_session.transport_frame()); } - g_atomic_int_set(const_cast(&_frames_pending_write), 0); - g_atomic_int_set(const_cast(&_num_captured_loops), 0); + g_atomic_int_set(const_cast (&_frames_pending_write), 0); + g_atomic_int_set(const_cast (&_num_captured_loops), 0); } int @@ -363,6 +366,15 @@ MidiDiskstream::process (BufferSet& bufs, framepos_t transport_frame, pframes_t Evoral::OverlapType ot = Evoral::coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes); calculate_record_range(ot, transport_frame, nframes, rec_nframes, rec_offset); + /* For audio: not writing frames to the capture ringbuffer offsets + * the recording. For midi: we need to keep track of the record range + * and subtract the accumulated difference from the event time. + */ + if (rec_nframes) { + _accumulated_capture_offset += rec_offset; + } else { + _accumulated_capture_offset += nframes; + } if (rec_nframes && !was_recording) { if (loop_loc) { @@ -376,8 +388,8 @@ MidiDiskstream::process (BufferSet& bufs, framepos_t transport_frame, pframes_t } _write_source->mark_write_starting_now( capture_start_frame, capture_captured, loop_length); - g_atomic_int_set(const_cast(&_frames_pending_write), 0); - g_atomic_int_set(const_cast(&_num_captured_loops), 0); + g_atomic_int_set(const_cast (&_frames_pending_write), 0); + g_atomic_int_set(const_cast (&_num_captured_loops), 0); was_recording = true; } } @@ -395,6 +407,9 @@ MidiDiskstream::process (BufferSet& bufs, framepos_t transport_frame, pframes_t for (MidiBuffer::iterator i = buf.begin(); i != buf.end(); ++i) { Evoral::MIDIEvent ev(*i, false); + if (ev.time() + rec_offset > rec_nframes) { + break; + } #ifndef NDEBUG if (DEBUG::MidiIO & PBD::debug_bits) { const uint8_t* __data = ev.buffer(); @@ -417,31 +432,34 @@ MidiDiskstream::process (BufferSet& bufs, framepos_t transport_frame, pframes_t any desirable behaviour. We don't want to send event with transport time here since that way the source can not reconstruct their actual time; future clever MIDI looping should - probabl be implemented in the source instead of here. + probably be implemented in the source instead of here. */ const framecnt_t loop_offset = _num_captured_loops * loop_length; - + const framepos_t event_time = transport_frame + loop_offset - _accumulated_capture_offset + ev.time(); + if (event_time < 0 || event_time < first_recordable_frame) { + continue; + } switch (mode) { case AllChannels: - _capture_buf->write(transport_frame + loop_offset + ev.time(), + _capture_buf->write(event_time, ev.type(), ev.size(), ev.buffer()); break; case FilterChannels: if (ev.is_channel_event()) { if ((1<write(transport_frame + loop_offset + ev.time(), + _capture_buf->write(event_time, ev.type(), ev.size(), ev.buffer()); } } else { - _capture_buf->write(transport_frame + loop_offset + ev.time(), + _capture_buf->write(event_time, ev.type(), ev.size(), ev.buffer()); } break; case ForceChannel: if (ev.is_channel_event()) { - ev.set_channel (ffs(mask) - 1); + ev.set_channel (PBD::ffs(mask) - 1); } - _capture_buf->write(transport_frame + loop_offset + ev.time(), + _capture_buf->write(event_time, ev.type(), ev.size(), ev.buffer()); break; } @@ -473,6 +491,7 @@ MidiDiskstream::process (BufferSet& bufs, framepos_t transport_frame, pframes_t if (was_recording) { finish_capture (); } + _accumulated_capture_offset = 0; } @@ -799,7 +818,7 @@ MidiDiskstream::do_flush (RunContext /*context*/, bool force_flush) return 0; } - const framecnt_t total = g_atomic_int_get(const_cast(&_frames_pending_write)); + const framecnt_t total = g_atomic_int_get(const_cast (&_frames_pending_write)); if (total == 0 || _capture_buf->read_space() == 0 || @@ -834,7 +853,7 @@ MidiDiskstream::do_flush (RunContext /*context*/, bool force_flush) error << string_compose(_("MidiDiskstream %1: cannot write to disk"), id()) << endmsg; return -1; } - g_atomic_int_add(const_cast(&_frames_pending_write), -to_write); + g_atomic_int_add(const_cast (&_frames_pending_write), -to_write); } out: @@ -973,6 +992,10 @@ MidiDiskstream::transport_stopped_wallclock (struct tm& /*when*/, time_t /*twhen RegionFactory::region_name (region_name, _write_source->name(), false); + DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 capture start @ %2 length %3 add new region %4\n", + _name, (*ci)->start, (*ci)->frames, region_name)); + + // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add a region\n"; try { @@ -1044,7 +1067,7 @@ MidiDiskstream::transport_looped (framepos_t) the Source and/or entirely after the capture is finished. */ if (was_recording) { - g_atomic_int_add(const_cast(&_num_captured_loops), 1); + g_atomic_int_add(const_cast (&_num_captured_loops), 1); } } @@ -1111,7 +1134,7 @@ MidiDiskstream::prep_record_enable () boost::shared_ptr sp = _source_port.lock (); if (sp && Config->get_monitoring_model() == HardwareMonitoring) { - sp->request_jack_monitors_input (!(_session.config.get_auto_input() && rolling)); + sp->request_input_monitoring (!(_session.config.get_auto_input() && rolling)); } return true; @@ -1195,11 +1218,12 @@ MidiDiskstream::use_new_write_source (uint32_t n) return 1; } + _accumulated_capture_offset = 0; _write_source.reset(); try { _write_source = boost::dynamic_pointer_cast( - _session.create_midi_source_for_session (0, name ())); + _session.create_midi_source_for_session (write_source_name ())); if (!_write_source) { throw failed_constructor(); @@ -1214,25 +1238,36 @@ MidiDiskstream::use_new_write_source (uint32_t n) return 0; } - -list > -MidiDiskstream::steal_write_sources() +/** + * We want to use the name of the existing write source (the one that will be + * used by the next capture) for another purpose. So change the name of the + * current source, and return its current name. + * + * Return an empty string if the change cannot be accomplished. + */ +std::string +MidiDiskstream::steal_write_source_name () { - list > ret; - - /* put some data on the disk, even if its just a header for an empty file */ - boost::dynamic_pointer_cast (_write_source)->ensure_disk_file (); - - /* never let it go away */ - _write_source->mark_nonremovable (); + string our_old_name = _write_source->name(); - ret.push_back (_write_source); - - /* get a new one */ - - use_new_write_source (0); + /* this will bump the name of the current write source to the next one + * (e.g. "MIDI 1-1" gets renamed to "MIDI 1-2"), thus leaving the + * current write source name (e.g. "MIDI 1-1" available). See the + * comments in Session::create_midi_source_by_stealing_name() about why + * we do this. + */ - return ret; + try { + string new_path = _session.new_midi_source_path (name()); + + if (_write_source->rename (new_path)) { + return string(); + } + } catch (...) { + return string (); + } + + return our_old_name; } void @@ -1259,12 +1294,12 @@ MidiDiskstream::allocate_temporary_buffers () } void -MidiDiskstream::ensure_jack_monitors_input (bool yn) +MidiDiskstream::ensure_input_monitoring (bool yn) { boost::shared_ptr sp = _source_port.lock (); if (sp) { - sp->ensure_jack_monitors_input (yn); + sp->ensure_input_monitoring (yn); } } @@ -1406,6 +1441,9 @@ MidiDiskstream::get_playback (MidiBuffer& dst, framecnt_t nframes) bool MidiDiskstream::set_name (string const & name) { + if (_name == name) { + return true; + } Diskstream::set_name (name); /* get a new write source so that its name reflects the new diskstream name */ @@ -1414,6 +1452,19 @@ MidiDiskstream::set_name (string const & name) return true; } +bool +MidiDiskstream::set_write_source_name (const std::string& str) { + if (_write_source_name == str) { + return true; + } + Diskstream::set_write_source_name (str); + if (_write_source_name == name()) { + return true; + } + use_new_write_source (0); + return true; +} + boost::shared_ptr MidiDiskstream::get_gui_feed_buffer () const {