change Audio backend sample time methods to use a 64 bit timeline
[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                         Loopback,
160                 };
161                 void setup_generator (GeneratorType const, float const);
162                 void fill_wavetable (const float* d, size_t n_samples) { assert(_wavetable != 0);  memcpy(_wavetable, d, n_samples * sizeof(float)); }
163                 void midi_to_wavetable (DummyMidiBuffer const * const src, size_t n_samples);
164
165         private:
166                 Sample _buffer[8192];
167
168                 // signal generator ('fake' physical inputs)
169                 void generate (const pframes_t n_samples);
170                 GeneratorType _gen_type;
171
172                 // generator buffers
173                 // pink-noise filters
174                 float _b0, _b1, _b2, _b3, _b4, _b5, _b6;
175                 // generated sinf() samples
176                 Sample * _wavetable;
177                 uint32_t _gen_period;
178                 uint32_t _gen_offset;
179                 uint32_t _gen_perio2;
180                 uint32_t _gen_count2;
181
182                 // gaussian noise generator
183                 float grandf ();
184                 bool _pass;
185                 float _rn1;
186
187 }; // class DummyAudioPort
188
189 class DummyMidiPort : public DummyPort {
190         public:
191                 DummyMidiPort (DummyAudioBackend &b, const std::string&, PortFlags);
192                 ~DummyMidiPort ();
193
194                 DataType type () const { return DataType::MIDI; };
195
196                 void* get_buffer (pframes_t nframes);
197                 const DummyMidiBuffer * const_buffer () const { return &_buffer; }
198
199                 void setup_generator (int, float const);
200                 void set_loopback (DummyMidiBuffer const * const src);
201
202         private:
203                 DummyMidiBuffer _buffer;
204                 DummyMidiBuffer _loopback;
205
206                 // midi event generator ('fake' physical inputs)
207                 void midi_generate (const pframes_t n_samples);
208                 float   _midi_seq_spb; // samples per beat
209                 int32_t _midi_seq_time;
210                 uint32_t _midi_seq_pos;
211                 DummyMidiData::MIDISequence const * _midi_seq_dat;
212 }; // class DummyMidiPort
213
214 class DummyAudioBackend : public AudioBackend {
215         friend class DummyPort;
216         public:
217                  DummyAudioBackend (AudioEngine& e, AudioBackendInfo& info);
218                 ~DummyAudioBackend ();
219
220                 bool is_running () const { return _running; }
221
222                 /* AUDIOBACKEND API */
223
224                 std::string name () const;
225                 bool is_realtime () const;
226
227                 std::vector<DeviceStatus> enumerate_devices () const;
228                 std::vector<float> available_sample_rates (const std::string& device) const;
229                 std::vector<uint32_t> available_buffer_sizes (const std::string& device) const;
230                 uint32_t available_input_channel_count (const std::string& device) const;
231                 uint32_t available_output_channel_count (const std::string& device) const;
232
233                 bool can_change_sample_rate_when_running () const;
234                 bool can_change_buffer_size_when_running () const;
235
236                 int set_device_name (const std::string&);
237                 int set_sample_rate (float);
238                 int set_buffer_size (uint32_t);
239                 int set_interleaved (bool yn);
240                 int set_input_channels (uint32_t);
241                 int set_output_channels (uint32_t);
242                 int set_systemic_input_latency (uint32_t);
243                 int set_systemic_output_latency (uint32_t);
244                 int set_systemic_midi_input_latency (std::string const, uint32_t) { return 0; }
245                 int set_systemic_midi_output_latency (std::string const, uint32_t) { return 0; }
246
247                 int reset_device () { return 0; };
248
249                 /* Retrieving parameters */
250                 std::string  device_name () const;
251                 float        sample_rate () const;
252                 uint32_t     buffer_size () const;
253                 bool         interleaved () const;
254                 uint32_t     input_channels () const;
255                 uint32_t     output_channels () const;
256                 uint32_t     systemic_input_latency () const;
257                 uint32_t     systemic_output_latency () const;
258                 uint32_t     systemic_midi_input_latency (std::string const) const { return 0; }
259                 uint32_t     systemic_midi_output_latency (std::string const) const { return 0; }
260
261                 /* External control app */
262                 std::string control_app_name () const { return std::string (); }
263                 void launch_control_app () {}
264
265                 /* MIDI */
266                 std::vector<std::string> enumerate_midi_options () const;
267                 int set_midi_option (const std::string&);
268                 std::string midi_option () const;
269
270                 std::vector<DeviceStatus> enumerate_midi_devices () const {
271                         return std::vector<AudioBackend::DeviceStatus> ();
272                 }
273                 int set_midi_device_enabled (std::string const, bool) {
274                         return 0;
275                 }
276                 bool midi_device_enabled (std::string const) const {
277                         return true;
278                 }
279                 bool can_set_systemic_midi_latencies () const {
280                         return false;
281                 }
282
283                 /* State Control */
284         protected:
285                 int _start (bool for_latency_measurement);
286         public:
287                 int stop ();
288                 int freewheel (bool);
289                 float dsp_load () const;
290                 size_t raw_buffer_size (DataType t);
291
292                 /* Process time */
293                 framepos_t sample_time ();
294                 framepos_t sample_time_at_cycle_start ();
295                 pframes_t samples_since_cycle_start ();
296
297                 int create_process_thread (boost::function<void()> func);
298                 int join_process_threads ();
299                 bool in_process_thread ();
300                 uint32_t process_thread_count ();
301
302                 void update_latencies ();
303
304                 /* PORTENGINE API */
305
306                 void* private_handle () const;
307                 const std::string& my_name () const;
308                 bool available () const;
309                 uint32_t port_name_size () const;
310
311                 int         set_port_name (PortHandle, const std::string&);
312                 std::string get_port_name (PortHandle) const;
313                 PortHandle  get_port_by_name (const std::string&) const;
314
315                 int get_ports (const std::string& port_name_pattern, DataType type, PortFlags flags, std::vector<std::string>&) const;
316
317                 DataType port_data_type (PortHandle) const;
318
319                 PortHandle register_port (const std::string& shortname, ARDOUR::DataType, ARDOUR::PortFlags);
320                 void unregister_port (PortHandle);
321
322                 int  connect (const std::string& src, const std::string& dst);
323                 int  disconnect (const std::string& src, const std::string& dst);
324                 int  connect (PortHandle, const std::string&);
325                 int  disconnect (PortHandle, const std::string&);
326                 int  disconnect_all (PortHandle);
327
328                 bool connected (PortHandle, bool process_callback_safe);
329                 bool connected_to (PortHandle, const std::string&, bool process_callback_safe);
330                 bool physically_connected (PortHandle, bool process_callback_safe);
331                 int  get_connections (PortHandle, std::vector<std::string>&, bool process_callback_safe);
332
333                 /* MIDI */
334                 int midi_event_get (pframes_t& timestamp, size_t& size, uint8_t** buf, void* port_buffer, uint32_t event_index);
335                 int midi_event_put (void* port_buffer, pframes_t timestamp, const uint8_t* buffer, size_t size);
336                 uint32_t get_midi_event_count (void* port_buffer);
337                 void     midi_clear (void* port_buffer);
338
339                 /* Monitoring */
340
341                 bool can_monitor_input () const;
342                 int  request_input_monitoring (PortHandle, bool);
343                 int  ensure_input_monitoring (PortHandle, bool);
344                 bool monitoring_input (PortHandle);
345
346                 /* Latency management */
347
348                 void         set_latency_range (PortHandle, bool for_playback, LatencyRange);
349                 LatencyRange get_latency_range (PortHandle, bool for_playback);
350
351                 /* Discovering physical ports */
352
353                 bool      port_is_physical (PortHandle) const;
354                 void      get_physical_outputs (DataType type, std::vector<std::string>&);
355                 void      get_physical_inputs (DataType type, std::vector<std::string>&);
356                 ChanCount n_physical_outputs () const;
357                 ChanCount n_physical_inputs () const;
358
359                 /* Getting access to the data buffer for a port */
360
361                 void* get_buffer (PortHandle, pframes_t);
362
363                 void* main_process_thread ();
364
365                 static size_t max_buffer_size() {return _max_buffer_size;}
366
367         private:
368                 enum MidiPortMode {
369                         MidiNoEvents,
370                         MidiGenerator,
371                         MidiLoopback,
372                         MidiToAudio,
373                 };
374
375                 std::string _instance_name;
376                 static std::vector<std::string> _midi_options;
377                 static std::vector<AudioBackend::DeviceStatus> _device_status;
378
379                 bool  _running;
380                 bool  _freewheel;
381                 bool  _freewheeling;
382
383                 std::string _device;
384
385                 float  _samplerate;
386                 size_t _samples_per_period;
387                 float  _dsp_load;
388                 static size_t _max_buffer_size;
389
390                 uint32_t _n_inputs;
391                 uint32_t _n_outputs;
392
393                 uint32_t _n_midi_inputs;
394                 uint32_t _n_midi_outputs;
395                 MidiPortMode _midi_mode;
396
397                 uint32_t _systemic_input_latency;
398                 uint32_t _systemic_output_latency;
399
400                 framecnt_t _processed_samples;
401
402                 pthread_t _main_thread;
403
404                 /* process threads */
405                 static void* dummy_process_thread (void *);
406                 std::vector<pthread_t> _threads;
407
408                 struct ThreadData {
409                         DummyAudioBackend* engine;
410                         boost::function<void ()> f;
411                         size_t stacksize;
412
413                         ThreadData (DummyAudioBackend* e, boost::function<void ()> fp, size_t stacksz)
414                                 : engine (e) , f (fp) , stacksize (stacksz) {}
415                 };
416
417                 /* port engine */
418                 PortHandle add_port (const std::string& shortname, ARDOUR::DataType, ARDOUR::PortFlags);
419                 int register_system_ports ();
420                 void unregister_ports (bool system_only = false);
421
422                 std::vector<DummyAudioPort *> _system_inputs;
423                 std::vector<DummyAudioPort *> _system_outputs;
424                 std::vector<DummyMidiPort *> _system_midi_in;
425                 std::vector<DummyMidiPort *> _system_midi_out;
426                 std::vector<DummyPort *> _ports;
427
428                 struct PortConnectData {
429                         std::string a;
430                         std::string b;
431                         bool c;
432
433                         PortConnectData (const std::string& a, const std::string& b, bool c)
434                                 : a (a) , b (b) , c (c) {}
435                 };
436
437                 std::vector<PortConnectData *> _port_connection_queue;
438                 pthread_mutex_t _port_callback_mutex;
439                 bool _port_change_flag;
440
441                 void port_connect_callback (const std::string& a, const std::string& b, bool conn) {
442                         pthread_mutex_lock (&_port_callback_mutex);
443                         _port_connection_queue.push_back(new PortConnectData(a, b, conn));
444                         pthread_mutex_unlock (&_port_callback_mutex);
445                 }
446
447                 void port_connect_add_remove_callback () {
448                         pthread_mutex_lock (&_port_callback_mutex);
449                         _port_change_flag = true;
450                         pthread_mutex_unlock (&_port_callback_mutex);
451                 }
452
453                 bool valid_port (PortHandle port) const {
454                         return std::find (_ports.begin (), _ports.end (), (DummyPort*)port) != _ports.end ();
455                 }
456
457                 DummyPort * find_port (const std::string& port_name) const {
458                         for (std::vector<DummyPort*>::const_iterator it = _ports.begin (); it != _ports.end (); ++it) {
459                                 if ((*it)->name () == port_name) {
460                                         return *it;
461                                 }
462                         }
463                         return NULL;
464                 }
465
466 }; // class DummyAudioBackend
467
468 } // namespace
469
470 #endif /* __libbackend_dummy_audiobackend_h__ */