ardour
buffer_set.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 Paul Davis
3 
4  This program is free software; you can redistribute it and/or modify it
5  under the terms of the GNU General Public License as published by the Free
6  Software Foundation; either version 2 of the License, or (at your option)
7  any later version.
8 
9  This program is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12  for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 
19 #ifndef __ardour_buffer_set_h__
20 #define __ardour_buffer_set_h__
21 
22 #ifdef WAF_BUILD
23 #include "libardour-config.h"
24 #endif
25 
26 #include <cassert>
27 #include <vector>
28 #include "ardour/chan_count.h"
29 #include "ardour/data_type.h"
31 #include "ardour/types.h"
32 
33 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT
34 #include "evoral/MIDIEvent.hpp"
35 struct _VstEvents;
36 typedef struct _VstEvents VstEvents;
37 struct _VstMidiEvent;
38 typedef struct _VstMidiEvent VstMidiEvent;
39 #endif
40 
41 #ifdef LV2_SUPPORT
42 typedef struct LV2_Evbuf_Impl LV2_Evbuf;
43 #endif
44 
45 namespace ARDOUR {
46 
47 class Buffer;
48 class AudioBuffer;
49 class MidiBuffer;
50 class PortSet;
51 
66 {
67 public:
68  BufferSet();
69  ~BufferSet();
70 
71  void clear();
72 
73  void attach_buffers (PortSet& ports);
74  void get_backend_port_addresses (PortSet &, framecnt_t);
75 
76  /* the capacity here is a size_t and has a different interpretation depending
77  on the DataType of the buffers. for audio, its a frame count. for MIDI
78  its a byte count.
79  */
80 
81  void ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity);
82  void ensure_buffers(const ChanCount& chns, size_t buffer_capacity);
83 
84  const ChanCount& available() const { return _available; }
85  ChanCount& available() { return _available; }
86 
87  const ChanCount& count() const { return _count; }
88  ChanCount& count() { return _count; }
89 
90  void silence (framecnt_t nframes, framecnt_t offset);
91  bool is_mirror() const { return _is_mirror; }
92 
93  void set_count(const ChanCount& count) { assert(count <= _available); _count = count; }
94 
95  size_t buffer_capacity(DataType type) const;
96 
97  Buffer& get(DataType type, size_t i);
98  const Buffer& get(DataType type, size_t i) const;
99 
100  AudioBuffer& get_audio(size_t i) {
101  return (AudioBuffer&)get(DataType::AUDIO, i);
102  }
103  const AudioBuffer& get_audio(size_t i) const {
104  return (const AudioBuffer&)get(DataType::AUDIO, i);
105  }
106 
107  MidiBuffer& get_midi(size_t i) {
108  return (MidiBuffer&)get(DataType::MIDI, i);
109  }
110  const MidiBuffer& get_midi(size_t i) const {
111  return (const MidiBuffer&)get(DataType::MIDI, i);
112  }
113 
114 #ifdef LV2_SUPPORT
115 
121  LV2_Evbuf* get_lv2_midi(bool input, size_t i, bool old_api);
122 
124  void ensure_lv2_bufsize(bool input, size_t i, size_t buffer_capacity);
125 
127  void flush_lv2_midi(bool input, size_t i);
128 
130  void forward_lv2_midi(LV2_Evbuf*, size_t, bool purge_ardour_buffer = true);
131 #endif
132 
133 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT
134  VstEvents* get_vst_midi (size_t);
135 #endif
136 
137  void read_from(const BufferSet& in, framecnt_t nframes);
138  void read_from(const BufferSet& in, framecnt_t nframes, DataType);
139  void merge_from(const BufferSet& in, framecnt_t nframes);
140 
141  template <typename BS, typename B>
143  public:
144  B& operator*() { return (B&)_set.get(_type, _index); }
145  B* operator->() { return &(B&)_set.get(_type, _index); }
146  iterator_base<BS,B>& operator++() { ++_index; return *this; } // yes, prefix only
147  bool operator==(const iterator_base<BS,B>& other) { return (_index == other._index); }
148  bool operator!=(const iterator_base<BS,B>& other) { return (_index != other._index); }
150  _set = other._set; _type = other._type; _index = other._index; return *this;
151  }
152 
153  private:
154  friend class BufferSet;
155 
156  iterator_base(BS& list, DataType type, size_t index)
157  : _set(list), _type(type), _index(index) {}
158 
159  BS& _set;
161  size_t _index;
162  };
163 
165  iterator begin(DataType type) { return iterator(*this, type, 0); }
166  iterator end(DataType type) { return iterator(*this, type, _count.get(type)); }
167 
169  const_iterator begin(DataType type) const { return const_iterator(*this, type, 0); }
170  const_iterator end(DataType type) const { return const_iterator(*this, type, _count.get(type)); }
171 
173  audio_iterator audio_begin() { return audio_iterator(*this, DataType::AUDIO, 0); }
174  audio_iterator audio_end() { return audio_iterator(*this, DataType::AUDIO, _count.n_audio()); }
175 
177  midi_iterator midi_begin() { return midi_iterator(*this, DataType::MIDI, 0); }
178  midi_iterator midi_end() { return midi_iterator(*this, DataType::MIDI, _count.n_midi()); }
179 
180 private:
181  typedef std::vector<Buffer*> BufferVec;
182 
184  std::vector<BufferVec> _buffers;
185 
186 #ifdef LV2_SUPPORT
187  typedef std::vector< std::pair<bool, LV2_Evbuf*> > LV2Buffers;
189  LV2Buffers _lv2_buffers;
190 #endif
191 
192 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT
193  class VSTBuffer {
194  public:
195  VSTBuffer (size_t);
196  ~VSTBuffer ();
197 
198  void clear ();
199  void push_back (Evoral::MIDIEvent<framepos_t> const &);
200  VstEvents* events () const {
201  return _events;
202  }
203 
204  private:
205  /* prevent copy construction */
206  VSTBuffer (VSTBuffer const &);
207 
208  VstEvents* _events;
209  VstMidiEvent* _midi_events;
210  size_t _capacity;
211  };
212 
213  typedef std::vector<VSTBuffer*> VSTBuffers;
214  VSTBuffers _vst_buffers;
215 #endif
216 
219 
222 
225 };
226 
227 
228 } // namespace ARDOUR
229 
230 #endif // __ardour_buffer_set_h__
bool operator!=(const iterator_base< BS, B > &other)
Definition: buffer_set.h:148
const MidiBuffer & get_midi(size_t i) const
Definition: buffer_set.h:110
MidiBuffer & get_midi(size_t i)
Definition: buffer_set.h:107
ChanCount _count
Use counts (there may be more actual buffers than this)
Definition: buffer_set.h:218
bool _is_mirror
False if we 'own' the contained buffers, if true we mirror a PortSet)
Definition: buffer_set.h:224
void set_count(const ChanCount &count)
Definition: buffer_set.h:93
bool is_mirror() const
Definition: buffer_set.h:91
std::vector< Buffer * > BufferVec
Definition: buffer_set.h:181
iterator_base< const BufferSet, const Buffer > const_iterator
Definition: buffer_set.h:168
iterator_base< BufferSet, MidiBuffer > midi_iterator
Definition: buffer_set.h:176
AudioBuffer & get_audio(size_t i)
Definition: buffer_set.h:100
int64_t framecnt_t
Definition: types.h:76
midi_iterator midi_begin()
Definition: buffer_set.h:177
ChanCount & count()
Definition: buffer_set.h:88
iterator begin(DataType type)
Definition: buffer_set.h:165
iterator end(DataType type)
Definition: buffer_set.h:166
const_iterator end(DataType type) const
Definition: buffer_set.h:170
audio_iterator audio_begin()
Definition: buffer_set.h:173
std::vector< BufferVec > _buffers
Vector of vectors, indexed by DataType.
Definition: buffer_set.h:184
Definition: amp.h:29
ChanCount & available()
Definition: buffer_set.h:85
audio_iterator audio_end()
Definition: buffer_set.h:174
iterator_base< BS, B > & operator++()
Definition: buffer_set.h:146
iterator_base< BufferSet, AudioBuffer > audio_iterator
Definition: buffer_set.h:172
#define LIBARDOUR_API
const AudioBuffer & get_audio(size_t i) const
Definition: buffer_set.h:103
midi_iterator midi_end()
Definition: buffer_set.h:178
const ChanCount & count() const
Definition: buffer_set.h:87
const ChanCount & available() const
Definition: buffer_set.h:84
iterator_base< BufferSet, Buffer > iterator
Definition: buffer_set.h:164
iterator_base< BS, B > operator=(const iterator_base< BS, B > &other)
Definition: buffer_set.h:149
bool operator==(const iterator_base< BS, B > &other)
Definition: buffer_set.h:147
const_iterator begin(DataType type) const
Definition: buffer_set.h:169
ChanCount _available
Available counts (number of buffers actually allocated)
Definition: buffer_set.h:221
iterator_base(BS &list, DataType type, size_t index)
Definition: buffer_set.h:156