Ardour  9.0-pre0-582-g084a23a80d
chunker.h
Go to the documentation of this file.
1 #ifndef AUDIOGRAPHER_CHUNKER_H
2 #define AUDIOGRAPHER_CHUNKER_H
3 
6 #include "audiographer/sink.h"
9 
10 namespace AudioGrapher
11 {
12 
14 template<typename T = DefaultSampleType>
15 class /*LIBAUDIOGRAPHER_API*/ Chunker
16  : public ListedSource<T>
17  , public Sink<T>
18  , public FlagDebuggable<>
19 {
20  public:
26  , position (0)
27  {
28  buffer = new T[chunk_size];
30  }
31 
33  {
34  delete [] buffer;
35  }
36 
41  void process (ProcessContext<T> const & context)
42  {
43  check_flags (*this, context);
44 
45  samplecnt_t samples_left = context.samples();
46  samplecnt_t input_position = 0;
47 
48  while (position + samples_left >= chunk_size) {
49  // Copy from context to buffer
50  samplecnt_t const samples_to_copy = chunk_size - position;
51  TypeUtils<T>::copy (&context.data()[input_position], &buffer[position], samples_to_copy);
52 
53  // Update counters
54  position = 0;
55  input_position += samples_to_copy;
56  samples_left -= samples_to_copy;
57 
58  // Output whole buffer
59  ProcessContext<T> c_out (context, buffer, chunk_size);
60  if (samples_left) { c_out.remove_flag(ProcessContext<T>::EndOfInput); }
62  }
63 
64  if (samples_left) {
65  // Copy the rest of the data
66  TypeUtils<T>::copy (&context.data()[input_position], &buffer[position], samples_left);
67  position += samples_left;
68  }
69 
70  if (context.has_flag (ProcessContext<T>::EndOfInput) && position > 0) {
71  ProcessContext<T> c_out (context, buffer, position);
73  }
74  }
75  using Sink<T>::process;
76 
77  private:
80  T * buffer;
81 
82 };
83 
84 } // namespace
85 
86 #endif // AUDIOGRAPHER_CHUNKER_H
87 
A class that chunks process cycles into equal sized samples.
Definition: chunker.h:19
void process(ProcessContext< T > const &context)
Definition: chunker.h:41
Chunker(samplecnt_t chunk_size)
Definition: chunker.h:24
samplecnt_t position
Definition: chunker.h:79
samplecnt_t chunk_size
Definition: chunker.h:78
A debugging class for nodes that support a certain set of flags.
void check_flags(SelfType &self, ProcessContext< ContextType > context)
Prints debug output if context contains flags that are not supported by this class.
void add_supported_flag(Flag flag)
Adds a flag to the set of flags supported.
An generic Source that uses a std::list for managing outputs.
Definition: listed_source.h:17
void output(ProcessContext< T > const &c)
Helper for derived classes.
Definition: listed_source.h:28
bool has_flag(Flag flag) const
T const * data() const
data points to the array of data to process
samplecnt_t const & samples() const
samples tells how many samples the array pointed by data contains
void remove_flag(Flag flag) const
static void copy(T const *source, T *destination, samplecnt_t samples)
Definition: type_utils.h:49