Abstraction cleanups/polish, towards merging with trunk
[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 <string>
28 #include <queue>
29 #include <map>
30 #include <vector>
31
32 #include <time.h>
33
34 #include <pbd/fastlog.h>
35 #include <pbd/ringbufferNPT.h>
36  
37
38 #include <ardour/ardour.h>
39 #include <ardour/configuration.h>
40 #include <ardour/session.h>
41 #include <ardour/route_group.h>
42 #include <ardour/route.h>
43 #include <ardour/port.h>
44 #include <ardour/utils.h>
45 #include <ardour/diskstream.h>
46 #include <ardour/midi_playlist.h>
47 struct tm;
48
49 namespace ARDOUR {
50
51 class MidiEngine;
52 class Send;
53 class Session;
54 class MidiPlaylist;
55 class SMFSource;
56 class IO;
57
58 class MidiDiskstream : public Diskstream
59 {       
60   public:
61         MidiDiskstream (Session &, const string& name, Diskstream::Flag f = Recordable);
62         MidiDiskstream (Session &, const XMLNode&);
63
64         void set_io (ARDOUR::IO& io);
65
66         MidiDiskstream& ref() { _refcnt++; return *this; }
67         //void unref() { if (_refcnt) _refcnt--; if (_refcnt == 0) delete this; }
68         //uint32_t refcnt() const { return _refcnt; }
69
70         float playback_buffer_load() const;
71         float capture_buffer_load() const;
72
73         //void set_align_style (AlignStyle);
74         //void set_persistent_align_style (AlignStyle);
75
76         void set_record_enabled (bool yn, void *src);
77         //void set_speed (double);
78
79         int use_playlist (Playlist *);
80         int use_new_playlist ();
81         int use_copy_playlist ();
82
83         Playlist *playlist () { return _playlist; }
84
85         static sigc::signal<void,list<SMFSource*>*> DeleteSources;
86
87         /* stateful */
88
89         XMLNode& get_state(void);
90         int set_state(const XMLNode& node);
91
92         void monitor_input (bool);
93
94         //void handle_input_change (IOChange, void *src);
95
96   protected:
97         friend class Session;
98
99         /* the Session is the only point of access for these
100            because they require that the Session is "inactive"
101            while they are called.
102         */
103
104         void set_pending_overwrite(bool);
105         int  overwrite_existing_buffers ();
106         void set_block_size (jack_nframes_t);
107         int  internal_playback_seek (jack_nframes_t distance);
108         int  can_internal_playback_seek (jack_nframes_t distance);
109         int  rename_write_sources ();
110         void reset_write_sources (bool, bool force = false);
111         void non_realtime_input_change ();
112
113         uint32_t read_data_count() const { return _read_data_count; }
114         uint32_t write_data_count() const { return _write_data_count; }
115
116   protected:
117         friend class Auditioner;
118         int  seek (jack_nframes_t which_sample, bool complete_refill = false);
119
120   protected:
121         friend class MidiTrack;
122
123         int  process (jack_nframes_t transport_frame, jack_nframes_t nframes, jack_nframes_t offset, bool can_record, bool rec_monitors_input);
124         bool commit  (jack_nframes_t nframes);
125
126   private:
127
128         /* use unref() to destroy a diskstream */
129         ~MidiDiskstream();
130
131         MidiPlaylist* _playlist;
132
133         /*Tthe two central butler operations */
134         int do_flush (Session::RunContext context, bool force = false) { return 0; }
135         int do_refill () { return 0; }
136         
137         int do_refill_with_alloc() { return 0; }
138
139         int read (RawMidi* buf, RawMidi* mixdown_buffer, char * workbuf, jack_nframes_t& start, jack_nframes_t cnt, bool reversed);
140
141         /* XXX fix this redundancy ... */
142
143         //void playlist_changed (Change);
144         //void playlist_modified ();
145         void playlist_deleted (Playlist*);
146
147         void finish_capture (bool rec_monitors_input);
148         void transport_stopped (struct tm&, time_t, bool abort);
149
150         struct CaptureInfo {
151             uint32_t start;
152             uint32_t frames;
153         };
154
155         void init (Diskstream::Flag);
156
157         int use_new_write_source (uint32_t n=0);
158
159         int find_and_use_playlist (const string&);
160
161         void allocate_temporary_buffers ();
162
163         //bool realtime_set_speed (double, bool global_change);
164         void non_realtime_set_speed ();
165
166         int use_pending_capture_data (XMLNode& node);
167
168         void get_input_sources ();
169         void check_record_status (jack_nframes_t transport_frame, jack_nframes_t nframes, bool can_record);
170         void set_align_style_from_io();
171         void setup_destructive_playlist ();
172         void use_destructive_playlist ();
173         
174         std::list<Region*>      _last_capture_regions;
175         std::vector<SMFSource*> _capturing_sources;
176 };
177
178 }; /* namespace ARDOUR */
179
180 #endif /* __ardour_midi_diskstream_h__ */