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