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