Re-integrate export-optimization branch.
[ardour.git] / libs / audiographer / audiographer / chunker.h
1 #ifndef AUDIOGRAPHER_CHUNKER_H
2 #define AUDIOGRAPHER_CHUNKER_H
3
4 #include "listed_source.h"
5 #include "sink.h"
6 #include <cstring>
7
8 namespace AudioGrapher
9 {
10
11 template<typename T>
12 class Chunker : public ListedSource<T>, public Sink<T>
13 {
14   public:
15         Chunker (nframes_t chunk_size)
16           : chunk_size (chunk_size)
17           , position (0)
18         {
19                 buffer = new T[chunk_size];
20         }
21         
22         ~Chunker()
23         {
24                 delete [] buffer;
25         }
26         
27         void process (ProcessContext<T> const & context)
28         {
29                 if (position + context.frames() < chunk_size) {
30                         memcpy (&buffer[position], (float const *)context.data(), context.frames() * sizeof(T));
31                         position += context.frames();
32                 } else {
33                         nframes_t const frames_to_copy = chunk_size - position;
34                         memcpy (&buffer[position], context.data(), frames_to_copy * sizeof(T));
35                         ProcessContext<T> c_out (context, buffer, chunk_size);
36                         ListedSource<T>::output (c_out);
37                         
38                         memcpy (buffer, &context.data()[frames_to_copy], (context.frames() - frames_to_copy) * sizeof(T));
39                         position =  context.frames() - frames_to_copy;
40                 }
41         }
42         using Sink<T>::process;
43         
44   private:
45         nframes_t chunk_size;
46         nframes_t position;
47         T * buffer;
48         
49 };
50
51 } // namespace
52
53 #endif // AUDIOGRAPHER_CHUNKER_H
54