revert last commit, wrong place...
[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, framecnt_t);
66         void flush_playback (framepos_t, framepos_t);
67
68         void set_record_enabled (bool yn);
69         
70         void reset_tracker ();
71
72         boost::shared_ptr<MidiPlaylist> midi_playlist () { return boost::dynamic_pointer_cast<MidiPlaylist>(_playlist); }
73
74         int use_playlist (boost::shared_ptr<Playlist>);
75         int use_new_playlist ();
76         int use_copy_playlist ();
77
78         bool set_name (std::string const &);
79
80         /* stateful */
81         XMLNode& get_state(void);
82         int set_state(const XMLNode&, int version);
83
84         void ensure_jack_monitors_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         /** Emitted when some MIDI data has been received for recording.
116          *  Parameter is the source that it is destined for.
117          *  A caller can get a copy of the data with get_gui_feed_buffer ()
118          */
119         PBD::Signal1<void, boost::weak_ptr<MidiSource> > DataRecorded;
120
121         boost::shared_ptr<MidiBuffer> get_gui_feed_buffer () const;
122
123   protected:
124         friend class Session;
125         friend class Butler;
126
127         /* the Session is the only point of access for these
128            because they require that the Session is "inactive"
129            while they are called.
130         */
131
132         void set_pending_overwrite(bool);
133         int  overwrite_existing_buffers ();
134         void set_block_size (pframes_t);
135         int  internal_playback_seek (framecnt_t distance);
136         int  can_internal_playback_seek (framecnt_t distance);
137         std::list<boost::shared_ptr<Source> > steal_write_sources();
138         void reset_write_sources (bool, bool force = false);
139         void non_realtime_input_change ();
140         void non_realtime_locate (framepos_t location);
141
142         static void set_readahead_frames (framecnt_t frames_ahead) { midi_readahead = frames_ahead; }
143
144   protected:
145         int seek (framepos_t which_sample, bool complete_refill = false);
146
147   protected:
148         friend class MidiTrack;
149
150         int  process (framepos_t transport_frame, pframes_t nframes, framecnt_t &);
151         bool commit  (framecnt_t nframes);
152         static framecnt_t midi_readahead;
153
154   private:
155
156         /* The two central butler operations */
157         int do_flush (RunContext context, bool force = false);
158         int do_refill ();
159
160         int do_refill_with_alloc();
161
162         int read (framepos_t& start, framecnt_t cnt, bool reversed);
163
164         void finish_capture ();
165         void transport_stopped_wallclock (struct tm&, time_t, bool abort);
166         void transport_looped (framepos_t transport_frame);
167
168         void init ();
169
170         int use_new_write_source (uint32_t n=0);
171
172         int find_and_use_playlist (const string&);
173
174         void allocate_temporary_buffers ();
175
176         int use_pending_capture_data (XMLNode& node);
177
178         void get_input_sources ();
179         void set_align_style_from_io();
180
181         /* fixed size buffers per instance of ardour for now (non-dynamic)
182          */
183
184         void adjust_playback_buffering () {}
185         void adjust_capture_buffering () {}
186
187         void engage_record_enable ();
188         void disengage_record_enable ();
189
190         MidiRingBuffer<framepos_t>*  _playback_buf;
191         MidiRingBuffer<framepos_t>*  _capture_buf;
192         boost::weak_ptr<MidiPort>    _source_port;
193         boost::shared_ptr<SMFSource> _write_source;
194         NoteMode                     _note_mode;
195         volatile gint                _frames_written_to_ringbuffer;
196         volatile gint                _frames_read_from_ringbuffer;
197
198         /** A buffer that we use to put newly-arrived MIDI data in for
199             the GUI to read (so that it can update itself).
200         */
201         MidiBuffer                   _gui_feed_buffer;
202         mutable Glib::Threads::Mutex          _gui_feed_buffer_mutex;
203 };
204
205 }; /* namespace ARDOUR */
206
207 #endif /* __ardour_midi_diskstream_h__ */