1 /* 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> 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_audio_source_h__ 24 #define __ardour_audio_source_h__ 25 26 #include <boost/shared_ptr.hpp> 27 #include <boost/shared_array.hpp> 28 #include <boost/enable_shared_from_this.hpp> 29 30 #include <time.h> 31 32 #include <glibmm/threads.h> 33 #include <boost/function.hpp> 34 #include <boost/scoped_array.hpp> 35 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" 41 42 namespace ARDOUR { 43 44 class LIBARDOUR_API AudioSource : virtual public Source, 45 public ARDOUR::Readable 46 { 47 public: 48 AudioSource (Session&, const std::string& name); 49 AudioSource (Session&, const XMLNode&); 50 virtual ~AudioSource (); 51 readable_length()52 samplecnt_t readable_length() const { return _length; } n_channels()53 virtual uint32_t n_channels() const { return 1; } 54 55 virtual bool empty() const; 56 samplecnt_t length (samplepos_t pos) const; 57 void update_length (samplecnt_t cnt); 58 59 virtual samplecnt_t available_peaks (double zoom) const; 60 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); 63 64 virtual float sample_rate () const = 0; 65 66 virtual void mark_streaming_write_completed (const Lock& lock); 67 can_truncate_peaks()68 virtual bool can_truncate_peaks() const { return true; } 69 70 int read_peaks (PeakData *peaks, samplecnt_t npeaks, 71 samplepos_t start, samplecnt_t cnt, double samples_per_visual_peak) const; 72 73 int build_peaks (); 74 bool peaks_ready (boost::function<void()> callWhenReady, PBD::ScopedConnection** connection_created_if_not_ready, PBD::EventLoop* event_loop) const; 75 76 mutable PBD::Signal0<void> PeaksReady; 77 mutable PBD::Signal2<void,samplepos_t,samplepos_t> PeakRangeReady; 78 79 XMLNode& get_state (); 80 int set_state (const XMLNode&, int version); 81 82 int rename_peakfile (std::string newpath); 83 void touch_peakfile (); 84 set_build_missing_peakfiles(bool yn)85 static void set_build_missing_peakfiles (bool yn) { 86 _build_missing_peakfiles = yn; 87 } 88 set_build_peakfiles(bool yn)89 static void set_build_peakfiles (bool yn) { 90 _build_peakfiles = yn; 91 } 92 get_build_peakfiles()93 static bool get_build_peakfiles () { 94 return _build_peakfiles; 95 } 96 setup_peakfile()97 virtual int setup_peakfile () { return 0; } 98 int close_peakfile (); 99 100 int prepare_for_peakfile_writes (); 101 void done_with_peakfile_writes (bool done = true); 102 103 /** @return true if the each source sample s must be clamped to -1 < s < 1 */ 104 virtual bool clamped_at_unity () const = 0; 105 106 protected: 107 static bool _build_missing_peakfiles; 108 static bool _build_peakfiles; 109 110 samplecnt_t _length; 111 std::string _peakpath; 112 113 int initialize_peakfile (const std::string& path, const bool in_session = false); 114 int build_peaks_from_scratch (); 115 int compute_and_write_peaks (Sample* buf, samplecnt_t first_sample, samplecnt_t cnt, 116 bool force, bool intermediate_peaks_ready_signal); 117 void truncate_peakfile(); 118 119 mutable off_t _peak_byte_max; // modified in compute_and_write_peak() 120 121 virtual samplecnt_t read_unlocked (Sample *dst, samplepos_t start, samplecnt_t cnt) const = 0; 122 virtual samplecnt_t write_unlocked (Sample *dst, samplecnt_t cnt) = 0; 123 virtual std::string construct_peak_filepath (const std::string& audio_path, const bool in_session = false, const bool old_peak_name = false) const = 0; 124 125 virtual int read_peaks_with_fpp (PeakData *peaks, 126 samplecnt_t npeaks, samplepos_t start, samplecnt_t cnt, 127 double samples_per_visual_peak, samplecnt_t fpp) const; 128 129 int compute_and_write_peaks (Sample* buf, samplecnt_t first_sample, samplecnt_t cnt, 130 bool force, bool intermediate_peaks_ready_signal, 131 samplecnt_t samples_per_peak); 132 133 private: 134 bool _peaks_built; 135 /** This mutex is used to protect both the _peaks_built 136 * variable and also the emission (and handling) of the 137 * PeaksReady signal. Holding the lock when emitting 138 * PeaksReady means that _peaks_built cannot be changed 139 * during the handling of the signal. 140 */ 141 mutable Glib::Threads::Mutex _peaks_ready_lock; 142 Glib::Threads::Mutex _initialize_peaks_lock; 143 144 int _peakfile_fd; 145 samplecnt_t peak_leftover_cnt; 146 samplecnt_t peak_leftover_size; 147 Sample* peak_leftovers; 148 samplepos_t peak_leftover_sample; 149 150 mutable bool _first_run; 151 mutable double _last_scale; 152 mutable off_t _last_map_off; 153 mutable size_t _last_raw_map_length; 154 mutable boost::scoped_array<PeakData> peak_cache; 155 }; 156 157 } 158 159 #endif /* __ardour_audio_source_h__ */ 160