2308f3cdc633fb31a6f60733fa62ec24eb4a70cc
[ardour.git] / libs / ardour / ardour / midi_track.h
1 /*
2     Copyright (C) 2006 Paul Davis
3     Written by Dave 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 #include "ardour/midi_state_tracker.h"
26
27 namespace ARDOUR
28 {
29
30 class Session;
31 class MidiDiskstream;
32 class MidiPlaylist;
33 class RouteGroup;
34 class SMFSource;        
35
36 class 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 roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
43                   int declick, bool can_record, bool rec_monitors_input, bool& need_butler);
44
45         void realtime_handle_transport_stopped ();
46
47         void use_new_diskstream ();
48         void set_diskstream (boost::shared_ptr<Diskstream>);
49         void set_record_enabled (bool yn, void *src);
50
51         DataType data_type () const {
52                 return DataType::MIDI;
53         }
54
55         int export_stuff (BufferSet& bufs, framecnt_t nframes, framepos_t end_frame);
56
57         void freeze_me (InterThreadInfo&);
58         void unfreeze ();
59         
60         boost::shared_ptr<Region> bounce (InterThreadInfo&);
61         boost::shared_ptr<Region> bounce_range (
62                         framepos_t start, framepos_t end, InterThreadInfo&, bool enable_processing
63                 );
64
65         int set_state(const XMLNode&, int version);
66
67         void midi_panic(void);
68         bool write_immediate_event(size_t size, const uint8_t* buf);
69
70         /** A control that will send "immediate" events to a MIDI track when twiddled */
71         struct MidiControl : public AutomationControl {
72                 MidiControl(MidiTrack* route, const Evoral::Parameter& param,
73                             boost::shared_ptr<AutomationList> al = boost::shared_ptr<AutomationList>())
74                         : AutomationControl (route->session(), param, al)
75                         , _route (route)
76                 {}
77
78                 void set_value (double val);
79
80                 MidiTrack* _route;
81         };
82
83         NoteMode note_mode() const { return _note_mode; }
84         void set_note_mode (NoteMode m);
85
86         bool step_editing() const { return _step_editing; }
87         void set_step_editing (bool yn);
88         MidiRingBuffer<framepos_t>& step_edit_ring_buffer() { return _step_edit_ring_buffer; }
89
90         PBD::Signal1<void,bool> StepEditStatusChange;
91
92         uint8_t default_channel() const { return _default_channel; }
93         void set_default_channel (uint8_t chn);
94
95         bool midi_thru() const { return _midi_thru; }
96         void set_midi_thru (bool yn);
97
98         boost::shared_ptr<SMFSource> write_source (uint32_t n = 0);
99         void set_channel_mode (ChannelMode, uint16_t);
100         ChannelMode get_channel_mode ();
101         uint16_t get_channel_mask ();
102         boost::shared_ptr<MidiPlaylist> midi_playlist ();
103
104         bool bounceable () const {
105                 return false;
106         }
107         
108         PBD::Signal2<void, boost::shared_ptr<MidiBuffer>, boost::weak_ptr<MidiSource> > DataRecorded;
109
110 protected:
111         XMLNode& state (bool full);
112         
113         int _set_state (const XMLNode&, int, bool call_base);
114         bool should_monitor () const;
115         bool send_silence () const;
116
117   private:
118         boost::shared_ptr<MidiDiskstream> midi_diskstream () const;
119
120         void write_out_of_band_data (BufferSet& bufs, framepos_t start_frame, framepos_t end_frame, framecnt_t nframes);
121
122         void set_state_part_two ();
123         void set_state_part_three ();
124
125         MidiRingBuffer<framepos_t> _immediate_events;
126         MidiRingBuffer<framepos_t> _step_edit_ring_buffer;
127         NoteMode                  _note_mode;
128         bool                      _step_editing;
129         uint8_t                   _default_channel;
130         bool                      _midi_thru;
131
132         int no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
133                         bool state_changing, bool can_record, bool rec_monitors_input);
134         void push_midi_input_to_step_edit_ringbuffer (framecnt_t nframes);
135
136         void diskstream_data_recorded (boost::shared_ptr<MidiBuffer>, boost::weak_ptr<MidiSource>);
137         PBD::ScopedConnection _diskstream_data_recorded_connection;
138 };
139
140 } /* namespace ARDOUR*/
141
142 #endif /* __ardour_midi_track_h__ */