rename all Evoral source from .(hpp|cpp)$ to .(h|cc)
[ardour.git] / libs / ardour / ardour / audiosource.h
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
52         samplecnt_t readable_length() const { return _length; }
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
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
85         static void set_build_missing_peakfiles (bool yn) {
86                 _build_missing_peakfiles = yn;
87         }
88
89         static void set_build_peakfiles (bool yn) {
90                 _build_peakfiles = yn;
91         }
92
93         static bool get_build_peakfiles () {
94                 return _build_peakfiles;
95         }
96
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         static void allocate_working_buffers (samplecnt_t framerate);
107
108   protected:
109         static bool _build_missing_peakfiles;
110         static bool _build_peakfiles;
111
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.
116         */
117
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;
121
122         static void ensure_buffers_for_level (uint32_t, samplecnt_t);
123         static void ensure_buffers_for_level_locked (uint32_t, samplecnt_t);
124
125         samplecnt_t           _length;
126         std::string         _peakpath;
127
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();
133
134         mutable off_t _peak_byte_max; // modified in compute_and_write_peak()
135
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;
139
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;
143
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);
147
148   private:
149         bool _peaks_built;
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.
155          */
156         mutable Glib::Threads::Mutex _peaks_ready_lock;
157         Glib::Threads::Mutex _initialize_peaks_lock;
158
159         int        _peakfile_fd;
160         samplecnt_t peak_leftover_cnt;
161         samplecnt_t peak_leftover_size;
162         Sample*    peak_leftovers;
163         samplepos_t peak_leftover_sample;
164
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;
170 };
171
172 }
173
174 #endif /* __ardour_audio_source_h__ */