X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Fexport_channel.cc;h=66fec3a53b4127057070b83d41f4e2e954ec8deb;hb=baa00942a20856cf332f547086ed5ebd2ff9078e;hp=58110ea5a95b7dc1bea3c67fe4c781ea5bb9a5d8;hpb=e0aaed6d65f160c328cb8b56d7c6552ee15d65e2;p=ardour.git diff --git a/libs/ardour/export_channel.cc b/libs/ardour/export_channel.cc index 58110ea5a9..66fec3a53b 100644 --- a/libs/ardour/export_channel.cc +++ b/libs/ardour/export_channel.cc @@ -22,12 +22,48 @@ #include "ardour/audio_port.h" #include "ardour/audio_track.h" #include "ardour/audioengine.h" +#include "ardour/audioregion.h" +#include "ardour/capturing_processor.h" #include "ardour/export_channel.h" #include "ardour/export_failed.h" #include "ardour/session.h" +#include "pbd/error.h" + +#include "pbd/i18n.h" + using namespace ARDOUR; +PortExportChannel::PortExportChannel () + : _buffer_size (0) +{ +} + +PortExportChannel::~PortExportChannel () +{ + _delaylines.clear (); +} + +void PortExportChannel::set_max_buffer_size(samplecnt_t samples) +{ + _buffer_size = samples; + _buffer.reset (new Sample[samples]); + + _delaylines.clear (); + + for (PortSet::const_iterator it = ports.begin(); it != ports.end(); ++it) { + boost::shared_ptr p = it->lock (); + if (!p) { continue; } + samplecnt_t latency = p->private_latency_range (true).max; + PBD::RingBuffer* rb = new PBD::RingBuffer (latency + 1 + _buffer_size); + for (samplepos_t i = 0; i < latency; ++i) { + Sample zero = 0; + rb->write (&zero, 1); + } + _delaylines.push_back (boost::shared_ptr >(rb)); + } +} + bool PortExportChannel::operator< (ExportChannel const & other) const { @@ -39,19 +75,41 @@ PortExportChannel::operator< (ExportChannel const & other) const } void -PortExportChannel::read (Sample * data, nframes_t frames) const +PortExportChannel::read (Sample const *& data, samplecnt_t samples) const { - memset (data, 0, frames * sizeof (float)); + assert(_buffer); + assert(samples <= _buffer_size); + + if (ports.size() == 1 && _delaylines.size() ==1 && _delaylines.front()->bufsize () == _buffer_size + 1) { + boost::shared_ptr p = ports.begin()->lock (); + AudioBuffer& ab (p->get_audio_buffer(samples)); // unsets AudioBuffer::_written + data = ab.data(); + ab.set_written (true); + return; + } + + memset (_buffer.get(), 0, samples * sizeof (Sample)); + std::list > >::const_iterator di = _delaylines.begin (); for (PortSet::const_iterator it = ports.begin(); it != ports.end(); ++it) { - if (*it != 0) { - Sample* port_buffer = (*it)->get_audio_buffer(frames, 0).data(); - - for (uint32_t i = 0; i < frames; ++i) { - data[i] += (float) port_buffer[i]; - } + boost::shared_ptr p = it->lock (); + if (!p) { + continue; + } + AudioBuffer& ab (p->get_audio_buffer(samples)); // unsets AudioBuffer::_written + Sample* port_buffer = ab.data(); + ab.set_written (true); + (*di)->write (port_buffer, samples); + // TODO optimze, get_read_vector() + for (uint32_t i = 0; i < samples; ++i) { + Sample spl; + (*di)->read (&spl, 1); + _buffer[i] += spl; } + ++di; } + + data = _buffer.get(); } void @@ -59,8 +117,9 @@ PortExportChannel::get_state (XMLNode * node) const { XMLNode * port_node; for (PortSet::const_iterator it = ports.begin(); it != ports.end(); ++it) { - if ((port_node = node->add_child ("Port"))) { - port_node->add_property ("name", (*it)->name()); + boost::shared_ptr p = it->lock (); + if (p && (port_node = node->add_child ("Port"))) { + port_node->set_property ("name", p->name()); } } } @@ -68,26 +127,28 @@ PortExportChannel::get_state (XMLNode * node) const void PortExportChannel::set_state (XMLNode * node, Session & session) { - XMLProperty * prop; XMLNodeList xml_ports = node->children ("Port"); for (XMLNodeList::iterator it = xml_ports.begin(); it != xml_ports.end(); ++it) { - if ((prop = (*it)->property ("name"))) { - ports.insert (dynamic_cast (session.engine().get_port_by_name (prop->value()))); + std::string name; + if ((*it)->get_property ("name", name)) { + boost::shared_ptr port = boost::dynamic_pointer_cast (session.engine().get_port_by_name (name)); + if (port) { + ports.insert (port); + } else { + PBD::warning << string_compose (_("Could not get port for export channel \"%1\", dropping the channel"), name) << endmsg; + } } } } -RegionExportChannelFactory::RegionExportChannelFactory (Session * session, AudioRegion const & region, AudioTrack & track, Type type) : - region (region), - track (track), - type (type), - frames_per_cycle (session->engine().frames_per_cycle ()), - buffers_up_to_date (false), - region_start (region.position()), - position (region_start), - - mixdown_buffer (0), - gain_buffer (0) +RegionExportChannelFactory::RegionExportChannelFactory (Session * session, AudioRegion const & region, AudioTrack & track, Type type) + : region (region) + , track (track) + , type (type) + , samples_per_cycle (session->engine().samples_per_cycle ()) + , buffers_up_to_date (false) + , region_start (region.position()) + , position (region_start) { switch (type) { case Raw: @@ -95,11 +156,11 @@ RegionExportChannelFactory::RegionExportChannelFactory (Session * session, Audio break; case Fades: n_channels = region.n_channels(); - - mixdown_buffer = new Sample [frames_per_cycle]; - gain_buffer = new Sample [frames_per_cycle]; - memset (gain_buffer, 1.0, sizeof (Sample) * frames_per_cycle); - + + mixdown_buffer.reset (new Sample [samples_per_cycle]); + gain_buffer.reset (new Sample [samples_per_cycle]); + std::fill_n (gain_buffer.get(), samples_per_cycle, Sample (1.0)); + break; case Processed: n_channels = track.n_outputs().n_audio(); @@ -107,17 +168,15 @@ RegionExportChannelFactory::RegionExportChannelFactory (Session * session, Audio default: throw ExportFailed ("Unhandled type in ExportChannelFactory constructor"); } - - session->ProcessExport.connect (sigc::hide (sigc::mem_fun (*this, &RegionExportChannelFactory::new_cycle_started))); - + + session->ProcessExport.connect_same_thread (export_connection, boost::bind (&RegionExportChannelFactory::new_cycle_started, this, _1)); + + buffers.ensure_buffers (DataType::AUDIO, n_channels, samples_per_cycle); buffers.set_count (ChanCount (DataType::AUDIO, n_channels)); - buffers.ensure_buffers (DataType::AUDIO, n_channels, frames_per_cycle); } RegionExportChannelFactory::~RegionExportChannelFactory () { - delete[] mixdown_buffer; - delete[] gain_buffer; } ExportChannelPtr @@ -128,43 +187,122 @@ RegionExportChannelFactory::create (uint32_t channel) } void -RegionExportChannelFactory::read (uint32_t channel, Sample * data, nframes_t frames_to_read) +RegionExportChannelFactory::read (uint32_t channel, Sample const *& data, samplecnt_t samples_to_read) { assert (channel < n_channels); - assert (frames_to_read <= frames_per_cycle); - + assert (samples_to_read <= samples_per_cycle); + if (!buffers_up_to_date) { - update_buffers(frames_to_read); + update_buffers(samples_to_read); buffers_up_to_date = true; } - - memcpy (data, buffers.get_audio (channel).data(), frames_to_read * sizeof (Sample)); + + data = buffers.get_audio (channel).data(); } void -RegionExportChannelFactory::update_buffers (nframes_t frames) +RegionExportChannelFactory::update_buffers (samplecnt_t samples) { - assert (frames <= frames_per_cycle); + assert (samples <= samples_per_cycle); switch (type) { case Raw: for (size_t channel = 0; channel < n_channels; ++channel) { - region.read (buffers.get_audio (channel).data(), position - region_start, frames, channel); + region.read (buffers.get_audio (channel).data(), position - region_start, samples, channel); } break; case Fades: assert (mixdown_buffer && gain_buffer); for (size_t channel = 0; channel < n_channels; ++channel) { - memset (mixdown_buffer, 0, sizeof (Sample) * frames); - region.read_at (buffers.get_audio (channel).data(), mixdown_buffer, gain_buffer, position, frames, channel); + memset (mixdown_buffer.get(), 0, sizeof (Sample) * samples); + buffers.get_audio (channel).silence(samples); + region.read_at (buffers.get_audio (channel).data(), mixdown_buffer.get(), gain_buffer.get(), position, samples, channel); } break; - case Processed: - track.export_stuff (buffers, position, frames); + case Processed: + track.export_stuff (buffers, position, samples, track.main_outs(), true, true, false); break; - default: + default: throw ExportFailed ("Unhandled type in ExportChannelFactory::update_buffers"); } - - position += frames; + + position += samples; +} + + +RouteExportChannel::RouteExportChannel(boost::shared_ptr processor, size_t channel, + boost::shared_ptr remover) + : processor (processor) + , channel (channel) + , remover (remover) +{ +} + +RouteExportChannel::~RouteExportChannel() +{ +} + +void +RouteExportChannel::create_from_route(std::list & result, boost::shared_ptr route) +{ + boost::shared_ptr processor = route->add_export_point(); + uint32_t channels = processor->input_streams().n_audio(); + + boost::shared_ptr remover (new ProcessorRemover (route, processor)); + result.clear(); + for (uint32_t i = 0; i < channels; ++i) { + result.push_back (ExportChannelPtr (new RouteExportChannel (processor, i, remover))); + } +} + +void +RouteExportChannel::set_max_buffer_size(samplecnt_t samples) +{ + if (processor) { + processor->set_block_size (samples); + } +} + +void +RouteExportChannel::read (Sample const *& data, samplecnt_t samples) const +{ + assert(processor); + AudioBuffer const & buffer = processor->get_capture_buffers().get_audio (channel); +#ifndef NDEBUG + (void) samples; +#else + assert (samples <= (samplecnt_t) buffer.capacity()); +#endif + data = buffer.data(); +} + +void +RouteExportChannel::get_state (XMLNode *) const +{ + // TODO +} + +void +RouteExportChannel::set_state (XMLNode *, Session &) +{ + // TODO +} + +bool +RouteExportChannel::operator< (ExportChannel const & other) const +{ + RouteExportChannel const * rec; + if ((rec = dynamic_cast(&other)) == 0) { + return this < &other; + } + + if (processor.get() == rec->processor.get()) { + return channel < rec->channel; + } + return processor.get() < rec->processor.get(); +} + +RouteExportChannel::ProcessorRemover::~ProcessorRemover() +{ + route->remove_processor (processor); }