- Changed IO's vector<Port*>'s to PortList
[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
39 #include <ardour/ardour.h>
40 #include <ardour/configuration.h>
41 #include <ardour/session.h>
42 #include <ardour/route_group.h>
43 #include <ardour/route.h>
44 #include <ardour/port.h>
45 #include <ardour/utils.h>
46 #include <ardour/diskstream.h>
47 #include <ardour/midi_playlist.h>
48 struct tm;
49
50 namespace ARDOUR {
51
52 class MidiEngine;
53 class Send;
54 class Session;
55 class MidiPlaylist;
56 class SMFSource;
57 class IO;
58
59 class MidiDiskstream : public Diskstream
60 {       
61   public:
62         MidiDiskstream (Session &, const string& name, Diskstream::Flag f = Recordable);
63         MidiDiskstream (Session &, const XMLNode&);
64
65         float playback_buffer_load() const;
66         float capture_buffer_load() const;
67         
68         RawMidi* playback_buffer () { return _current_playback_buffer; }
69         RawMidi* capture_buffer ()  { return _current_capture_buffer; }
70
71         void set_record_enabled (bool yn);
72
73         MidiPlaylist* midi_playlist () { return dynamic_cast<MidiPlaylist*>(_playlist); }
74
75         int use_playlist (Playlist *);
76         int use_new_playlist ();
77         int use_copy_playlist ();
78
79         /* stateful */
80
81         XMLNode& get_state(void);
82         int set_state(const XMLNode& node);
83
84         void monitor_input (bool);
85
86   protected:
87         friend class Session;
88
89         /* the Session is the only point of access for these
90            because they require that the Session is "inactive"
91            while they are called.
92         */
93
94         void set_pending_overwrite(bool);
95         int  overwrite_existing_buffers ();
96         void set_block_size (jack_nframes_t);
97         int  internal_playback_seek (jack_nframes_t distance);
98         int  can_internal_playback_seek (jack_nframes_t distance);
99         int  rename_write_sources ();
100         void reset_write_sources (bool, bool force = false);
101         void non_realtime_input_change ();
102
103   protected:
104         int seek (jack_nframes_t which_sample, bool complete_refill = false);
105
106   protected:
107         friend class MidiTrack;
108
109         int  process (jack_nframes_t transport_frame, jack_nframes_t nframes, jack_nframes_t offset, bool can_record, bool rec_monitors_input);
110         bool commit  (jack_nframes_t nframes);
111
112   private:
113
114         /* use unref() to destroy a diskstream */
115         ~MidiDiskstream();
116
117         /* The two central butler operations */
118         int do_flush (Session::RunContext context, bool force = false);
119         int do_refill ();
120         
121         int do_refill_with_alloc();
122
123         int read (RawMidi* buf, jack_nframes_t& start, jack_nframes_t cnt, bool reversed);
124
125         void finish_capture (bool rec_monitors_input);
126         void transport_stopped (struct tm&, time_t, bool abort);
127
128         void init (Diskstream::Flag);
129
130         int use_new_write_source (uint32_t n=0);
131
132         int find_and_use_playlist (const string&);
133
134         void allocate_temporary_buffers ();
135
136         int use_pending_capture_data (XMLNode& node);
137
138         void get_input_sources ();
139         void check_record_status (jack_nframes_t transport_frame, jack_nframes_t nframes, bool can_record);
140         void set_align_style_from_io();
141         void setup_destructive_playlist ();
142         void use_destructive_playlist ();
143         
144         void engage_record_enable ();
145         void disengage_record_enable ();
146         
147         // FIXME: This is basically a single ChannelInfo.. abstractify that concept?
148         RingBufferNPT<RawMidi>*           _playback_buf;
149         RingBufferNPT<RawMidi>*           _capture_buf;
150         RawMidi*                          _current_playback_buffer;
151         RawMidi*                          _current_capture_buffer;
152         RawMidi*                          _playback_wrap_buffer;
153         RawMidi*                          _capture_wrap_buffer;
154         MidiPort*                         _source_port;
155         SMFSource*                        _write_source; ///< aka capturing source
156         RingBufferNPT<CaptureTransition>* _capture_transition_buf;
157         RingBufferNPT<RawMidi>::rw_vector _playback_vector;
158         RingBufferNPT<RawMidi>::rw_vector _capture_vector;
159 };
160
161 }; /* namespace ARDOUR */
162
163 #endif /* __ardour_midi_diskstream_h__ */