1 /*
2  * Copyright (C) 2006-2014 David Robillard <d@drobilla.net>
3  * Copyright (C) 2006-2017 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2016-2019 Robin Gareus <robin@gareus.org>
6  * Copyright (C) 2018-2019 Ben Loftis <ben@harrisonconsoles.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #ifndef __ardour_source_h__
24 #define __ardour_source_h__
25 
26 #include <string>
27 #include <set>
28 
29 #include <glibmm/threads.h>
30 
31 #include <boost/shared_ptr.hpp>
32 #include <boost/enable_shared_from_this.hpp>
33 #include <boost/utility.hpp>
34 
35 #include "pbd/statefuldestructible.h"
36 #include "pbd/g_atomic_compat.h"
37 
38 #include "ardour/ardour.h"
39 #include "ardour/session_object.h"
40 #include "ardour/data_type.h"
41 
42 namespace ARDOUR {
43 
44 class Session;
45 
46 class LIBARDOUR_API Source : public SessionObject,
47 		public boost::enable_shared_from_this<ARDOUR::Source>
48 {
49 public:
50 	enum Flag {
51 		Writable = 0x1,
52 		CanRename = 0x2,
53 		Broadcast = 0x4,
54 		Removable = 0x8,
55 		RemovableIfEmpty = 0x10,
56 		RemoveAtDestroy = 0x20,
57 		NoPeakFile = 0x40,
58 		/* No longer in use but kept to allow loading of older sessions */
59 		Destructive = 0x80,
60 		Empty = 0x100, /* used for MIDI only */
61 		RF64_RIFF = 0x200,
62 		Missing = 0x400, /* used for MIDI only */
63 	};
64 
65 	typedef Glib::Threads::Mutex::Lock Lock;
66 
67 	Source (Session&, DataType type, const std::string& name, Flag flags=Flag(0));
68 	Source (Session&, const XMLNode&);
69 
70 	virtual ~Source ();
71 
type()72 	DataType type() { return _type; }
73 
timestamp()74 	time_t timestamp() const { return _timestamp; }
stamp(time_t when)75 	void stamp (time_t when) { _timestamp = when; }
76 
77 	virtual bool        empty () const = 0;
78 	virtual samplecnt_t length (samplepos_t pos) const = 0;
79 	virtual void        update_length (samplecnt_t cnt) = 0;
80 
set_take_id(std::string id)81 	void                 set_take_id (std::string id) { _take_id =id; }
take_id()82 	const std::string&   take_id ()        const { return _take_id; }
83 
84 	void mark_for_remove();
85 
mark_streaming_write_started(const Lock & lock)86 	virtual void mark_streaming_write_started (const Lock& lock) {}
87 	virtual void mark_streaming_write_completed (const Lock& lock) = 0;
88 
session_saved()89 	virtual void session_saved() {}
90 
91 	XMLNode& get_state ();
92 	int set_state (XMLNode const &, int version);
93 
94 	bool         writable () const;
95 
length_mutable()96 	virtual bool length_mutable() const    { return false; }
97 
98 	static PBD::Signal1<void,Source*>             SourceCreated;
99 
100 	bool has_been_analysed() const;
can_be_analysed()101 	virtual bool can_be_analysed() const { return false; }
102 	virtual void set_been_analysed (bool yn);
103 	virtual bool check_for_analysis_data_on_disk();
104 
105 	PBD::Signal0<void> AnalysisChanged;
106 
107 	AnalysisFeatureList transients;
108 	std::string get_transients_path() const;
109 	int load_transients (const std::string&);
110 
n_captured_xruns()111 	size_t n_captured_xruns () const { return _xruns.size (); }
captured_xruns()112 	XrunPositions const& captured_xruns () const { return _xruns; }
set_captured_xruns(XrunPositions const & xruns)113 	void set_captured_xruns (XrunPositions const& xruns) { _xruns = xruns; }
114 
cue_markers()115 	CueMarkers const & cue_markers() const { return _cue_markers; }
116 	bool add_cue_marker (CueMarker const &);
117 	bool move_cue_marker (CueMarker const &, samplepos_t source_relative_position);
118 	bool remove_cue_marker (CueMarker const &);
119 	bool rename_cue_marker (CueMarker&, std::string const &);
120 	bool clear_cue_markers ();
121 	PBD::Signal0<void> CueMarkersChanged;
122 
natural_position()123 	virtual samplepos_t natural_position() const { return _natural_position; }
124 	virtual void set_natural_position (samplepos_t pos);
have_natural_position()125 	bool have_natural_position() const { return _have_natural_position; }
126 
127 	void set_allow_remove_if_empty (bool yn);
128 
mutex()129 	Glib::Threads::Mutex& mutex() { return _lock; }
flags()130 	Flag flags() const { return _flags; }
131 
132 	virtual void inc_use_count ();
133 	virtual void dec_use_count ();
use_count()134 	int  use_count() const { return g_atomic_int_get (&_use_count); }
used()135 	bool used() const { return use_count() > 0; }
136 
level()137 	uint32_t level() const { return _level; }
138 
ancestor_name()139 	std::string ancestor_name() { return _ancestor_name.empty() ? name() : _ancestor_name; }
set_ancestor_name(const std::string & name)140 	void set_ancestor_name(const std::string& name) { _ancestor_name = name; }
141 
set_captured_for(std::string str)142 	void set_captured_for (std::string str) { _captured_for = str; }
captured_for()143 	std::string captured_for() const { return _captured_for; }
144 
145   protected:
146 	DataType          _type;
147 	Flag              _flags;
148 	time_t            _timestamp;
149 	std::string       _take_id;
150 	samplepos_t       _natural_position;
151 	samplepos_t       _have_natural_position;
152 	bool              _analysed;
153 	GATOMIC_QUAL gint _use_count; /* atomic */
154 	uint32_t          _level; /* how deeply nested is this source w.r.t a disk file */
155 	std::string       _ancestor_name;
156 	std::string       _captured_for;
157 	XrunPositions     _xruns;
158 	CueMarkers        _cue_markers;
159 
160 	mutable Glib::Threads::Mutex _lock;
161 	mutable Glib::Threads::Mutex _analysis_lock;
162 
163   private:
164 	void fix_writable_flags ();
165 
166 	XMLNode& get_cue_state () const;
167 	int set_cue_state (XMLNode const &, int);
168 };
169 
170 }
171 
172 #endif /* __ardour_source_h__ */
173