2ada2552369a490eee4d672ebab8d43fa35732c8
[ardour.git] / libs / ardour / ardour / audiosource.h
1 /*
2     Copyright (C) 2000 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id: audio_source.h 486 2006-04-27 09:04:24Z pauld $
19 */
20
21 #ifndef __ardour_audio_source_h__
22 #define __ardour_audio_source_h__
23
24 #include <list>
25 #include <vector>
26 #include <string>
27
28 #include <boost/shared_ptr.hpp>
29 #include <boost/enable_shared_from_this.hpp>
30
31 #include <time.h>
32
33 #include <glibmm/thread.h>
34
35 #include <sigc++/signal.h>
36
37 #include <ardour/source.h>
38 #include <ardour/ardour.h>
39 #include <pbd/stateful.h> 
40 #include <pbd/xml++.h>
41
42 using std::list;
43 using std::vector;
44 using std::string;
45
46 namespace ARDOUR {
47
48 const nframes_t frames_per_peak = 256;
49
50  class AudioSource : public Source, public boost::enable_shared_from_this<ARDOUR::AudioSource>
51 {
52   public:
53         AudioSource (Session&, string name);
54         AudioSource (Session&, const XMLNode&);
55         virtual ~AudioSource ();
56         
57         virtual nframes_t available_peaks (double zoom) const;
58
59         virtual nframes_t read (Sample *dst, nframes_t start, nframes_t cnt) const;
60         virtual nframes_t write (Sample *src, nframes_t cnt);
61
62         virtual float sample_rate () const = 0;
63
64         virtual void mark_for_remove() = 0;
65         virtual void mark_streaming_write_completed () {}
66
67         void set_captured_for (string str) { _captured_for = str; }
68         string captured_for() const { return _captured_for; }
69
70         uint32_t read_data_count() const { return _read_data_count; }
71         uint32_t write_data_count() const { return _write_data_count; }
72
73         virtual int read_peaks (PeakData *peaks, nframes_t npeaks, nframes_t start, nframes_t cnt, double samples_per_unit) const;
74         int  build_peaks ();
75         bool peaks_ready (sigc::slot<void>, sigc::connection&) const;
76
77         mutable sigc::signal<void>  PeaksReady;
78         mutable sigc::signal<void,nframes_t,nframes_t>  PeakRangeReady;
79         
80         XMLNode& get_state ();
81         int set_state (const XMLNode&);
82
83         static int  start_peak_thread ();
84         static void stop_peak_thread ();
85
86         int rename_peakfile (std::string newpath);
87         void touch_peakfile ();
88
89         static void set_build_missing_peakfiles (bool yn) {
90                 _build_missing_peakfiles = yn;
91         }
92
93         static void set_build_peakfiles (bool yn) {
94                 _build_peakfiles = yn;
95         }
96
97         virtual int setup_peakfile () { return 0; }
98
99   protected:
100         static bool _build_missing_peakfiles;
101         static bool _build_peakfiles;
102
103         bool                _peaks_built;
104         mutable Glib::Mutex _lock;
105         bool                 next_peak_clear_should_notify;
106         string               peakpath;
107         string              _captured_for;
108
109         mutable uint32_t _read_data_count;  // modified in read()
110         mutable uint32_t _write_data_count; // modified in write()
111
112         int initialize_peakfile (bool newfile, string path);
113         void build_peaks_from_scratch ();
114
115         int  do_build_peak (nframes_t, nframes_t);
116
117         virtual nframes_t read_unlocked (Sample *dst, nframes_t start, nframes_t cnt) const = 0;
118         virtual nframes_t write_unlocked (Sample *dst, nframes_t cnt) = 0;
119         virtual string peak_path(string audio_path) = 0;
120         virtual string old_peak_path(string audio_path) = 0;
121         
122         static pthread_t peak_thread;
123         static bool      have_peak_thread;
124         static void*     peak_thread_work(void*);
125
126         static int peak_request_pipe[2];
127
128         struct PeakRequest {
129             enum Type {
130                     Build,
131                     Quit
132             };
133         };
134
135         static vector<boost::shared_ptr<AudioSource> > pending_peak_sources;
136         static Glib::Mutex* pending_peak_sources_lock;
137
138         static void queue_for_peaks (boost::shared_ptr<AudioSource>);
139         static void clear_queue_for_peaks ();
140         
141         struct PeakBuildRecord {
142             nframes_t frame;
143             nframes_t cnt;
144
145             PeakBuildRecord (nframes_t f, nframes_t c) 
146                     : frame (f), cnt (c) {}
147             PeakBuildRecord (const PeakBuildRecord& other) {
148                     frame = other.frame;
149                     cnt = other.cnt;
150             }
151         };
152
153         list<AudioSource::PeakBuildRecord *> pending_peak_builds;
154
155   private:
156         bool file_changed (string path);
157 };
158
159 }
160
161 #endif /* __ardour_audio_source_h__ */