Optimize automation-event process splitting
[ardour.git] / libs / ardour / ardour / sndfilesource.h
1 /*
2     Copyright (C) 2006 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 */
19
20 #ifndef __sndfile_source_h__
21 #define __sndfile_source_h__
22
23 #include <sndfile.h>
24
25 #include "ardour/audiofilesource.h"
26 #include "ardour/broadcast_info.h"
27 #include "ardour/progress.h"
28
29 namespace ARDOUR {
30
31 class LIBARDOUR_API SndFileSource : public AudioFileSource {
32   public:
33         /** Constructor to be called for existing external-to-session files */
34         SndFileSource (Session&, const std::string& path, int chn, Flag flags);
35
36         /* Constructor to be called for new in-session files */
37         SndFileSource (Session&, const std::string& path, const std::string& origin,
38                        SampleFormat samp_format, HeaderFormat hdr_format, samplecnt_t rate,
39                        Flag flags = SndFileSource::default_writable_flags);
40
41         /* Constructor to be called for recovering files being used for
42          * capture. They are in-session, they already exist, they should not
43          * be writable. They are an odd hybrid (from a constructor point of
44          * view) of the previous two constructors.
45          */
46         SndFileSource (Session&, const std::string& path, int chn);
47
48         /** Constructor to be called for existing in-session files during
49          * session loading
50          */
51         SndFileSource (Session&, const XMLNode&);
52
53         /** Constructor to losslessly compress existing source */
54         SndFileSource (Session& s, const AudioFileSource& other, const std::string& path, bool use16bits = false, Progress* p = NULL);
55
56         ~SndFileSource ();
57
58         float sample_rate () const;
59         int update_header (samplepos_t when, struct tm&, time_t);
60         int flush_header ();
61         void flush ();
62
63         samplepos_t natural_position () const;
64
65         samplepos_t last_capture_start_sample() const;
66         void mark_capture_start (samplepos_t);
67         void mark_capture_end ();
68         void clear_capture_marks();
69
70         bool one_of_several_channels () const;
71     uint32_t channel_count () const { return _info.channels; }
72
73         bool clamped_at_unity () const;
74
75         static void setup_standard_crossfades (Session const &, samplecnt_t sample_rate);
76         static const Source::Flag default_writable_flags;
77
78         static int get_soundfile_info (const std::string& path, SoundFileInfo& _info, std::string& error_msg);
79
80   protected:
81         void close ();
82
83         void set_path (const std::string& p);
84         void set_header_timeline_position ();
85
86         samplecnt_t read_unlocked (Sample *dst, samplepos_t start, samplecnt_t cnt) const;
87         samplecnt_t write_unlocked (Sample *dst, samplecnt_t cnt);
88         samplecnt_t write_float (Sample* data, samplepos_t pos, samplecnt_t cnt);
89
90   private:
91         SNDFILE* _sndfile;
92         SF_INFO _info;
93         BroadcastInfo *_broadcast_info;
94
95         void init_sndfile ();
96         int open();
97         int setup_broadcast_info (samplepos_t when, struct tm&, time_t);
98         void file_closed ();
99
100         /* destructive */
101
102         static samplecnt_t xfade_samples;
103         static gain_t* out_coefficient;
104         static gain_t* in_coefficient;
105
106         bool          _capture_start;
107         bool          _capture_end;
108         samplepos_t     capture_start_sample;
109         samplepos_t     file_pos; // unit is samples
110         Sample*        xfade_buf;
111
112         samplecnt_t crossfade (Sample* data, samplecnt_t cnt, int dir);
113         void set_timeline_position (samplepos_t);
114         samplecnt_t destructive_write_unlocked (Sample *dst, samplecnt_t cnt);
115         samplecnt_t nondestructive_write_unlocked (Sample *dst, samplecnt_t cnt);
116         void handle_header_position_change ();
117         PBD::ScopedConnection header_position_connection;
118 };
119
120 } // namespace ARDOUR
121
122 #endif /* __sndfile_source_h__ */
123