Ardour  9.0-pre0-582-g084a23a80d
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 #pragma once
24 
25 #include <map>
26 
27 #include <glibmm/threads.h>
28 
29 #include "evoral/Event.h"
30 #include "evoral/EventSink.h"
31 #include "evoral/midi_util.h"
32 
33 #include "ardour/types.h"
34 
35 namespace ARDOUR {
36 
37 class MidiBuffer;
38 class MidiNoteTracker;
39 class MidiStateTracker;
40 
42 template<typename TimeType, typename DistanceType>
44 {
45  private:
46  struct Blob {
47  uint32_t size;
48  uint8_t data[0];
49  };
50 
51  public:
54 
55  /* After calling convert(), this RTMidiBufferBase no longer owns or has
56  a reference to any data. The data is all "moved" to the returned
57  RTMidiBufferBase and timestamps modified to its time domain if nececssary.
58  */
60 
61  void clear();
62  void resize(size_t);
63  size_t size() const { return _size; }
64  bool empty() const { return _size == 0; }
65 
66  DistanceType span() const;
67 
68  uint32_t write (TimeType time, Evoral::EventType type, uint32_t size, const uint8_t* buf);
69  uint32_t read (MidiBuffer& dst, TimeType start, TimeType end, MidiNoteTracker& tracker, DistanceType offset = 0);
70  void track (MidiStateTracker&, TimeType start, TimeType end);
71 
72  void dump (uint32_t);
73  void reverse ();
74  bool reversed() const;
75 
76  struct Item {
77  TimeType timestamp;
78  union {
79  uint8_t bytes[4];
80  uint32_t offset;
81  };
82  };
83 
84  Item const & operator[](size_t n) const {
85  if (n >= _size) {
86  throw std::exception ();
87  }
88  return _data[n];
89  }
90 
91  uint8_t const * bytes (Item const & item, uint32_t& size) const {
92  if (!item.bytes[0]) {
93  size = Evoral::midi_event_size (item.bytes[1]);
94  return &item.bytes[1];
95  } else {
96  uint32_t offset = item.offset & ~(1<<(CHAR_BIT-1));
97  Blob* blob = reinterpret_cast<Blob*> (&_pool[offset]);
98 
99  size = blob->size;
100  return blob->data;
101  }
102  }
103 
104  /* XXX this really requires a 3rd template argument for a potentially
105  * negative offset
106  */
107 
108  void shift (DistanceType distance) {
109  for (size_t n = 0; n < _size; ++n) {
110  _data[n].timestamp += distance;
111  }
112  }
113 
114  void track_state (TimeType when, MidiStateTracker& mst) const;
115 
116  private:
117  friend struct WriteProtectRender;
118  /* any cousin of ours is a friend */
119  template<typename T, typename D> friend class RTMidiBufferBase;
120 
121  /* The main store. Holds Items (timestamp+up to 3 bytes of data OR
122  * offset into secondary storage below)
123  */
124 
125  size_t _size;
126  size_t _capacity;
128  bool _reversed;
129  /* secondary blob storage. Holds Blobs (arbitrary size + data) */
130 
131  uint32_t alloc_blob (uint32_t size);
132  uint32_t store_blob (uint32_t size, uint8_t const * data);
133  uint32_t _pool_size;
134  uint32_t _pool_capacity;
135  uint8_t* _pool;
136 
137  Glib::Threads::RWLock _lock;
138 
139  public:
141  public:
142  WriteProtectRender (RTMidiBufferBase& rtm) : lm (rtm._lock, Glib::Threads::NOT_LOCK) {}
143  void acquire () { lm.acquire(); }
144 
145  private:
146  Glib::Threads::RWLock::WriterLock lm;
147  };
148 };
149 
151 
152 } // namespace ARDOUR
153 
Glib::Threads::RWLock::WriterLock lm
void track(MidiStateTracker &, TimeType start, TimeType end)
void convert(RTMidiBufferBase< Temporal::Beats, Temporal::Beats > &)
void track_state(TimeType when, MidiStateTracker &mst) const
DistanceType span() const
Item const & operator[](size_t n) const
Definition: rt_midibuffer.h:84
uint32_t read(MidiBuffer &dst, TimeType start, TimeType end, MidiNoteTracker &tracker, DistanceType offset=0)
uint32_t write(TimeType time, Evoral::EventType type, uint32_t size, const uint8_t *buf)
uint32_t alloc_blob(uint32_t size)
void shift(DistanceType distance)
uint32_t store_blob(uint32_t size, uint8_t const *data)
uint8_t const * bytes(Item const &item, uint32_t &size) const
Definition: rt_midibuffer.h:91
Glib::Threads::RWLock _lock
#define LIBARDOUR_API
PBD::PropertyDescriptor< timepos_t > start
RTMidiBufferBase< samplepos_t, samplecnt_t > RTMidiBuffer
static int midi_event_size(uint8_t status)
DebugBits Threads
minimum disk read bytes