fix merge errors with master
[ardour.git] / libs / ardour / midi_diskstream.cc
index cf54bef82ee2e93115531da724364bca456a2379..4771f7128cf7ac3e6263e07273be7fd456f42fd9 100644 (file)
@@ -26,6 +26,7 @@
 #include <fcntl.h>
 #include <cstdlib>
 #include <ctime>
+#include <strings.h> // for ffs(3)
 #include <sys/stat.h>
 #include <sys/mman.h>
 
@@ -201,8 +202,8 @@ MidiDiskstream::non_realtime_input_change ()
                seek (_session.transport_frame());
        }
 
-       g_atomic_int_set(&_frames_pending_write, 0);
-       g_atomic_int_set(&_num_captured_loops, 0);
+       g_atomic_int_set(const_cast<gint*> (&_frames_pending_write), 0);
+       g_atomic_int_set(const_cast<gint*> (&_num_captured_loops), 0);
 }
 
 int
@@ -320,7 +321,7 @@ get_location_times(const Location* location,
 }
 
 int
-MidiDiskstream::process (framepos_t transport_frame, pframes_t nframes, framecnt_t& playback_distance)
+MidiDiskstream::process (BufferSet& bufs, framepos_t transport_frame, pframes_t nframes, framecnt_t& playback_distance, bool need_disk_signal)
 {
        framecnt_t rec_offset = 0;
        framecnt_t rec_nframes = 0;
@@ -375,8 +376,8 @@ MidiDiskstream::process (framepos_t transport_frame, pframes_t nframes, framecnt
                        }
                        _write_source->mark_write_starting_now(
                                capture_start_frame, capture_captured, loop_length);
-                       g_atomic_int_set(&_frames_pending_write, 0);
-                       g_atomic_int_set(&_num_captured_loops, 0);
+                       g_atomic_int_set(const_cast<gint*> (&_frames_pending_write), 0);
+                       g_atomic_int_set(const_cast<gint*> (&_num_captured_loops), 0);
                        was_recording = true;
                }
        }
