replace fixed-point linear interpolation with double-based version, thereby removing...
[ardour.git] / libs / ardour / audioengine.cc
index 095fb205c1b15144fe0b0a65fc8c71e64bb57081..7533a1ed8f71ce9ad9f500363655b76d772b626c 100644 (file)
 #include <sstream>
 
 #include <glibmm/timer.h>
-#include <pbd/pthread_utils.h>
-#include <pbd/stacktrace.h>
-#include <pbd/unknown_type.h>
-
-#include <midi++/jack.h>
-
-#include <ardour/audioengine.h>
-#include <ardour/buffer.h>
-#include <ardour/port.h>
-#include <ardour/audio_port.h>
-#include <ardour/midi_port.h>
-#include <ardour/session.h>
-#include <ardour/cycle_timer.h>
-#include <ardour/utils.h>
-#include <ardour/event_type_map.h>
-#include <ardour/io.h>
-#ifdef VST_SUPPORT
-#include <fst.h>
-#endif
-
-#include <ardour/timestamps.h>
+#include "pbd/pthread_utils.h"
+#include "pbd/stacktrace.h"
+#include "pbd/unknown_type.h"
+
+#include "midi++/jack.h"
+
+#include "ardour/amp.h"
+#include "ardour/audio_port.h"
+#include "ardour/audioengine.h"
+#include "ardour/buffer.h"
+#include "ardour/buffer_set.h"
+#include "ardour/cycle_timer.h"
+#include "ardour/delivery.h"
+#include "ardour/event_type_map.h"
+#include "ardour/internal_return.h"
+#include "ardour/io.h"
+#include "ardour/meter.h"
+#include "ardour/midi_port.h"
+#include "ardour/port.h"
+#include "ardour/port_set.h"
+#include "ardour/session.h"
+#include "ardour/timestamps.h"
+#include "ardour/utils.h"
 
 #include "i18n.h"
 
@@ -55,12 +57,6 @@ using namespace PBD;
 
 gint AudioEngine::m_meter_exit;
 
-static void 
-ardour_jack_error (const char* msg) 
-{
-       error << "JACK: " << msg << endmsg;
-}
-
 AudioEngine::AudioEngine (string client_name) 
        : ports (new Ports)
 {
@@ -149,14 +145,14 @@ AudioEngine::start ()
 
        if (!_running) {
 
+               nframes_t blocksize = jack_get_buffer_size (_jack);
+               
                if (session) {
-                       nframes_t blocksize = jack_get_buffer_size (_jack);
-
                        BootMessage (_("Connect session to engine"));
 
                        session->set_block_size (blocksize);
                        session->set_frame_rate (jack_get_sample_rate (_jack));
-
+                       
                        /* page in as much of the session process code as we
                           can before we really start running.
                        */
@@ -184,7 +180,7 @@ AudioEngine::start ()
                jack_set_sync_callback (_jack, _jack_sync_callback, this);
                jack_set_freewheel_callback (_jack, _freewheel_callback, this);
 
-               if (Config->get_jack_time_master()) {
+               if (session && session->config.get_jack_time_master()) {
                        jack_set_timebase_callback (_jack, 0, _jack_timebase_callback, this);
                }
 
@@ -197,6 +193,8 @@ AudioEngine::start ()
                }
 
                start_metering_thread();
+
+               _raw_buffer_sizes[DataType::AUDIO] = blocksize * sizeof(float);
        }
 
        return _running ? 0 : -1;
@@ -309,6 +307,22 @@ AudioEngine::_freewheel_callback (int onoff, void *arg)
        static_cast<AudioEngine*>(arg)->_freewheeling = onoff;
 }
 
+void
+AudioEngine::split_cycle (nframes_t offset)
+{
+       /* caller must hold process lock */
+
+       Port::increment_port_offset (offset);
+
+       /* tell all Ports that we're going to start a new (split) cycle */
+
+       boost::shared_ptr<Ports> p = ports.reader();
+
+       for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
+               (*i)->cycle_split ();
+       }
+}
+
 /** Method called by JACK (via _process_callback) which says that there
  * is work to be done.
  * @param nframes Number of frames to process.
@@ -317,6 +331,7 @@ int
 AudioEngine::process_callback (nframes_t nframes)
 {
        // CycleTimer ct ("AudioEngine::process");
+
        Glib::Mutex::Lock tm (_process_lock, Glib::TRY_LOCK);
 
        /// The number of frames that will have been processed when we've finished
@@ -345,17 +360,18 @@ AudioEngine::process_callback (nframes_t nframes)
                return 0;
        }
 
-       boost::shared_ptr<Ports> p = ports.reader();
+       /* tell all relevant objects that we're starting a new cycle */
 
