merge to trunk
[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         typedef vector<AudioSource *> SourceList;
61
62         static Change FadeInChanged;
63         static Change FadeOutChanged;
64         static Change FadeInActiveChanged;
65         static Change FadeOutActiveChanged;
66         static Change EnvelopeActiveChanged;
67         static Change ScaleAmplitudeChanged;
68         static Change EnvelopeChanged;
69
70         AudioRegion (AudioSource&, jack_nframes_t start, jack_nframes_t length, bool announce = true);
71         AudioRegion (AudioSource&, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t = 0, Region::Flag flags = Region::DefaultFlags, bool announce = true);
72         AudioRegion (SourceList &, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t = 0, Region::Flag flags = Region::DefaultFlags, bool announce = true);
73         AudioRegion (const AudioRegion&, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t = 0, Region::Flag flags = Region::DefaultFlags, bool announce = true);
74         AudioRegion (const AudioRegion&);
75         AudioRegion (AudioSource&, const XMLNode&);
76         AudioRegion (SourceList &, const XMLNode&);
77         ~AudioRegion();
78
79         bool source_equivalent (const Region&) const;
80
81         bool speed_mismatch (float) const;
82
83         void lock_sources ();
84         void unlock_sources ();
85         AudioSource& source (uint32_t n=0) const { if (n < sources.size()) return *sources[n]; else return *sources[0]; } 
86
87         void set_scale_amplitude (gain_t);
88         gain_t scale_amplitude() const { return _scale_amplitude; }
89         
90         void normalize_to (float target_in_dB = 0.0f);
91
92         uint32_t n_channels() { return sources.size(); }
93         vector<string> master_source_names();
94         
95         bool envelope_active () const { return _flags & Region::EnvelopeActive; }
96         bool fade_in_active ()  const { return _flags & Region::FadeIn; }
97         bool fade_out_active () const { return _flags & Region::FadeOut; }
98         bool captured() const { return !(_flags & (Region::Flag (Region::Import|Region::External))); }
99
100         Curve& fade_in()  { return _fade_in; }
101         Curve& fade_out() { return _fade_out; }
102         Curve& envelope() { return _envelope; }
103
104         jack_nframes_t read_peaks (PeakData *buf, jack_nframes_t npeaks,
105                         jack_nframes_t offset, jack_nframes_t cnt,
106                         uint32_t chan_n=0, double samples_per_unit= 1.0) const;
107
108         virtual jack_nframes_t read_at (Sample *buf, Sample *mixdown_buf,
109                         float *gain_buf, jack_nframes_t position, jack_nframes_t cnt, 
110                         uint32_t       chan_n      = 0,
111                         jack_nframes_t read_frames = 0,
112                         jack_nframes_t skip_frames = 0) const;
113
114         jack_nframes_t master_read_at (Sample *buf, Sample *mixdown_buf, 
115                         float *gain_buf,
116                         jack_nframes_t position, jack_nframes_t cnt, uint32_t chan_n=0) const;
117
118         XMLNode& state (bool);
119         int      set_state (const XMLNode&);
120
121         static void set_default_fade (float steepness, jack_nframes_t len);
122
123         enum FadeShape {
124                 Linear,
125                 Fast,
126                 Slow,
127                 LogA,
128                 LogB
129         };
130
131         void set_fade_in_active (bool yn);
132         void set_fade_in_shape (FadeShape);
133         void set_fade_in_length (jack_nframes_t);
134         void set_fade_in (FadeShape, jack_nframes_t);
135
136         void set_fade_out_active (bool yn);
137         void set_fade_out_shape (FadeShape);
138         void set_fade_out_length (jack_nframes_t);
139         void set_fade_out (FadeShape, jack_nframes_t);
140
141         void set_envelope_active (bool yn);
142
143         int separate_by_channel (ARDOUR::Session&, vector<AudioRegion*>&) const;
144
145         UndoAction get_memento() const;
146
147         /* filter */
148
149         int apply (AudioFilter&);
150
151         /* export */
152
153         int exportme (ARDOUR::Session&, ARDOUR::AudioExportSpecification&);
154
155         Region* get_parent();
156
157         /* xfade/fade interactions */
158
159         void suspend_fade_in ();
160         void suspend_fade_out ();
161         void resume_fade_in ();
162         void resume_fade_out ();
163
164   private:
165         friend class Playlist;
166
167   private:
168         void set_default_fades ();
169         void set_default_fade_in ();
170         void set_default_fade_out ();
171         void set_default_envelope ();
172
173         StateManager::State* state_factory (std::string why) const;
174         Change restore_state (StateManager::State&);
175
176         void recompute_gain_at_end ();
177         void recompute_gain_at_start ();
178
179         jack_nframes_t _read_at (const SourceList&, Sample *buf, Sample *mixdown_buffer, 
180                                  float *gain_buffer, jack_nframes_t position, jack_nframes_t cnt, 
181                                  uint32_t chan_n = 0,
182                                  jack_nframes_t read_frames = 0,
183                                  jack_nframes_t skip_frames = 0) const;
184
185         bool verify_start (jack_nframes_t position);
186         bool verify_length (jack_nframes_t position);
187         bool verify_start_mutable (jack_nframes_t& start);
188         bool verify_start_and_length (jack_nframes_t start, jack_nframes_t length);
189         void recompute_at_start ();
190         void recompute_at_end ();
191
192         void envelope_changed (Change);
193
194         void source_deleted (Source*);
195         
196         
197         SourceList        sources;
198         
199         /** Used when timefx are applied, so we can always use the original source. */
200         SourceList        master_sources; 
201
202         mutable Curve     _fade_in;
203         FadeShape         _fade_in_shape;
204         mutable Curve     _fade_out;
205         FadeShape         _fade_out_shape;
206         mutable Curve     _envelope;
207         gain_t            _scale_amplitude;
208         uint32_t          _fade_in_disabled;
209         uint32_t          _fade_out_disabled;
210 };
211
212 } /* namespace ARDOUR */
213
214 /* access from C objects */
215
216 extern "C" {
217         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);
218         uint32_t region_length_from_c (void *arg);
219         uint32_t sourcefile_length_from_c (void *arg, double);
220 }
221
222 #endif /* __ardour_audio_region_h__ */