Ardour  8.12
rt_midibuffer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2016 David Robillard <d@drobilla.net>
3  * Copyright (C) 2007-2018 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2009-2010 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2009 Hans Baier <hansfbaier@googlemail.com>
6  * Copyright (C) 2014-2016 Robin Gareus <robin@gareus.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #ifndef __ardour_rt_midi_buffer_h__
24 #define __ardour_rt_midi_buffer_h__
25 
26 #include <map>
27 
28 #include <glibmm/threads.h>
29 
30 #include "evoral/Event.h"
31 #include "evoral/EventSink.h"
32 #include "evoral/midi_util.h"
33 
34 #include "ardour/types.h"
35 
36 namespace ARDOUR {
37 
38 class MidiBuffer;
39 class MidiNoteTracker;
40 class MidiStateTracker;
41 
43 class LIBARDOUR_API RTMidiBuffer : public Evoral::EventSink<samplepos_t>
44 {
45  private:
46  struct Blob {
47  uint32_t size;
48  uint8_t data[0];
49  };
50 
51  public:
53 
56 
57  void clear();
58  void resize(size_t);
59  size_t size() const { return _size; }
60  bool empty() const { return _size == 0; }
61 
62  samplecnt_t span() const;
63 
64  uint32_t write (TimeType time, Evoral::EventType type, uint32_t size, const uint8_t* buf);
65  uint32_t read (MidiBuffer& dst, samplepos_t start, samplepos_t end, MidiNoteTracker& tracker, samplecnt_t offset = 0);
67 
68  void dump (uint32_t);
69  void reverse ();
70  bool reversed() const;
71 
72  struct Item {
74  union {
75  uint8_t bytes[4];
76  uint32_t offset;
77  };
78  };
79 
80  Item const & operator[](size_t n) const {
81  if (n >= _size) {
82  throw std::exception ();
83  }
84  return _data[n];
85  }
86 
87  uint8_t const * bytes (Item const & item, uint32_t& size) {
88  if (!item.bytes[0]) {
89  size = Evoral::midi_event_size (item.bytes[1]);
90  return &item.bytes[1];
91  } else {
92  uint32_t offset = item.offset & ~(1<<(CHAR_BIT-1));
93  Blob* blob = reinterpret_cast<Blob*> (&_pool[offset]);
94 
95  size = blob->size;
96  return blob->data;
97  }
98  }
99 
100  void shift (sampleoffset_t distance) {
101  if (_size == 0) {
102  return;
103  }
104  for (size_t n = 0; n < _size; ++n) {
105  _data[n].timestamp += distance;
106  }
107  }
108 
109  private:
110  friend struct WriteProtectRender;
111 
112  /* The main store. Holds Items (timestamp+up to 3 bytes of data OR
113  * offset into secondary storage below)
114  */
115 
116  size_t _size;
117  size_t _capacity;
119  bool _reversed;
120  /* secondary blob storage. Holds Blobs (arbitrary size + data) */
121 
122  uint32_t alloc_blob (uint32_t size);
123  uint32_t store_blob (uint32_t size, uint8_t const * data);
124  uint32_t _pool_size;
125  uint32_t _pool_capacity;
126  uint8_t* _pool;
127 
128  Glib::Threads::RWLock _lock;
129 
130  public:
132  public:
133  WriteProtectRender (RTMidiBuffer& rtm) : lm (rtm._lock, Glib::Threads::NOT_LOCK) {}
134  void acquire () { lm.acquire(); }
135 
136  private:
137  Glib::Threads::RWLock::WriterLock lm;
138  };
139 };
140 
141 } // namespace ARDOUR
142 
143 #endif // __ardour_rt_midi_buffer_h__
Glib::Threads::RWLock::WriterLock lm
bool reversed() const
uint32_t read(MidiBuffer &dst, samplepos_t start, samplepos_t end, MidiNoteTracker &tracker, samplecnt_t offset=0)
size_t size() const
Definition: rt_midibuffer.h:59
uint8_t const * bytes(Item const &item, uint32_t &size)
Definition: rt_midibuffer.h:87
uint32_t store_blob(uint32_t size, uint8_t const *data)
void resize(size_t)
void track(MidiStateTracker &, samplepos_t start, samplepos_t end)
samplecnt_t span() const
Glib::Threads::RWLock _lock
uint32_t alloc_blob(uint32_t size)
Item const & operator[](size_t n) const
Definition: rt_midibuffer.h:80
bool empty() const
Definition: rt_midibuffer.h:60
uint32_t write(TimeType time, Evoral::EventType type, uint32_t size, const uint8_t *buf)
void shift(sampleoffset_t distance)
samplepos_t TimeType
Definition: rt_midibuffer.h:52
void dump(uint32_t)
#define LIBARDOUR_API
PBD::PropertyDescriptor< timepos_t > start
Temporal::samplecnt_t samplecnt_t
Temporal::sampleoffset_t sampleoffset_t
Temporal::samplepos_t samplepos_t
static int midi_event_size(uint8_t status)
DebugBits Threads
minimum disk read bytes