Keep to a quota of open files by closing least recently used ones as required. Shoul...
[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/file_manager.h"
28
29 namespace ARDOUR {
30
31 class SndFileSource : public AudioFileSource {
32   public:
33         /** Constructor to be called for existing external-to-session files */
34         SndFileSource (Session&, const Glib::ustring& path, int chn, Flag flags);
35
36         /* Constructor to be called for new in-session files */
37         SndFileSource (Session&, const Glib::ustring& path, 
38                         SampleFormat samp_format, HeaderFormat hdr_format, nframes_t rate,
39                         Flag flags = SndFileSource::default_writable_flags);
40
41         /** Constructor to be called for existing in-session files */
42         SndFileSource (Session&, const XMLNode&);
43
44         ~SndFileSource ();
45
46         float sample_rate () const;
47         int update_header (sframes_t when, struct tm&, time_t);
48         int flush_header ();
49
50         nframes64_t natural_position () const;
51
52         sframes_t last_capture_start_frame() const;
53         void mark_capture_start (sframes_t);
54         void mark_capture_end ();
55         void clear_capture_marks();
56
57         bool set_destructive (bool yn);
58
59         bool one_of_several_channels () const;
60
61         bool clamped_at_unity () const;
62
63         static void setup_standard_crossfades (Session const &, nframes_t sample_rate);
64         static const Source::Flag default_writable_flags;
65
66         static int get_soundfile_info (const Glib::ustring& path, SoundFileInfo& _info, std::string& error_msg);
67
68   protected:
69         void set_header_timeline_position ();
70
71         framecnt_t read_unlocked (Sample *dst, framepos_t start, framecnt_t cnt) const;
72         framecnt_t write_unlocked (Sample *dst, framecnt_t cnt);
73         framecnt_t write_float (Sample* data, framepos_t pos, framecnt_t cnt);
74
75   private:
76         SndFileDescriptor* _descriptor;
77         SF_INFO _info;
78         BroadcastInfo *_broadcast_info;
79
80         void init_sndfile ();
81         int open();
82         int setup_broadcast_info (framepos_t when, struct tm&, time_t);
83         void file_closed ();
84
85         /* destructive */
86
87         static framecnt_t xfade_frames;
88         static gain_t* out_coefficient;
89         static gain_t* in_coefficient;
90
91         bool          _capture_start;
92         bool          _capture_end;
93         framepos_t     capture_start_frame;
94         framepos_t     file_pos; // unit is frames
95         framecnt_t     xfade_out_count;
96         framecnt_t     xfade_in_count;
97         Sample*        xfade_buf;
98         
99         framecnt_t crossfade (Sample* data, framecnt_t cnt, int dir);
100         void set_timeline_position (framepos_t);
101         framecnt_t destructive_write_unlocked (Sample *dst, framecnt_t cnt);
102         framecnt_t nondestructive_write_unlocked (Sample *dst, framecnt_t cnt);
103         void handle_header_position_change ();
104         PBD::ScopedConnection header_position_connection;
105         PBD::ScopedConnection file_manager_connection;
106 };
107
108 } // namespace ARDOUR
109
110 #endif /* __sndfile_source_h__ */
111