First stage of options rework.
[ardour.git] / libs / ardour / ardour / midi_diskstream.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: diskstream.h 579 2006-06-12 19:56:37Z essej $
19 */
20
21 #ifndef __ardour_midi_diskstream_h__
22 #define __ardour_midi_diskstream_h__
23
24 #include <sigc++/signal.h>
25
26 #include <cmath>
27 #include <cassert>
28 #include <string>
29 #include <queue>
30 #include <map>
31 #include <vector>
32
33 #include <time.h>
34
35 #include "pbd/fastlog.h"
36 #include "pbd/ringbufferNPT.h"
37
38 #include "ardour/ardour.h"
39 #include "ardour/diskstream.h"
40 #include "ardour/midi_playlist.h"
41 #include "ardour/midi_ring_buffer.h"
42 #include "ardour/midi_state_tracker.h"
43 #include "ardour/utils.h"
44
45 struct tm;
46
47 namespace ARDOUR {
48
49 class IO;
50 class MidiEngine;
51 class MidiPort;
52 class MidiRingbuffer;
53 class SMFSource;
54 class Send;
55 class Session;
56
57 class MidiDiskstream : public Diskstream
58 {       
59   public:
60         MidiDiskstream (Session &, const string& name, Diskstream::Flag f = Recordable);
61         MidiDiskstream (Session &, const XMLNode&);
62         ~MidiDiskstream();
63
64         float playback_buffer_load() const;
65         float capture_buffer_load() const;
66         
67         void get_playback(MidiBuffer& dst, nframes_t start, nframes_t end);
68
69         void set_record_enabled (bool yn);
70
71         boost::shared_ptr<MidiPlaylist> midi_playlist () { return boost::dynamic_pointer_cast<MidiPlaylist>(_playlist); }
72
73         int use_playlist (boost::shared_ptr<Playlist>);
74         int use_new_playlist ();
75         int use_copy_playlist ();
76
77         /* stateful */
78         XMLNode& get_state(void);
79         int set_state(const XMLNode& node);
80
81         void monitor_input (bool);
82         
83         MidiRingBuffer<nframes_t>*   playback_buffer () { return _playback_buf; }
84         MidiRingBuffer<nframes_t>*   capture_buffer ()  { return _capture_buf; }
85         boost::shared_ptr<SMFSource> write_source ()    { return _write_source; }
86         
87         int set_destructive (bool yn); // doom!
88         
89         void set_note_mode (NoteMode m);
90         
91         uint16_t get_channel_mask() { 
92                 uint16_t playback_mask = _playback_buf->get_channel_mask();
93 #ifndef NDEBUG
94                 uint16_t capture_mask  = _capture_buf->get_channel_mask();
95                 assert(playback_mask == capture_mask);
96 #endif
97                 return playback_mask;
98         }
99
100         void set_channel_mode(ChannelMode mode, uint16_t mask) {
101                 _playback_buf->set_channel_mode(mode, mask); 
102                 _capture_buf->set_channel_mode(mode, mask); 
103         }
104         
105         ChannelMode get_channel_mode() {
106                 ChannelMode playback_mode = _playback_buf->get_channel_mode();
107 #ifndef NDEBUG
108                 ChannelMode capture_mode  = _capture_buf->get_channel_mode();
109                 assert(playback_mode == capture_mode);
110 #endif
111                 return playback_mode;
112         }
113
114   protected:
115         friend class Session;
116
117         /* the Session is the only point of access for these
118            because they require that the Session is "inactive"
119            while they are called.
120         */
121
122         void set_pending_overwrite(bool);
123         int  overwrite_existing_buffers ();
124         void set_block_size (nframes_t);
125         int  internal_playback_seek (nframes_t distance);
126         int  can_internal_playback_seek (nframes_t distance);
127         int  rename_write_sources ();
128         void reset_write_sources (bool, bool force = false);
129         void non_realtime_input_change ();
130         void non_realtime_locate (nframes_t location);
131
132         static void set_readahead_frames(nframes_t frames_ahead) { midi_readahead = frames_ahead; }
133
134   protected:
135         int seek (nframes_t which_sample, bool complete_refill = false);
136
137   protected:
138         friend class MidiTrack;
139
140         int  process (nframes_t transport_frame, nframes_t nframes, bool can_record, bool rec_monitors_input);
141         bool commit  (nframes_t nframes);
142         static nframes_t midi_readahead;
143
144   private:
145
146         /* The two central butler operations */
147         int do_flush (RunContext context, bool force = false);
148         int do_refill ();
149         
150         int do_refill_with_alloc();
151
152         int read (nframes_t& start, nframes_t cnt, bool reversed);
153
154         void finish_capture (bool rec_monitors_input);
155         void transport_stopped (struct tm&, time_t, bool abort);
156         void transport_looped (nframes_t transport_frame);
157
158         void init (Diskstream::Flag);
159
160         int use_new_write_source (uint32_t n=0);
161
162         int find_and_use_playlist (const string&);
163
164         void allocate_temporary_buffers ();
165
166         int use_pending_capture_data (XMLNode& node);
167
168         void get_input_sources ();
169         void check_record_status (nframes_t transport_frame, nframes_t nframes, bool can_record);
170         void set_align_style_from_io();
171         
172         void engage_record_enable ();
173         void disengage_record_enable ();
174
175         MidiRingBuffer<nframes_t>*   _playback_buf;
176         MidiRingBuffer<nframes_t>*   _capture_buf;
177         MidiPort*                    _source_port;
178         boost::shared_ptr<SMFSource> _write_source;
179         nframes_t                    _last_flush_frame;
180         NoteMode                     _note_mode;  
181         MidiStateTracker             _midi_state_tracker;
182         MidiStateTracker             _incoming_midi_state_tracker;
183         volatile gint                _frames_written_to_ringbuffer;
184         volatile gint                _frames_read_from_ringbuffer;
185 };
186
187 }; /* namespace ARDOUR */
188
189 #endif /* __ardour_midi_diskstream_h__ */