Merge branch 'master' into windows
[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 "pbd/ffs.h"
24
25 #include "ardour/track.h"
26 #include "ardour/midi_ring_buffer.h"
27
28 namespace ARDOUR
29 {
30
31 class InterThreadInfo;
32 class MidiDiskstream;
33 class MidiPlaylist;
34 class RouteGroup;
35 class SMFSource;
36 class Session;
37
38 class MidiTrack : public Track
39 {
40 public:
41         MidiTrack (Session&, string name, Route::Flag f = Route::Flag (0), TrackMode m = Normal);
42         ~MidiTrack ();
43
44         int init ();
45
46         int roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler);
47
48         void realtime_handle_transport_stopped ();
49         void realtime_locate ();
50
51         boost::shared_ptr<Diskstream> create_diskstream ();
52         void set_diskstream (boost::shared_ptr<Diskstream>);
53         void set_record_enabled (bool yn, void *src);
54
55         DataType data_type () const {
56                 return DataType::MIDI;
57         }
58
59         void freeze_me (InterThreadInfo&);
60         void unfreeze ();
61
62         bool bounceable (boost::shared_ptr<Processor>, bool) const { return false; }
63         boost::shared_ptr<Region> bounce (InterThreadInfo&);
64         boost::shared_ptr<Region> bounce_range (framepos_t                   start,
65                                                 framepos_t                   end,
66                                                 InterThreadInfo&             iti,
67                                                 boost::shared_ptr<Processor> endpoint,
68                                                 bool                         include_endpoint);
69
70         int export_stuff (BufferSet&                   bufs,
71                           framepos_t                   start_frame,
72                           framecnt_t                   end_frame,
73                           boost::shared_ptr<Processor> endpoint,
74                           bool                         include_endpoint,
75                           bool                         for_export);
76
77         int set_state (const XMLNode&, int version);
78
79         void midi_panic(void);
80         bool write_immediate_event(size_t size, const uint8_t* buf);
81
82         /** A control that will send "immediate" events to a MIDI track when twiddled */
83         struct MidiControl : public AutomationControl {
84                 MidiControl(MidiTrack* route, const Evoral::Parameter& param,
85                             boost::shared_ptr<AutomationList> al = boost::shared_ptr<AutomationList>())
86                         : AutomationControl (route->session(), param, al)
87                         , _route (route)
88                 {}
89
90                 void set_value (double val);
91
92                 MidiTrack* _route;
93         };
94
95         NoteMode note_mode() const { return _note_mode; }
96         void set_note_mode (NoteMode m);
97
98         std::string describe_parameter (Evoral::Parameter param);
99
100         bool step_editing() const { return _step_editing; }
101         void set_step_editing (bool yn);
102         MidiRingBuffer<framepos_t>& step_edit_ring_buffer() { return _step_edit_ring_buffer; }
103
104         PBD::Signal1<void,bool> StepEditStatusChange;
105
106         boost::shared_ptr<SMFSource> write_source (uint32_t n = 0);
107
108         /** Channel filtering mode.
109          * @param mask If mode is FilterChannels, each bit represents a midi channel:
110          *     bit 0 = channel 0, bit 1 = channel 1 etc. the read and write methods will only
111          *     process events whose channel bit is 1.
112          *     If mode is ForceChannel, mask is simply a channel number which all events will
113          *     be forced to while reading.
114          */
115         void set_capture_channel_mode (ChannelMode mode, uint16_t mask);
116         void set_playback_channel_mode (ChannelMode mode, uint16_t mask);
117         void set_playback_channel_mask (uint16_t mask);
118         void set_capture_channel_mask (uint16_t mask);
119
120         ChannelMode get_playback_channel_mode() const {
121                 return static_cast<ChannelMode>((g_atomic_int_get(&_playback_channel_mask) & 0xffff0000) >> 16);
122         }
123         uint16_t get_playback_channel_mask() const {
124                 return g_atomic_int_get(&_playback_channel_mask) & 0x0000ffff;
125         }
126         ChannelMode get_capture_channel_mode() const {
127                 return static_cast<ChannelMode>((g_atomic_int_get(&_capture_channel_mask) & 0xffff0000) >> 16);
128         }
129         uint16_t get_capture_channel_mask() const {
130                 return g_atomic_int_get(&_capture_channel_mask) & 0x0000ffff;
131         }
132
133         boost::shared_ptr<MidiPlaylist> midi_playlist ();
134
135         PBD::Signal0<void> PlaybackChannelMaskChanged;
136         PBD::Signal0<void> PlaybackChannelModeChanged;
137         PBD::Signal0<void> CaptureChannelMaskChanged;
138         PBD::Signal0<void> CaptureChannelModeChanged;
139     
140         PBD::Signal1<void, boost::weak_ptr<MidiSource> > DataRecorded;
141         boost::shared_ptr<MidiBuffer> get_gui_feed_buffer () const;
142
143         void set_monitoring (MonitorChoice);
144         MonitorState monitoring_state () const;
145
146         void set_input_active (bool);
147         bool input_active () const;
148         PBD::Signal0<void> InputActiveChanged;
149
150 protected:
151         XMLNode& state (bool full);
152
153         void act_on_mute ();
154
155 private:
156         MidiRingBuffer<framepos_t> _immediate_events;
157         MidiRingBuffer<framepos_t> _step_edit_ring_buffer;
158         NoteMode                   _note_mode;
159         bool                       _step_editing;
160         bool                       _input_active;
161         uint32_t                   _playback_channel_mask; // 16 bits mode, 16 bits mask
162         uint32_t                   _capture_channel_mask; // 16 bits mode, 16 bits mask
163
164         virtual boost::shared_ptr<Diskstream> diskstream_factory (XMLNode const &);
165         
166         boost::shared_ptr<MidiDiskstream> midi_diskstream () const;
167
168         void write_out_of_band_data (BufferSet& bufs, framepos_t start_frame, framepos_t end_frame, framecnt_t nframes);
169
170         void set_state_part_two ();
171         void set_state_part_three ();
172
173
174         int no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, bool state_changing);
175         void push_midi_input_to_step_edit_ringbuffer (framecnt_t nframes);
176
177         void diskstream_data_recorded (boost::weak_ptr<MidiSource>);
178         PBD::ScopedConnection _diskstream_data_recorded_connection;
179
180         void track_input_active (IOChange, void*);
181         void map_input_active (bool);
182
183         void filter_channels (BufferSet& bufs, ChannelMode mode, uint32_t mask); 
184
185 /* if mode is ForceChannel, force mask to the lowest set channel or 1 if no
186  * channels are set.
187  */
188 #define force_mask(mode,mask) (((mode) == ForceChannel) ? (((mask) ? (1<<(PBD::ffs((mask))-1)) : 1)) : mask)
189
190         void _set_playback_channel_mode(ChannelMode mode, uint16_t mask) {
191                 mask = force_mask (mode, mask);
192                 g_atomic_int_set(&_playback_channel_mask, (uint32_t(mode) << 16) | uint32_t(mask));
193         }
194         void _set_playback_channel_mask (uint16_t mask) {
195                 mask = force_mask (get_playback_channel_mode(), mask);
196                 g_atomic_int_set(&_playback_channel_mask, (uint32_t(get_playback_channel_mode()) << 16) | uint32_t(mask));
197         }
198         void _set_capture_channel_mode(ChannelMode mode, uint16_t mask) {
199                 mask = force_mask (mode, mask);
200                 g_atomic_int_set(&_capture_channel_mask, (uint32_t(mode) << 16) | uint32_t(mask));
201         }
202         void _set_capture_channel_mask (uint16_t mask) {
203                 mask = force_mask (get_capture_channel_mode(), mask);
204                 g_atomic_int_set(&_capture_channel_mask, (uint32_t(get_capture_channel_mode()) << 16) | uint32_t(mask));
205         }
206
207 #undef force_mask
208 };
209
210 } /* namespace ARDOUR*/
211
212 #endif /* __ardour_midi_track_h__ */