Add a dedicated export method to MidiRegion
authorJulien "_FrnchFrgg_" RIVAUD <frnchfrgg@free.fr>
Tue, 19 Jul 2016 23:53:31 +0000 (01:53 +0200)
committerJulien "_FrnchFrgg_" RIVAUD <frnchfrgg@free.fr>
Wed, 20 Jul 2016 00:01:40 +0000 (02:01 +0200)
To export a MIDI region to a file, the code used MidiRegion::clone()
since it takes care of creating a new file-backed source with the wanted
contents. Nevertheless, it had several side-effects:
- it created and registered a new region which is confusing to users
- it only exported notes that were in the region range, but didn't
  remove the region start offset from MIDI events, essentially producing
  a spurious silence at the beginning of the exported file (this is not
  a problem for region cloning because the newly created region is made
  aware of the offset and caters for it).

Add a dedicated code path for export, that uses the new offsetting
capabilities of MidiModel::write_section_to().

libs/ardour/ardour/midi_region.h
libs/ardour/ardour/midi_source.h
libs/ardour/midi_region.cc
libs/ardour/midi_source.cc

index 197dcdbe6c64e46c8faccd753361ce40dec7bd17..5a1dfe0b44c2585cd9307c5b2430d673f2429884 100644 (file)
@@ -61,6 +61,8 @@ class LIBARDOUR_API MidiRegion : public Region
 
        ~MidiRegion();
 
+       bool do_export (std::string path) const;
+
        boost::shared_ptr<MidiRegion> clone (std::string path = std::string()) const;
        boost::shared_ptr<MidiRegion> clone (boost::shared_ptr<MidiSource>) const;
 
index 7f2ddfbc22b66b0af8f4d34eb904ba0eab508929..c8b4263e2a4fe481e48bad9a95b1838d946b6b2a 100644 (file)
@@ -63,6 +63,19 @@ class LIBARDOUR_API MidiSource : virtual public Source, public boost::enable_sha
                      Evoral::Beats                 begin = Evoral::MinBeats,
                      Evoral::Beats                 end   = Evoral::MaxBeats);
 
+       /** Export the midi data in the given time range to another MidiSource
+        * \param newsrc MidiSource to which data will be written. Should be a
+        *        new, empty source. If it already has contents, the results are
+        *        undefined. Source must be writable.
+        * \param begin time of earliest event that can be written.
+        * \param end time of latest event that can be written.
+        * \return zero on success, non-zero if the write failed for any reason.
+        */
+       int export_write_to (const Lock&                   lock,
+                            boost::shared_ptr<MidiSource> newsrc,
+                            Evoral::Beats                 begin,
+                            Evoral::Beats                 end);
+
        /** Read the data in a given time range from the MIDI source.
         * All time stamps in parameters are in audio frames (even if the source has tempo time).
         * \param dst Ring buffer where read events are written.
index 4c678c04e0d818f961a451c760857a6ab03d36a4..eef2811c5b963c7b438862cb02486ba86f0c783c 100644 (file)
@@ -120,6 +120,37 @@ MidiRegion::~MidiRegion ()
 {
 }
 
+/** Export the MIDI data of the MidiRegion to a new MIDI file (SMF).
+ */
+bool
+MidiRegion::do_export (string path) const
+{
+       boost::shared_ptr<MidiSource> newsrc;
+
+       /* caller must check for pre-existing file */
+       assert (!path.empty());
+       assert (!Glib::file_test (path, Glib::FILE_TEST_EXISTS));
+       newsrc = boost::dynamic_pointer_cast<MidiSource>(
+               SourceFactory::createWritable(DataType::MIDI, _session,
+                                             path, false, _session.frame_rate()));
+
+       BeatsFramesConverter bfc (_session.tempo_map(), _position);
+       Evoral::Beats const bbegin = bfc.from (_start);
+       Evoral::Beats const bend = bfc.from (_start + _length);
+
+       {
+               /* Lock our source since we'll be reading from it.  write_to() will
+                  take a lock on newsrc. */
+               Source::Lock lm (midi_source(0)->mutex());
+               if (midi_source(0)->export_write_to (lm, newsrc, bbegin, bend)) {
+                       return false;
+               }
+       }
+
+       return true;
+}
+
+
 /** Create a new MidiRegion that has its own version of some/all of the Source used by another.
  */
 boost::shared_ptr<MidiRegion>
index 5b671b4a2606eca00843d2f0db1ff50384847ad6..97bce4b1abc9f9c3b92c3a9f6d3bb9e842b9bf98 100644 (file)
@@ -384,6 +384,23 @@ MidiSource::mark_streaming_write_completed (const Lock& lock)
        mark_midi_streaming_write_completed (lock, Evoral::Sequence<Evoral::Beats>::DeleteStuckNotes);
 }
 
+int
+MidiSource::export_write_to (const Lock& lock, boost::shared_ptr<MidiSource> newsrc, Evoral::Beats begin, Evoral::Beats end)
+{
+       Lock newsrc_lock (newsrc->mutex ());
+
+       if (!_model) {
+               error << string_compose (_("programming error: %1"), X_("no model for MidiSource during export"));
+               return -1;
+       }
+
+       _model->write_section_to (newsrc, newsrc_lock, begin, end, true);
+
+       newsrc->flush_midi(newsrc_lock);
+
+       return 0;
+}
+
 int
 MidiSource::write_to (const Lock& lock, boost::shared_ptr<MidiSource> newsrc, Evoral::Beats begin, Evoral::Beats end)
 {