2 Interleaver<T>::Interleaver()
10 Interleaver<T>::init (unsigned int num_channels, nframes_t max_frames_per_channel)
13 channels = num_channels;
14 max_frames = max_frames_per_channel;
16 buffer = new T[channels * max_frames];
18 for (unsigned int i = 0; i < channels; ++i) {
19 inputs.push_back (InputPtr (new Input (*this, i)));
24 typename Source<T>::SinkPtr
25 Interleaver<T>::input (unsigned int channel)
27 if (channel >= channels) {
28 throw Exception (*this, "Channel out of range");
31 return boost::static_pointer_cast<Sink<T> > (inputs[channel]);
36 Interleaver<T>::reset_channels ()
38 for (unsigned int i = 0; i < channels; ++i) {
46 Interleaver<T>::reset ()
57 Interleaver<T>::write_channel (ProcessContext<T> const & c, unsigned int channel)
59 if (c.frames() > max_frames) {
61 throw Exception (*this, "Too many frames given to an input");
64 for (unsigned int i = 0; i < c.frames(); ++i) {
65 buffer[channel + (channels * i)] = c.data()[i];
68 nframes_t const ready_frames = ready_to_output();
70 ProcessContext<T> c_out (c, buffer, ready_frames, channels);
71 ListedSource<T>::output (c_out);
78 Interleaver<T>::ready_to_output ()
80 nframes_t ready_frames = inputs[0]->frames();
81 if (!ready_frames) { return 0; }
83 for (unsigned int i = 1; i < channels; ++i) {
84 nframes_t const frames = inputs[i]->frames();
85 if (!frames) { return 0; }
86 if (frames != ready_frames) {
87 init (channels, max_frames);
88 throw Exception (*this, "Frames count out of sync");
91 return ready_frames * channels;