-       for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
+       Delivery::CycleStart (nframes);
+       Port::set_port_offset (0);
+       InternalReturn::CycleStart (nframes);
 
-               /* Only run cycle_start() on output ports, because 
-                  inputs must be done in the correct processing order,
-                  which requires interleaving with route processing.
-               */
+       /* tell all Ports that we're starting a new cycle */
 
-               /* XXX: we're running this on both inputs and outputs... */
-               (*i)->cycle_start (nframes, 0);
+       boost::shared_ptr<Ports> p = ports.reader();
+
+       for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
+               (*i)->cycle_start (nframes);
        }
 
        if (_freewheeling) {
@@ -408,16 +424,15 @@ AudioEngine::process_callback (nframes_t nframes)
                        Port *port = (*i);
                        
                        if (port->sends_output()) {
-                               port->get_buffer(nframes, 0 ).silence(nframes);
+                               port->get_buffer(nframes).silence(nframes);
                        }
                }
-       }
+       } 
 
-       // Finalize ports (ie write data if necessary)
+       // Finalize ports
 
        for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
-
-               (*i)->cycle_end (nframes, 0);
+               (*i)->cycle_end (nframes);
        }
 
        _processed_frames = next_processed_frames;
@@ -460,6 +475,9 @@ int
 AudioEngine::jack_bufsize_callback (nframes_t nframes)
 {
        _buffer_size = nframes;
+       _raw_buffer_sizes[DataType::AUDIO] = nframes * sizeof(float);
+       cout << "FIXME: Assuming maximum MIDI buffer size " << nframes * 4 << "bytes" << endl;
+       _raw_buffer_sizes[DataType::MIDI] = nframes * 4;
        _usecs_per_cycle = (int) floor ((((double) nframes / frame_rate())) * 1000000.0);
        last_monitor_check = 0;
 
@@ -504,7 +522,7 @@ AudioEngine::meter_thread ()
                if (g_atomic_int_get(&m_meter_exit)) {
                        break;
                }
-               IO::update_meters ();
+               Metering::update_meters ();
        }
 }
 
@@ -526,7 +544,7 @@ AudioEngine::set_session (Session *s)
                boost::shared_ptr<Ports> p = ports.reader();
 
                for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
-                       (*i)->cycle_start (blocksize, 0);
+                       (*i)->cycle_start (blocksize);
                }
 
                s->process (blocksize);
@@ -539,7 +557,7 @@ AudioEngine::set_session (Session *s)
                s->process (blocksize);
 
                for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
-                       (*i)->cycle_end (blocksize, 0);
+                       (*i)->cycle_end (blocksize);
                }
        }
 }
@@ -576,58 +594,63 @@ AudioEngine::port_registration_failure (const std::string& portname)
        string reason;
        
        if (p) {
-               reason = _("a port with this name already exists: check for duplicated track/bus names");
+               reason = string_compose (_("a port with the name \"%1\" already exists: check for duplicated track/bus names"), portname);
        } else {
-               reason = _("unknown error");
+               reason = _("No more JACK ports are available. You will need to stop Ardour and restart JACK with ports if you need this many tracks.");
        }
        
        throw PortRegistrationFailure (string_compose (_("AudioEngine: cannot register port \"%1\": %2"), portname, reason).c_str());
 }      
 
 Port *
