use SilentFileSource when sources cannot be found
[ardour.git] / libs / ardour / ardour / audiofilesource.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 __ardour_audiofilesource_h__ 
21 #define __ardour_audiofilesource_h__
22
23 #include <exception>
24
25 #include <time.h>
26
27 #include <ardour/audiosource.h>
28
29 namespace ARDOUR {
30
31 class non_existent_source : public std::exception {
32   public:
33         virtual const char *what() const throw() { return "audio file does not exist"; }
34 };
35
36 struct SoundFileInfo {
37     float       samplerate;
38     uint16_t    channels;
39     int64_t     length;
40     std::string format_name;
41     int64_t     timecode;
42 };
43
44 class AudioFileSource : public AudioSource {
45   public:
46         enum Flag {
47                 Writable = 0x1,
48                 CanRename = 0x2,
49                 Broadcast = 0x4,
50                 Removable = 0x8,
51                 RemovableIfEmpty = 0x10,
52                 RemoveAtDestroy = 0x20,
53                 NoPeakFile = 0x40,
54                 Destructive = 0x80
55         };
56
57         virtual ~AudioFileSource ();
58
59         int set_name (string newname, bool destructive);
60
61         string path() const { return _path; }
62         string peak_path (string audio_path);
63         string old_peak_path (string audio_path);
64
65         static void set_peak_dir (string dir) { peak_dir = dir; }
66
67         static bool get_soundfile_info (string path, SoundFileInfo& _info, string& error);
68
69         static bool safe_file_extension (string path);
70
71         void set_allow_remove_if_empty (bool yn);
72         void mark_for_remove();
73
74         /* this block of methods do nothing for regular file sources, but are significant
75            for files used in destructive recording.
76         */
77
78         virtual nframes_t last_capture_start_frame() const { return 0; }
79         virtual void           mark_capture_start (nframes_t) {}
80         virtual void           mark_capture_end () {}
81         virtual void           clear_capture_marks() {}
82
83         virtual int update_header (nframes_t when, struct tm&, time_t) = 0;
84         virtual int flush_header () = 0;
85
86         int move_to_trash (const string trash_dir_name);
87
88         static bool is_empty (Session&, string path);
89         void mark_streaming_write_completed ();
90
91         void   mark_take (string);
92         string take_id() const { return _take_id; }
93
94         bool is_embedded() const { return _is_embedded; }
95
96         static void set_bwf_serial_number (int);
97         
98         static void set_search_path (string);
99         static void set_header_position_offset (nframes_t offset );
100
101         int setup_peakfile ();
102
103         static sigc::signal<void> HeaderPositionOffsetChanged;
104
105         XMLNode& get_state ();
106         int set_state (const XMLNode&);
107
108         bool destructive() const { return (_flags & Destructive); }
109         virtual bool set_destructive (bool yn) { return false; }
110
111         Flag flags() const { return _flags; }
112
113         void mark_immutable ();
114
115         /* this should really be protected, but C++ is getting stricter
116            and creating slots from protected member functions is starting
117            to cause issues.
118         */
119
120         virtual void handle_header_position_change () {}
121
122   protected:
123         
124         /* constructor to be called for existing external-to-session files */
125
126         AudioFileSource (Session&, std::string path, Flag flags);
127
128         /* constructor to be called for new in-session files */
129
130         AudioFileSource (Session&, std::string path, Flag flags,
131                          SampleFormat samp_format, HeaderFormat hdr_format);
132
133         /* constructor to be called for existing in-session files */
134
135         AudioFileSource (Session&, const XMLNode&, bool must_exit = true);
136
137         int init (string idstr, bool must_exist);
138
139         string        _path;
140         Flag          _flags;
141         string        _take_id;
142         int64_t       timeline_position;
143         bool           file_is_new;
144         uint16_t       channel;
145
146         bool          _is_embedded;
147         static bool determine_embeddedness(string path);
148
149         static string peak_dir;
150         static string search_path;
151
152         static char bwf_country_code[3];
153         static char bwf_organization_code[4];
154         static char bwf_serial_number[13];
155
156         static uint64_t header_position_offset;
157
158         virtual void set_timeline_position (int64_t pos);
159         virtual void set_header_timeline_position () = 0;
160
161         bool find (std::string path, bool must_exist, bool& is_new);
162         bool removable() const;
163         bool writable() const { return _flags & Writable; }
164 };
165
166 } // namespace ARDOUR
167
168 #endif /* __ardour_audiofilesource_h__ */
169