many important changes to configuration system and specific parameters
[ardour.git] / libs / ardour / audio_diskstream.cc
index 9619f404fabd466b975ac748fbe9f87f900178ea..71d16583e00f1cf9945e2eda060e55eaa11a793c 100644 (file)
 #include <ardour/audiofilesource.h>
 #include <ardour/destructive_filesource.h>
 #include <ardour/send.h>
+#include <ardour/region_factory.h>
 #include <ardour/audioplaylist.h>
 #include <ardour/cycle_timer.h>
 #include <ardour/audioregion.h>
+#include <ardour/source_factory.h>
 
 #include "i18n.h"
 #include <locale.h>
@@ -71,8 +73,6 @@ AudioDiskstream::AudioDiskstream (Session &sess, const string &name, Diskstream:
        use_new_playlist ();
 
        in_set_state = false;
-
-       DiskstreamCreated (this); /* EMIT SIGNAL */
 }
        
 AudioDiskstream::AudioDiskstream (Session& sess, const XMLNode& node)
@@ -92,8 +92,6 @@ AudioDiskstream::AudioDiskstream (Session& sess, const XMLNode& node)
        if (destructive()) {
                use_destructive_playlist ();
        }
-
-       DiskstreamCreated (this); /* EMIT SIGNAL */
 }
 
 void
@@ -103,7 +101,6 @@ AudioDiskstream::init_channel (ChannelInfo &chan)
        chan.capture_wrap_buffer = 0;
        chan.speed_buffer = 0;
        chan.peak_power = 0.0f;
-       chan.write_source = 0;
        chan.source = 0;
        chan.current_capture_buffer = 0;
        chan.current_playback_buffer = 0;
