dramatic change in logic and naming for operations related to adding a MIDI region...
[ardour.git] / libs / ardour / midi_diskstream.cc
index f059269b638a85efa54c12c94d2d65fe680e565f..fdf155d78b76cdc9795d7928161d13cc07a7d222 100644 (file)
 #include <fcntl.h>
 #include <cstdlib>
 #include <ctime>
-#include <strings.h> // for ffs(3)
 #include <sys/stat.h>
-#include <sys/mman.h>
 
 #include "pbd/error.h"
+#include "pbd/ffs.h"
 #include "pbd/basename.h"
 #include <glibmm/threads.h>
 #include "pbd/xml++.h"
@@ -202,8 +201,8 @@ MidiDiskstream::non_realtime_input_change ()
                seek (_session.transport_frame());
        }
 
-       g_atomic_int_set(const_cast<gint*>(&_frames_pending_write), 0);
-       g_atomic_int_set(const_cast<gint*>(&_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
@@ -376,8 +375,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<gint*>(&_frames_pending_write), 0);
-                       g_atomic_int_set(const_cast<gint*>(&_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;
                }
        }
@@ -439,7 +438,7 @@ MidiDiskstream::process (BufferSet& bufs, framepos_t transport_frame, pframes_t
                                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(),
                                                    ev.type(), ev.size(), ev.buffer());
@@ -799,7 +798,7 @@ MidiDiskstream::do_flush (RunContext /*context*/, bool force_flush)
                return 0;
        }
 
-       const framecnt_t total = g_atomic_int_get(const_cast<gint*>(&_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 || 
@@ -834,7 +833,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<gint*>(&_frames_pending_write), -to_write);
+               g_atomic_int_add(const_cast<gint*> (&_frames_pending_write), -to_write);
        }
 
 out:
@@ -1044,7 +1043,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<gint*>(&_num_captured_loops), 1);
+               g_atomic_int_add(const_cast<gint*> (&_num_captured_loops), 1);
        }
 }
 
@@ -1111,7 +1110,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;
@@ -1199,7 +1198,7 @@ MidiDiskstream::use_new_write_source (uint32_t n)
 
        try {
                _write_source = boost::dynamic_pointer_cast<SMFSource>(
-                       _session.create_midi_source_for_session (0, name ()));
+                       _session.create_midi_source_for_session (name ()));
 
                if (!_write_source) {
                        throw failed_constructor();
@@ -1214,25 +1213,30 @@ MidiDiskstream::use_new_write_source (uint32_t n)
 
        return 0;
 }
-
-list<boost::shared_ptr<Source> >
-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<boost::shared_ptr<Source> > ret;
-
-       /* put some data on the disk, even if its just a header for an empty file */
-       boost::dynamic_pointer_cast<SMFSource> (_write_source)->ensure_disk_file ();
-
-       /* never let it go away */
-       _write_source->mark_nonremovable ();
-
-       ret.push_back (_write_source);
+       string our_old_name = _write_source->name();
 
-       /* 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_for_track() about why we do
+        * this.
+        */
 
-       return ret;
+       if (_write_source->set_source_name (name(), false)) {
+               return string();
+       }
+       
+       return our_old_name;
 }
 
 void
@@ -1259,12 +1263,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);
        }
 }