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