O(log(n)) port and connection lookup for ALSA
[ardour.git] / libs / backends / alsa / alsa_audiobackend.h
index c95593c349b75638daa07fe2ea76c581a32ec5da..48115f9f05ea1eac059f03f7e8a0ab3176432c43 100644 (file)
@@ -70,9 +70,11 @@ class AlsaPort {
                virtual ~AlsaPort ();
 
                const std::string& name () const { return _name; }
+               const std::string& pretty_name () const { return _pretty_name; }
                PortFlags flags () const { return _flags; }
 
                int set_name (const std::string &name) { _name = name; return 0; }
+               int set_pretty_name (const std::string &name) { _pretty_name = name; return 0; }
 
                virtual DataType type () const = 0;
 
@@ -84,7 +86,7 @@ class AlsaPort {
                bool is_connected (const AlsaPort *port) const;
                bool is_physically_connected () const;
 
-               const std::vector<AlsaPort *>& get_connections () const { return _connections; }
+               const std::set<AlsaPort *>& get_connections () const { return _connections; }
 
                int connect (AlsaPort *port);
                int disconnect (AlsaPort *port);
@@ -112,10 +114,11 @@ class AlsaPort {
        private:
                AlsaAudioBackend &_alsa_backend;
                std::string _name;
+               std::string _pretty_name;
                const PortFlags _flags;
                LatencyRange _capture_latency_range;
                LatencyRange _playback_latency_range;
-               std::vector<AlsaPort*> _connections;
+               std::set<AlsaPort*> _connections;
 
                void _connect (AlsaPort* , bool);
                void _disconnect (AlsaPort* , bool);
@@ -148,10 +151,10 @@ class AlsaMidiPort : public AlsaPort {
                const AlsaMidiBuffer * const_buffer () const { return & _buffer[_bufperiod]; }
 
                void next_period() { if (_n_periods > 1) { get_buffer(0); _bufperiod = (_bufperiod + 1) % _n_periods; } }
-               void set_n_periods(int n) { if (n > 0 && n < 3) { _n_periods = n; } }
+               void set_n_periods(int n) { if (n > 0 && n < 4) { _n_periods = n; } }
 
        private:
-               AlsaMidiBuffer _buffer[2];
+               AlsaMidiBuffer _buffer[3];
                int _n_periods;
                int _bufperiod;
 }; // class AlsaMidiPort
@@ -168,6 +171,8 @@ class AlsaAudioBackend : public AudioBackend {
                bool is_realtime () const;
 
                bool use_separate_input_and_output_devices () const { return true; }
+               bool can_set_period_size () const { return true; }
+
                std::vector<DeviceStatus> enumerate_devices () const;
                std::vector<DeviceStatus> enumerate_input_devices () const;
                std::vector<DeviceStatus> enumerate_output_devices () const;
@@ -175,17 +180,21 @@ class AlsaAudioBackend : public AudioBackend {
                std::vector<float> available_sample_rates2 (const std::string&, const std::string&) const;
                std::vector<uint32_t> available_buffer_sizes (const std::string& device) const;
                std::vector<uint32_t> available_buffer_sizes2 (const std::string&, const std::string&) const;
+               std::vector<uint32_t> available_period_sizes (const std::string& driver) const;
                uint32_t available_input_channel_count (const std::string& device) const;
                uint32_t available_output_channel_count (const std::string& device) const;
 
                bool can_change_sample_rate_when_running () const;
                bool can_change_buffer_size_when_running () const;
 
+               bool can_change_systemic_latency_when_running () const { return true; }
+
                int set_device_name (const std::string&);
                int set_input_device_name (const std::string&);
                int set_output_device_name (const std::string&);
                int set_sample_rate (float);
                int set_buffer_size (uint32_t);
+               int set_peridod_size (uint32_t);
                int set_interleaved (bool yn);
                int set_input_channels (uint32_t);
                int set_output_channels (uint32_t);
@@ -202,6 +211,7 @@ class AlsaAudioBackend : public AudioBackend {
                std::string  output_device_name () const;
                float        sample_rate () const;
                uint32_t     buffer_size () const;
+               uint32_t     period_size () const;
                bool         interleaved () const;
                uint32_t     input_channels () const;
                uint32_t     output_channels () const;
@@ -257,6 +267,9 @@ class AlsaAudioBackend : public AudioBackend {
                std::string get_port_name (PortHandle) const;
                PortHandle  get_port_by_name (const std::string&) const;
 
+               int get_port_property (PortHandle, const std::string& key, std::string& value, std::string& type) const;
+               int set_port_property (PortHandle, const std::string& key, const std::string& value, const std::string& type);
+
                int get_ports (const std::string& port_name_pattern, DataType type, PortFlags flags, std::vector<std::string>&) const;
 
                DataType port_data_type (PortHandle) const;
@@ -388,18 +401,25 @@ class AlsaAudioBackend : public AudioBackend {
                /* port engine */
                PortHandle add_port (const std::string& shortname, ARDOUR::DataType, ARDOUR::PortFlags);
                int register_system_audio_ports ();
-               int register_system_midi_ports ();
+               int register_system_midi_ports (const std::string device = "");
                void unregister_ports (bool system_only = false);
 
-               std::vector<AlsaPort *> _ports;
                std::vector<AlsaPort *> _system_inputs;
                std::vector<AlsaPort *> _system_outputs;
                std::vector<AlsaPort *> _system_midi_in;
                std::vector<AlsaPort *> _system_midi_out;
 
+               typedef std::map<std::string, AlsaPort *> PortMap; // fast lookup in _ports
+               typedef std::set<AlsaPort *> PortIndex; // fast lookup in _ports
+               PortMap _portmap;
+               PortIndex _ports;
+
                std::vector<AlsaMidiOut *> _rmidi_out;
                std::vector<AlsaMidiIn  *> _rmidi_in;
 
+               unsigned _midi_ins;
+               unsigned _midi_outs;
+
                struct PortConnectData {
                        std::string a;
                        std::string b;
@@ -426,18 +446,20 @@ class AlsaAudioBackend : public AudioBackend {
                }
 
                bool valid_port (PortHandle port) const {
-                       return std::find (_ports.begin (), _ports.end (), (AlsaPort*)port) != _ports.end ();
+                       return _ports.find (static_cast<AlsaPort*>(port)) != _ports.end ();
                }
 
-               AlsaPort * find_port (const std::string& port_name) const {
-                       for (std::vector<AlsaPort*>::const_iterator it = _ports.begin (); it != _ports.end (); ++it) {
-                               if ((*it)->name () == port_name) {
-                                       return *it;
-                               }
+               AlsaPort* find_port (const std::string& port_name) const {
+                       PortMap::const_iterator it = _portmap.find (port_name);
+                       if (it == _portmap.end()) {
+                               return NULL;
                        }
-                       return NULL;
+                       return (*it).second;
                }
 
+               void update_systemic_audio_latencies ();
+               void update_systemic_midi_latencies ();
+
 }; // class AlsaAudioBackend
 
 } // namespace