globally change all use of "frame" to refer to audio into "sample".
[ardour.git] / libs / audiographer / audiographer / general / sr_converter.h
1 #ifndef AUDIOGRAPHER_SR_CONVERTER_H
2 #define AUDIOGRAPHER_SR_CONVERTER_H
3
4 #include <samplerate.h>
5
6 #include "audiographer/visibility.h"
7 #include "audiographer/flag_debuggable.h"
8 #include "audiographer/sink.h"
9 #include "audiographer/throwing.h"
10 #include "audiographer/types.h"
11 #include "audiographer/utils/listed_source.h"
12
13 namespace AudioGrapher
14 {
15
16 /// Samplerate converter
17 class LIBAUDIOGRAPHER_API SampleRateConverter
18   : public ListedSource<float>
19   , public Sink<float>
20   , public FlagDebuggable<>
21   , public Throwing<>
22 {
23   public:
24         /// Constructor. \n RT safe
25         SampleRateConverter (uint32_t channels);
26         ~SampleRateConverter ();
27
28         /// Init converter \n Not RT safe
29         void init (samplecnt_t in_rate, samplecnt_t out_rate, int quality = 0);
30
31         /// Returns max amount of samples that will be output \n RT safe
32         samplecnt_t allocate_buffers (samplecnt_t max_samples);
33
34         /** Does sample rate conversion.
35           * Note that outpt size may vary a lot.
36           * May or may not output several contexts of data.
37           * \n Should be RT safe.
38           * \TODO Check RT safety from libsamplerate
39           */
40         void process (ProcessContext<float> const & c);
41         using Sink<float>::process;
42
43   private:
44
45         void set_end_of_input (ProcessContext<float> const & c);
46         void reset ();
47
48         bool           active;
49         uint32_t       channels;
50         samplecnt_t     max_samples_in;
51
52         float *        leftover_data;
53         samplecnt_t     leftover_samples;
54         samplecnt_t     max_leftover_samples;
55
56         float *        data_out;
57         samplecnt_t     data_out_size;
58
59         SRC_DATA       src_data;
60         SRC_STATE*     src_state;
61 };
62
63 } // namespace
64
65 #endif // AUDIOGRAPHER_SR_CONVERTER_H