4ad967c132a2a4e8ce46ad7ccb9b416453be5b6e
[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
27 namespace ARDOUR {
28
29 class SndFileSource : public AudioFileSource {
30   public:
31         /* constructor to be called for existing external-to-session files */
32
33         SndFileSource (Session&, Glib::ustring path, int chn, Flag flags);
34
35         /* constructor to be called for new in-session files */
36
37         SndFileSource (Session&, Glib::ustring path, 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         
42         SndFileSource (Session&, const XMLNode&);
43
44         ~SndFileSource ();
45
46         float sample_rate () const;
47         int update_header (nframes_t when, struct tm&, time_t);
48         int flush_header ();
49
50         nframes_t natural_position () const;
51
52         nframes_t last_capture_start_frame() const;
53         void mark_capture_start (nframes_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         static void setup_standard_crossfades (nframes_t sample_rate);
62         static const AudioFileSource::Flag default_writable_flags;
63
64         static int get_soundfile_info (const Glib::ustring& path, SoundFileInfo& _info, string& error_msg);
65
66   protected:
67         void set_header_timeline_position ();
68
69         nframes_t read_unlocked (Sample *dst, nframes_t start, nframes_t cnt) const;
70         nframes_t write_unlocked (Sample *dst, nframes_t cnt);
71
72         nframes_t write_float (Sample* data, nframes_t pos, nframes_t cnt);
73
74   private:
75         SNDFILE *sf;
76         SF_INFO _info;
77         SF_BROADCAST_INFO *_broadcast_info;
78
79         void init ();
80         int open();
81         int setup_broadcast_info (nframes_t when, struct tm&, time_t);
82
83         /* destructive */
84
85         static nframes_t xfade_frames;
86         static gain_t* out_coefficient;
87         static gain_t* in_coefficient;
88
89         bool          _capture_start;
90         bool          _capture_end;
91         nframes_t      capture_start_frame;
92         nframes_t      file_pos; // unit is frames
93         nframes_t      xfade_out_count;
94         nframes_t      xfade_in_count;
95         Sample*        xfade_buf;
96
97         nframes_t crossfade (Sample* data, nframes_t cnt, int dir);
98         void set_timeline_position (int64_t);
99         nframes_t destructive_write_unlocked (Sample *dst, nframes_t cnt);
100         nframes_t nondestructive_write_unlocked (Sample *dst, nframes_t cnt);
101         void handle_header_position_change ();
102
103         static int64_t get_timecode_info (SNDFILE* sf, SF_BROADCAST_INFO* binfo, bool& exists);
104 };
105
106 } // namespace ARDOUR
107
108 #endif /* __sndfile_source_h__ */
109