1 #ifndef AUDIOGRAPHER_CHUNKER_H
2 #define AUDIOGRAPHER_CHUNKER_H
4 #include "audiographer/flag_debuggable.h"
5 #include "audiographer/sink.h"
6 #include "audiographer/type_utils.h"
7 #include "audiographer/utils/listed_source.h"
12 /// A class that chunks process cycles into equal sized frames
13 template<typename T = DefaultSampleType>
15 : public ListedSource<T>
17 , public FlagDebuggable<>
20 /** Constructs a new Chunker with a constant chunk size.
23 Chunker (framecnt_t chunk_size)
24 : chunk_size (chunk_size)
27 buffer = new T[chunk_size];
28 add_supported_flag (ProcessContext<T>::EndOfInput);
36 /** Outputs data in \a context in chunks with the size specified in the constructor.
37 * Note that some calls might not produce any output, while others may produce several.
40 void process (ProcessContext<T> const & context)
42 check_flags (*this, context);
44 framecnt_t frames_left = context.frames();
45 framecnt_t input_position = 0;
47 while (position + frames_left >= chunk_size) {
48 // Copy from context to buffer
49 framecnt_t const frames_to_copy = chunk_size - position;
50 TypeUtils<T>::copy (&context.data()[input_position], &buffer[position], frames_to_copy);
52 // Output whole buffer
53 ProcessContext<T> c_out (context, buffer, chunk_size);
54 ListedSource<T>::output (c_out);
58 input_position += frames_to_copy;
59 frames_left -= frames_to_copy;
63 // Copy the rest of the data
64 TypeUtils<T>::copy (&context.data()[input_position], &buffer[position], frames_left);
65 position += frames_left;
68 if (context.has_flag (ProcessContext<T>::EndOfInput)) {
69 ProcessContext<T> c_out (context, buffer, position);
70 ListedSource<T>::output (c_out);
73 using Sink<T>::process;
76 framecnt_t chunk_size;
84 #endif // AUDIOGRAPHER_CHUNKER_H