34a53a5a25abb3561c410d13458864bc9cf0a1be
[ardour.git] / libs / ardour / ardour / disk_writer.h
1 /*
2     Copyright (C) 2009-2016 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 */
19
20 #ifndef __ardour_disk_writer_h__
21 #define __ardour_disk_writer_h__
22
23 #include <list>
24 #include <vector>
25
26 #include "pbd/i18n.h"
27
28 #include "ardour/disk_io.h"
29 #include "ardour/midi_buffer.h"
30
31 namespace ARDOUR
32 {
33
34 class AudioFileSource;
35 class SMFSource;
36 class MidiSource;
37
38 class LIBARDOUR_API DiskWriter : public DiskIOProcessor
39 {
40   public:
41         DiskWriter (Session&, std::string const & name, DiskIOProcessor::Flag f = DiskIOProcessor::Flag (0));
42
43         bool set_name (std::string const & str);
44         std::string display_name() const { return std::string (_("writer")); }
45
46         virtual bool set_write_source_name (const std::string& str);
47
48         bool           recordable()  const { return _flags & Recordable; }
49
50         static framecnt_t chunk_frames() { return _chunk_frames; }
51         static framecnt_t default_chunk_frames ();
52         static void set_chunk_frames (framecnt_t n) { _chunk_frames = n; }
53
54         void run (BufferSet& /*bufs*/, framepos_t /*start_frame*/, framepos_t /*end_frame*/, double speed, pframes_t /*nframes*/, bool /*result_required*/);
55         void non_realtime_locate (framepos_t);
56         void realtime_handle_transport_stopped ();
57
58         virtual XMLNode& state (bool full);
59         int set_state (const XMLNode&, int version);
60
61         std::string write_source_name () const {
62                 if (_write_source_name.empty()) {
63                         return name();
64                 } else {
65                         return _write_source_name;
66                 }
67         }
68
69         boost::shared_ptr<AudioFileSource> audio_write_source (uint32_t n=0) {
70                 boost::shared_ptr<ChannelList> c = channels.reader();
71                 if (n < c->size()) {
72                         return (*c)[n]->write_source;
73                 }
74
75                 return boost::shared_ptr<AudioFileSource>();
76         }
77
78         boost::shared_ptr<SMFSource> midi_write_source () { return _midi_write_source; }
79
80         virtual std::string steal_write_source_name ();
81         int use_new_write_source (DataType, uint32_t n = 0);
82         void reset_write_sources (bool, bool force = false);
83
84         AlignStyle  alignment_style() const { return _alignment_style; }
85         AlignChoice alignment_choice() const { return _alignment_choice; }
86         void       set_align_style (AlignStyle, bool force=false);
87         void       set_align_choice (AlignChoice a, bool force=false);
88
89         PBD::Signal0<void> AlignmentStyleChanged;
90
91         void set_input_latency (framecnt_t);
92         framecnt_t input_latency () const { return _input_latency; }
93
94         bool configure_io (ChanCount in, ChanCount out);
95
96         std::list<boost::shared_ptr<Source> >& last_capture_sources () { return _last_capture_sources; }
97
98         bool         record_enabled() const { return g_atomic_int_get (const_cast<gint*>(&_record_enabled)); }
99         bool         record_safe () const { return g_atomic_int_get (const_cast<gint*>(&_record_safe)); }
100         virtual void set_record_enabled (bool yn);
101         virtual void set_record_safe (bool yn);
102
103         bool destructive() const { return _flags & Destructive; }
104         int set_destructive (bool yn);
105         int set_non_layered (bool yn);
106         bool can_become_destructive (bool& requires_bounce) const;
107
108         /** @return Start position of currently-running capture (in session frames) */
109         framepos_t current_capture_start() const { return capture_start_frame; }
110         framepos_t current_capture_end()   const { return capture_start_frame + capture_captured; }
111         framepos_t get_capture_start_frame (uint32_t n = 0) const;
112         framecnt_t get_captured_frames (uint32_t n = 0) const;
113
114         float buffer_load() const;
115
116         virtual void request_input_monitoring (bool) {}
117         virtual void ensure_input_monitoring (bool) {}
118
119         framecnt_t   capture_offset() const { return _capture_offset; }
120         virtual void set_capture_offset ();
121
122         int seek (framepos_t frame, bool complete_refill);
123
124         static PBD::Signal0<void> Overrun;
125
126         void set_note_mode (NoteMode m);
127
128         /** Emitted when some MIDI data has been received for recording.
129          *  Parameter is the source that it is destined for.
130          *  A caller can get a copy of the data with get_gui_feed_buffer ()
131          */
132         PBD::Signal1<void, boost::weak_ptr<MidiSource> > DataRecorded;
133
134         PBD::Signal0<void> RecordEnableChanged;
135         PBD::Signal0<void> RecordSafeChanged;
136
137         void check_record_status (framepos_t transport_frame, bool can_record);
138
139         void transport_looped (framepos_t transport_frame);
140         void transport_stopped_wallclock (struct tm&, time_t, bool abort);
141
142         void adjust_buffering ();
143
144   protected:
145         friend class Track;
146         int do_flush (RunContext context, bool force = false);
147
148         void get_input_sources ();
149         void prepare_record_status (framepos_t /*capture_start_frame*/);
150         void set_align_style_from_io();
151         void setup_destructive_playlist ();
152         void use_destructive_playlist ();
153         void prepare_to_stop (framepos_t transport_pos, framepos_t audible_frame);
154
155         void engage_record_enable ();
156         void disengage_record_enable ();
157         void engage_record_safe ();
158         void disengage_record_safe ();
159
160         bool prep_record_enable ();
161         bool prep_record_disable ();
162
163         void calculate_record_range (
164                 Evoral::OverlapType ot, framepos_t transport_frame, framecnt_t nframes,
165                 framecnt_t& rec_nframes, framecnt_t& rec_offset
166                 );
167
168         struct CaptureInfo {
169                 framepos_t start;
170                 framecnt_t frames;
171         };
172
173         std::vector<CaptureInfo*> capture_info;
174         mutable Glib::Threads::Mutex capture_info_lock;
175
176   private:
177         framecnt_t   _input_latency;
178         gint         _record_enabled;
179         gint         _record_safe;
180         framepos_t    capture_start_frame;
181         framecnt_t    capture_captured;
182         bool          was_recording;
183         framecnt_t    adjust_capture_position;
184         framecnt_t   _capture_offset;
185         framepos_t    first_recordable_frame;
186         framepos_t    last_recordable_frame;
187         int           last_possibly_recording;
188         AlignStyle   _alignment_style;
189         AlignChoice  _alignment_choice;
190         std::string   _write_source_name;
191         boost::shared_ptr<SMFSource> _midi_write_source;
192
193         std::list<boost::shared_ptr<Source> > _last_capture_sources;
194         std::vector<boost::shared_ptr<AudioFileSource> > capturing_sources;
195
196         static framecnt_t _chunk_frames;
197
198         NoteMode                     _note_mode;
199         volatile gint                _frames_pending_write;
200         volatile gint                _num_captured_loops;
201         framepos_t                   _accumulated_capture_offset;
202
203         /** A buffer that we use to put newly-arrived MIDI data in for
204             the GUI to read (so that it can update itself).
205         */
206         MidiBuffer                   _gui_feed_buffer;
207         mutable Glib::Threads::Mutex _gui_feed_buffer_mutex;
208
209         void finish_capture (boost::shared_ptr<ChannelList> c);
210 };
211
212 } // namespace
213
214 #endif /* __ardour_disk_writer_h__ */