06115aa8b092180f9d4c3c157b190c763e00561d
[ardour.git] / libs / ardour / ardour / audioregion.h
1 /*
2     Copyright (C) 2000-2001 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id$
19 */
20
21 #ifndef __ardour_audio_region_h__
22 #define __ardour_audio_region_h__
23
24 #include <vector>
25
26 #include <pbd/fastlog.h>
27 #include <pbd/undo.h>
28
29 #include <ardour/ardour.h>
30 #include <ardour/region.h>
31 #include <ardour/gain.h>
32 #include <ardour/logcurve.h>
33 #include <ardour/export.h>
34
35 class XMLNode;
36
37 namespace ARDOUR {
38
39 class Route;
40 class Playlist;
41 class Session;
42 class AudioFilter;
43 class AudioSource;
44
45 struct AudioRegionState : public RegionState 
46 {
47         AudioRegionState (std::string why);
48
49         Curve    _fade_in;
50         Curve    _fade_out;
51         Curve    _envelope;
52         gain_t   _scale_amplitude;
53         uint32_t _fade_in_disabled;
54         uint32_t _fade_out_disabled;
55 };
56
57 class AudioRegion : public Region
58 {
59   public:
60         static Change FadeInChanged;
61         static Change FadeOutChanged;
62         static Change FadeInActiveChanged;
63         static Change FadeOutActiveChanged;
64         static Change EnvelopeActiveChanged;
65         static Change ScaleAmplitudeChanged;
66         static Change EnvelopeChanged;
67
68         ~AudioRegion();
69
70         bool source_equivalent (boost::shared_ptr<const Region>) const;
71
72         bool speed_mismatch (float) const;
73
74         boost::shared_ptr<AudioSource> source (uint32_t n=0) const { if (n < sources.size()) return sources[n]; else return sources[0]; } 
75
76         void set_scale_amplitude (gain_t);
77         gain_t scale_amplitude() const { return _scale_amplitude; }
78         
79         void normalize_to (float target_in_dB = 0.0f);
80
81         uint32_t n_channels() { return sources.size(); }
82         vector<string> master_source_names();
83         
84         bool envelope_active () const { return _flags & Region::EnvelopeActive; }
85         bool fade_in_active ()  const { return _flags & Region::FadeIn; }
86         bool fade_out_active () const { return _flags & Region::FadeOut; }
87         bool captured() const { return !(_flags & (Region::Flag (Region::Import|Region::External))); }
88
89         Curve& fade_in()  { return _fade_in; }
90         Curve& fade_out() { return _fade_out; }
91         Curve& envelope() { return _envelope; }
92
93         jack_nframes_t read_peaks (PeakData *buf, jack_nframes_t npeaks,
94                         jack_nframes_t offset, jack_nframes_t cnt,
95                         uint32_t chan_n=0, double samples_per_unit= 1.0) const;
96
97         virtual jack_nframes_t read_at (Sample *buf, Sample *mixdown_buf,
98                         float *gain_buf, jack_nframes_t position, jack_nframes_t cnt, 
99                         uint32_t       chan_n      = 0,
100                         jack_nframes_t read_frames = 0,
101                         jack_nframes_t skip_frames = 0) const;
102
103         jack_nframes_t master_read_at (Sample *buf, Sample *mixdown_buf, 
104                         float *gain_buf,
105                         jack_nframes_t position, jack_nframes_t cnt, uint32_t chan_n=0) const;
106
107         XMLNode& state (bool);
108         int      set_state (const XMLNode&);
109
110         static void set_default_fade (float steepness, jack_nframes_t len);
111
112         enum FadeShape {
113                 Linear,
114                 Fast,
115                 Slow,
116                 LogA,
117                 LogB
118         };
119
120         void set_fade_in_active (bool yn);
121         void set_fade_in_shape (FadeShape);
122         void set_fade_in_length (jack_nframes_t);
123         void set_fade_in (FadeShape, jack_nframes_t);
124
125         void set_fade_out_active (bool yn);
126         void set_fade_out_shape (FadeShape);
127         void set_fade_out_length (jack_nframes_t);
128         void set_fade_out (FadeShape, jack_nframes_t);
129
130         void set_envelope_active (bool yn);
131
132         int separate_by_channel (ARDOUR::Session&, vector<AudioRegion*>&) const;
133
134         UndoAction get_memento() const;
135
136         /* filter */
137
138         int apply (AudioFilter&);
139
140         /* export */
141
142         int exportme (ARDOUR::Session&, ARDOUR::AudioExportSpecification&);
143
144         boost::shared_ptr<Region> get_parent();
145
146         /* xfade/fade interactions */
147
148         void suspend_fade_in ();
149         void suspend_fade_out ();
150         void resume_fade_in ();
151         void resume_fade_out ();
152
153   private:
154         friend class RegionFactory;
155
156         AudioRegion (boost::shared_ptr<AudioSource>, jack_nframes_t start, jack_nframes_t length);
157         AudioRegion (boost::shared_ptr<AudioSource>, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t = 0, Region::Flag flags = Region::DefaultFlags);
158         AudioRegion (SourceList &, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t = 0, Region::Flag flags = Region::DefaultFlags);
159         AudioRegion (boost::shared_ptr<const AudioRegion>, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t = 0, Region::Flag flags = Region::DefaultFlags);
160         AudioRegion (boost::shared_ptr<const AudioRegion>);
161         AudioRegion (boost::shared_ptr<AudioSource>, const XMLNode&);
162         AudioRegion (SourceList &, const XMLNode&);
163
164   private:
165         void set_default_fades ();
166         void set_default_fade_in ();
167         void set_default_fade_out ();
168         void set_default_envelope ();
169
170         StateManager::State* state_factory (std::string why) const;
171         Change restore_state (StateManager::State&);
172
173         void recompute_gain_at_end ();
174         void recompute_gain_at_start ();
175
176         jack_nframes_t _read_at (const SourceList&, Sample *buf, Sample *mixdown_buffer, 
177                                  float *gain_buffer, jack_nframes_t position, jack_nframes_t cnt, 
178                                  uint32_t chan_n = 0,
179                                  jack_nframes_t read_frames = 0,
180                                  jack_nframes_t skip_frames = 0) const;
181
182         bool verify_start (jack_nframes_t position);
183         bool verify_length (jack_nframes_t position);
184         bool verify_start_mutable (jack_nframes_t& start);
185         bool verify_start_and_length (jack_nframes_t start, jack_nframes_t length);
186         void recompute_at_start ();
187         void recompute_at_end ();
188
189         void envelope_changed (Change);
190         void source_offset_changed ();
191
192         void source_deleted (boost::shared_ptr<Source>);
193         
194         SourceList        sources;
195         
196         /** Used when timefx are applied, so we can always use the original source. */
197         SourceList        master_sources; 
198
199         mutable Curve     _fade_in;
200         FadeShape         _fade_in_shape;
201         mutable Curve     _fade_out;
202         FadeShape         _fade_out_shape;
203         mutable Curve     _envelope;
204         gain_t            _scale_amplitude;
205         uint32_t          _fade_in_disabled;
206         uint32_t          _fade_out_disabled;
207 };
208
209 } /* namespace ARDOUR */
210
211 /* access from C objects */
212
213 extern "C" {
214         int    region_read_peaks_from_c   (void *arg, uint32_t npeaks, uint32_t start, uint32_t length, intptr_t data, uint32_t n_chan, double samples_per_unit);
215         uint32_t region_length_from_c (void *arg);
216         uint32_t sourcefile_length_from_c (void *arg, double);
217 }
218
219 #endif /* __ardour_audio_region_h__ */