Merged with trunk R1141
[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     $Id$
19 */
20
21 #ifndef __sndfile_source_h__ 
22 #define __sndfile_source_h__
23
24 #include <sndfile.h>
25
26 #include <ardour/audiofilesource.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
34         SndFileSource (Session&, std::string path, Flag flags);
35
36         /* constructor to be called for new in-session files */
37
38         SndFileSource (Session&, std::string path, 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         
43         SndFileSource (Session&, const XMLNode&);
44
45         ~SndFileSource ();
46
47         float sample_rate () const;
48         int update_header (nframes_t when, struct tm&, time_t);
49         int flush_header ();
50
51         nframes_t natural_position () const;
52
53         nframes_t last_capture_start_frame() const;
54         void mark_capture_start (nframes_t);
55         void mark_capture_end ();
56         void clear_capture_marks();
57
58         bool set_destructive (bool yn);
59
60         static void setup_standard_crossfades (nframes_t sample_rate);
61         static const AudioFileSource::Flag default_writable_flags;
62
63         static int get_soundfile_info (string path, SoundFileInfo& _info, string& error_msg);
64
65   protected:
66         void set_header_timeline_position ();
67
68         nframes_t read_unlocked (Sample *dst, nframes_t start, nframes_t cnt) const;
69         nframes_t write_unlocked (Sample *dst, nframes_t cnt);
70
71         nframes_t write_float (Sample* data, nframes_t pos, nframes_t cnt);
72
73   private:
74         SNDFILE *sf;
75         SF_INFO _info;
76         SF_BROADCAST_INFO *_broadcast_info;
77
78         mutable float *interleave_buf;
79         mutable nframes_t interleave_bufsize;
80
81         void init (string str);
82         int open();
83         void close();
84         int setup_broadcast_info (nframes_t when, struct tm&, time_t);
85
86         /* destructive */
87
88         static nframes_t xfade_frames;
89         static gain_t* out_coefficient;
90         static gain_t* in_coefficient;
91
92         bool          _capture_start;
93         bool          _capture_end;
94         nframes_t      capture_start_frame;
95         nframes_t      file_pos; // unit is frames
96         nframes_t      xfade_out_count;
97         nframes_t      xfade_in_count;
98         Sample*        xfade_buf;
99
100         nframes_t crossfade (Sample* data, nframes_t cnt, int dir);
101         void set_timeline_position (int64_t);
102         nframes_t destructive_write_unlocked (Sample *dst, nframes_t cnt);
103         nframes_t nondestructive_write_unlocked (Sample *dst, nframes_t cnt);
104         void handle_header_position_change ();
105
106         static int64_t get_timecode_info (SNDFILE* sf, SF_BROADCAST_INFO* binfo, bool& exists);
107 };
108
109 } // namespace ARDOUR
110
111 #endif /* __sndfile_source_h__ */
112