0734a66319b66183a7cbe6ab6470ed12e0b9728c
[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         void truncate_peakfile();
117
118         mutable off_t _peak_byte_max; // modified in do_build_peaks()
119
120         virtual nframes_t read_unlocked (Sample *dst, nframes_t start, nframes_t cnt) const = 0;
121         virtual nframes_t write_unlocked (Sample *dst, nframes_t cnt) = 0;
122         virtual string peak_path(string audio_path) = 0;
123         virtual string old_peak_path(string audio_path) = 0;
124         
125         static pthread_t peak_thread;
126         static bool      have_peak_thread;
127         static void*     peak_thread_work(void*);
128
129         static int peak_request_pipe[2];
130
131         struct PeakRequest {
132             enum Type {
133                     Build,
134                     Quit
135             };
136         };
137
138         static vector<boost::shared_ptr<AudioSource> > pending_peak_sources;
139         static Glib::Mutex* pending_peak_sources_lock;
140
141         static void queue_for_peaks (boost::shared_ptr<AudioSource>, bool notify=true);
142         static void clear_queue_for_peaks ();
143         
144         struct PeakBuildRecord {
145             nframes_t frame;
146             nframes_t cnt;
147
148             PeakBuildRecord (nframes_t f, nframes_t c) 
149                     : frame (f), cnt (c) {}
150             PeakBuildRecord (const PeakBuildRecord& other) {
151                     frame = other.frame;
152                     cnt = other.cnt;
153             }
154         };
155
156         list<AudioSource::PeakBuildRecord *> pending_peak_builds;
157
158   private:
159         bool file_changed (string path);
160 };
161
162 }
163
164 #endif /* __ardour_audio_source_h__ */