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