Ardour  9.0-pre0-582-g084a23a80d
audiosource.h
Go to the documentation of this file.
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 #pragma once
24 
25 #include <memory>
26 
27 #include <time.h>
28 
29 #include <glibmm/threads.h>
30 
31 #include "ardour/source.h"
32 #include "ardour/ardour.h"
33 #include "ardour/readable.h"
34 #include "pbd/stateful.h"
35 #include "pbd/xml++.h"
36 
37 namespace ARDOUR {
38 
39 class LIBARDOUR_API AudioSource : virtual public Source, public ARDOUR::AudioReadable
40 {
41  public:
42  AudioSource (Session&, const std::string& name);
44  virtual ~AudioSource ();
45 
46  samplecnt_t readable_length_samples() const { return _length.samples(); }
47  virtual uint32_t n_channels() const { return 1; }
48 
49  void update_length (timepos_t const & dur);
50 
51  virtual samplecnt_t available_peaks (double zoom) const;
52 
53  virtual samplecnt_t read (Sample *dst, samplepos_t start, samplecnt_t cnt, int channel=0) const;
54  virtual samplecnt_t write (Sample const * src, samplecnt_t cnt);
55 
56  virtual float sample_rate () const = 0;
57 
58  virtual void mark_streaming_write_completed (const WriterLock& lock, Temporal::timecnt_t const & duration);
59 
60  virtual bool can_truncate_peaks() const { return true; }
61 
63  samplepos_t start, samplecnt_t cnt, double samples_per_visual_peak) const;
64 
65  int build_peaks ();
66  bool peaks_ready (std::function<void()> callWhenReady, PBD::ScopedConnection** connection_created_if_not_ready, PBD::EventLoop* event_loop) const;
67 
68  mutable PBD::Signal<void()> PeaksReady;
70 
71  XMLNode& get_state () const;
72  int set_state (const XMLNode&, int version);
73 
74  int rename_peakfile (std::string newpath);
75  void touch_peakfile ();
76 
77  static void set_build_missing_peakfiles (bool yn) {
78  _build_missing_peakfiles = yn;
79  }
80 
81  static void set_build_peakfiles (bool yn) {
82  _build_peakfiles = yn;
83  }
84 
85  static bool get_build_peakfiles () {
86  return _build_peakfiles;
87  }
88 
89  virtual int setup_peakfile () { return 0; }
91 
93  void done_with_peakfile_writes (bool done = true);
94 
96  virtual bool clamped_at_unity () const = 0;
97 
98  protected:
100  static bool _build_peakfiles;
101 
102  /* these collections of working buffers for supporting
103  playlist's reading from potentially nested/recursive
104  sources assume SINGLE THREADED reads by the butler
105  thread, or a lock around calls that use them.
106  */
107 
108  static std::vector<std::shared_ptr<Sample[]> > _mixdown_buffers;
109  static std::vector<std::shared_ptr<gain_t[]> > _gain_buffers;
110  static Glib::Threads::Mutex _level_buffer_lock;
111 
112  std::string _peakpath;
113 
114  int initialize_peakfile (const std::string& path, const bool in_session = false);
116  int compute_and_write_peaks (Sample const * buf, samplecnt_t first_sample, samplecnt_t cnt,
117  bool force, bool intermediate_peaks_ready_signal);
119 
120  mutable off_t _peak_byte_max; // modified in compute_and_write_peak()
121 
123  virtual samplecnt_t write_unlocked (Sample const * dst, samplecnt_t cnt) = 0;
124  virtual std::string construct_peak_filepath (const std::string& audio_path, const bool in_session = false, const bool old_peak_name = false) const = 0;
125 
128  double samples_per_visual_peak, samplecnt_t fpp) const;
129 
130  int compute_and_write_peaks (Sample const * buf, samplecnt_t first_sample, samplecnt_t cnt,
131  bool force, bool intermediate_peaks_ready_signal,
132  samplecnt_t samples_per_peak);
133 
134  private:
142  mutable Glib::Threads::Mutex _peaks_ready_lock;
143  Glib::Threads::Mutex _initialize_peaks_lock;
144 
150 
151  mutable bool _first_run;
152  mutable double _last_scale;
153  mutable off_t _last_map_off;
154  mutable size_t _last_raw_map_length;
155  mutable std::unique_ptr<PeakData[]> peak_cache;
156 };
157 
158 }
159 
virtual void mark_streaming_write_completed(const WriterLock &lock, Temporal::timecnt_t const &duration)
bool peaks_ready(std::function< void()> callWhenReady, PBD::ScopedConnection **connection_created_if_not_ready, PBD::EventLoop *event_loop) const
virtual samplecnt_t read_unlocked(Sample *dst, samplepos_t start, samplecnt_t cnt) const =0
int compute_and_write_peaks(Sample const *buf, samplecnt_t first_sample, samplecnt_t cnt, bool force, bool intermediate_peaks_ready_signal)
samplecnt_t peak_leftover_cnt
Definition: audiosource.h:146
static void set_build_missing_peakfiles(bool yn)
Definition: audiosource.h:77
XMLNode & get_state() const
int set_state(const XMLNode &, int version)
virtual samplecnt_t read(Sample *dst, samplepos_t start, samplecnt_t cnt, int channel=0) const
static void set_build_peakfiles(bool yn)
Definition: audiosource.h:81
int prepare_for_peakfile_writes()
AudioSource(Session &, const XMLNode &)
int read_peaks(PeakData *peaks, samplecnt_t npeaks, samplepos_t start, samplecnt_t cnt, double samples_per_visual_peak) const
static bool get_build_peakfiles()
Definition: audiosource.h:85
PBD::Signal< void()> PeaksReady
Definition: audiosource.h:68
virtual std::string construct_peak_filepath(const std::string &audio_path, const bool in_session=false, const bool old_peak_name=false) const =0
virtual samplecnt_t available_peaks(double zoom) const
std::string _peakpath
Definition: audiosource.h:112
int rename_peakfile(std::string newpath)
int build_peaks_from_scratch()
Glib::Threads::Mutex _initialize_peaks_lock
Definition: audiosource.h:143
static std::vector< std::shared_ptr< Sample[]> > _mixdown_buffers
Definition: audiosource.h:108
size_t _last_raw_map_length
Definition: audiosource.h:154
samplecnt_t readable_length_samples() const
Definition: audiosource.h:46
static Glib::Threads::Mutex _level_buffer_lock
Definition: audiosource.h:110
PBD::Signal< void(samplepos_t, samplepos_t)> PeakRangeReady
Definition: audiosource.h:69
Glib::Threads::Mutex _peaks_ready_lock
Definition: audiosource.h:142
virtual bool can_truncate_peaks() const
Definition: audiosource.h:60
virtual int setup_peakfile()
Definition: audiosource.h:89
virtual bool clamped_at_unity() const =0
void update_length(timepos_t const &dur)
int initialize_peakfile(const std::string &path, const bool in_session=false)
void done_with_peakfile_writes(bool done=true)
static std::vector< std::shared_ptr< gain_t[]> > _gain_buffers
Definition: audiosource.h:109
static bool _build_missing_peakfiles
Definition: audiosource.h:99
samplepos_t peak_leftover_sample
Definition: audiosource.h:149
virtual uint32_t n_channels() const
Definition: audiosource.h:47
virtual ~AudioSource()
Sample * peak_leftovers
Definition: audiosource.h:148
AudioSource(Session &, const std::string &name)
virtual int read_peaks_with_fpp(PeakData *peaks, samplecnt_t npeaks, samplepos_t start, samplecnt_t cnt, double samples_per_visual_peak, samplecnt_t fpp) const
std::unique_ptr< PeakData[]> peak_cache
Definition: audiosource.h:155
int compute_and_write_peaks(Sample const *buf, samplecnt_t first_sample, samplecnt_t cnt, bool force, bool intermediate_peaks_ready_signal, samplecnt_t samples_per_peak)
samplecnt_t peak_leftover_size
Definition: audiosource.h:147
virtual samplecnt_t write_unlocked(Sample const *dst, samplecnt_t cnt)=0
virtual samplecnt_t write(Sample const *src, samplecnt_t cnt)
virtual float sample_rate() const =0
static bool _build_peakfiles
Definition: audiosource.h:100
Glib::Threads::RWLock::WriterLock WriterLock
Definition: xml++.h:114
GtkImageIconNameData name
Definition: gtkimage.h:6
#define LIBARDOUR_API
void peaks(const float *data, float &min, float &max, uint32_t n_samples)
PBD::PropertyDescriptor< timepos_t > start
Temporal::samplecnt_t samplecnt_t
Temporal::samplepos_t samplepos_t