@@ -146,8 +143,7 @@ void
 AudioDiskstream::destroy_channel (ChannelInfo &chan)
 {
        if (chan.write_source) {
-               chan.write_source->release ();
-               chan.write_source = 0;
+               chan.write_source.reset ();
        }
                
        if (chan.speed_buffer) {
@@ -192,8 +188,8 @@ AudioDiskstream::allocate_working_buffers()
 void
 AudioDiskstream::free_working_buffers()
 {
-       delete _mixdown_buffer;
-       delete _gain_buffer;
+       delete [] _mixdown_buffer;
+       delete [] _gain_buffer;
        _working_buffers_size = 0;
        _mixdown_buffer       = 0;
        _gain_buffer          = 0;
@@ -253,9 +249,8 @@ AudioDiskstream::non_realtime_input_change ()
        /* now refill channel buffers */
 
        if (speed() != 1.0f || speed() != -1.0f) {
-               seek ((jack_nframes_t) (_session.transport_frame() * (double) speed()));
-       }
-       else {
+               seek ((nframes_t) (_session.transport_frame() * (double) speed()));
+       } else {
                seek (_session.transport_frame());
        }
 }
@@ -371,7 +366,7 @@ AudioDiskstream::use_copy_playlist ()
 void
 AudioDiskstream::setup_destructive_playlist ()
 {
-       AudioRegion::SourceList srcs;
+       SourceList srcs;
 
        for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
                srcs.push_back ((*chan).write_source);
@@ -379,37 +374,37 @@ AudioDiskstream::setup_destructive_playlist ()
 
        /* a single full-sized region */
 
-       cerr << "setup DS using " << srcs.front()->natural_position () << endl;
-
-       AudioRegion* region = new AudioRegion (srcs, 0, max_frames, _name);
-       _playlist->add_region (*region, srcs.front()->natural_position());              
+       boost::shared_ptr<Region> region (RegionFactory::create (srcs, 0, max_frames - srcs.front()->natural_position(), _name));
+       _playlist->add_region (region, srcs.front()->natural_position());               
 }
 
 void
 AudioDiskstream::use_destructive_playlist ()
 {
-       /* use the sources associated with the single full-extent region */
-       
-       Playlist::RegionList* rl = _playlist->regions_at (0);
+       /* this is called from the XML-based constructor. when its done,
+          we already have a playlist and a region, but we need to
+          set up our sources for write. we use the sources associated 
+          with the (presumed single, full-extent) region.
+       */
+
+       boost::shared_ptr<Region> rp = _playlist->find_next_region (_session.current_start_frame(), Start, 1);
 
-       if (rl->empty()) {
+       if (!rp) {
                reset_write_sources (false, true);
                return;
        }
 
-       AudioRegion* region = dynamic_cast<AudioRegion*> (rl->front());
+       boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (rp);
 
        if (region == 0) {
                throw failed_constructor();
        }
 
-       delete rl;
-
        uint32_t n;
        ChannelList::iterator chan;
 
        for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
-               (*chan).write_source = dynamic_cast<AudioFileSource*>(&region->source (n));
+               (*chan).write_source = boost::dynamic_pointer_cast<AudioFileSource>(region->source (n));
                assert((*chan).write_source);
                (*chan).write_source->set_allow_remove_if_empty (false);
        }
@@ -418,7 +413,7 @@ AudioDiskstream::use_destructive_playlist ()
 }
 
 void
-AudioDiskstream::check_record_status (jack_nframes_t transport_frame, jack_nframes_t nframes, bool can_record)
+AudioDiskstream::check_record_status (nframes_t transport_frame, nframes_t nframes, bool can_record)
 {
        int possibly_recording;
        int rolling;
@@ -468,8 +463,7 @@ AudioDiskstream::check_record_status (jack_nframes_t transport_frame, jack_nfram
 
                        if (_alignment_style == ExistingMaterial) {
 
-
-                               if (!_session.get_punch_in()) {
+                               if (!Config->get_punch_in()) {
 
                                        /* manual punch in happens at the correct transport frame
                                           because the user hit a button. but to get alignment correct 
@@ -498,7 +492,7 @@ AudioDiskstream::check_record_status (jack_nframes_t transport_frame, jack_nfram
 
                        } else {
 
-                               if (_session.get_punch_in()) {
+                               if (Config->get_punch_in()) {
                                        first_recordable_frame += _roll_delay;
                                } else {
                                        capture_start_frame -= _roll_delay;
@@ -543,13 +537,13 @@ AudioDiskstream::check_record_status (jack_nframes_t transport_frame, jack_nfram
 }
 
 int
-AudioDiskstream::process (jack_nframes_t transport_frame, jack_nframes_t nframes, jack_nframes_t offset, bool can_record, bool rec_monitors_input)
+AudioDiskstream::process (nframes_t transport_frame, nframes_t nframes, nframes_t offset, bool can_record, bool rec_monitors_input)
 {
        uint32_t n;
        ChannelList::iterator c;
        int ret = -1;
-       jack_nframes_t rec_offset = 0;
-       jack_nframes_t rec_nframes = 0;
+       nframes_t rec_offset = 0;
+       nframes_t rec_nframes = 0;
        bool nominally_recording;
        bool re = record_enabled ();
        bool collect_playback = false;
@@ -592,7 +586,7 @@ AudioDiskstream::process (jack_nframes_t transport_frame, jack_nframes_t nframes
                (*c).current_playback_buffer  = 0;
        }
 
-       if (nominally_recording || (_session.get_record_enabled() && _session.get_punch_in())) {
+       if (nominally_recording || (_session.get_record_enabled() && Config->get_punch_in())) {
                OverlapType ot;
                
                ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
@@ -668,7 +662,7 @@ AudioDiskstream::process (jack_nframes_t transport_frame, jack_nframes_t nframes
 
                        } else {
 
-                               jack_nframes_t total = chan.capture_vector.len[0] + chan.capture_vector.len[1];
+                               nframes_t total = chan.capture_vector.len[0] + chan.capture_vector.len[1];
 
                                if (rec_nframes > total) {
                                        DiskOverrun ();
@@ -676,7 +670,7 @@ AudioDiskstream::process (jack_nframes_t transport_frame, jack_nframes_t nframes
                                }
 
                                Sample* buf = _io->input (n)->get_buffer (nframes) + offset;
-                               jack_nframes_t first = chan.capture_vector.len[0];
+                               nframes_t first = chan.capture_vector.len[0];
 
                                memcpy (chan.capture_wrap_buffer, buf, sizeof (Sample) * first);
                                memcpy (chan.capture_vector.buf[0], buf, sizeof (Sample) * first);
@@ -739,12 +733,12 @@ AudioDiskstream::process (jack_nframes_t transport_frame, jack_nframes_t nframes
 
                /* we're doing playback */
 
-               jack_nframes_t necessary_samples;
+               nframes_t necessary_samples;
 
                /* no varispeed playback if we're recording, because the output .... TBD */
 
                if (rec_nframes == 0 && _actual_speed != 1.0f) {
-                       necessary_samples = (jack_nframes_t) floor ((nframes * fabs (_actual_speed))) + 1;
+                       necessary_samples = (nframes_t) floor ((nframes * fabs (_actual_speed))) + 1;
                } else {
                        necessary_samples = nframes;
                }
@@ -764,7 +758,7 @@ AudioDiskstream::process (jack_nframes_t transport_frame, jack_nframes_t nframes
                                chan.current_playback_buffer = chan.playback_vector.buf[0];
 
                        } else {
-                               jack_nframes_t total = chan.playback_vector.len[0] + chan.playback_vector.len[1];
+                               nframes_t total = chan.playback_vector.len[0] + chan.playback_vector.len[1];
                                
                                if (necessary_samples > total) {
                                        DiskUnderrun ();
@@ -785,7 +779,7 @@ AudioDiskstream::process (jack_nframes_t transport_frame, jack_nframes_t nframes
                if (rec_nframes == 0 && _actual_speed != 1.0f && _actual_speed != -1.0f) {
                        
                        uint64_t phase = last_phase;
-                       jack_nframes_t i = 0;
+                       nframes_t i = 0;
 
                        // Linearly interpolate into the alt buffer
                        // using 40.24 fixp maths (swh)
@@ -798,7 +792,7 @@ AudioDiskstream::process (jack_nframes_t transport_frame, jack_nframes_t nframes
                                i = 0;
                                phase = last_phase;
 
-                               for (jack_nframes_t outsample = 0; outsample < nframes; ++outsample) {
+                               for (nframes_t outsample = 0; outsample < nframes; ++outsample) {
                                        i = phase >> 24;
                                        fr = (phase & 0xFFFFFF) / 16777216.0f;
                                        chan.speed_buffer[outsample] = 
@@ -837,7 +831,7 @@ AudioDiskstream::process (jack_nframes_t transport_frame, jack_nframes_t nframes
 }
 
 bool
-AudioDiskstream::commit (jack_nframes_t nframes)
+AudioDiskstream::commit (nframes_t nframes)
 {
        bool need_butler = false;
 
@@ -897,7 +891,7 @@ AudioDiskstream::overwrite_existing_buffers ()
        overwrite_queued = false;
 
        /* assume all are the same size */
-       jack_nframes_t size = channels[0].playback_buf->bufsize();
+       nframes_t size = channels[0].playback_buf->bufsize();
        
        mixdown_buffer = new Sample[size];
        gain_buffer = new float[size];
@@ -906,12 +900,12 @@ AudioDiskstream::overwrite_existing_buffers ()
        size--;
        
        uint32_t n=0;
-       jack_nframes_t start;
+       nframes_t start;
 
        for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
 
                start = overwrite_frame;
-               jack_nframes_t cnt = size;
+               nframes_t cnt = size;
                
                /* to fill the buffer without resetting the playback sample, we need to
                   do it one or two chunks (normally two).
@@ -924,7 +918,7 @@ AudioDiskstream::overwrite_existing_buffers ()
                   
                */
                
-               jack_nframes_t to_read = size - overwrite_offset;
+               nframes_t to_read = size - overwrite_offset;
 
                if (read ((*chan).playback_buf->buffer() + overwrite_offset, mixdown_buffer, gain_buffer, start, to_read, *chan, n, reversed)) {
                        error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
@@ -955,7 +949,7 @@ AudioDiskstream::overwrite_existing_buffers ()
 }
 
 int
-AudioDiskstream::seek (jack_nframes_t frame, bool complete_refill)
+AudioDiskstream::seek (nframes_t frame, bool complete_refill)
 {
        Glib::Mutex::Lock lm (state_lock);
        uint32_t n;
@@ -986,7 +980,7 @@ AudioDiskstream::seek (jack_nframes_t frame, bool complete_refill)
 }
 
 int
-AudioDiskstream::can_internal_playback_seek (jack_nframes_t distance)
+AudioDiskstream::can_internal_playback_seek (nframes_t distance)
 {
        ChannelList::iterator chan;
 
@@ -999,7 +993,7 @@ AudioDiskstream::can_internal_playback_seek (jack_nframes_t distance)
 }
 
 int
-AudioDiskstream::internal_playback_seek (jack_nframes_t distance)
+AudioDiskstream::internal_playback_seek (nframes_t distance)
 {
        ChannelList::iterator chan;
 
@@ -1014,15 +1008,15 @@ AudioDiskstream::internal_playback_seek (jack_nframes_t distance)
 }
 
 int
-AudioDiskstream::read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer, jack_nframes_t& start, jack_nframes_t cnt, 
+AudioDiskstream::read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer, nframes_t& start, nframes_t cnt, 
                  ChannelInfo& channel_info, int channel, bool reversed)
 {
-       jack_nframes_t this_read = 0;
+       nframes_t this_read = 0;
        bool reloop = false;
-       jack_nframes_t loop_end = 0;
-       jack_nframes_t loop_start = 0;
-       jack_nframes_t loop_length = 0;
-       jack_nframes_t offset = 0;
+       nframes_t loop_end = 0;
+       nframes_t loop_start = 0;
+       nframes_t loop_length = 0;
+       nframes_t offset = 0;
        Location *loc = 0;
 
        if (!reversed) {
@@ -1122,14 +1116,14 @@ int
 AudioDiskstream::_do_refill (Sample* mixdown_buffer, float* gain_buffer)
 {
        int32_t ret = 0;
-       jack_nframes_t to_read;
+       nframes_t to_read;
        RingBufferNPT<Sample>::rw_vector vector;
        bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
-       jack_nframes_t total_space;
-       jack_nframes_t zero_fill;
+       nframes_t total_space;
+       nframes_t zero_fill;
        uint32_t chan_n;
        ChannelList::iterator i;
-       jack_nframes_t ts;
+       nframes_t ts;
 
        assert(mixdown_buffer);
        assert(gain_buffer);
@@ -1241,14 +1235,14 @@ AudioDiskstream::_do_refill (Sample* mixdown_buffer, float* gain_buffer)
                }
        }
        
-       jack_nframes_t file_frame_tmp = 0;
+       nframes_t file_frame_tmp = 0;
 
        for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
 
                ChannelInfo& chan (*i);
                Sample* buf1;
                Sample* buf2;
-               jack_nframes_t len1, len2;
+               nframes_t len1, len2;
 
                chan.playback_buf->get_write_vector (&vector);
 
@@ -1329,7 +1323,7 @@ AudioDiskstream::do_flush (Session::RunContext context, bool force_flush)
        int32_t ret = 0;
        RingBufferNPT<Sample>::rw_vector vector;
        RingBufferNPT<CaptureTransition>::rw_vector transvec;
-       jack_nframes_t total;
+       nframes_t total;
 
        _write_data_count = 0;
 
@@ -1359,7 +1353,7 @@ AudioDiskstream::do_flush (Session::RunContext context, bool force_flush)
                        ret = 1;
                } 
 
-               to_write = min (disk_io_chunk_frames, (jack_nframes_t) vector.len[0]);
+               to_write = min (disk_io_chunk_frames, (nframes_t) vector.len[0]);
                
                // check the transition buffer when recording destructive
                // important that we get this after the capture buf
@@ -1429,7 +1423,7 @@ AudioDiskstream::do_flush (Session::RunContext context, bool force_flush)
                           of vector.len[1] to be flushed to disk as well.
                        */
                
-                       to_write = min ((jack_nframes_t)(disk_io_chunk_frames - to_write), (jack_nframes_t) vector.len[1]);
+                       to_write = min ((nframes_t)(disk_io_chunk_frames - to_write), (nframes_t) vector.len[1]);
                
                        if ((*chan).write_source->write (vector.buf[1], to_write) != to_write) {
                                error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
@@ -1453,10 +1447,10 @@ AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_ca
        uint32_t buffer_position;
        bool more_work = true;
        int err = 0;
-       AudioRegion* region = 0;
-       jack_nframes_t total_capture;
-       AudioRegion::SourceList srcs;
-       AudioRegion::SourceList::iterator src;
+       boost::shared_ptr<AudioRegion> region;
+       nframes_t total_capture;
+       SourceList srcs;
+       SourceList::iterator src;
        ChannelList::iterator chan;
        vector<CaptureInfo*>::iterator ci;
        uint32_t n = 0; 
@@ -1492,18 +1486,17 @@ AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_ca
                
                ChannelList::iterator chan;
                
-               list<Source*>* deletion_list = new list<Source*>;
+               list<boost::shared_ptr<Source> >* deletion_list = new list<boost::shared_ptr<Source> >;
 
                for ( chan = channels.begin(); chan != channels.end(); ++chan) {
 
                        if ((*chan).write_source) {
                                
                                (*chan).write_source->mark_for_remove ();
-                               (*chan).write_source->release ();
                                
                                deletion_list->push_back ((*chan).write_source);
 
-                               (*chan).write_source = 0;
+                               (*chan).write_source.reset ();
                        }
                        
                        /* new source set up in "out" below */
@@ -1526,19 +1519,11 @@ AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_ca
 
        for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
 
-               AudioFileSource* s = (*chan).write_source;
+               boost::shared_ptr<AudioFileSource> s = (*chan).write_source;
                
                if (s) {
-
-                       AudioFileSource* fsrc;
-
                        srcs.push_back (s);
-
-                       if ((fsrc = dynamic_cast<AudioFileSource *>(s)) != 0) {
-                               cerr << "updating source after capture\n";
-                               fsrc->update_header (capture_info.front()->start, when, twhen);
-                       }
-
+                       s->update_header (capture_info.front()->start, when, twhen);
                        s->set_captured_for (_name);
                        
                }
@@ -1565,10 +1550,11 @@ AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_ca
                */
                
                try {
-                       region = new AudioRegion (srcs, channels[0].write_source->last_capture_start_frame(), total_capture, 
-                                                 region_name_from_path (channels[0].write_source->name()), 
-                                                 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile));
-                       
+                       boost::shared_ptr<Region> rx (RegionFactory::create (srcs, channels[0].write_source->last_capture_start_frame(), total_capture, 
+                                                                            region_name_from_path (channels[0].write_source->name()), 
+                                                                            0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile)));
+
+                       region = boost::dynamic_pointer_cast<AudioRegion> (rx);
                        region->special_set_position (capture_info.front()->start);
                }
                
@@ -1590,10 +1576,11 @@ AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_ca
                        string region_name;
                        _session.region_name (region_name, channels[0].write_source->name(), false);
                        
-                       // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add a region\n";
+                       // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add region " << region_name << endl;
                        
                        try {
-                               region = new AudioRegion (srcs, buffer_position, (*ci)->frames, region_name);
+                               boost::shared_ptr<Region> rx (RegionFactory::create (srcs, buffer_position, (*ci)->frames, region_name));
+                               region = boost::dynamic_pointer_cast<AudioRegion> (rx);
                        }
                        
                        catch (failed_constructor& err) {
@@ -1606,7 +1593,7 @@ AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_ca
                        // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
                        
                        i_am_the_modifier++;
-                       _playlist->add_region (*region, (*ci)->start);
+                       _playlist->add_region (region, (*ci)->start);
                        i_am_the_modifier--;
                        
                        buffer_position += (*ci)->frames;
@@ -1614,7 +1601,7 @@ AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_ca
 
                _playlist->thaw ();
                 XMLNode &after = _playlist->get_state();
-               _session.add_command (new MementoCommand<Playlist>(*_playlist, before, after));
+               _session.add_command (new MementoCommand<Playlist>(*_playlist, &before, &after));
        }
 
        mark_write_completed = true;
@@ -1721,10 +1708,10 @@ AudioDiskstream::engage_record_enable ()
 
        g_atomic_int_set (&_record_enabled, 1);
        capturing_sources.clear ();
-       if (Config->get_use_hardware_monitoring())  {
+       if (Config->get_monitoring_model() == HardwareMonitoring) {
                for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
                        if ((*chan).source) {
-                               (*chan).source->request_monitor_input (!(_session.get_auto_input() && rolling));
+                               (*chan).source->ensure_monitor_input (!(Config->get_auto_input() && rolling));
                        }
                        capturing_sources.push_back ((*chan).write_source);
                }
@@ -1741,10 +1728,10 @@ void
 AudioDiskstream::disengage_record_enable ()
 {
        g_atomic_int_set (&_record_enabled, 0);
-       if (Config->get_use_hardware_monitoring()) {
+       if (Config->get_monitoring_model() == HardwareMonitoring) {
                for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
                        if ((*chan).source) {
-                               (*chan).source->request_monitor_input (false);
+                               (*chan).source->ensure_monitor_input (false);
                        }
                }
        }
@@ -1780,7 +1767,7 @@ AudioDiskstream::get_state ()
                XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
                XMLNode* cs_grandchild;
 
-               for (vector<AudioFileSource*>::iterator i = capturing_sources.begin(); i != capturing_sources.end(); ++i) {
+               for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = capturing_sources.begin(); i != capturing_sources.end(); ++i) {
                        cs_grandchild = new XMLNode (X_("file"));
                        cs_grandchild->add_property (X_("path"), (*i)->path());
                        cs_child->add_child_nocopy (*cs_grandchild);
@@ -1790,7 +1777,7 @@ AudioDiskstream::get_state ()
 
                Location* pi;
 
-               if (_session.get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
+               if (Config->get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
                        snprintf (buf, sizeof (buf), "%" PRIu32, pi->start());
                } else {
                        snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
@@ -1947,13 +1934,11 @@ AudioDiskstream::use_new_write_source (uint32_t n)
        
        if (chan.write_source) {
 
-               if (AudioFileSource::is_empty (chan.write_source->path())) {
+               if (AudioFileSource::is_empty (_session, chan.write_source->path())) {
                        chan.write_source->mark_for_remove ();
-                       chan.write_source->release();
-                       delete chan.write_source;
+                       chan.write_source.reset ();
                } else {
-                       chan.write_source->release();
-                       chan.write_source = 0;
+                       chan.write_source.reset ();
                }
        }
 
@@ -1965,12 +1950,10 @@ AudioDiskstream::use_new_write_source (uint32_t n)
 
        catch (failed_constructor &err) {
                error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
-               chan.write_source = 0;
+               chan.write_source.reset ();
                return -1;
        }
 
-       chan.write_source->use ();
-
        /* do not remove destructive files even if they are empty */
 
        chan.write_source->set_allow_remove_if_empty (!destructive());
@@ -1984,6 +1967,8 @@ AudioDiskstream::reset_write_sources (bool mark_write_complete, bool force)
        ChannelList::iterator chan;
        uint32_t n;
 
+       cerr << _name << " RWS\n";
+
        if (!recordable()) {
                return;
        }
@@ -2038,7 +2023,7 @@ AudioDiskstream::rename_write_sources ()
 }
 
 void
-AudioDiskstream::set_block_size (jack_nframes_t nframes)
+AudioDiskstream::set_block_size (nframes_t nframes)
 {
        if (_session.get_block_size() > speed_buffer_size) {
                speed_buffer_size = _session.get_block_size();
@@ -2060,7 +2045,7 @@ AudioDiskstream::allocate_temporary_buffers ()
        */
 
        double sp = max (fabsf (_actual_speed), 1.2f);
-       jack_nframes_t required_wrap_size = (jack_nframes_t) floor (_session.get_block_size() * sp) + 1;
+       nframes_t required_wrap_size = (nframes_t) floor (_session.get_block_size() * sp) + 1;
 
        if (required_wrap_size > wrap_buffer_size) {
 
@@ -2081,7 +2066,7 @@ AudioDiskstream::monitor_input (bool yn)
        for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
                
                if ((*chan).source) {
-                       (*chan).source->request_monitor_input (yn);
+                       (*chan).source->ensure_monitor_input (yn);
                }
        }
 }
@@ -2167,10 +2152,10 @@ AudioDiskstream::use_pending_capture_data (XMLNode& node)
        const XMLProperty* prop;
        XMLNodeList nlist = node.children();
        XMLNodeIterator niter;
-       AudioFileSource* fs;
-       AudioFileSource* first_fs = 0;
-       AudioRegion::SourceList pending_sources;
-       jack_nframes_t position;
+       boost::shared_ptr<AudioFileSource> fs;
+       boost::shared_ptr<AudioFileSource> first_fs;
+       SourceList pending_sources;
+       nframes_t position;
 
        if ((prop = node.property (X_("at"))) == 0) {
                return -1;
@@ -2188,10 +2173,7 @@ AudioDiskstream::use_pending_capture_data (XMLNode& node)
                        }
 
                        try {
-                               fs = new SndFileSource (prop->value(), 
-                                                       Config->get_native_file_data_format(),
-                                                       Config->get_native_file_header_format(),
-                                                       _session.frame_rate());
+                               fs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (_session, prop->value(), false, _session.frame_rate()));
                        }
 
                        catch (failed_constructor& err) {
@@ -2222,13 +2204,12 @@ AudioDiskstream::use_pending_capture_data (XMLNode& node)
                return -1;
        }
 
-       AudioRegion* region;
+       boost::shared_ptr<AudioRegion> region;
        
        try {
-               region = new AudioRegion (pending_sources, 0, first_fs->length(),
-                                         region_name_from_path (first_fs->name()), 
-                                         0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile));
-               
+               region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, 0, first_fs->length(),
+                                                                                         region_name_from_path (first_fs->name()), 
+                                                                                         0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile)));
                region->special_set_position (0);
        }
 
@@ -2241,7 +2222,7 @@ AudioDiskstream::use_pending_capture_data (XMLNode& node)
        }
 
        try {
-               region = new AudioRegion (pending_sources, 0, first_fs->length(), region_name_from_path (first_fs->name()));
+               region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, 0, first_fs->length(), region_name_from_path (first_fs->name())));
        }
 
        catch (failed_constructor& err) {
@@ -2252,7 +2233,7 @@ AudioDiskstream::use_pending_capture_data (XMLNode& node)
                return -1;
        }
 
-       _playlist->add_region (*region, position);
+       _playlist->add_region (region, position);
 
        return 0;
 }