@@ -389,8 +390,11 @@ MidiDiskstream::process (framepos_t transport_frame, pframes_t nframes, framecnt
 
                // Pump entire port buffer into the ring buffer (FIXME: split cycles?)
                MidiBuffer& buf = sp->get_midi_buffer(nframes);
+               ChannelMode mode = AllChannels; // _track->get_capture_channel_mode ();
+               uint32_t mask = 0xffff; // _track->get_capture_channel_mask ();
+
                for (MidiBuffer::iterator i = buf.begin(); i != buf.end(); ++i) {
-                       const Evoral::MIDIEvent<MidiBuffer::TimeType> ev(*i, false);
+                       Evoral::MIDIEvent<MidiBuffer::TimeType> ev(*i, false);
 #ifndef NDEBUG
                        if (DEBUG::MidiIO & PBD::debug_bits) {
                                const uint8_t* __data = ev.buffer();
@@ -416,10 +420,33 @@ MidiDiskstream::process (framepos_t transport_frame, pframes_t nframes, framecnt
                           probabl be implemented in the source instead of here.
                        */
                        const framecnt_t loop_offset = _num_captured_loops * loop_length;
-                       _capture_buf->write(transport_frame + loop_offset + ev.time(),
-                                           ev.type(), ev.size(), ev.buffer());
+                       
+                       switch (mode) {
+                       case AllChannels:
+                               _capture_buf->write(transport_frame + loop_offset + ev.time(),
+                                                   ev.type(), ev.size(), ev.buffer());
+                               break;
+                       case FilterChannels:
+                               if (ev.is_channel_event()) {
+                                       if ((1<<ev.channel()) & mask) {
+                                               _capture_buf->write(transport_frame + loop_offset + ev.time(),
+                                                                   ev.type(), ev.size(), ev.buffer());
+                                       }
+                               } else {
+                                       _capture_buf->write(transport_frame + loop_offset + ev.time(),
+                                                           ev.type(), ev.size(), ev.buffer());
+                               }
+                               break;
+                       case ForceChannel:
+                               if (ev.is_channel_event()) {
+                                       ev.set_channel (ffs(mask) - 1);
+                               }
+                               _capture_buf->write(transport_frame + loop_offset + ev.time(),
+                                                   ev.type(), ev.size(), ev.buffer());
+                               break;
+                       }
                }
-               g_atomic_int_add(&_frames_pending_write, nframes);
+               g_atomic_int_add(const_cast<gint*>(&_frames_pending_write), nframes);
 
                if (buf.size() != 0) {
                        Glib::Threads::Mutex::Lock lm (_gui_feed_buffer_mutex, Glib::Threads::TRY_LOCK);
@@ -475,9 +502,35 @@ MidiDiskstream::process (framepos_t transport_frame, pframes_t nframes, framecnt
 
        }
 
+       if (need_disk_signal) {
+               /* copy the diskstream data to all output buffers */
+               
+               MidiBuffer& mbuf (bufs.get_midi (0));
+               get_playback (mbuf, nframes);
+               
+               /* leave the audio count alone */
+               ChanCount cnt (DataType::MIDI, 1);
+               cnt.set (DataType::AUDIO, bufs.count().n_audio());
+               bufs.set_count (cnt);
+       }
+
        return 0;
 }
 
+frameoffset_t
+MidiDiskstream::calculate_playback_distance (pframes_t nframes)
+{
+       frameoffset_t playback_distance = nframes;
+
+       /* XXX: should be doing varispeed stuff once it's implemented in ::process() above */
+
+       if (_actual_speed < 0.0) {
+               return -playback_distance;
+       } else {
+               return playback_distance;
+       }
+}
+
 bool
 MidiDiskstream::commit (framecnt_t playback_distance)
 {
@@ -746,7 +799,7 @@ MidiDiskstream::do_flush (RunContext /*context*/, bool force_flush)
                return 0;
        }
 
-       const framecnt_t total = g_atomic_int_get(&_frames_pending_write);
+       const framecnt_t total = g_atomic_int_get(const_cast<gint*> (&_frames_pending_write));
 
        if (total == 0 || 
            _capture_buf->read_space() == 0 || 
@@ -755,7 +808,7 @@ MidiDiskstream::do_flush (RunContext /*context*/, bool force_flush)
        }
 
        /* if there are 2+ chunks of disk i/o possible for
-          this track, let the caller know so that it can arrange
+          this track), let the caller know so that it can arrange
           for us to be called again, ASAP.
 
           if we are forcing a flush, then if there is* any* extra
@@ -781,7 +834,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(&_frames_pending_write, -to_write);
+               g_atomic_int_add(const_cast<gint*> (&_frames_pending_write), -to_write);
        }
 
 out:
@@ -991,7 +1044,7 @@ MidiDiskstream::transport_looped (framepos_t)
           the Source and/or entirely after the capture is finished.
        */
        if (was_recording) {
-               g_atomic_int_add(&_num_captured_loops, 1);
+               g_atomic_int_add(const_cast<gint*> (&_num_captured_loops), 1);
        }
 }
 
@@ -1058,7 +1111,7 @@ MidiDiskstream::prep_record_enable ()
        boost::shared_ptr<MidiPort> 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;
@@ -1078,10 +1131,6 @@ MidiDiskstream::get_state ()
        char buf[64];
        LocaleGuard lg (X_("POSIX"));
 
-       node.add_property("channel-mode", enum_2_string(get_channel_mode()));
-       snprintf (buf, sizeof(buf), "0x%x", get_channel_mask());
-       node.add_property("channel-mask", buf);
-
        if (_write_source && _session.get_record_enabled()) {
 
                XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
@@ -1111,7 +1160,6 @@ MidiDiskstream::get_state ()
 int
 MidiDiskstream::set_state (const XMLNode& node, int version)
 {
-       const XMLProperty* prop;
        XMLNodeList nlist = node.children();
        XMLNodeIterator niter;
        XMLNode* capture_pending_node = 0;
@@ -1131,26 +1179,10 @@ MidiDiskstream::set_state (const XMLNode& node, int version)
                return -1;
        }
 
-       ChannelMode channel_mode = AllChannels;
-       if ((prop = node.property ("channel-mode")) != 0) {
-               channel_mode = ChannelMode (string_2_enum(prop->value(), channel_mode));
-       }
-
-       unsigned int channel_mask = 0xFFFF;
-       if ((prop = node.property ("channel-mask")) != 0) {
-               sscanf (prop->value().c_str(), "0x%x", &channel_mask);
-               if (channel_mask & (~0xFFFF)) {
-                       warning << _("MidiDiskstream: XML property channel-mask out of range") << endmsg;
-               }
-       }
-
-
        if (capture_pending_node) {
                use_pending_capture_data (*capture_pending_node);
        }
 
-       set_channel_mode (channel_mode, channel_mask);
-
        in_set_state = false;
 
        return 0;
@@ -1227,12 +1259,12 @@ MidiDiskstream::allocate_temporary_buffers ()
 }
 
 void
-MidiDiskstream::ensure_jack_monitors_input (bool yn)
+MidiDiskstream::ensure_input_monitoring (bool yn)
 {
        boost::shared_ptr<MidiPort> sp = _source_port.lock ();
        
        if (sp) {
-               sp->ensure_jack_monitors_input (yn);
+               sp->ensure_input_monitoring (yn);
        }
 }