rename all Evoral source from .(hpp|cpp)$ to .(h|cc)
[ardour.git] / libs / ardour / ardour / sndfilesource.h
1 /*
2  * Copyright (C) 2005-2006 Taybin Rutkin <taybin@taybin.com>
3  * Copyright (C) 2006-2008 Doug McLain <doug@nostar.net>
4  * Copyright (C) 2006-2014 David Robillard <d@drobilla.net>
5  * Copyright (C) 2006-2017 Paul Davis <paul@linuxaudiosystems.com>
6  * Copyright (C) 2009-2010 Carl Hetherington <carl@carlh.net>
7  * Copyright (C) 2013 John Emmas <john@creativepost.co.uk>
8  * Copyright (C) 2016-2019 Robin Gareus <robin@gareus.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #ifndef __sndfile_source_h__
26 #define __sndfile_source_h__
27
28 #include <sndfile.h>
29
30 #include "ardour/audiofilesource.h"
31 #include "ardour/broadcast_info.h"
32 #include "ardour/progress.h"
33
34 namespace ARDOUR {
35
36 class LIBARDOUR_API SndFileSource : public AudioFileSource {
37   public:
38         /** Constructor to be called for existing external-to-session files */
39         SndFileSource (Session&, const std::string& path, int chn, Flag flags);
40
41         /* Constructor to be called for new in-session files */
42         SndFileSource (Session&, const std::string& path, const std::string& origin,
43                        SampleFormat samp_format, HeaderFormat hdr_format, samplecnt_t rate,
44                        Flag flags = SndFileSource::default_writable_flags);
45
46         /* Constructor to be called for recovering files being used for
47          * capture. They are in-session, they already exist, they should not
48          * be writable. They are an odd hybrid (from a constructor point of
49          * view) of the previous two constructors.
50          */
51         SndFileSource (Session&, const std::string& path, int chn);
52
53         /** Constructor to be called for existing in-session files during
54          * session loading
55          */
56         SndFileSource (Session&, const XMLNode&);
57
58         /** Constructor to losslessly compress existing source */
59         SndFileSource (Session& s, const AudioFileSource& other, const std::string& path, bool use16bits = false, Progress* p = NULL);
60
61         ~SndFileSource ();
62
63         float sample_rate () const;
64         int update_header (samplepos_t when, struct tm&, time_t);
65         int flush_header ();
66         void flush ();
67
68         samplepos_t last_capture_start_sample() const;
69         void mark_capture_start (samplepos_t);
70         void mark_capture_end ();
71         void clear_capture_marks();
72
73         bool one_of_several_channels () const;
74     uint32_t channel_count () const { return _info.channels; }
75
76         bool clamped_at_unity () const;
77
78         static void setup_standard_crossfades (Session const &, samplecnt_t sample_rate);
79         static const Source::Flag default_writable_flags;
80
81         static int get_soundfile_info (const std::string& path, SoundFileInfo& _info, std::string& error_msg);
82
83   protected:
84         void close ();
85
86         void set_path (const std::string& p);
87         void set_header_natural_position ();
88
89         samplecnt_t read_unlocked (Sample *dst, samplepos_t start, samplecnt_t cnt) const;
90         samplecnt_t write_unlocked (Sample *dst, samplecnt_t cnt);
91         samplecnt_t write_float (Sample* data, samplepos_t pos, samplecnt_t cnt);
92
93   private:
94         SNDFILE* _sndfile;
95         SF_INFO _info;
96         BroadcastInfo *_broadcast_info;
97
98         void init_sndfile ();
99         int open();
100         int setup_broadcast_info (samplepos_t when, struct tm&, time_t);
101         void file_closed ();
102
103         /* destructive */
104
105         static samplecnt_t xfade_samples;
106
107         static gain_t* out_coefficient;
108         static gain_t* in_coefficient;
109
110         bool           _capture_start;
111         bool           _capture_end;
112         samplepos_t    capture_start_sample;
113         samplepos_t    file_pos; // unit is samples
114         Sample*        xfade_buf;
115
116         samplecnt_t crossfade (Sample* data, samplecnt_t cnt, int dir);
117         void set_natural_position (samplepos_t);
118         samplecnt_t destructive_write_unlocked (Sample *dst, samplecnt_t cnt);
119         samplecnt_t nondestructive_write_unlocked (Sample *dst, samplecnt_t cnt);
120         void handle_header_position_change ();
121         PBD::ScopedConnection header_position_connection;
122 };
123
124 } // namespace ARDOUR
125
126 #endif /* __sndfile_source_h__ */
127