Ardour  9.0-pre0-582-g084a23a80d
process_context.h
Go to the documentation of this file.
1 #ifndef AUDIOGRAPHER_PROCESS_CONTEXT_H
2 #define AUDIOGRAPHER_PROCESS_CONTEXT_H
3 
4 #include "pbd/compose.h"
5 
7 #include "exception.h"
8 #include "debug_utils.h"
9 #include "types.h"
10 #include "flag_field.h"
11 #include "throwing.h"
12 #include "type_utils.h"
13 
14 namespace AudioGrapher
15 {
16 
17 
22 template <typename T = DefaultSampleType>
23 class /*LIBAUDIOGRAPHER_API*/ ProcessContext
24  : public Throwing<>
25 {
26  // Support older compilers that don't support template base class initialization without template parameters
27  // This will need to be modified if if it's modified above
29 
30  static_assert (std::is_trivially_destructible<T>::value);
31 
32 public:
33 
35 
36  enum Flags {
37  EndOfInput = 0
38  };
39 
40 public:
41 
45  { validate_data(); }
46 
49  : Throwing<throwLevel>(), _data (other._data), _samples (other._samples), _channels (other._channels), _flags (other._flags)
50  { /* No need to validate data */ }
51 
53  template<typename Y>
56  { validate_data(); }
57 
59  template<typename Y>
61  : Throwing<throwLevel>(), _data (data), _samples (samples), _channels (other.channels()), _flags (other.flags())
62  { validate_data(); }
63 
65  template<typename Y>
66  ProcessContext (ProcessContext<Y> const & other, T * data)
67  : Throwing<throwLevel>(), _data (data), _samples (other.samples()), _channels (other.channels()), _flags (other.flags())
68  { /* No need to validate data */ }
69 
72  {
74  throw Exception (*this, string_compose ("Trying to use too many samples of %1 for a new Context: %2 instead of %3" ,DebugUtils::demangled_name (*this), samples, _samples));
75  }
76  validate_data ();
77 
78  return ProcessContext (*this, _data, samples);
79  }
80 
81  virtual ~ProcessContext () {}
82 
84  inline T const * data() const { return _data; }
85  inline T * data() { return _data; }
86 
88  inline samplecnt_t const & samples() const { return _samples; }
89 
93  inline ChannelCount const & channels() const { return _channels; }
94 
96  inline samplecnt_t samples_per_channel() const { return _samples / _channels; }
97 
98  /* Flags */
99 
100  inline bool has_flag (Flag flag) const { return _flags.has (flag); }
101  inline void set_flag (Flag flag) const { _flags.set (flag); }
102  inline void remove_flag (Flag flag) const { _flags.remove (flag); }
103  inline FlagField const & flags () const { return _flags; }
104 
105 protected:
106  T * const _data;
109 
110  mutable FlagField _flags;
111 
112  private:
113  inline void validate_data()
114  {
115  if (throw_level (ThrowProcess) && (_samples % _channels != 0)) {
116  throw Exception (*this, string_compose ("Number of samples given to %1% was not a multiple of channels: %2 samples with %3 channels", DebugUtils::demangled_name (*this), _samples, _channels));
117  }
118  }
119 };
120 
122 template <typename T = DefaultSampleType>
123 class /*LIBAUDIOGRAPHER_API*/ AllocatingProcessContext : public ProcessContext<T>
124 {
125 public:
128  : ProcessContext<T> (new T[samples], samples, channels) {}
129 
132  : ProcessContext<T> (new T[samples], samples, channels)
134 
137  : ProcessContext<T> (other, new T[other._samples])
139 
141  template<typename Y>
143  : ProcessContext<T> (other, new T[samples], samples, channels) {}
144 
146  template<typename Y>
148  : ProcessContext<T> (other, new T[samples], samples, other.channels()) {}
149 
151  template<typename Y>
153  : ProcessContext<T> (other, new T[other._samples]) {}
154 
156 };
157 
159 template <typename T = DefaultSampleType>
160 class /*LIBAUDIOGRAPHER_API*/ ConstProcessContext
161 {
162  public:
164  ConstProcessContext (T const * data, samplecnt_t samples, ChannelCount channels)
165  : context (const_cast<T *>(data), samples, channels) {}
166 
169  : context (const_cast<ProcessContext<T> &> (other)) {}
170 
172  template<typename ProcessContext>
173  ConstProcessContext (ProcessContext const & other, T const * data, samplecnt_t samples, ChannelCount channels)
174  : context (other, const_cast<T *>(data), samples, channels) {}
175 
177  template<typename ProcessContext>
178  ConstProcessContext (ProcessContext const & other, T const * data, samplecnt_t samples)
179  : context (other, const_cast<T *>(data), samples) {}
180 
182  template<typename ProcessContext>
183  ConstProcessContext (ProcessContext const & other, T const * data)
184  : context (other, const_cast<T *>(data)) {}
185 
186  inline operator ProcessContext<T> const & () { return context; }
187  inline ProcessContext<T> const & operator() () { return context; }
188  inline ProcessContext<T> const * operator& () { return &context; }
189 
190  private:
192 };
193 
194 } // namespace
195 
196 #endif // AUDIOGRAPHER_PROCESS_CONTEXT_H
A process context that allocates and owns it's data buffer.
AllocatingProcessContext(ProcessContext< Y > const &other, samplecnt_t samples)
"Copy constructor" with uninitialized data, unique sample count, but copies channel count and flags
AllocatingProcessContext(ProcessContext< Y > const &other)
"Copy constructor" uninitialized data, that copies sample and channel count + flags
AllocatingProcessContext(ProcessContext< Y > const &other, samplecnt_t samples, ChannelCount channels)
"Copy constructor" with uninitialized data, unique sample and channel count, but copies flags
AllocatingProcessContext(ProcessContext< T > const &other)
Copy constructor, copies data from other ProcessContext.
AllocatingProcessContext(samplecnt_t samples, ChannelCount channels)
Allocates uninitialized memory.
AllocatingProcessContext(T const *data, samplecnt_t samples, ChannelCount channels)
Allocates and copies data from raw buffer.
A wrapper for a const ProcesContext which can be created from const data.
ConstProcessContext(ProcessContext const &other, T const *data, samplecnt_t samples)
"Copy constructor", with unique data and sample count, but copies channel count and flags
ConstProcessContext(ProcessContext const &other, T const *data)
"Copy constructor", with unique data, but copies sample and channel count + flags
ConstProcessContext(ProcessContext const &other, T const *data, samplecnt_t samples, ChannelCount channels)
"Copy constructor", with unique data, sample and channel count, but copies flags
ProcessContext< T > const context
ProcessContext< T > const & operator()()
ConstProcessContext(T const *data, samplecnt_t samples, ChannelCount channels)
Basic constructor with data, sample and channel count.
ConstProcessContext(ProcessContext< T > const &other)
Copy constructor from const ProcessContext.
ProcessContext< T > const * operator&()
void set(Flag flag)
Definition: flag_field.h:76
bool has(Flag flag) const
Definition: flag_field.h:73
void remove(Flag flag)
Definition: flag_field.h:77
ChannelCount const & channels() const
bool has_flag(Flag flag) const
T const * data() const
data points to the array of data to process
ProcessContext beginning(samplecnt_t samples)
Make new Context out of the beginning of this context.
ProcessContext(T *data, samplecnt_t samples, ChannelCount channels)
Basic constructor with data, sample and channel count.
ProcessContext(ProcessContext< Y > const &other, T *data)
"Copy constructor" with unique data, but copies sample and channel count + flags
static const ThrowLevel throwLevel
samplecnt_t samples_per_channel() const
Returns the amount of samples per channel.
ProcessContext(ProcessContext< T > const &other)
Normal copy constructor.
FlagField const & flags() const
samplecnt_t const & samples() const
samples tells how many samples the array pointed by data contains
void set_flag(Flag flag) const
ProcessContext(ProcessContext< Y > const &other, T *data, samplecnt_t samples)
"Copy constructor" with unique data and sample count, but copies channel count and flags
void remove_flag(Flag flag) const
ProcessContext(ProcessContext< Y > const &other, T *data, samplecnt_t samples, ChannelCount channels)
"Copy constructor" with unique data, sample and channel count, but copies flags
bool throw_level(ThrowLevel level)
Definition: throwing.h:47
static void copy(T const *source, T *destination, samplecnt_t samples)
Definition: type_utils.h:49
std::string string_compose(const std::string &fmt, const T1 &o1)
Definition: compose.h:246
@ ThrowProcess
Process cycle level stuff.
Definition: throwing.h:23
static std::string demangled_name(T const &obj)
Returns the demangled name of the object passed as the parameter.
Definition: debug_utils.h:24
#define DEFAULT_THROW_LEVEL
Definition: throwing.h:5