Fix MIDI selection/tool issues (issue #0002415 and other bugs).
[ardour.git] / libs / ardour / audioengine.cc
index c77f1b9fd8f84aa2c72367117a0203534c94163c..8bbed4673346229158aaaf8d7fb98238e2fab53f 100644 (file)
 #include <vector>
 #include <exception>
 #include <stdexcept>
+#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>
@@ -81,9 +84,6 @@ AudioEngine::AudioEngine (string client_name)
        if (connect_to_jack (client_name)) {
                throw NoBackendAvailable ();
        }
-
-       start_metering_thread();
-
        Port::set_engine (this);
 }
 
@@ -115,6 +115,7 @@ _thread_init_callback (void *arg)
        */
 
        PBD::ThreadCreatedWithRequestSize (pthread_self(), X_("Audioengine"), 4096);
+       MIDI::JACK_MidiPort::set_process_thread (pthread_self());
 }
 
 int
@@ -125,6 +126,8 @@ AudioEngine::start ()
                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));
 
@@ -166,6 +169,8 @@ AudioEngine::start ()
                } else {
                        // error << _("cannot activate JACK client") << endmsg;
                }
+
+               start_metering_thread();
        }
 
        return _running ? 0 : -1;
@@ -176,11 +181,11 @@ AudioEngine::stop (bool forever)
 {
        if (_running) {
                _running = false;
+               stop_metering_thread ();
                if (forever) {
                        jack_client_t* foo = _jack;
                        _jack = 0;
                        jack_client_close (foo);
-                       stop_metering_thread ();
                } else {
                        jack_deactivate (_jack);
                }
@@ -336,11 +341,6 @@ AudioEngine::process_callback (nframes_t nframes)
        if (session) {
                session->process (nframes);
        }
-
-       if (!_running) {
-               _processed_frames = next_processed_frames;
-               return 0;
-       }
        
        // Finalize ports (ie write data if necessary)
 
@@ -348,6 +348,11 @@ AudioEngine::process_callback (nframes_t nframes)
                (*i)->cycle_end (nframes, 0);
        }
 
+       if (!_running) {
+               _processed_frames = next_processed_frames;
+               return 0;
+       }
+
        if (last_monitor_check + monitor_check_interval < next_processed_frames) {
 
                boost::shared_ptr<Ports> p = ports.reader();
@@ -368,6 +373,20 @@ AudioEngine::process_callback (nframes_t nframes)
                last_monitor_check = next_processed_frames;
        }
 
+       if (session->silent()) {
+
+               boost::shared_ptr<Ports> p = ports.reader();
+
+               for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
+                       
+                       Port *port = (*i);
+                       
+                       if (port->sends_output()) {
+                               port->get_buffer().silence(nframes);
+                       }
+               }
+       }
+
        _processed_frames = next_processed_frames;
        return 0;
 }
@@ -438,8 +457,9 @@ void
 AudioEngine::start_metering_thread ()
 {
        if (m_meter_thread == 0) {
+               g_atomic_int_set (&m_meter_exit, 0);
                m_meter_thread = Glib::Thread::create (sigc::mem_fun(this, &AudioEngine::meter_thread),
-                               500000, true, true, Glib::THREAD_PRIORITY_NORMAL);
+                                                      500000, true, true, Glib::THREAD_PRIORITY_NORMAL);
        }
 }
 
@@ -507,14 +527,36 @@ AudioEngine::remove_session ()
                session = 0;
        }
        
-       remove_all_ports ();
+       //FIXME: Preliminary bugfix for  http://tracker.ardour.org/view.php?id=1985
+       //remove_all_ports ();
 }
 
+void
+AudioEngine::port_registration_failure (const std::string& portname)
+{
+       string full_portname = jack_client_name;
+       full_portname += ':';
+       full_portname += portname;
+       
+       
+       jack_port_t* p = jack_port_by_name (_jack, full_portname.c_str());
+       string reason;
+       
+       if (p) {
+               reason = _("a port with this name already exists: check for duplicated track/bus names");
+       } else {
+               reason = _("unknown error");
+       }
+       
+       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)
 {
        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());
@@ -524,16 +566,22 @@ AudioEngine::register_port (DataType dtype, const string& portname, bool input,
                        throw unknown_type();
                }
 