-AudioEngine::register_port (DataType dtype, const string& portname, bool input, bool publish)
+AudioEngine::register_port (DataType dtype, const string& portname, bool input)
 {
        Port* newport = 0;
 
-       /*cerr << "trying to register port with name " << portname << endl;*/
        try {
                if (dtype == DataType::AUDIO) {
-                       newport = new AudioPort (portname, (input ? Port::IsInput : Port::IsOutput), publish, frames_per_cycle());
+                       newport = new AudioPort (portname, (input ? Port::IsInput : Port::IsOutput));
                } else if (dtype == DataType::MIDI) {
-                       newport = new MidiPort (portname, (input ? Port::IsInput : Port::IsOutput), publish, frames_per_cycle());
+                       newport = new MidiPort (portname, (input ? Port::IsInput : Port::IsOutput));
                } else {
-                       throw unknown_type();
+                       throw PortRegistrationFailure("unable to create port (unknown type)");
+               }
+               
+               size_t& old_buffer_size  = _raw_buffer_sizes[newport->type()];
+               size_t  port_buffer_size = newport->raw_buffer_size(0);
+               if (port_buffer_size > old_buffer_size) {
+                       old_buffer_size = port_buffer_size;
                }
-
-               /*cerr << "successfully got port " << portname << " with address " << newport << endl;*/
 
                RCUWriter<Ports> writer (ports);
                boost::shared_ptr<Ports> ps = writer.get_copy ();
-               /*cerr << "Address of ports list: " << ps << endl
-                    << "Ports set size before insert: " << ps->size() << endl;*/
                ps->insert (ps->begin(), newport);
-               /*cerr << "Ports set size after insert: " << ps->size() << endl;*/
 
                /* writer goes out of scope, forces update */
 
                return newport;
        }
 
-       catch (...) {
-               throw PortRegistrationFailure("unable to create port (unknown type?)");
+       catch (PortRegistrationFailure& err) {
+               throw err;
+       } catch (std::exception& e) {
+               throw PortRegistrationFailure(string_compose(
+                               _("unable to create port: %1"), e.what()).c_str());
+       } catch (...) {
+               throw PortRegistrationFailure("unable to create port (unknown error)");
        }
 }
 
 Port *
-AudioEngine::register_input_port (DataType type, const string& portname, bool publish)
+AudioEngine::register_input_port (DataType type, const string& portname)
 {
-       return register_port (type, portname, true, publish);
+       return register_port (type, portname, true);
 }
 
 Port *
-AudioEngine::register_output_port (DataType type, const string& portname, bool publish)
+AudioEngine::register_output_port (DataType type, const string& portname)
 {
-       return register_port (type, portname, false, publish);
+       return register_port (type, portname, false);
 }
 
 int
@@ -635,31 +658,21 @@ AudioEngine::unregister_port (Port& port)
 {
        /* caller must hold process lock */
 
-       cerr << "about to unregister Port xx  x" << &port << "\n";
-
        if (!_running) { 
                /* probably happening when the engine has been halted by JACK,
                   in which case, there is nothing we can do here.
                   */
-               cerr << "not running\n";
                return 0;
        }
 
        {
-               cerr << "before getcopy\n";
-               
                RCUWriter<Ports> writer (ports);
                boost::shared_ptr<Ports> ps = writer.get_copy ();
                
-               cerr << "Ports set size: " << ps.get()->size() << endl;
-
                for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
-                       cerr << "before delete" << endl;
                        if ((*i) == &port) {
-                               cerr << "About to delete " << &port << endl;
                                delete *i;
                                ps->erase (i);
-                               cerr << "After erasing ports size: " << ps->size();
                                break;
                        }
                }
@@ -667,7 +680,6 @@ AudioEngine::unregister_port (Port& port)
                /* writer goes out of scope, forces update */
        }
                
-       cerr << "before remove_connections\n";
        remove_connections_for (port);
 
        return 0;
@@ -695,34 +707,17 @@ AudioEngine::connect (const string& source, const string& destination)
        Port* src = get_port_by_name_locked (s);
        Port* dst = get_port_by_name_locked (d);
 
-       if (src && dst) {
-
-               /* both ports are known to us, so do the internal connect stuff */
-
-               ret = src->connect (dst);
-
-       } else if (src || dst) {
-
-               /* one port is known to us, try to connect it to something external */
-
                if (src) {
                        ret = src->connect (d);
-               } else {
+       } else if (dst) {
                        ret = dst->connect (s);
-               }
-
        } else {
-
                /* neither port is known to us, and this API isn't intended for use as a general patch bay */
-
                ret = -1;
-               
        }
        
        if (ret > 0) {
-               error << string_compose(_("AudioEngine: connection already exists: %1 (%2) to %3 (%4)"), 
-                                       source, s, destination, d) 
-                     << endmsg;
+               /* already exists - no error, no warning */
        } else if (ret < 0) {
                error << string_compose(_("AudioEngine: cannot connect %1 (%2) to %3 (%4)"), 
                                        source, s, destination, d) 
@@ -754,30 +749,14 @@ AudioEngine::disconnect (const string& source, const string& destination)
        Port* src = get_port_by_name_locked (s);
        Port* dst = get_port_by_name_locked (d);
 
-       if (src && dst) {
-
-               /* both ports are known to us, so do the internal disconnect stuff */
-               
-               ret = src->disconnect (dst);
-
-       } else if (src || dst) {
-
-               /* one port is known to us, try to disconnect it from something external */
-
-               if (src) {
+       if (src) {
                        ret = src->disconnect (d);
-               } else {
+       } else if (dst) {
                        ret = dst->disconnect (s);
-               }
-
        } else {
-
                /* neither port is known to us, and this API isn't intended for use as a general patch bay */
-               
                ret = -1;
-               
        }
-       
        return ret;
 }
 
@@ -813,6 +792,13 @@ AudioEngine::frame_rate ()
        }
 }
 
