Merged with trunk revision 600
[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 <ardour/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 jack_nframes_t frames_per_peak = 256;
46
47 class AudioSource : public Source
48 {
49   public:
50         AudioSource (string name);
51         AudioSource (const XMLNode&);
52         virtual ~AudioSource ();
53
54         /* returns the number of items in this `audio_source' */
55
56         virtual jack_nframes_t length() const {
57                 return _length;
58         }
59
60         virtual jack_nframes_t available_peaks (double zoom) const;
61
62         virtual jack_nframes_t read (Sample *dst, jack_nframes_t start, jack_nframes_t cnt, char * workbuf) const;
63         virtual jack_nframes_t write (Sample *src, jack_nframes_t cnt, char * workbuf);
64
65         virtual float sample_rate () const = 0;
66
67         virtual void mark_for_remove() = 0;
68         virtual void mark_streaming_write_completed () {}
69
70         void set_captured_for (string str) { _captured_for = str; }
71         string captured_for() const { return _captured_for; }
72
73         uint32_t read_data_count() const { return _read_data_count; }
74         uint32_t write_data_count() const { return _write_data_count; }
75
76         int  read_peaks (PeakData *peaks, jack_nframes_t npeaks, jack_nframes_t start, jack_nframes_t cnt, double samples_per_unit) const;
77         int  build_peaks ();
78         bool peaks_ready (sigc::slot<void>, sigc::connection&) const;
79
80         static sigc::signal<void,AudioSource*> AudioSourceCreated;
81                
82         mutable sigc::signal<void>  PeaksReady;
83         mutable sigc::signal<void,jack_nframes_t,jack_nframes_t>  PeakRangeReady;
84         
85         XMLNode& get_state ();
86         int set_state (const XMLNode&);
87
88         static int  start_peak_thread ();
89         static void stop_peak_thread ();
90
91         int rename_peakfile (std::string newpath);
92
93         static void set_build_missing_peakfiles (bool yn) {
94                 _build_missing_peakfiles = yn;
95         }
96
97         static void set_build_peakfiles (bool yn) {
98                 _build_peakfiles = yn;
99         }
100
101   protected:
102         static bool _build_missing_peakfiles;
103         static bool _build_peakfiles;
104
105         bool                _peaks_built;
106         mutable Glib::Mutex _lock;
107         jack_nframes_t      _length;
108         bool                 next_peak_clear_should_notify;
109         string               peakpath;
110         string              _captured_for;
111
112         mutable uint32_t _read_data_count;  // modified in read()
113         mutable uint32_t _write_data_count; // modified in write()
114
115         int initialize_peakfile (bool newfile, string path);
116         void build_peaks_from_scratch ();
117
118         int  do_build_peak (jack_nframes_t, jack_nframes_t);
119
120         virtual jack_nframes_t read_unlocked (Sample *dst, jack_nframes_t start, jack_nframes_t cnt, char * workbuf) const = 0;
121         virtual jack_nframes_t write_unlocked (Sample *dst, jack_nframes_t cnt, char * workbuf) = 0;
122         virtual string peak_path(string audio_path) = 0;
123         virtual string old_peak_path(string audio_path) = 0;
124         
125         void update_length (jack_nframes_t pos, jack_nframes_t cnt);
126
127         static pthread_t peak_thread;
128         static bool      have_peak_thread;
129         static void*     peak_thread_work(void*);
130
131         static int peak_request_pipe[2];
132
133         struct PeakRequest {
134             enum Type {
135                     Build,
136                     Quit
137             };
138         };
139
140         static vector<AudioSource*> pending_peak_sources;
141         static Glib::Mutex* pending_peak_sources_lock;
142
143         static void queue_for_peaks (AudioSource&);
144         static void clear_queue_for_peaks ();
145         
146         struct PeakBuildRecord {
147             jack_nframes_t frame;
148             jack_nframes_t cnt;
149
150             PeakBuildRecord (jack_nframes_t f, jack_nframes_t c) 
151                     : frame (f), cnt (c) {}
152             PeakBuildRecord (const PeakBuildRecord& other) {
153                     frame = other.frame;
154                     cnt = other.cnt;
155             }
156         };
157
158         list<AudioSource::PeakBuildRecord *> pending_peak_builds;
159
160   private:
161         bool file_changed (string path);
162 };
163
164 }
165
166 #endif /* __ardour_audio_source_h__ */