fixes for various bugs including dangling ref to route in session, opening sessions...
[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 <time.h>
29
30 #include <glibmm/thread.h>
31
32 #include <sigc++/signal.h>
33
34 #include <ardour/source.h>
35 #include <ardour/ardour.h>
36 #include <pbd/stateful.h> 
37 #include <pbd/xml++.h>
38
39 using std::list;
40 using std::vector;
41 using std::string;
42
43 namespace ARDOUR {
44
45 const nframes_t frames_per_peak = 256;
46
47 class AudioSource : public Source
48 {
49   public:
50         AudioSource (Session&, string name);
51         AudioSource (Session&, const XMLNode&);
52         virtual ~AudioSource ();
53
54         /* one could argue that this should belong to Source, but other data types
55            generally do not come with a model of "offset along an audio timeline"
56            so its here in AudioSource for now.
57         */
58
59         virtual nframes_t natural_position() const { return 0; }
60         
61         /* returns the number of items in this `audio_source' */
62
63         virtual nframes_t length() const {
64                 return _length;
65         }
66
67         virtual nframes_t available_peaks (double zoom) const;
68
69         virtual nframes_t read (Sample *dst, nframes_t start, nframes_t cnt) const;
70         virtual nframes_t write (Sample *src, nframes_t cnt);
71
72         virtual float sample_rate () const = 0;
73
74         virtual void mark_for_remove() = 0;
75         virtual void mark_streaming_write_completed () {}
76
77         void set_captured_for (string str) { _captured_for = str; }
78         string captured_for() const { return _captured_for; }
79
80         uint32_t read_data_count() const { return _read_data_count; }
81         uint32_t write_data_count() const { return _write_data_count; }
82
83         int  read_peaks (PeakData *peaks, nframes_t npeaks, nframes_t start, nframes_t cnt, double samples_per_unit) const;
84         int  build_peaks ();
85         bool peaks_ready (sigc::slot<void>, sigc::connection&) const;
86
87         mutable sigc::signal<void>  PeaksReady;
88         mutable sigc::signal<void,nframes_t,nframes_t>  PeakRangeReady;
89         
90         XMLNode& get_state ();
91         int set_state (const XMLNode&);
92
93         static int  start_peak_thread ();
94         static void stop_peak_thread ();
95
96         int rename_peakfile (std::string newpath);
97         void touch_peakfile ();
98
99         static void set_build_missing_peakfiles (bool yn) {
100                 _build_missing_peakfiles = yn;
101         }
102
103         static void set_build_peakfiles (bool yn) {
104                 _build_peakfiles = yn;
105         }
106
107   protected:
108         static bool _build_missing_peakfiles;
109         static bool _build_peakfiles;
110
111         bool                _peaks_built;
112         mutable Glib::Mutex _lock;
113         nframes_t      _length;
114         bool                 next_peak_clear_should_notify;
115         string               peakpath;
116         string              _captured_for;
117
118         mutable uint32_t _read_data_count;  // modified in read()
119         mutable uint32_t _write_data_count; // modified in write()
120
121         int initialize_peakfile (bool newfile, string path);
122         void build_peaks_from_scratch ();
123
124         int  do_build_peak (nframes_t, nframes_t);
125
126         virtual nframes_t read_unlocked (Sample *dst, nframes_t start, nframes_t cnt) const = 0;
127         virtual nframes_t write_unlocked (Sample *dst, nframes_t cnt) = 0;
128         virtual string peak_path(string audio_path) = 0;
129         virtual string old_peak_path(string audio_path) = 0;
130         
131         void update_length (nframes_t pos, nframes_t cnt);
132
133         static pthread_t peak_thread;
134         static bool      have_peak_thread;
135         static void*     peak_thread_work(void*);
136
137         static int peak_request_pipe[2];
138
139         struct PeakRequest {
140             enum Type {
141                     Build,
142                     Quit
143             };
144         };
145
146         static vector<AudioSource*> pending_peak_sources;
147         static Glib::Mutex* pending_peak_sources_lock;
148
149         static void queue_for_peaks (AudioSource*);
150         static void clear_queue_for_peaks ();
151         
152         struct PeakBuildRecord {
153             nframes_t frame;
154             nframes_t cnt;
155
156             PeakBuildRecord (nframes_t f, nframes_t c) 
157                     : frame (f), cnt (c) {}
158             PeakBuildRecord (const PeakBuildRecord& other) {
159                     frame = other.frame;
160                     cnt = other.cnt;
161             }
162         };
163
164         list<AudioSource::PeakBuildRecord *> pending_peak_builds;
165
166   private:
167         bool file_changed (string path);
168 };
169
170 }
171
172 #endif /* __ardour_audio_source_h__ */