+size_t
+AudioEngine::raw_buffer_size (DataType t)
+{
+       std::map<DataType,size_t>::const_iterator s = _raw_buffer_sizes.find(t);
+       return (s != _raw_buffer_sizes.end()) ? s->second : 0;
+}
+
 ARDOUR::nframes_t
 AudioEngine::frames_per_cycle ()
 {
@@ -837,8 +823,15 @@ AudioEngine::frames_per_cycle ()
 Port *
 AudioEngine::get_port_by_name (const string& portname)
 {
+       string s;
+       if (portname.find_first_of (':') == string::npos) {
+               s = make_port_name_non_relative (portname);
+       } else {
+               s = portname;
+       }
+       
        Glib::Mutex::Lock lm (_process_lock);
-       return get_port_by_name_locked (portname);
+       return get_port_by_name_locked (s);
 }
 
 Port *
@@ -941,7 +934,7 @@ AudioEngine::n_physical_outputs (DataType type) const
                return 0;
        }
 
-       for (i = 0; ports[i]; ++i);
+       for (i = 0; ports[i]; ++i) {}
        free (ports);
 
        return i;
@@ -962,7 +955,7 @@ AudioEngine::n_physical_inputs (DataType type) const
        }
 
        if (ports) {
-               for (i = 0; ports[i]; ++i);
+               for (i = 0; ports[i]; ++i) {}
                free (ports);
        }
        return i;
@@ -1036,7 +1029,7 @@ AudioEngine::get_nth_physical (DataType type, uint32_t n, int flag)
                return "";
        }
 
-       for (i = 0; i < n && ports[i]; ++i);
+       for (i = 0; i < n && ports[i]; ++i) {}
 
        if (ports[i]) {
                ret = ports[i];
@@ -1104,8 +1097,8 @@ AudioEngine::transport_state ()
 int
 AudioEngine::reset_timebase ()
 {
-       if (_jack) {
-               if (Config->get_jack_time_master()) {
+       if (_jack && session) {
+               if (session->config.get_jack_time_master()) {
                        return jack_set_timebase_callback (_jack, 0, _jack_timebase_callback, this);
                } else {
                        return jack_release_timebase (_jack);
@@ -1168,7 +1161,11 @@ AudioEngine::remove_connections_for (Port& port)
 }
 
 
-#ifdef HAVE_JACK_CLIENT_OPEN
+static void 
+ardour_jack_error (const char* msg) 
+{
+       error << "JACK: " << msg << endmsg;
+}
 
 int
 AudioEngine::connect_to_jack (string client_name)
@@ -1194,22 +1191,6 @@ AudioEngine::connect_to_jack (string client_name)
        return 0;
 }
 
-#else
-
-int
-AudioEngine::connect_to_jack (string client_name)
-{
-       jack_client_name = client_name;
-
-       if ((_jack = jack_client_new (client_name.c_str())) == 0) {
-               return -1;
-       }
-
-       return 0;
-}
-
-#endif /* HAVE_JACK_CLIENT_OPEN */
-
 int 
 AudioEngine::disconnect_from_jack ()
 {
@@ -1217,7 +1198,6 @@ AudioEngine::disconnect_from_jack ()
                return 0;
        }
 
-
        if (_running) {
                stop_metering_thread ();
        }
@@ -1230,6 +1210,7 @@ AudioEngine::disconnect_from_jack ()
 
        _buffer_size = 0;
        _frame_rate = 0;
+       _raw_buffer_sizes.clear();
 
        if (_running) {
                _running = false;
@@ -1275,6 +1256,10 @@ AudioEngine::reconnect_to_jack ()
                nframes_t blocksize = jack_get_buffer_size (_jack);
                session->set_block_size (blocksize);
                session->set_frame_rate (jack_get_sample_rate (_jack));
+               
+               _raw_buffer_sizes[DataType::AUDIO] = blocksize * sizeof(float);
+               cout << "FIXME: Assuming maximum MIDI buffer size " << blocksize * 4 << "bytes" << endl;
+               _raw_buffer_sizes[DataType::MIDI] = blocksize * 4;
        }
 
        last_monitor_check = 0;
@@ -1289,7 +1274,7 @@ AudioEngine::reconnect_to_jack ()
        jack_set_sync_callback (_jack, _jack_sync_callback, this);
        jack_set_freewheel_callback (_jack, _freewheel_callback, this);
        
-       if (Config->get_jack_time_master()) {
+       if (session && session->config.get_jack_time_master()) {
                jack_set_timebase_callback (_jack, 0, _jack_timebase_callback, this);
        }