enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[ardour.git] / libs / ardour / audio_playlist_source.cc
index 568b72129d3d8807704705bac2446c131b6382f1..f77c8b526fe1280582f464495f742e7f4a3107fb 100644 (file)
 #include <cstdio>
 
 #include <glibmm/fileutils.h>
+#include <glibmm/miscutils.h>
 
 #include "pbd/error.h"
-#include "pbd/convert.h"
-#include "pbd/enumwriter.h"
 
 #include "ardour/audioplaylist.h"
 #include "ardour/audio_playlist_source.h"
 #include "ardour/audioregion.h"
-#include "ardour/debug.h"
+#include "ardour/filename_extensions.h"
 #include "ardour/session.h"
 #include "ardour/session_directory.h"
-#include "ardour/session_playlists.h"
-#include "ardour/source_factory.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
 
-AudioPlaylistSource::AudioPlaylistSource (Session& s, const std::string& name, boost::shared_ptr<AudioPlaylist> p, 
-                                         uint32_t chn, frameoffset_t begin, framecnt_t len, bool copy, Source::Flag flags)
+AudioPlaylistSource::AudioPlaylistSource (Session& s, const ID& orig, const std::string& name, boost::shared_ptr<AudioPlaylist> p,
+                                         uint32_t chn, frameoffset_t begin, framecnt_t len, Source::Flag flags)
        : Source (s, DataType::AUDIO, name)
+       , PlaylistSource (s, orig, name, p, DataType::AUDIO, begin, len, flags)
        , AudioSource (s, name)
        , _playlist_channel (chn)
 {
-       /* PlaylistSources are never writable, renameable, removable or destructive */
-       _flags = Flag (_flags & ~(Writable|CanRename|Removable|RemovableIfEmpty|RemoveAtDestroy|Destructive));
-
-       if (!copy) {
-               _playlist = p;
-               _playlist_offset = begin;
-               _playlist_length = len;
-       } else {
-               _playlist.reset (new AudioPlaylist (p, begin, len, "XXXNAMEXXX", true));
-               _playlist_offset = 0;
-               _playlist_length = len;
-       }
-
-       _peak_path = tempnam (_session.session_directory().peak_path().to_string().c_str(), "apspk");
+       AudioSource::_length = len;
+       ensure_buffers_for_level (_level, _session.frame_rate());
 }
 
 AudioPlaylistSource::AudioPlaylistSource (Session& s, const XMLNode& node)
-       : Source (s, DataType::AUDIO, "toBeRenamed")
+       : Source (s, node)
+       , PlaylistSource (s, node)
        , AudioSource (s, node)
 {
        /* PlaylistSources are never writable, renameable, removable or destructive */
        _flags = Flag (_flags & ~(Writable|CanRename|Removable|RemovableIfEmpty|RemoveAtDestroy|Destructive));
-       
-       set_state (node, 3000);
+
+       /* ancestors have already called ::set_state() in their XML-based
+          constructors.
+       */
+
+       if (set_state (node, Stateful::loading_state_version, false)) {
+               throw failed_constructor ();
+       }
+
+       AudioSource::_length = _playlist_length;
 }
 
 AudioPlaylistSource::~AudioPlaylistSource ()
@@ -85,57 +80,38 @@ AudioPlaylistSource::get_state ()
 {
        XMLNode& node (AudioSource::get_state ());
        char buf[64];
-       snprintf (buf, sizeof (buf), "%" PRIi64, _playlist_offset);
-       node.add_property ("offset", buf);
-       snprintf (buf, sizeof (buf), "%" PRIu64, _playlist_length);
-       node.add_property ("length", buf);
+
+       /* merge PlaylistSource state */
+
+       PlaylistSource::add_state (node);
+
        snprintf (buf, sizeof (buf), "%" PRIu32, _playlist_channel);
        node.add_property ("channel", buf);
-       node.add_property ("name", name());
-       node.add_property ("peak-path", _peak_path);
 
        return node;
 }
 
 int
-AudioPlaylistSource::set_state (const XMLNode& node, int /* version */) 
+AudioPlaylistSource::set_state (const XMLNode& node, int version)
 {
-       /* get playlist ID */
-
-       const XMLProperty *prop = node.property (X_("playlist"));
-
-       if (!prop) {
-               throw failed_constructor ();
-       }
-
-       PBD::ID id (prop->value());
-
-       /* get playlist */
-
-       boost::shared_ptr<Playlist> p = _session.playlists->by_id (id);
-
-       if (!p) {
-               throw failed_constructor ();
-       }
-
-       /* other properties */
-
-       if ((prop = node.property (X_("name"))) == 0) {
-               throw failed_constructor ();
-       }
-       
-       set_name (prop->value());
+       return set_state (node, version, true);
+}
 
