Use ARDOUR::DSPLoadCalculator in DummyBackend
[ardour.git] / libs / backends / dummy / dummy_audiobackend.h
1 /*
2  * Copyright (C) 2014 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2013 Paul Davis
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #ifndef __libbackend_dummy_audiobackend_h__
21 #define __libbackend_dummy_audiobackend_h__
22
23 #include <string>
24 #include <vector>
25 #include <map>
26 #include <set>
27
28 #include <stdint.h>
29 #include <pthread.h>
30
31 #include <boost/shared_ptr.hpp>
32
33 #include "ardour/types.h"
34 #include "ardour/audio_backend.h"
35 #include "ardour/dsp_load_calculator.h"
36
37 namespace ARDOUR {
38
39 class DummyAudioBackend;
40
41 namespace DummyMidiData {
42         typedef struct _MIDISequence {
43                 float   beat_time;
44                 uint8_t size;
45                 uint8_t event[3];
46         } MIDISequence;
47 };
48
49
50 class DummyMidiEvent {
51         public:
52                 DummyMidiEvent (const pframes_t timestamp, const uint8_t* data, size_t size);
53                 DummyMidiEvent (const DummyMidiEvent& other);
54                 ~DummyMidiEvent ();
55                 size_t size () const { return _size; };
56                 pframes_t timestamp () const { return _timestamp; };
57                 const unsigned char* const_data () const { return _data; };
58                 unsigned char* data () { return _data; };
59                 bool operator< (const DummyMidiEvent &other) const { return timestamp () < other.timestamp (); };
60         private:
61                 size_t _size;
62                 pframes_t _timestamp;
63                 uint8_t *_data;
64 };
65
66 typedef std::vector<boost::shared_ptr<DummyMidiEvent> > DummyMidiBuffer;
67
68 class DummyPort {
69         protected:
70                 DummyPort (DummyAudioBackend &b, const std::string&, PortFlags);
71         public:
72                 virtual ~DummyPort ();
73
74                 const std::string& name () const { return _name; }
75                 PortFlags flags () const { return _flags; }
76
77                 int set_name (const std::string &name) { _name = name; return 0; }
78
79                 virtual DataType type () const = 0;
80
81                 bool is_input ()     const { return flags () & IsInput; }
82                 bool is_output ()    const { return flags () & IsOutput; }
83                 bool is_physical ()  const { return flags () & IsPhysical; }
84                 bool is_terminal ()  const { return flags () & IsTerminal; }
85                 bool is_connected () const { return _connections.size () != 0; }
86                 bool is_connected (const DummyPort *port) const;
87                 bool is_physically_connected () const;
88
89                 const std::vector<DummyPort *>& get_connections () const { return _connections; }
90
91                 int connect (DummyPort *port);
92                 int disconnect (DummyPort *port);
93                 void disconnect_all ();
94
95                 virtual void* get_buffer (pframes_t nframes) = 0;
96                 void next_period () { _gen_cycle = false; }
97
98                 const LatencyRange latency_range (bool for_playback) const
99                 {
100                         return for_playback ? _playback_latency_range : _capture_latency_range;
101                 }
102
103                 void set_latency_range (const LatencyRange &latency_range, bool for_playback)
104                 {
105                         if (for_playback)
106                         {
107                                 _playback_latency_range = latency_range;
108                         }
109                         else
110                         {
111                                 _capture_latency_range = latency_range;
112                         }
113                 }
114
115         private:
116                 DummyAudioBackend &_dummy_backend;
117                 std::string _name;
118                 const PortFlags _flags;
119                 LatencyRange _capture_latency_range;
120                 LatencyRange _playback_latency_range;
121                 std::vector<DummyPort*> _connections;
122
123                 void _connect (DummyPort* , bool);
124                 void _disconnect (DummyPort* , bool);
125
126         protected:
127                 // random number generator
128                 void setup_random_number_generator ();
129                 inline float    randf ();
130                 inline uint32_t randi ();
131                 uint32_t _rseed;
132
133                 // signal generator
134                 volatile bool _gen_cycle;
135                 Glib::Threads::Mutex generator_lock;
136
137 }; // class DummyPort
138
139 class DummyAudioPort : public DummyPort {
140         public:
141                 DummyAudioPort (DummyAudioBackend &b, const std::string&, PortFlags);
142                 ~DummyAudioPort ();
143
144                 DataType type () const { return DataType::AUDIO; };
145
146                 Sample* buffer () { return _buffer; }
147                 const Sample* const_buffer () const { return _buffer; }
148                 void* get_buffer (pframes_t nframes);
149
150                 enum GeneratorType {
151                         Silence,
152                         UniformWhiteNoise,
153                         GaussianWhiteNoise,
154                         PinkNoise,
155                         PonyNoise,
156                         SineWave,
157                         SquareWave,
158                         KronekerDelta,
159                         SineSweep,
160                         SineSweepSwell,
161                         SquareSweep,
162                         SquareSweepSwell,
163                         Loopback,
164                 };
165                 void setup_generator (GeneratorType const, float const);
166                 void fill_wavetable (const float* d, size_t n_samples) { assert(_wavetable != 0);  memcpy(_wavetable, d, n_samples * sizeof(float)); }
167                 void midi_to_wavetable (DummyMidiBuffer const * const src, size_t n_samples);
168
169         private:
170                 Sample _buffer[8192];
171
172                 // signal generator ('fake' physical inputs)
173                 void generate (const pframes_t n_samples);
174                 GeneratorType _gen_type;
175
176                 // generator buffers
177                 // pink-noise filters
178                 float _b0, _b1, _b2, _b3, _b4, _b5, _b6;
179                 // generated sinf() samples
180                 Sample * _wavetable;
181                 uint32_t _gen_period;
182                 uint32_t _gen_offset;
183                 uint32_t _gen_perio2;
184                 uint32_t _gen_count2;
185
186                 // gaussian noise generator
187                 float grandf ();
188                 bool _pass;
189                 float _rn1;
190
191 }; // class DummyAudioPort
192
193 class DummyMidiPort : public DummyPort {
194         public:
195                 DummyMidiPort (DummyAudioBackend &b, const std::string&, PortFlags);
196                 ~DummyMidiPort ();
197
198                 DataType type () const { return DataType::MIDI; };
199
200                 void* get_buffer (pframes_t nframes);
201                 const DummyMidiBuffer * const_buffer () const { return &_buffer; }
202
203                 void setup_generator (int, float const);
204                 void set_loopback (DummyMidiBuffer const * const src);
205
206         private:
207                 DummyMidiBuffer _buffer;
208                 DummyMidiBuffer _loopback;
209
210                 // midi event generator ('fake' physical inputs)
211                 void midi_generate (const pframes_t n_samples);
212                 float   _midi_seq_spb; // samples per beat
213                 int32_t _midi_seq_time;
214                 uint32_t _midi_seq_pos;
215                 DummyMidiData::MIDISequence const * _midi_seq_dat;
216 }; // class DummyMidiPort
217
218 class DummyAudioBackend : public AudioBackend {
219         friend class DummyPort;
220         public:
221                  DummyAudioBackend (AudioEngine& e, AudioBackendInfo& info);
222                 ~DummyAudioBackend ();
223
224                 bool is_running () const { return _running; }
225
226                 /* AUDIOBACKEND API */
227
228                 std::string name () const;
229                 bool is_realtime () const;
230
231                 bool requires_driver_selection() const { return true; }
232                 std::string driver_name () const;
233                 std::vector<std::string> enumerate_drivers () const;
234                 int set_driver (const std::string&);
235
236                 std::vector<DeviceStatus> enumerate_devices () const;
237                 std::vector<float> available_sample_rates (const std::string& device) const;
238                 std::vector<uint32_t> available_buffer_sizes (const std::string& device) const;
239                 uint32_t available_input_channel_count (const std::string& device) const;
240                 uint32_t available_output_channel_count (const std::string& device) const;
241
242                 bool can_change_sample_rate_when_running () const;
243                 bool can_change_buffer_size_when_running () const;
244
245                 int set_device_name (const std::string&);
246                 int set_sample_rate (float);
247                 int set_buffer_size (uint32_t);
248                 int set_interleaved (bool yn);
249                 int set_input_channels (uint32_t);
250                 int set_output_channels (uint32_t);
251                 int set_systemic_input_latency (uint32_t);
252                 int set_systemic_output_latency (uint32_t);
253                 int set_systemic_midi_input_latency (std::string const, uint32_t) { return 0; }
254                 int set_systemic_midi_output_latency (std::string const, uint32_t) { return 0; }
255
256                 int reset_device () { return 0; };
257
258                 /* Retrieving parameters */
259                 std::string  device_name () const;
260                 float        sample_rate () const;
261                 uint32_t     buffer_size () const;
262                 bool         interleaved () const;
263                 uint32_t     input_channels () const;
264                 uint32_t     output_channels () const;
265                 uint32_t     systemic_input_latency () const;
266                 uint32_t     systemic_output_latency () const;
267                 uint32_t     systemic_midi_input_latency (std::string const) const { return 0; }
268                 uint32_t     systemic_midi_output_latency (std::string const) const { return 0; }
269
270                 /* External control app */
271                 std::string control_app_name () const { return std::string (); }
272                 void launch_control_app () {}
273
274                 /* MIDI */
275                 std::vector<std::string> enumerate_midi_options () const;
276                 int set_midi_option (const std::string&);
277                 std::string midi_option () const;
278
279                 std::vector<DeviceStatus> enumerate_midi_devices () const {
280                         return std::vector<AudioBackend::DeviceStatus> ();
281                 }
282                 int set_midi_device_enabled (std::string const, bool) {
283                         return 0;
284                 }
285                 bool midi_device_enabled (std::string const) const {
286                         return true;
287                 }
288                 bool can_set_systemic_midi_latencies () const {
289                         return false;
290                 }
291
292                 /* State Control */
293         protected:
294                 int _start (bool for_latency_measurement);
295         public:
296                 int stop ();
297                 int freewheel (bool);
298                 float dsp_load () const;
299                 size_t raw_buffer_size (DataType t);
300
301                 /* Process time */
302                 framepos_t sample_time ();
303                 framepos_t sample_time_at_cycle_start ();
304                 pframes_t samples_since_cycle_start ();
305
306                 int create_process_thread (boost::function<void()> func);
307                 int join_process_threads ();
308                 bool in_process_thread ();
309                 uint32_t process_thread_count ();
310
311                 void update_latencies ();
312
313                 /* PORTENGINE API */
314
315                 void* private_handle () const;
316                 const std::string& my_name () const;
317                 bool available () const;
318                 uint32_t port_name_size () const;
319
320                 int         set_port_name (PortHandle, const std::string&);
321                 std::string get_port_name (PortHandle) const;
322                 PortHandle  get_port_by_name (const std::string&) const;
323
324                 int get_ports (const std::string& port_name_pattern, DataType type, PortFlags flags, std::vector<std::string>&) const;
325
326                 DataType port_data_type (PortHandle) const;
327
328                 PortHandle register_port (const std::string& shortname, ARDOUR::DataType, ARDOUR::PortFlags);
329                 void unregister_port (PortHandle);
330
331                 int  connect (const std::string& src, const std::string& dst);
332                 int  disconnect (const std::string& src, const std::string& dst);
333                 int  connect (PortHandle, const std::string&);
334                 int  disconnect (PortHandle, const std::string&);
335                 int  disconnect_all (PortHandle);
336
337                 bool connected (PortHandle, bool process_callback_safe);
338                 bool connected_to (PortHandle, const std::string&, bool process_callback_safe);
339                 bool physically_connected (PortHandle, bool process_callback_safe);
340                 int  get_connections (PortHandle, std::vector<std::string>&, bool process_callback_safe);
341
342                 /* MIDI */
343                 int midi_event_get (pframes_t& timestamp, size_t& size, uint8_t** buf, void* port_buffer, uint32_t event_index);
344                 int midi_event_put (void* port_buffer, pframes_t timestamp, const uint8_t* buffer, size_t size);
345                 uint32_t get_midi_event_count (void* port_buffer);
346                 void     midi_clear (void* port_buffer);
347
348                 /* Monitoring */
349
350                 bool can_monitor_input () const;
351                 int  request_input_monitoring (PortHandle, bool);
352                 int  ensure_input_monitoring (PortHandle, bool);
353                 bool monitoring_input (PortHandle);
354
355                 /* Latency management */
356
357                 void         set_latency_range (PortHandle, bool for_playback, LatencyRange);
358                 LatencyRange get_latency_range (PortHandle, bool for_playback);
359
360                 /* Discovering physical ports */
361
362                 bool      port_is_physical (PortHandle) const;
363                 void      get_physical_outputs (DataType type, std::vector<std::string>&);
364                 void      get_physical_inputs (DataType type, std::vector<std::string>&);
365                 ChanCount n_physical_outputs () const;
366                 ChanCount n_physical_inputs () const;
367
368                 /* Getting access to the data buffer for a port */
369
370                 void* get_buffer (PortHandle, pframes_t);
371
372                 void* main_process_thread ();
373
374                 static size_t max_buffer_size() {return _max_buffer_size;}
375
376         private:
377                 enum MidiPortMode {
378                         MidiNoEvents,
379                         MidiGenerator,
380                         MidiLoopback,
381                         MidiToAudio,
382                 };
383
384                 struct DriverSpeed {
385                         std::string name;
386                         float speedup;
387                         DriverSpeed (const std::string& n, float s) : name (n), speedup (s) {}
388                 };
389
390                 std::string _instance_name;
391                 static std::vector<std::string> _midi_options;
392                 static std::vector<AudioBackend::DeviceStatus> _device_status;
393                 static std::vector<DummyAudioBackend::DriverSpeed> _driver_speed;
394
395                 bool  _running;
396                 bool  _freewheel;
397                 bool  _freewheeling;
398                 float _speedup;
399
400                 std::string _device;
401
402                 float  _samplerate;
403                 size_t _samples_per_period;
404                 float  _dsp_load;
405                 DSPLoadCalculator _dsp_load_calc;
406                 static size_t _max_buffer_size;
407
408                 uint32_t _n_inputs;
409                 uint32_t _n_outputs;
410
411                 uint32_t _n_midi_inputs;
412                 uint32_t _n_midi_outputs;
413                 MidiPortMode _midi_mode;
414
415                 uint32_t _systemic_input_latency;
416                 uint32_t _systemic_output_latency;
417
418                 framecnt_t _processed_samples;
419
420                 pthread_t _main_thread;
421
422                 /* process threads */
423                 static void* dummy_process_thread (void *);
424                 std::vector<pthread_t> _threads;
425
426                 struct ThreadData {
427                         DummyAudioBackend* engine;
428                         boost::function<void ()> f;
429                         size_t stacksize;
430
431                         ThreadData (DummyAudioBackend* e, boost::function<void ()> fp, size_t stacksz)
432                                 : engine (e) , f (fp) , stacksize (stacksz) {}
433                 };
434
435                 /* port engine */
436                 PortHandle add_port (const std::string& shortname, ARDOUR::DataType, ARDOUR::PortFlags);
437                 int register_system_ports ();
438                 void unregister_ports (bool system_only = false);
439
440                 std::vector<DummyAudioPort *> _system_inputs;
441                 std::vector<DummyAudioPort *> _system_outputs;
442                 std::vector<DummyMidiPort *> _system_midi_in;
443                 std::vector<DummyMidiPort *> _system_midi_out;
444                 std::vector<DummyPort *> _ports;
445
446                 struct PortConnectData {
447                         std::string a;
448                         std::string b;
449                         bool c;
450
451                         PortConnectData (const std::string& a, const std::string& b, bool c)
452                                 : a (a) , b (b) , c (c) {}
453                 };
454
455                 std::vector<PortConnectData *> _port_connection_queue;
456                 pthread_mutex_t _port_callback_mutex;
457                 bool _port_change_flag;
458
459                 void port_connect_callback (const std::string& a, const std::string& b, bool conn) {
460                         pthread_mutex_lock (&_port_callback_mutex);
461                         _port_connection_queue.push_back(new PortConnectData(a, b, conn));
462                         pthread_mutex_unlock (&_port_callback_mutex);
463                 }
464
465                 void port_connect_add_remove_callback () {
466                         pthread_mutex_lock (&_port_callback_mutex);
467                         _port_change_flag = true;
468                         pthread_mutex_unlock (&_port_callback_mutex);
469                 }
470
471                 bool valid_port (PortHandle port) const {
472                         return std::find (_ports.begin (), _ports.end (), (DummyPort*)port) != _ports.end ();
473                 }
474
475                 DummyPort * find_port (const std::string& port_name) const {
476                         for (std::vector<DummyPort*>::const_iterator it = _ports.begin (); it != _ports.end (); ++it) {
477                                 if ((*it)->name () == port_name) {
478                                         return *it;
479                                 }
480                         }
481                         return NULL;
482                 }
483
484 }; // class DummyAudioBackend
485
486 } // namespace
487
488 #endif /* __libbackend_dummy_audiobackend_h__ */