fix merge errors with master
[ardour.git] / libs / ardour / ardour / midi_track.h
1 /*
2     Copyright (C) 2006 Paul Davis
3     Author: David Robillard
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #ifndef __ardour_midi_track_h__
21 #define __ardour_midi_track_h__
22
23 #include "ardour/track.h"
24 #include "ardour/midi_ring_buffer.h"
25
26 namespace ARDOUR
27 {
28
29 class InterThreadInfo;
30 class MidiDiskstream;
31 class MidiPlaylist;
32 class RouteGroup;
33 class SMFSource;
34 class Session;
35
36 class LIBARDOUR_API MidiTrack : public Track
37 {
38 public:
39         MidiTrack (Session&, string name, Route::Flag f = Route::Flag (0), TrackMode m = Normal);
40         ~MidiTrack ();
41
42         int init ();
43
44         int roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler);
45
46         void realtime_handle_transport_stopped ();
47         void realtime_locate ();
48
49         boost::shared_ptr<Diskstream> create_diskstream ();
50         void set_diskstream (boost::shared_ptr<Diskstream>);
51         void set_record_enabled (bool yn, void *src);
52
53         DataType data_type () const {
54                 return DataType::MIDI;
55         }
56
57         void freeze_me (InterThreadInfo&);
58         void unfreeze ();
59
60         bool bounceable (boost::shared_ptr<Processor>, bool) const { return false; }
61         boost::shared_ptr<Region> bounce (InterThreadInfo&);
62         boost::shared_ptr<Region> bounce_range (framepos_t                   start,
63                                                 framepos_t                   end,
64                                                 InterThreadInfo&             iti,
65                                                 boost::shared_ptr<Processor> endpoint,
66                                                 bool                         include_endpoint);
67
68         int export_stuff (BufferSet&                   bufs,
69                           framepos_t                   start_frame,
70                           framecnt_t                   end_frame,
71                           boost::shared_ptr<Processor> endpoint,
72                           bool                         include_endpoint,
73                           bool                         for_export);
74
75         int set_state (const XMLNode&, int version);
76
77         void midi_panic(void);
78         bool write_immediate_event(size_t size, const uint8_t* buf);
79
80         /** A control that will send "immediate" events to a MIDI track when twiddled */
81         struct MidiControl : public AutomationControl {
82                 MidiControl(MidiTrack* route, const Evoral::Parameter& param,
83                             boost::shared_ptr<AutomationList> al = boost::shared_ptr<AutomationList>())
84                         : AutomationControl (route->session(), param, al)
85                         , _route (route)
86                 {}
87
88                 void set_value (double val);
89
90                 MidiTrack* _route;
91         };
92
93         NoteMode note_mode() const { return _note_mode; }
94         void set_note_mode (NoteMode m);
95
96         std::string describe_parameter (Evoral::Parameter param);
97
98         bool step_editing() const { return _step_editing; }
99         void set_step_editing (bool yn);
100         MidiRingBuffer<framepos_t>& step_edit_ring_buffer() { return _step_edit_ring_buffer; }
101
102         PBD::Signal1<void,bool> StepEditStatusChange;
103
104         boost::shared_ptr<SMFSource> write_source (uint32_t n = 0);
105
106         /** Channel filtering mode.
107          * @param mask If mode is FilterChannels, each bit represents a midi channel:
108          *     bit 0 = channel 0, bit 1 = channel 1 etc. the read and write methods will only
109          *     process events whose channel bit is 1.
110          *     If mode is ForceChannel, mask is simply a channel number which all events will
111          *     be forced to while reading.
112          */
113         void set_capture_channel_mode (ChannelMode mode, uint16_t mask);
114         void set_playback_channel_mode (ChannelMode mode, uint16_t mask);
115         void set_playback_channel_mask (uint16_t mask);
116         void set_capture_channel_mask (uint16_t mask);
117
118         ChannelMode get_playback_channel_mode() const {
119                 return static_cast<ChannelMode>((g_atomic_int_get(&_playback_channel_mask) & 0xffff0000) >> 16);
120         }
121         uint16_t get_playback_channel_mask() const {
122                 return g_atomic_int_get(&_playback_channel_mask) & 0x0000ffff;
123         }
124         ChannelMode get_capture_channel_mode() const {
125                 return static_cast<ChannelMode>((g_atomic_int_get(&_capture_channel_mask) & 0xffff0000) >> 16);
126         }
127         uint16_t get_capture_channel_mask() const {
128                 return g_atomic_int_get(&_capture_channel_mask) & 0x0000ffff;
129         }
130
131         boost::shared_ptr<MidiPlaylist> midi_playlist ();
132
133         PBD::Signal0<void> PlaybackChannelMaskChanged;
134         PBD::Signal0<void> PlaybackChannelModeChanged;
135         PBD::Signal0<void> CaptureChannelMaskChanged;
136         PBD::Signal0<void> CaptureChannelModeChanged;
137     
138         PBD::Signal1<void, boost::weak_ptr<MidiSource> > DataRecorded;
139         boost::shared_ptr<MidiBuffer> get_gui_feed_buffer () const;
140
141         void set_monitoring (MonitorChoice);
142         MonitorState monitoring_state () const;
143
144         void set_input_active (bool);
145         bool input_active () const;
146         PBD::Signal0<void> InputActiveChanged;
147
148 protected:
149         XMLNode& state (bool full);
150
151         void act_on_mute ();
152
153 private:
154         MidiRingBuffer<framepos_t> _immediate_events;
155         MidiRingBuffer<framepos_t> _step_edit_ring_buffer;
156         NoteMode                   _note_mode;
157         bool                       _step_editing;
158         bool                       _input_active;
159         uint32_t                   _playback_channel_mask; // 16 bits mode, 16 bits mask
160         uint32_t                   _capture_channel_mask; // 16 bits mode, 16 bits mask
161
162         virtual boost::shared_ptr<Diskstream> diskstream_factory (XMLNode const &);
163         
164         boost::shared_ptr<MidiDiskstream> midi_diskstream () const;
165
166         void write_out_of_band_data (BufferSet& bufs, framepos_t start_frame, framepos_t end_frame, framecnt_t nframes);
167
168         void set_state_part_two ();
169         void set_state_part_three ();
170
171
172         int no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, bool state_changing);
173         void push_midi_input_to_step_edit_ringbuffer (framecnt_t nframes);
174
175         void diskstream_data_recorded (boost::weak_ptr<MidiSource>);
176         PBD::ScopedConnection _diskstream_data_recorded_connection;
177
178         void track_input_active (IOChange, void*);
179         void map_input_active (bool);
180
181         void filter_channels (BufferSet& bufs, ChannelMode mode, uint32_t mask); 
182
183 /* if mode is ForceChannel, force mask to the lowest set channel or 1 if no
184  * channels are set.
185  */
186 #define force_mask(mode,mask) (((mode) == ForceChannel) ? (((mask) ? (1<<(ffs((mask))-1)) : 1)) : mask)
187
188         void _set_playback_channel_mode(ChannelMode mode, uint16_t mask) {
189                 mask = force_mask (mode, mask);
190                 g_atomic_int_set(&_playback_channel_mask, (uint32_t(mode) << 16) | uint32_t(mask));
191         }
192         void _set_playback_channel_mask (uint16_t mask) {
193                 mask = force_mask (get_playback_channel_mode(), mask);
194                 g_atomic_int_set(&_playback_channel_mask, (uint32_t(get_playback_channel_mode()) << 16) | uint32_t(mask));
195         }
196         void _set_capture_channel_mode(ChannelMode mode, uint16_t mask) {
197                 mask = force_mask (mode, mask);
198                 g_atomic_int_set(&_capture_channel_mask, (uint32_t(mode) << 16) | uint32_t(mask));
199         }
200         void _set_capture_channel_mask (uint16_t mask) {
201                 mask = force_mask (get_capture_channel_mode(), mask);
202                 g_atomic_int_set(&_capture_channel_mask, (uint32_t(get_capture_channel_mode()) << 16) | uint32_t(mask));
203         }
204
205 #undef force_mask
206 };
207
208 } /* namespace ARDOUR*/
209
210 #endif /* __ardour_midi_track_h__ */