fixed peak waveform issue introduced by the ftruncate preallocation of peakfile....
[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         /* one could argue that this should belong to Source, but other data types
58            generally do not come with a model of "offset along an audio timeline"
59            so its here in AudioSource for now.
60         */
61
62         virtual nframes_t natural_position() const { return 0; }
63         
64         /* returns the number of items in this `audio_source' */
65
66         virtual nframes_t length() const {
67                 return _length;
68         }
69
70         virtual nframes_t available_peaks (double zoom) const;
71
72         virtual nframes_t read (Sample *dst, nframes_t start, nframes_t cnt) const;
73         virtual nframes_t write (Sample *src, nframes_t cnt);
74
75         virtual float sample_rate () const = 0;
76
77         virtual void mark_for_remove() = 0;
78         virtual void mark_streaming_write_completed () {}
79
80         void set_captured_for (string str) { _captured_for = str; }
81         string captured_for() const { return _captured_for; }
82
83         uint32_t read_data_count() const { return _read_data_count; }
84         uint32_t write_data_count() const { return _write_data_count; }
85
86         virtual int read_peaks (PeakData *peaks, nframes_t npeaks, nframes_t start, nframes_t cnt, double samples_per_unit) const;
87         int  build_peaks ();
88         bool peaks_ready (sigc::slot<void>, sigc::connection&) const;
89
90         mutable sigc::signal<void>  PeaksReady;
91         mutable sigc::signal<void,nframes_t,nframes_t>  PeakRangeReady;
92         
93         XMLNode& get_state ();
94         int set_state (const XMLNode&);
95
96         static int  start_peak_thread ();
97         static void stop_peak_thread ();
98
99         int rename_peakfile (std::string newpath);
100         void touch_peakfile ();
101
102         static void set_build_missing_peakfiles (bool yn) {
103                 _build_missing_peakfiles = yn;
104         }
105
106         static void set_build_peakfiles (bool yn) {
107                 _build_peakfiles = yn;
108         }
109
110         virtual int setup_peakfile () { return 0; }
111
112   protected:
113         static bool _build_missing_peakfiles;
114         static bool _build_peakfiles;
115
116         bool                _peaks_built;
117         mutable Glib::Mutex _lock;
118         nframes_t           _length;
119         bool                 next_peak_clear_should_notify;
120         string               peakpath;
121         string              _captured_for;
122
123         mutable uint32_t _read_data_count;  // modified in read()
124         mutable uint32_t _write_data_count; // modified in write()
125
126         int initialize_peakfile (bool newfile, string path);
127         void build_peaks_from_scratch ();
128
129         int  do_build_peak (nframes_t, nframes_t);
130         void truncate_peakfile();
131
132         mutable off_t _peak_byte_max; // modified in do_build_peaks()
133
134         virtual nframes_t read_unlocked (Sample *dst, nframes_t start, nframes_t cnt) const = 0;
135         virtual nframes_t write_unlocked (Sample *dst, nframes_t cnt) = 0;
136         virtual string peak_path(string audio_path) = 0;
137         virtual string old_peak_path(string audio_path) = 0;
138         
139         void update_length (nframes_t pos, nframes_t cnt);
140
141         static pthread_t peak_thread;
142         static bool      have_peak_thread;
143         static void*     peak_thread_work(void*);
144
145         static int peak_request_pipe[2];
146
147         struct PeakRequest {
148             enum Type {
149                     Build,
150                     Quit
151             };
152         };
153
154         static vector<boost::shared_ptr<AudioSource> > pending_peak_sources;
155         static Glib::Mutex* pending_peak_sources_lock;
156
157         static void queue_for_peaks (boost::shared_ptr<AudioSource>, bool notify=true);
158         static void clear_queue_for_peaks ();
159         
160         struct PeakBuildRecord {
161             nframes_t frame;
162             nframes_t cnt;
163
164             PeakBuildRecord (nframes_t f, nframes_t c) 
165                     : frame (f), cnt (c) {}
166             PeakBuildRecord (const PeakBuildRecord& other) {
167                     frame = other.frame;
168                     cnt = other.cnt;
169             }
170         };
171
172         list<AudioSource::PeakBuildRecord *> pending_peak_builds;
173
174   private:
175         bool file_changed (string path);
176 };
177
178 }
179
180 #endif /* __ardour_audio_source_h__ */