2 * Copyright (C) 2006-2014 David Robillard <d@drobilla.net>
3 * Copyright (C) 2007-2017 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
5 * Copyright (C) 2015 Robin Gareus <robin@gareus.org>
6 * Copyright (C) 2018-2019 Ben Loftis <ben@harrisonconsoles.com>
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.
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.
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.
23 #ifndef __ardour_audio_source_h__
24 #define __ardour_audio_source_h__
26 #include <boost/shared_ptr.hpp>
27 #include <boost/shared_array.hpp>
28 #include <boost/enable_shared_from_this.hpp>
32 #include <glibmm/threads.h>
33 #include <boost/function.hpp>
34 #include <boost/scoped_array.hpp>
36 #include "ardour/source.h"
37 #include "ardour/ardour.h"
38 #include "ardour/readable.h"
39 #include "pbd/stateful.h"
40 #include "pbd/xml++.h"
44 class LIBARDOUR_API AudioSource : virtual public Source,
45 public ARDOUR::Readable
48 AudioSource (Session&, const std::string& name);
49 AudioSource (Session&, const XMLNode&);
50 virtual ~AudioSource ();
52 samplecnt_t readable_length() const { return _length; }
53 virtual uint32_t n_channels() const { return 1; }
55 virtual bool empty() const;
56 samplecnt_t length (samplepos_t pos) const;
57 void update_length (samplecnt_t cnt);
59 virtual samplecnt_t available_peaks (double zoom) const;
61 virtual samplecnt_t read (Sample *dst, samplepos_t start, samplecnt_t cnt, int channel=0) const;
62 virtual samplecnt_t write (Sample *src, samplecnt_t cnt);
64 virtual float sample_rate () const = 0;
66 virtual void mark_streaming_write_completed (const Lock& lock);
68 virtual bool can_truncate_peaks() const { return true; }
70 int read_peaks (PeakData *peaks, samplecnt_t npeaks,
71 samplepos_t start, samplecnt_t cnt, double samples_per_visual_peak) const;
74 bool peaks_ready (boost::function<void()> callWhenReady, PBD::ScopedConnection** connection_created_if_not_ready, PBD::EventLoop* event_loop) const;
76 mutable PBD::Signal0<void> PeaksReady;
77 mutable PBD::Signal2<void,samplepos_t,samplepos_t> PeakRangeReady;
79 XMLNode& get_state ();
80 int set_state (const XMLNode&, int version);
82 int rename_peakfile (std::string newpath);
83 void touch_peakfile ();
85 static void set_build_missing_peakfiles (bool yn) {
86 _build_missing_peakfiles = yn;
89 static void set_build_peakfiles (bool yn) {
90 _build_peakfiles = yn;
93 static bool get_build_peakfiles () {
94 return _build_peakfiles;
97 virtual int setup_peakfile () { return 0; }
98 int close_peakfile ();
100 int prepare_for_peakfile_writes ();
101 void done_with_peakfile_writes (bool done = true);
103 /** @return true if the each source sample s must be clamped to -1 < s < 1 */
104 virtual bool clamped_at_unity () const = 0;
106 static void allocate_working_buffers (samplecnt_t framerate);
109 static bool _build_missing_peakfiles;
110 static bool _build_peakfiles;
112 /* these collections of working buffers for supporting
113 playlist's reading from potentially nested/recursive
114 sources assume SINGLE THREADED reads by the butler
115 thread, or a lock around calls that use them.
118 static std::vector<boost::shared_array<Sample> > _mixdown_buffers;
119 static std::vector<boost::shared_array<gain_t> > _gain_buffers;
120 static Glib::Threads::Mutex _level_buffer_lock;
122 static void ensure_buffers_for_level (uint32_t, samplecnt_t);
123 static void ensure_buffers_for_level_locked (uint32_t, samplecnt_t);
126 std::string _peakpath;
128 int initialize_peakfile (const std::string& path, const bool in_session = false);
129 int build_peaks_from_scratch ();
130 int compute_and_write_peaks (Sample* buf, samplecnt_t first_sample, samplecnt_t cnt,
131 bool force, bool intermediate_peaks_ready_signal);
132 void truncate_peakfile();
134 mutable off_t _peak_byte_max; // modified in compute_and_write_peak()
136 virtual samplecnt_t read_unlocked (Sample *dst, samplepos_t start, samplecnt_t cnt) const = 0;
137 virtual samplecnt_t write_unlocked (Sample *dst, samplecnt_t cnt) = 0;
138 virtual std::string construct_peak_filepath (const std::string& audio_path, const bool in_session = false, const bool old_peak_name = false) const = 0;
140 virtual int read_peaks_with_fpp (PeakData *peaks,
141 samplecnt_t npeaks, samplepos_t start, samplecnt_t cnt,
142 double samples_per_visual_peak, samplecnt_t fpp) const;
144 int compute_and_write_peaks (Sample* buf, samplecnt_t first_sample, samplecnt_t cnt,
145 bool force, bool intermediate_peaks_ready_signal,
146 samplecnt_t samples_per_peak);
150 /** This mutex is used to protect both the _peaks_built
151 * variable and also the emission (and handling) of the
152 * PeaksReady signal. Holding the lock when emitting
153 * PeaksReady means that _peaks_built cannot be changed
154 * during the handling of the signal.
156 mutable Glib::Threads::Mutex _peaks_ready_lock;
157 Glib::Threads::Mutex _initialize_peaks_lock;
160 samplecnt_t peak_leftover_cnt;
161 samplecnt_t peak_leftover_size;
162 Sample* peak_leftovers;
163 samplepos_t peak_leftover_sample;
165 mutable bool _first_run;
166 mutable double _last_scale;
167 mutable off_t _last_map_off;
168 mutable size_t _last_raw_map_length;
169 mutable boost::scoped_array<PeakData> peak_cache;
174 #endif /* __ardour_audio_source_h__ */