Re-integrate export-optimization branch.
[ardour.git] / libs / audiographer / audiographer / interleaver.h
1 #ifndef AUDIOGRAPHER_INTERLEAVER_H
2 #define AUDIOGRAPHER_INTERLEAVER_H
3
4 #include "types.h"
5 #include "listed_source.h"
6 #include "sink.h"
7 #include "exception.h"
8
9 #include <vector>
10 #include <cmath>
11
12 namespace AudioGrapher
13 {
14
15 template<typename T>
16 class Interleaver : public ListedSource<T>
17 {
18   public: 
19         
20         Interleaver();
21         ~Interleaver() { reset(); }
22         
23         void init (unsigned int num_channels, nframes_t max_frames_per_channel);
24         typename Source<T>::SinkPtr input (unsigned int channel);
25         
26   private: 
27  
28         class Input : public Sink<T>
29         {
30           public:
31                 Input (Interleaver & parent, unsigned int channel)
32                   : frames_written (0), parent (parent), channel (channel) {}
33                 
34                 void process (ProcessContext<T> const & c)
35                 {
36                         if (c.channels() > 1) { throw Exception (*this, "Data input has more than on channel"); }
37                         if (frames_written) { throw Exception (*this, "Input channels out of sync"); }
38                         frames_written = c.frames();
39                         parent.write_channel (c, channel);
40                 }
41                 
42                 using Sink<T>::process;
43                 
44                 nframes_t frames() { return frames_written; }
45                 void reset() { frames_written = 0; }
46                 
47           private:
48                 nframes_t frames_written;
49                 Interleaver & parent;
50                 unsigned int channel;
51         };
52           
53         void reset ();
54         void reset_channels ();
55         void write_channel (ProcessContext<T> const & c, unsigned int channel);
56         nframes_t ready_to_output();
57         void output();  
58
59         typedef boost::shared_ptr<Input> InputPtr;
60         std::vector<InputPtr> inputs;
61         
62         unsigned int channels;
63         nframes_t max_frames;
64         T * buffer;
65 };
66
67 #include "interleaver-inl.h"
68
69 } // namespace
70
71 #endif // AUDIOGRAPHER_INTERLEAVER_H