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