+               /*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();
+               throw PortRegistrationFailure("unable to create port (unknown type?)");
        }
 }
 
@@ -543,6 +591,7 @@ AudioEngine::get_port (const std::string& full_name)
        boost::shared_ptr<Ports> p = ports.reader();
        
        for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
+               //cerr << "comparing port name '" << (*i)->name() << "' with '" << full_name << "'" << endl;
                if ((*i)->name() == full_name) {
                        return *i;
                }
@@ -563,31 +612,45 @@ AudioEngine::register_output_port (DataType type, const string& portname, bool p
        return register_port (type, portname, false, publish);
 }
 
-int          
+int
 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;
                        }
                }
                
                /* writer goes out of scope, forces update */
        }
+               
+       cerr << "before remove_connections\n";
+       remove_connections_for (port);
 
        return 0;
 }
@@ -609,6 +672,8 @@ AudioEngine::connect (const string& source, const string& destination)
        string s = make_port_name_non_relative (source);
        string d = make_port_name_non_relative (destination);
                
+       //cerr << "Trying to connect source: " << s << " with destination " << d << endl;
+       
        Port* src = get_port (s);
        Port* dst = get_port (d);
 
@@ -679,6 +744,8 @@ AudioEngine::disconnect (const string& source, const string& destination)
        string s = make_port_name_non_relative (source);
        string d = make_port_name_non_relative (destination);
 
+       //cerr << "trying to disconnect port '" << s << "' from port '" << d << endl;
+       
        Port* src = get_port (s);
        Port* dst = get_port (d);
 
@@ -818,15 +885,39 @@ void
 AudioEngine::halted (void *arg)
 {
        AudioEngine* ae = static_cast<AudioEngine *> (arg);
+       bool was_running = ae->_running;
+
+       ae->stop_metering_thread ();
 
        ae->_running = false;
        ae->_buffer_size = 0;
        ae->_frame_rate = 0;
        ae->_jack = 0;
 
-       ae->Halted(); /* EMIT SIGNAL */
+       if (was_running) {
+               ae->Halted(); /* EMIT SIGNAL */
+       }
+}
+
+bool
+AudioEngine::can_request_hardware_monitoring () 
+{
+       const char ** ports;
+
+       if (!_jack) {
+               return 0;
+       }
+
+       if ((ports = jack_get_ports (_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortCanMonitor)) == 0) {
+               return false;
+       }
+
+       free (ports);
+
+       return true;
 }
 
+
 uint32_t
 AudioEngine::n_physical_outputs () const
 {
@@ -841,10 +932,9 @@ AudioEngine::n_physical_outputs () const
                return 0;
        }
 
-       if (ports) {
-               for (i = 0; ports[i]; ++i);
-               free (ports);
-       }
+       for (i = 0; ports[i]; ++i);
+       free (ports);
+
        return i;
 }
 
@@ -1052,6 +1142,23 @@ AudioEngine::remove_all_ports ()
        }
 }
 
+void
+AudioEngine::remove_connections_for (Port& port)
+{
+       for (PortConnections::iterator i = port_connections.begin(); i != port_connections.end(); ) {
+               PortConnections::iterator tmp;
+               
+               tmp = i;
+               ++tmp;
+               
+               if ((*i).first == port.name()) {
+                       port_connections.erase (i);
+               }
+
+               i = tmp;
+       }
+}
+
 
 #ifdef HAVE_JACK_CLIENT_OPEN
 
@@ -1063,9 +1170,8 @@ AudioEngine::connect_to_jack (string client_name)
        const char *server_name = NULL;
 
        jack_client_name = client_name; /* might be reset below */
-
        _jack = jack_client_open (jack_client_name.c_str(), options, &status, server_name);
-       
+
        if (_jack == NULL) {
 
                if (status & JackServerFailed) {
@@ -1118,6 +1224,7 @@ AudioEngine::disconnect_from_jack ()
        _frame_rate = 0;
 
        if (_running) {
+               stop_metering_thread ();
                _running = false;
                Stopped(); /* EMIT SIGNAL */
        }
@@ -1195,6 +1302,8 @@ AudioEngine::reconnect_to_jack ()
 
        Running (); /* EMIT SIGNAL*/
 
+       start_metering_thread ();
+
        return 0;
 }