Ardour  9.0-pre0-582-g084a23a80d
audio_backend.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013-2016 Tim Mayberry <mojofunk@gmail.com>
3  * Copyright (C) 2013-2017 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2014-2016 Robin Gareus <robin@gareus.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #pragma once
22 
23 #include <string>
24 #include <vector>
25 
26 #include <stdint.h>
27 #include <stdlib.h>
28 
29 
30 #include "ardour/audioengine.h"
32 #include "ardour/port_engine.h"
33 #include "ardour/types.h"
34 
35 #ifdef ARDOURBACKEND_DLL_EXPORTS // defined if we are building the ARDOUR Panners DLLs (instead of using them)
36 # define ARDOURBACKEND_API LIBARDOUR_DLL_EXPORT
37 #else
38 # define ARDOURBACKEND_API LIBARDOUR_DLL_IMPORT
39 #endif
40 #define ARDOURBACKEND_LOCAL LIBARDOUR_DLL_LOCAL
41 
42 namespace ARDOUR
43 {
45  const char* name;
46 
51  int (*instantiate) (const std::string& arg1, const std::string& arg2);
52 
54  int (*deinstantiate) (void);
55 
61  std::shared_ptr<AudioBackend> (*factory) (AudioEngine&);
62 
70  bool (*already_configured) ();
71 
78  bool (*available) ();
79 };
80 
85 {
86 public:
88  : PortEngine (e)
89  , _info (i)
90  , engine (e)
91  {}
92 
93  virtual ~AudioBackend () {}
94 
95  enum ErrorCode {
96  NoError = 0,
97  BackendInitializationError = -64,
130  };
131 
132  static std::string get_error_string (ErrorCode);
133 
136  DeviceDefault
137  };
138 
140 
145  {
146  return _info;
147  }
148 
154  virtual std::string name () const = 0;
155 
160  virtual bool is_realtime () const = 0;
161 
163  virtual bool is_jack () const { return false; }
164 
165  virtual int client_real_time_priority () { return 0; }
166 
167  /* Discovering devices and parameters */
168 
177  virtual bool requires_driver_selection () const
178  {
179  return false;
180  }
181 
189  virtual std::vector<std::string> enumerate_drivers () const
190  {
191  return std::vector<std::string> ();
192  }
193 
200  virtual int set_driver (const std::string& drivername)
201  {
202  return 0;
203  }
204 
208  struct DeviceStatus {
209  std::string name;
210  bool available;
211 
212  DeviceStatus (const std::string& s, bool avail)
213  : name (s)
214  , available (avail)
215  {}
216  };
217 
227  {
228  return false;
229  }
230 
231  /* Return true if the backend uses separate I/O devices only for the case
232  * of allowing one to be "None".
233  *
234  * ie. Input Device must match Output Device, except if either of them
235  * is get_standard_device_name (DeviceNone).
236  */
238  {
239  return false;
240  }
241 
249  virtual std::vector<DeviceStatus> enumerate_devices () const = 0;
250 
258  virtual std::vector<DeviceStatus> enumerate_input_devices () const
259  {
260  return std::vector<DeviceStatus> ();
261  }
262 
270  virtual std::vector<DeviceStatus> enumerate_output_devices () const
271  {
272  return std::vector<DeviceStatus> ();
273  }
274 
280  virtual bool can_set_period_size () const
281  {
282  return false;
283  }
284 
286  virtual std::vector<uint32_t> available_period_sizes (const std::string& driver, const std::string& device) const
287  {
288  return std::vector<uint32_t> ();
289  }
290 
294  virtual int set_peridod_size (uint32_t)
295  {
296  return -1;
297  }
298 
304  {
305  return false;
306  }
307 
314  virtual bool update_devices ()
315  {
316  return false;
317  }
318 
323  virtual bool can_use_buffered_io ()
324  {
325  return false;
326  }
327 
331  virtual void set_use_buffered_io (bool) {}
332 
337  virtual bool get_use_buffered_io ()
338  {
339  return false;
340  }
341 
349  virtual std::vector<float> available_sample_rates (const std::string& device) const = 0;
350 
351  /* backends that suppor586t separate input and output devices should
352  * implement this function and return an intersection (not union) of available
353  * sample rates valid for the given input + output device combination.
354  */
355  virtual std::vector<float> available_sample_rates2 (const std::string& input_device, const std::string& output_device) const
356  {
357  std::vector<float> input_sizes = available_sample_rates (input_device);
358  std::vector<float> output_sizes = available_sample_rates (output_device);
359  std::vector<float> rv;
360  std::set_union (input_sizes.begin (), input_sizes.end (),
361  output_sizes.begin (), output_sizes.end (),
362  std::back_inserter (rv));
363  return rv;
364  }
365 
366  /* Returns the default sample rate that will be shown to the user when
367  * configuration options are first presented. If the derived class
368  * needs or wants to override this, it can. It also MUST override this
369  * if there is any chance that an SR of 48kHz is not in the list
370  * returned by available_sample_rates()
371  */
372  virtual float default_sample_rate () const
373  {
374  return 48000.0;
375  }
376 
384  virtual std::vector<uint32_t> available_buffer_sizes (const std::string& device) const = 0;
385 
386  /* backends that support separate input and output devices should
387  * implement this function and return an intersection (not union) of available
388  * buffer sizes valid for the given input + output device combination.
389  */
390  virtual std::vector<uint32_t> available_buffer_sizes2 (const std::string& input_device, const std::string& output_device) const
391  {
392  std::vector<uint32_t> input_rates = available_buffer_sizes (input_device);
393  std::vector<uint32_t> output_rates = available_buffer_sizes (output_device);
394  std::vector<uint32_t> rv;
395  std::set_union (input_rates.begin (), input_rates.end (),
396  output_rates.begin (), output_rates.end (),
397  std::back_inserter (rv));
398  return rv;
399  }
400  /* Returns the default buffer size that will be shown to the user when
401  * configuration options are first presented. If the derived class
402  * needs or wants to override this, it can. It also MUST override this
403  * if there is any chance that a buffer size of 1024 is not in the list
404  * returned by available_buffer_sizes()
405  */
406  virtual uint32_t default_buffer_size (const std::string& device) const
407  {
408  return 1024;
409  }
410  /* Return true if the derived class can change the sample rate of the
411  * device in use while the device is already being used. Return false
412  * otherwise. (example: JACK cannot do this as of September 2013)
413  */
414  virtual bool can_change_sample_rate_when_running () const = 0;
415  /* Return true if the derived class can change the buffer size of the
416  * device in use while the device is already being used. Return false
417  * otherwise.
418  */
419  virtual bool can_change_buffer_size_when_running () const = 0;
420 
425  virtual bool can_measure_systemic_latency () const = 0;
426 
431  {
432  return false;
433  }
434 
435  /* Set the hardware parameters.
436  *
437  * If called when the current state is stopped or paused,
438  * the changes will not take effect until the state changes to running.
439  *
440  * If called while running, the state will change as fast as the
441  * implementation allows.
442  *
443  * All set_*() methods return zero on success, non-zero otherwise.
444  */
445 
447  virtual int set_device_name (const std::string&) = 0;
448 
454  virtual int set_input_device_name (const std::string&)
455  {
456  return 0;
457  }
458 
464  virtual int set_output_device_name (const std::string&)
465  {
466  return 0;
467  }
468 
470  virtual int drop_device ()
471  {
472  return 0;
473  };
474 
476  virtual int set_sample_rate (float) = 0;
477 
486  virtual int set_buffer_size (uint32_t) = 0;
487 
496  virtual int set_interleaved (bool yn) = 0;
497 
502  virtual int set_systemic_input_latency (uint32_t) = 0;
503 
508  virtual int set_systemic_output_latency (uint32_t) = 0;
509 
513  virtual int set_systemic_midi_input_latency (std::string const, uint32_t) = 0;
514 
518  virtual int set_systemic_midi_output_latency (std::string const, uint32_t) = 0;
519 
520  /* Retrieving parameters */
521 
522  virtual std::string device_name () const = 0;
523  virtual std::string input_device_name () const
524  {
525  return std::string ();
526  }
527 
528  virtual std::string output_device_name () const
529  {
530  return std::string ();
531  }
532 
533  virtual float sample_rate () const = 0;
534  virtual uint32_t buffer_size () const = 0;
535  virtual bool interleaved () const = 0;
536  virtual uint32_t systemic_input_latency () const = 0;
537  virtual uint32_t systemic_output_latency () const = 0;
538  virtual uint32_t systemic_midi_input_latency (std::string const) const = 0;
539  virtual uint32_t systemic_midi_output_latency (std::string const) const = 0;
540 
541  virtual uint32_t period_size () const { return 0; }
542 
546  virtual std::string driver_name () const
547  {
548  return std::string ();
549  }
550 
556  virtual std::string control_app_name () const = 0;
557 
562  virtual void launch_control_app () = 0;
563 
564  /* @return a vector of strings that describe the available
565  * MIDI options.
566  *
567  * These can be presented to the user to decide which
568  * MIDI drivers, options etc. can be used. The returned strings
569  * should be thought of as the key to a map of possible
570  * approaches to handling MIDI within the backend. Ensure that
571  * the strings will make sense to the user.
572  */
573  virtual std::vector<std::string> enumerate_midi_options () const = 0;
574 
575  /* Request the use of the MIDI option named \p option, which
576  * should be one of the strings returned by enumerate_midi_options()
577  *
578  * @return zero if successful, non-zero otherwise
579  */
580  virtual int set_midi_option (const std::string& option) = 0;
581 
582  virtual std::string midi_option () const = 0;
583 
585  virtual std::vector<DeviceStatus> enumerate_midi_devices () const = 0;
586 
588  virtual int set_midi_device_enabled (std::string const, bool) = 0;
589 
591  virtual bool midi_device_enabled (std::string const) const = 0;
592 
594  virtual bool can_set_systemic_midi_latencies () const = 0;
595 
596  /* State Control */
597 
626  int start (bool for_latency_measurement = false)
627  {
628  return _start (for_latency_measurement);
629  }
630 
645  virtual int stop () = 0;
646 
651  virtual int reset_device () = 0;
652 
670  virtual int freewheel (bool start_stop) = 0;
671 
682  virtual float dsp_load () const = 0;
683 
684  /* Transport Control (JACK is the only audio API that currently offers
685  * the concept of shared transport control)
686  */
687 
689  virtual void transport_start () {}
690 
692  virtual void transport_stop () {}
693 
696  {
697  return TransportStopped;
698  }
699 
701  virtual void transport_locate (samplepos_t pos) {}
702 
706  virtual samplepos_t transport_sample () const
707  {
708  return 0;
709  }
710 
719  virtual int set_time_master (bool yn)
720  {
721  return 0;
722  }
723 
724  virtual int usecs_per_cycle () const
725  {
726  return 1000000 * (buffer_size () / sample_rate ());
727  }
728  virtual size_t raw_buffer_size (DataType t) = 0;
729 
730  /* Process time */
731 
743  virtual samplepos_t sample_time () = 0;
744 
749 
757 
773  virtual bool get_sync_offset (pframes_t& offset) const
774  {
775  return false;
776  }
777 
786  virtual int create_process_thread (std::function<void()> func) = 0;
787 
792  virtual int join_process_threads () = 0;
793 
795  virtual bool in_process_thread () = 0;
796 
798  static size_t thread_stack_size ()
799  {
800  return 100000;
801  }
802 
804  virtual uint32_t process_thread_count () = 0;
805 
806  virtual void update_latencies () = 0;
807 
817  virtual bool speed_and_position (double& speed, samplepos_t& position)
818  {
819  speed = 0.0;
820  position = 0;
821  return false;
822  }
823 
824  enum TimingTypes {
825  DeviceWait = 0,
827  /* end */
828  NTT
829  };
830 
831  PBD::TimingStats dsp_stats[NTT];
832 
833 protected:
836 
837  virtual int _start (bool for_latency_measurement) = 0;
838 };
839 
840 } // namespace ARDOUR
841 
virtual samplepos_t transport_sample() const
virtual void update_latencies()=0
virtual std::string output_device_name() const
virtual bool interleaved() const =0
virtual int stop()=0
virtual int set_systemic_midi_input_latency(std::string const, uint32_t)=0
virtual int create_process_thread(std::function< void()> func)=0
virtual int set_sample_rate(float)=0
virtual int _start(bool for_latency_measurement)=0
static std::string get_standard_device_name(StandardDeviceName)
virtual bool match_input_output_devices_or_none() const
virtual void launch_control_app()=0
virtual int set_time_master(bool yn)
virtual bool is_realtime() const =0
virtual int client_real_time_priority()
virtual pframes_t samples_since_cycle_start()=0
virtual uint32_t buffer_size() const =0
virtual uint32_t default_buffer_size(const std::string &device) const
virtual int freewheel(bool start_stop)=0
virtual std::vector< uint32_t > available_buffer_sizes(const std::string &device) const =0
virtual std::vector< uint32_t > available_period_sizes(const std::string &driver, const std::string &device) const
virtual std::vector< uint32_t > available_buffer_sizes2(const std::string &input_device, const std::string &output_device) const
virtual float dsp_load() const =0
virtual std::string control_app_name() const =0
virtual std::vector< std::string > enumerate_midi_options() const =0
virtual std::string name() const =0
virtual bool can_change_sample_rate_when_running() const =0
virtual int set_input_device_name(const std::string &)
virtual std::string input_device_name() const
virtual uint32_t systemic_input_latency() const =0
virtual bool speed_and_position(double &speed, samplepos_t &position)
virtual int set_systemic_midi_output_latency(std::string const, uint32_t)=0
virtual std::string midi_option() const =0
virtual int set_peridod_size(uint32_t)
virtual int set_interleaved(bool yn)=0
virtual void transport_stop()
virtual uint32_t process_thread_count()=0
virtual bool is_jack() const
virtual int set_systemic_input_latency(uint32_t)=0
AudioBackend(AudioEngine &e, AudioBackendInfo &i)
Definition: audio_backend.h:87
virtual TransportState transport_state() const
virtual bool update_devices()
virtual float sample_rate() const =0
virtual std::vector< DeviceStatus > enumerate_input_devices() const
virtual size_t raw_buffer_size(DataType t)=0
virtual uint32_t systemic_midi_output_latency(std::string const) const =0
virtual samplepos_t sample_time_at_cycle_start()=0
virtual bool requires_driver_selection() const
virtual int set_output_device_name(const std::string &)
virtual int drop_device()
virtual std::vector< std::string > enumerate_drivers() const
virtual std::vector< DeviceStatus > enumerate_output_devices() const
AudioBackendInfo & _info
virtual uint32_t period_size() const
virtual bool use_separate_input_and_output_devices() const
virtual bool midi_device_enabled(std::string const) const =0
virtual std::vector< float > available_sample_rates2(const std::string &input_device, const std::string &output_device) const
static size_t thread_stack_size()
virtual std::string driver_name() const
virtual void transport_start()
virtual int usecs_per_cycle() const
virtual bool in_process_thread()=0
virtual void transport_locate(samplepos_t pos)
virtual bool can_set_systemic_midi_latencies() const =0
virtual bool can_change_systemic_latency_when_running() const
virtual int set_midi_option(const std::string &option)=0
virtual std::string device_name() const =0
AudioBackendInfo & info() const
virtual float default_sample_rate() const
virtual bool can_request_update_devices()
virtual int set_device_name(const std::string &)=0
virtual int set_midi_device_enabled(std::string const, bool)=0
virtual int set_driver(const std::string &drivername)
virtual bool get_use_buffered_io()
virtual std::vector< DeviceStatus > enumerate_devices() const =0
virtual int reset_device()=0
virtual samplepos_t sample_time()=0
int start(bool for_latency_measurement=false)
virtual bool can_measure_systemic_latency() const =0
virtual uint32_t systemic_midi_input_latency(std::string const) const =0
virtual std::vector< DeviceStatus > enumerate_midi_devices() const =0
virtual bool can_use_buffered_io()
static std::string get_error_string(ErrorCode)
virtual int set_systemic_output_latency(uint32_t)=0
virtual bool can_change_buffer_size_when_running() const =0
virtual std::vector< float > available_sample_rates(const std::string &device) const =0
virtual int join_process_threads()=0
virtual int set_buffer_size(uint32_t)=0
virtual bool can_set_period_size() const
virtual uint32_t systemic_output_latency() const =0
virtual bool get_sync_offset(pframes_t &offset) const
AudioEngine & engine
virtual void set_use_buffered_io(bool)
GtkImageIconNameData name
Definition: gtkimage.h:6
#define LIBARDOUR_API
uint32_t pframes_t
Temporal::samplepos_t samplepos_t
DeviceStatus(const std::string &s, bool avail)
Definition: getopt.h:74