5733ebb4036c64b46372dd4e9a12d20de3b092c7
[ardour.git] / libs / ardour / ardour / export_utilities.h
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #ifndef __ardour_export_utilities_h__
22 #define __ardour_export_utilities_h__
23
24 #include <samplerate.h>
25
26 #include "ardour/graph.h"
27 #include "ardour/types.h"
28 #include "ardour/ardour.h"
29 #include "ardour/export_format_base.h"
30 #include "ardour/runtime_functions.h"
31
32 namespace ARDOUR
33 {
34
35 /* Processors */
36
37 /* Sample rate converter */
38
39 class SampleRateConverter : public GraphSinkVertex<float, float>
40 {
41   public:
42         SampleRateConverter (uint32_t channels, nframes_t in_rate, nframes_t out_rate, int quality);
43         ~SampleRateConverter ();
44
45   protected:
46         nframes_t process (float * data, nframes_t frames);
47
48   private:
49         bool           active;
50         uint32_t       channels;
51
52         nframes_t      leftover_frames;
53         nframes_t      max_leftover_frames;
54         nframes_t      frames_in;
55         nframes_t      frames_out;
56
57         float *        data_in;
58         float *        leftover_data;
59
60         float *        data_out;
61         nframes_t      data_out_size;
62
63         SRC_DATA       src_data;
64         SRC_STATE*     src_state;
65 };
66
67 /* Sample format converter */
68
69 template <typename TOut>
70 class SampleFormatConverter : public GraphSinkVertex<float, TOut>
71 {
72   public:
73         SampleFormatConverter (uint32_t channels, ExportFormatBase::DitherType type = ExportFormatBase::D_None, int data_width_ = 0);
74         ~SampleFormatConverter ();
75
76         void set_clip_floats (bool yn) { clip_floats = yn; }
77
78   protected:
79         nframes_t process (float * data, nframes_t frames);
80
81   private:
82         uint32_t     channels;
83         int          data_width;
84         GDither      dither;
85         nframes_t    data_out_size;
86         TOut *       data_out;
87
88         bool         clip_floats;
89
90 };
91
92 /* Peak reader */
93
94 class PeakReader : public GraphSinkVertex<float, float>
95 {
96   public:
97         PeakReader (uint32_t channels) : channels (channels), peak (0) {}
98         ~PeakReader () {}
99
100         float get_peak () { return peak; }
101
102   protected:
103         nframes_t process (float * data, nframes_t frames)
104         {
105                 peak = compute_peak (data, channels * frames, peak);
106                 return piped_to->write (data, frames);
107         }
108
109   private:
110         uint32_t    channels;
111         float       peak;
112 };
113
114 /* Normalizer */
115
116 class Normalizer : public GraphSinkVertex<float, float>
117 {
118   public:
119         Normalizer (uint32_t channels, float target_dB);
120         ~Normalizer ();
121
122         void set_peak (float peak);
123
124   protected:
125         nframes_t process (float * data, nframes_t frames);
126
127   private:
128         uint32_t    channels;
129
130         bool        enabled;
131         gain_t      target;
132         gain_t      gain;
133 };
134
135 /* Other */
136
137 class NullSink : public GraphSink<float>
138 {
139   public:
140         nframes_t write (float * /*data*/, nframes_t frames) { return frames; }
141 };
142
143
144 } // namespace ARDOUR
145
146 #endif /* __ardour_export_utilities_h__ */