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