-       if ((prop = node.property (X_("offset"))) == 0) {
-               throw failed_constructor ();
+int
+AudioPlaylistSource::set_state (const XMLNode& node, int version, bool with_descendants)
+{
+       if (with_descendants) {
+               if (Source::set_state (node, version) ||
+                   PlaylistSource::set_state (node, version) ||
+                   AudioSource::set_state (node, version)) {
+                       return -1;
+               }
        }
-       sscanf (prop->value().c_str(), "%" PRIi64, &_playlist_offset);
 
-       if ((prop = node.property (X_("length"))) == 0) {
-               throw failed_constructor ();
-       }
+       XMLProperty const * prop;
+       pair<framepos_t,framepos_t> extent = _playlist->get_extent();
 
-       sscanf (prop->value().c_str(), "%" PRIu64, &_playlist_length);
+       AudioSource::_length = extent.second - extent.first;
 
        if ((prop = node.property (X_("channel"))) == 0) {
                throw failed_constructor ();
@@ -143,21 +119,18 @@ AudioPlaylistSource::set_state (const XMLNode& node, int /* version */)
 
        sscanf (prop->value().c_str(), "%" PRIu32, &_playlist_channel);
 
-       if ((prop = node.property (X_("peak-path"))) == 0) {
-               throw failed_constructor ();
-       }
-
-       _peak_path = prop->value ();
+       ensure_buffers_for_level (_level, _session.frame_rate());
 
        return 0;
 }
 
-framecnt_t 
+framecnt_t
 AudioPlaylistSource::read_unlocked (Sample* dst, framepos_t start, framecnt_t cnt) const
 {
+       boost::shared_array<Sample> sbuf;
+       boost::shared_array<gain_t> gbuf;
        framecnt_t to_read;
        framecnt_t to_zero;
-       pair<framepos_t,framepos_t> extent = _playlist->get_extent();
 
        /* we must be careful not to read beyond the end of our "section" of
         * the playlist, because otherwise we may read data that exists, but
@@ -172,8 +145,19 @@ AudioPlaylistSource::read_unlocked (Sample* dst, framepos_t start, framecnt_t cn
                to_zero = 0;
        }
 
-       _playlist->read (dst, 0, 0, start+_playlist_offset, to_read, _playlist_channel);
-       
+       {
+               /* Don't need to hold the lock for the actual read, and
+                  actually, we cannot, but we do want to interlock
+                  with any changes to the list of buffers caused
+                  by creating new nested playlists/sources
+               */
+               Glib::Threads::Mutex::Lock lm (_level_buffer_lock);
+               sbuf = _mixdown_buffers[_level-1];
+               gbuf = _gain_buffers[_level-1];
+       }
+
+       boost::dynamic_pointer_cast<AudioPlaylist>(_playlist)->read (dst, sbuf.get(), gbuf.get(), start+_playlist_offset, to_read, _playlist_channel);
+
        if (to_zero) {
                memset (dst+to_read, 0, sizeof (Sample) * to_zero);
        }
@@ -181,11 +165,11 @@ AudioPlaylistSource::read_unlocked (Sample* dst, framepos_t start, framecnt_t cn
        return cnt;
 }
 
-framecnt_t 
-AudioPlaylistSource::write_unlocked (Sample *src, framecnt_t cnt) 
+framecnt_t
+AudioPlaylistSource::write_unlocked (Sample *, framecnt_t)
 {
        fatal << string_compose (_("programming error: %1"), "AudioPlaylistSource::write() called - should be impossible") << endmsg;
-       /*NOTREACHED*/
+       abort(); /*NOTREACHED*/
        return 0;
 }
 
@@ -204,7 +188,7 @@ AudioPlaylistSource::n_channels () const
                return 1;
        }
 
-       boost::shared_ptr<Region> r = _playlist->region_list().front ();
+       boost::shared_ptr<Region> r = _playlist->region_list_property().front ();
        boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (r);
 
        return ar->audio_source()->n_channels ();
@@ -219,7 +203,7 @@ AudioPlaylistSource::sample_rate () const
                _session.frame_rate ();
        }
 
-       boost::shared_ptr<Region> r = _playlist->region_list().front ();
+       boost::shared_ptr<Region> r = _playlist->region_list_property().front ();
        boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (r);
 
        return ar->audio_source()->sample_rate ();
@@ -228,23 +212,14 @@ AudioPlaylistSource::sample_rate () const
 int
 AudioPlaylistSource::setup_peakfile ()
 {
-       /* the peak data is setup once and once only 
-        */
-
-       if (!Glib::file_test (_peak_path, Glib::FILE_TEST_EXISTS)) {
-               /* the 2nd argument here will be passed
-                  in to ::peak_path, and is irrelevant
-                  since our peak file path is fixed and
-                  not dependent on anything.
-               */
-               
-               return initialize_peakfile (true, string());
-       } 
-       return 0;
+       _peak_path = Glib::build_filename (_session.session_directory().peak_path(), name() + ARDOUR::peakfile_suffix);
+       return initialize_peakfile (string());
 }
 
 string
-AudioPlaylistSource::peak_path (string /*audio_path*/)
+AudioPlaylistSource::construct_peak_filepath (const string& /*audio_path_*/, const bool /* in_session */, const bool /* old_peak_name */) const
 {
        return _peak_path;
 }
+
+