use primary, not secondary, clock mode to drive other clock modes, and dynamically...
[ardour.git] / libs / backends / alsa / alsa_rawmidi.h
index 554012e66af3a40f301ad378f93d5a10c03d9a05..654f084c4e465d31a73a54bdcf7818d12c954dc1 100644 (file)
 
 #include "pbd/ringbuffer.h"
 #include "ardour/types.h"
+#include "alsa_midi.h"
 
 namespace ARDOUR {
 
-class AlsaRawMidiIO {
+class AlsaRawMidiIO : virtual public AlsaMidiIO {
 public:
-       AlsaRawMidiIO (const char *device, const bool input);
+       AlsaRawMidiIO (const std::string &name, const char *device, const bool input);
        virtual ~AlsaRawMidiIO ();
 
-       int state (void) const { return _state; }
-       int start ();
-       int stop ();
-
-       void setup_timing (const size_t samples_per_period, const float samplerate);
-       void sync_time(uint64_t);
-
-       virtual void* main_process_thread () = 0;
-
 protected:
-       pthread_t _main_thread;
-       pthread_mutex_t _notify_mutex;
-       pthread_cond_t _notify_ready;
-
-       int  _state;
-       bool  _running;
-
        snd_rawmidi_t *_device;
-       int _npfds;
-       struct pollfd *_pfds;
-
-       double _sample_length_us;
-       double _period_length_us;
-       size_t _samples_per_period;
-       uint64_t _clock_monotonic;
-
-       struct MidiEventHeader {
-               uint64_t time;
-               size_t size;
-               MidiEventHeader(const uint64_t t, const size_t s)
-                       : time(t)
-                       , size(s) {}
-       };
-
-       RingBuffer<uint8_t>* _rb;
 
 private:
        void init (const char *device_name, const bool input);
-
 };
 
-class AlsaRawMidiOut : public AlsaRawMidiIO
+class AlsaRawMidiOut : public AlsaRawMidiIO, public AlsaMidiOut
 {
 public:
-       AlsaRawMidiOut (const char *device);
-
+       AlsaRawMidiOut (const std::string &name, const char *device);
        void* main_process_thread ();
-       int send_event (const pframes_t, const uint8_t *, const size_t);
 };
 
-class AlsaRawMidiIn : public AlsaRawMidiIO
+class AlsaRawMidiIn : public AlsaRawMidiIO, public AlsaMidiIn
 {
 public:
-       AlsaRawMidiIn (const char *device);
+       AlsaRawMidiIn (const std::string &name, const char *device);
 
        void* main_process_thread ();
 
-       size_t recv_event (pframes_t &, uint8_t *, size_t &);
+protected:
+       int queue_event (const uint64_t, const uint8_t *, const size_t);
+private:
+       void parse_events (const uint64_t, const uint8_t *, const size_t);
+       bool process_byte (const uint64_t, const uint8_t);
+
+       void record_byte(uint8_t byte) {
+               if (_total_bytes < sizeof(_parser_buffer)) {
+                       _parser_buffer[_total_bytes] = byte;
+               } else {
+                       ++_unbuffered_bytes;
+               }
+               ++_total_bytes;
+       }
+
+       void prepare_byte_event(const uint64_t time, const uint8_t byte) {
+               _parser_buffer[0] = byte;
+               _event.prepare(time, 1);
+       }
+
+       bool prepare_buffered_event(const uint64_t time) {
+               const bool result = _unbuffered_bytes == 0;
+               if (result) {
+                       _event.prepare(time, _total_bytes);
+               }
+               _total_bytes = 0;
+               _unbuffered_bytes = 0;
+               if (_status_byte >= 0xf0) {
+                       _expected_bytes = 0;
+                       _status_byte = 0;
+               }
+               return result;
+       }
+
+       struct ParserEvent {
+               uint64_t _time;
+               size_t _size;
+               bool _pending;
+               ParserEvent (const uint64_t time, const size_t size)
+                       : _time(time)
+                       , _size(size)
+                       , _pending(false) {}
+
+               void prepare(const uint64_t time, const size_t size) {
+                       _time = time;
+                       _size = size;
+                       _pending = true;
+               }
+       } _event;
+
+       bool    _first_time;
+       size_t  _unbuffered_bytes;
+       size_t  _total_bytes;
+       size_t  _expected_bytes;
+       uint8_t _status_byte;
+       uint8_t _parser_buffer[1024];
 };
 
 } // namespace