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