Merged timbyr's win32 branch. -r 547:566.
[ardour.git] / libs / ardour / ardour / source.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$
19 */
20
21 #ifndef __ardour_source_h__
22 #define __ardour_source_h__
23
24 #include <list>
25 #include <vector>
26 #include <string>
27
28 #include <ctime>
29
30 #include <sigc++/signal.h>
31
32 #include <glibmm/thread.h>
33
34 #include <ardour/ardour.h>
35 #include <ardour/stateful.h>
36 #include <pbd/xml++.h>
37
38 using std::list;
39 using std::vector;
40 using std::string;
41
42 namespace ARDOUR {
43
44 struct PeakData {
45     typedef Sample PeakDatum;
46
47     PeakDatum min;
48     PeakDatum max;
49 };
50
51 const jack_nframes_t frames_per_peak = 256;
52
53 class Source : public Stateful, public sigc::trackable
54 {
55   public:
56         Source (bool announce=true);
57         Source (const XMLNode&);
58         virtual ~Source ();
59
60         const string& name() const { return _name; }
61
62         ARDOUR::id_t  id() const   { return _id; }
63
64         /* returns the number of items in this `source' */
65
66         virtual jack_nframes_t length() const {
67                 return _length;
68         }
69
70         virtual jack_nframes_t available_peaks (double zoom) const;
71
72         virtual jack_nframes_t read (Sample *dst, jack_nframes_t start, jack_nframes_t cnt, char * workbuf) const {
73                 return 0;
74         }
75
76         virtual jack_nframes_t write (Sample *src, jack_nframes_t cnt, char * workbuf) {
77                 return 0;
78         }
79
80         virtual float sample_rate () const { return 0; }
81
82         uint32_t use_cnt() const { return _use_cnt; }
83         void use ();
84         void release ();
85
86         virtual void mark_for_remove() = 0;
87         virtual void mark_streaming_write_completed () {}
88
89         time_t timestamp() const { return _timestamp; }
90         void stamp (time_t when) { _timestamp = when; }
91
92         void set_captured_for (string str) { _captured_for = str; }
93         string captured_for() const { return _captured_for; }
94
95         uint32_t read_data_count() const { return _read_data_count; }
96         uint32_t write_data_count() const { return _write_data_count; }
97
98         int  read_peaks (PeakData *peaks, jack_nframes_t npeaks, jack_nframes_t start, jack_nframes_t cnt, double samples_per_unit) const;
99         int  build_peaks ();
100         bool peaks_ready (sigc::slot<void>, sigc::connection&) const;
101
102         static sigc::signal<void,Source*> SourceCreated;
103                
104         sigc::signal<void,Source *> GoingAway;
105         mutable sigc::signal<void>  PeaksReady;
106         mutable sigc::signal<void,jack_nframes_t,jack_nframes_t>  PeakRangeReady;
107         
108         XMLNode& get_state ();
109         int set_state (const XMLNode&);
110
111         static int  start_peak_thread ();
112         static void stop_peak_thread ();
113
114         int rename_peakfile (std::string newpath);
115
116         static void set_build_missing_peakfiles (bool yn) {
117                 _build_missing_peakfiles = yn;
118         }
119         static void set_build_peakfiles (bool yn) {
120                 _build_peakfiles = yn;
121         }
122
123   protected:
124         static bool _build_missing_peakfiles;
125         static bool _build_peakfiles;
126
127         string            _name;
128         uint32_t     _use_cnt;
129         bool              _peaks_built;
130         mutable Glib::Mutex _lock;
131         jack_nframes_t    _length;
132         bool               next_peak_clear_should_notify;
133         string             peakpath;
134         time_t            _timestamp;
135         string            _captured_for;
136
137         mutable uint32_t _read_data_count;  // modified in read()
138         mutable uint32_t _write_data_count; // modified in write()
139
140         int initialize_peakfile (bool newfile, string path);
141         void build_peaks_from_scratch ();
142
143         int  do_build_peak (jack_nframes_t, jack_nframes_t);
144         virtual jack_nframes_t read_unlocked (Sample *dst, jack_nframes_t start, jack_nframes_t cnt, char * workbuf) const = 0;
145         virtual string peak_path(string audio_path) = 0;
146
147         static pthread_t peak_thread;
148         static bool      have_peak_thread;
149         static void*     peak_thread_work(void*);
150
151         static int peak_request_pipe[2];
152
153         struct PeakRequest {
154             enum Type {
155                     Build,
156                     Quit
157             };
158         };
159
160         static vector<Source*> pending_peak_sources;
161         static Glib::StaticMutex pending_peak_sources_lock;
162
163         static void queue_for_peaks (Source&);
164         static void clear_queue_for_peaks ();
165         
166         struct PeakBuildRecord {
167             jack_nframes_t frame;
168             jack_nframes_t cnt;
169
170             PeakBuildRecord (jack_nframes_t f, jack_nframes_t c) 
171                     : frame (f), cnt (c) {}
172             PeakBuildRecord (const PeakBuildRecord& other) {
173                     frame = other.frame;
174                     cnt = other.cnt;
175             }
176         };
177
178         list<Source::PeakBuildRecord *> pending_peak_builds;
179
180   private:
181         ARDOUR::id_t _id;
182         
183         bool file_changed (string path);
184 };
185
186 }
187
188 #endif /* __ardour_source_h__ */