Use ISC license for RDFF (same idea, MIT style, just prettier).
[ardour.git] / libs / ardour / export_channel.cc
index e0242a9f527eb1b337fecf7bdde7d6a0b652c911..936b0f8a625f148230fe2c5ac7143186fd79e852 100644 (file)
 
 */
 
-#include <ardour/export_channel.h>
+#include "ardour/audio_buffer.h"
+#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 <ardour/export_failed.h>
-#include <ardour/audioengine.h>
+#include "pbd/error.h"
+
+#include "i18n.h"
 
 using namespace ARDOUR;
 
+PortExportChannel::PortExportChannel ()
+       : buffer_size(0)
+{
+}
+
+void PortExportChannel::set_max_buffer_size(framecnt_t frames)
+{
+       buffer_size = frames;
+       buffer.reset (new Sample[frames]);
+}
+
 bool
 PortExportChannel::operator< (ExportChannel const & other) const
 {
@@ -36,19 +56,29 @@ PortExportChannel::operator< (ExportChannel const & other) const
 }
 
 void
-PortExportChannel::read (Sample * data, nframes_t frames) const
+PortExportChannel::read (Sample const *& data, framecnt_t frames) const
 {
-       memset (data, 0, frames * sizeof (float));
+       assert(buffer);
+       assert(frames <= buffer_size);
+
+       if (ports.size() == 1) {
+               data = (*ports.begin())->get_audio_buffer(frames).data();
+               return;
+       }
+       
+       memset (buffer.get(), 0, frames * sizeof (Sample));
 
        for (PortSet::const_iterator it = ports.begin(); it != ports.end(); ++it) {
                if (*it != 0) {
-                       Sample* port_buffer = (*it)->get_audio_buffer(frames, 0).data();
-                       
+                       Sample* port_buffer = (*it)->get_audio_buffer(frames).data();
+
                        for (uint32_t i = 0; i < frames; ++i) {
-                               data[i] += (float) port_buffer[i];
+                               buffer[i] += (float) port_buffer[i];
                        }
                }
        }
+
+       data = buffer.get();
 }
 
 void
@@ -69,7 +99,13 @@ PortExportChannel::set_state (XMLNode * node, Session & session)
        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<AudioPort *> (session.engine().get_port_by_name (prop->value())));
+                       std::string const & name = prop->value();
+                       AudioPort * port = dynamic_cast<AudioPort *> (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;
+                       }
                }
        }
 }
@@ -86,6 +122,14 @@ RegionExportChannelFactory::RegionExportChannelFactory (Session * session, Audio
        switch (type) {
          case Raw:
                n_channels = region.n_channels();
+               break;
+         case Fades:
+               n_channels = region.n_channels();
+
+               mixdown_buffer.reset (new Sample [frames_per_cycle]);
+               gain_buffer.reset (new Sample [frames_per_cycle]);
+               memset (gain_buffer.get(), 1.0, sizeof (Sample) * frames_per_cycle);
+
                break;
          case Processed:
                n_channels = track.n_outputs().n_audio();
@@ -93,11 +137,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)));
-       
-       buffers.set_count (ChanCount (DataType::AUDIO, n_channels));
+
+       session->ProcessExport.connect_same_thread (export_connection, boost::bind (&RegionExportChannelFactory::new_cycle_started, this, _1));
+
        buffers.ensure_buffers (DataType::AUDIO, n_channels, frames_per_cycle);
+       buffers.set_count (ChanCount (DataType::AUDIO, n_channels));
+}
+
+RegionExportChannelFactory::~RegionExportChannelFactory ()
+{
 }
 
 ExportChannelPtr
@@ -108,35 +156,117 @@ 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, framecnt_t frames_to_read)
 {
        assert (channel < n_channels);
        assert (frames_to_read <= frames_per_cycle);
-       
+
        if (!buffers_up_to_date) {
                update_buffers(frames_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 (framecnt_t frames)
 {
+       assert (frames <= frames_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);
                }
                break;
+         case Fades:
+               assert (mixdown_buffer && gain_buffer);
+               for (size_t channel = 0; channel < n_channels; ++channel) {
+                       memset (mixdown_buffer.get(), 0, sizeof (Sample) * frames);
+                       region.read_at (buffers.get_audio (channel).data(), mixdown_buffer.get(), gain_buffer.get(), position, frames, channel);
+               }
+               break;
          case Processed:
-               std::cout << "exporting " << frames << " frames from position " << position << std::endl;
                track.export_stuff (buffers, position, frames);
                break;
          default:
                throw ExportFailed ("Unhandled type in ExportChannelFactory::update_buffers");
        }
-       
+
        position += frames;
 }
+
+
+RouteExportChannel::RouteExportChannel(boost::shared_ptr<CapturingProcessor> processor, size_t channel,
+                                       boost::shared_ptr<ProcessorRemover> remover)
+  : processor (processor)
+  , channel (channel)
+  , remover (remover)
+{
+}
+
+RouteExportChannel::~RouteExportChannel()
+{
+}
+
+void
+RouteExportChannel::create_from_route(std::list<ExportChannelPtr> & result, Route & route)
+{
+       boost::shared_ptr<CapturingProcessor> processor = route.add_export_point();
+       uint32_t channels = processor->input_streams().n_audio();
+
+       boost::shared_ptr<ProcessorRemover> 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(framecnt_t frames)
+{
+       if (processor) {
+               processor->set_block_size (frames);
+       }
+}
+
+void
+RouteExportChannel::read (Sample const *& data, framecnt_t frames) const
+{
+       assert(processor);
+       AudioBuffer const & buffer = processor->get_capture_buffers().get_audio (channel);
+       assert (frames <= (framecnt_t) buffer.size());
+       data = buffer.data();
+}
+
+void
+RouteExportChannel::get_state (XMLNode * node) const
+{
+       // TODO
+}
+
+void
+RouteExportChannel::set_state (XMLNode * node, Session & session)
+{
+       // TODO
+}
+
+bool
+RouteExportChannel::operator< (ExportChannel const & other) const
+{
+       RouteExportChannel const * rec;
+       if ((rec = dynamic_cast<RouteExportChannel const *>(&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);
+}