d27df2f7ea253577e1d44bcc8292402abe33938c
[ardour.git] / libs / ardour / ardour / session.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 */
19
20 #ifndef __ardour_session_h__
21 #define __ardour_session_h__
22
23 #include <string>
24 #include <list>
25 #include <map>
26 #include <vector>
27 #include <set>
28 #include <stack>
29
30 #include <boost/scoped_ptr.hpp>
31 #include <boost/weak_ptr.hpp>
32 #include <boost/dynamic_bitset.hpp>
33
34 #include <stdint.h>
35
36 #include <sndfile.h>
37
38 #include <glibmm/thread.h>
39
40 #include <pbd/error.h>
41 #include <pbd/undo.h>
42 #include <pbd/pool.h>
43 #include <pbd/rcu.h>
44 #include <pbd/statefuldestructible.h>
45
46 #include <midi++/types.h>
47 #include <midi++/mmc.h>
48
49 #include <pbd/stateful.h> 
50 #include <pbd/destructible.h> 
51
52 #include <ardour/ardour.h>
53 #include <ardour/configuration.h>
54 #include <ardour/location.h>
55 #include <ardour/gain.h>
56 #include <ardour/io.h>
57
58 #include <ardour/smpte.h>
59
60 class XMLTree;
61 class XMLNode;
62 class AEffect;
63
64 namespace MIDI {
65         class Port;
66 }
67
68 namespace PBD {
69         class Controllable;
70 }
71
72 namespace ARDOUR {
73
74 class Port;
75 class AudioEngine;
76 class Slave;
77 class Diskstream;
78 class Route;
79 class AuxInput;
80 class Source;
81 class AudioSource;
82 class BufferSet;
83
84 class Diskstream;
85 class AudioDiskstream;
86 class MidiDiskstream;
87 class AudioFileSource;
88 class MidiSource;
89 class Auditioner;
90 class Processor;
91 class Send;
92 class IOProcessor;
93 class PortInsert;
94 class PluginInsert;
95 class Bundle;
96 class TempoMap;
97 class AudioTrack;
98 class NamedSelection;
99 class AudioRegion;
100
101 class Region;
102 class Playlist;
103 class VSTPlugin;
104 class ControlProtocolInfo;
105
106 class MidiTrack;
107 class MidiRegion;
108 class SMFSource;
109
110 class SessionDirectory;
111
112 struct AudioExportSpecification;
113 struct RouteGroup;
114
115 using std::vector;
116 using std::string;
117 using std::map;
118 using std::set;
119
120 class Session : public PBD::StatefulDestructible
121 {
122   private:
123         typedef std::pair<boost::weak_ptr<Route>,bool> RouteBooleanState;
124         typedef vector<RouteBooleanState> GlobalRouteBooleanState;
125         typedef std::pair<boost::weak_ptr<Route>,MeterPoint> RouteMeterState;
126         typedef vector<RouteMeterState> GlobalRouteMeterState;
127
128   public:
129         enum RecordState {
130                 Disabled = 0,
131                 Enabled = 1,
132                 Recording = 2
133         };
134
135         struct Event {
136             enum Type {
137                     SetTransportSpeed,
138                     SetDiskstreamSpeed,
139                     Locate,
140                     LocateRoll,
141                     LocateRollLocate,
142                     SetLoop,
143                     PunchIn,
144                     PunchOut,
145                     RangeStop,
146                     RangeLocate,
147                     Overwrite,
148                     SetSlaveSource,
149                     Audition,
150                     InputConfigurationChange,
151                     SetAudioRange,
152                     SetPlayRange,
153                     
154                     /* only one of each of these events
155                        can be queued at any one time
156                     */
157
158                     StopOnce,
159                     AutoLoop
160             };
161
162             enum Action {
163                     Add,
164                     Remove,
165                     Replace,
166                     Clear
167             };
168                 
169                 Type           type;
170             Action         action;
171             nframes_t action_frame;
172             nframes_t target_frame;
173             float          speed;
174
175             union {
176                         void*                ptr;
177                         bool                 yes_or_no;
178                         SlaveSource slave;
179                         Route*               route;
180             };
181
182             boost::shared_ptr<Region>     region;
183
184             list<AudioRange>     audio_range;
185             list<MusicRange>     music_range;
186
187             Event(Type t, Action a, nframes_t when, nframes_t where, float spd, bool yn = false)
188                     : type (t), 
189                       action (a),
190                       action_frame (when),
191                       target_frame (where),
192                       speed (spd),
193                       yes_or_no (yn) {}
194
195             void set_ptr (void* p) { 
196                     ptr = p;
197             }
198
199             bool before (const Event& other) const {
200                     return action_frame < other.action_frame;
201             }
202
203             bool after (const Event& other) const {
204                     return action_frame > other.action_frame;
205             }
206
207             static bool compare (const Event *e1, const Event *e2) {
208                     return e1->before (*e2);
209             }
210
211             void *operator new (size_t ignored) {
212                     return pool.alloc ();
213             }
214
215             void operator delete(void *ptr, size_t size) {
216                     pool.release (ptr);
217             }
218
219             static const nframes_t Immediate = 0;
220
221          private:
222             static MultiAllocSingleReleasePool pool;
223         };
224
225         /* creating from an XML file */
226
227         Session (AudioEngine&,
228                  const string& fullpath,
229                  const string& snapshot_name,
230                  string mix_template = "");
231
232         /* creating a new Session */
233
234         Session (AudioEngine&,
235                  string fullpath,
236                  string snapshot_name,
237                  AutoConnectOption input_auto_connect,
238                  AutoConnectOption output_auto_connect,
239                  uint32_t control_out_channels,
240                  uint32_t master_out_channels,
241                  uint32_t n_physical_in,
242                  uint32_t n_physical_out,
243                  nframes_t initial_length);
244         
245         virtual ~Session ();
246
247         string path() const { return _path; }
248         string name() const { return _name; }
249         string snap_name() const { return _current_snapshot_name; }
250         string raid_path () const;
251
252         void set_snap_name ();
253
254         void set_dirty ();
255         void set_clean ();
256         bool dirty() const { return _state_of_the_state & Dirty; }
257         void set_deletion_in_progress ();
258         bool deletion_in_progress() const { return _state_of_the_state & Deletion; }
259         sigc::signal<void> DirtyChanged;
260
261         const SessionDirectory& session_directory () const { return *(_session_dir.get()); }
262
263         std::string automation_dir () const;
264         std::string analysis_dir() const;
265         
266         int ensure_subdirs ();
267
268         Glib::ustring peak_path (Glib::ustring) const;
269
270         static string change_audio_path_by_name (string oldpath, string oldname, string newname, bool destructive);
271         static string change_midi_path_by_name (string oldpath, string oldname, string newname, bool destructive);
272         
273         string peak_path_from_audio_path (string) const;
274         string audio_path_from_name (string, uint32_t nchans, uint32_t chan, bool destructive);
275         string midi_path_from_name (string);
276
277         void process (nframes_t nframes);
278
279         BufferSet& get_silent_buffers (ChanCount count = ChanCount::ZERO);
280         BufferSet& get_scratch_buffers (ChanCount count = ChanCount::ZERO);
281         BufferSet& get_mix_buffers (ChanCount count = ChanCount::ZERO);
282         
283         void add_diskstream (boost::shared_ptr<Diskstream>);
284         boost::shared_ptr<Diskstream> diskstream_by_id (const PBD::ID& id);
285         boost::shared_ptr<Diskstream> diskstream_by_name (string name);
286
287         bool have_captured() const { return _have_captured; }
288
289         void refill_all_diskstream_buffers ();
290         uint32_t diskstream_buffer_size() const { return dstream_buffer_size; }
291         
292         uint32_t get_next_diskstream_id() const { return n_diskstreams(); }
293         uint32_t n_diskstreams() const;
294         
295         typedef std::list<boost::shared_ptr<Diskstream> > DiskstreamList;
296         typedef std::list<boost::shared_ptr<Route> >      RouteList; 
297         
298         boost::shared_ptr<RouteList> get_routes() const {
299                 return routes.reader ();
300         }
301         
302         uint32_t nroutes() const { return routes.reader()->size(); }
303         uint32_t ntracks () const;
304         uint32_t nbusses () const;
305
306         struct RoutePublicOrderSorter {
307             bool operator() (boost::shared_ptr<Route>, boost::shared_ptr<Route> b);
308         };
309         
310         template<class T> void foreach_route (T *obj, void (T::*func)(Route&));
311         template<class T> void foreach_route (T *obj, void (T::*func)(boost::shared_ptr<Route>));
312         template<class T, class A> void foreach_route (T *obj, void (T::*func)(Route&, A), A arg);
313
314         boost::shared_ptr<Route> route_by_name (string);
315         boost::shared_ptr<Route> route_by_id (PBD::ID);
316         boost::shared_ptr<Route> route_by_remote_id (uint32_t id);
317
318         bool route_name_unique (string) const;
319
320         bool get_record_enabled() const { 
321                 return (record_status () >= Enabled);
322         }
323
324         RecordState record_status() const {
325                 return (RecordState) g_atomic_int_get (&_record_status);
326         }
327
328         bool actively_recording () {
329                 return record_status() == Recording;
330         }
331
332         bool record_enabling_legal () const;
333         void maybe_enable_record ();
334         void disable_record (bool rt_context, bool force = false);
335         void step_back_from_record ();
336         
337         void maybe_write_autosave ();
338
339         /* Proxy signal for region hidden changes */
340
341         sigc::signal<void,boost::shared_ptr<Region> > RegionHiddenChange;
342
343         /* Emitted when all i/o connections are complete */
344         
345         sigc::signal<void> IOConnectionsComplete;
346         
347         /* Record status signals */
348
349         sigc::signal<void> RecordStateChanged;
350
351         /* Transport mechanism signals */
352
353         sigc::signal<void> TransportStateChange; /* generic */
354         sigc::signal<void,nframes_t> PositionChanged; /* sent after any non-sequential motion */
355         sigc::signal<void> DurationChanged;
356         sigc::signal<void> HaltOnXrun;
357         sigc::signal<void> TransportLooped;
358
359         sigc::signal<void,RouteList&> RouteAdded;
360
361         void request_roll_at_and_return (nframes_t start, nframes_t return_to);
362         void request_bounded_roll (nframes_t start, nframes_t end);
363         void request_stop (bool abort = false);
364         void request_locate (nframes_t frame, bool with_roll = false);
365
366         void request_play_loop (bool yn);
367         bool get_play_loop () const { return play_loop; }
368
369         nframes_t  last_transport_start() const { return _last_roll_location; }
370         void goto_end ()   { request_locate (end_location->start(), false);}
371         void goto_start () { request_locate (start_location->start(), false); }
372         void set_session_start (nframes_t start) { start_location->set_start(start); }
373         void set_session_end (nframes_t end) { end_location->set_start(end); _end_location_is_free = false; }
374         void use_rf_shuttle_speed ();
375         void allow_auto_play (bool yn);
376         void request_transport_speed (float speed);
377         void request_overwrite_buffer (Diskstream*);
378         void request_diskstream_speed (Diskstream&, float speed);
379         void request_input_change_handling ();
380
381         bool locate_pending() const { return static_cast<bool>(post_transport_work&PostTransportLocate); }
382         bool transport_locked () const;
383
384         int wipe ();
385         //int wipe_diskstream (AudioDiskstream *);
386
387         int remove_region_from_region_list (boost::shared_ptr<Region>);
388
389         nframes_t get_maximum_extent () const;
390         nframes_t current_end_frame() const { return end_location->start(); }
391         nframes_t current_start_frame() const { return start_location->start(); }
392         nframes_t frame_rate() const   { return _current_frame_rate; }
393         nframes_t frames_per_hour() const { return _frames_per_hour; }
394
395         double frames_per_smpte_frame() const { return _frames_per_smpte_frame; }
396         nframes_t smpte_frames_per_hour() const { return _smpte_frames_per_hour; }
397
398         float smpte_frames_per_second() const;
399         bool smpte_drop_frames() const;
400
401         /* Locations */
402
403         Locations *locations() { return &_locations; }
404
405         sigc::signal<void,Location*>    auto_loop_location_changed;
406         sigc::signal<void,Location*>    auto_punch_location_changed;
407         sigc::signal<void>              locations_modified;
408
409         void set_auto_punch_location (Location *);
410         void set_auto_loop_location (Location *);
411         int location_name(string& result, string base = string(""));
412
413         void reset_input_monitor_state ();
414
415         void add_event (nframes_t action_frame, Event::Type type, nframes_t target_frame = 0);
416         void remove_event (nframes_t frame, Event::Type type);
417         void clear_events (Event::Type type);
418
419         nframes_t get_block_size() const { return current_block_size; }
420         nframes_t worst_output_latency () const { return _worst_output_latency; }
421         nframes_t worst_input_latency () const { return _worst_input_latency; }
422         nframes_t worst_track_latency () const { return _worst_track_latency; }
423
424         int save_state (string snapshot_name, bool pending = false);
425         int restore_state (string snapshot_name);
426         int save_template (string template_name);
427         int save_history (string snapshot_name = "");
428         int restore_history (string snapshot_name);
429         void remove_state (string snapshot_name);
430         void rename_state (string old_name, string new_name);
431         void remove_pending_capture_state ();
432         
433         sigc::signal<void,string> StateSaved;
434         sigc::signal<void> StateReady;
435
436         XMLNode& get_state();
437         int      set_state(const XMLNode& node); // not idempotent
438         XMLNode& get_template();
439
440         /// The instant xml file is written to the session directory
441         void add_instant_xml (XMLNode&);
442         XMLNode * instant_xml (const std::string& str);
443
444         enum StateOfTheState {
445                 Clean = 0x0,
446                 Dirty = 0x1,
447                 CannotSave = 0x2,
448                 Deletion = 0x4,
449                 InitialConnecting = 0x8,
450                 Loading = 0x10,
451                 InCleanup = 0x20
452         };
453
454         StateOfTheState state_of_the_state() const { return _state_of_the_state; }
455
456         RouteGroup* add_edit_group (string);
457         RouteGroup* add_mix_group (string);
458
459         void remove_edit_group (RouteGroup&);
460         void remove_mix_group (RouteGroup&);
461
462         RouteGroup *mix_group_by_name (string);
463         RouteGroup *edit_group_by_name (string);
464
465         sigc::signal<void,RouteGroup*> edit_group_added;
466         sigc::signal<void,RouteGroup*> mix_group_added;
467         sigc::signal<void> edit_group_removed;
468         sigc::signal<void> mix_group_removed;
469
470         void foreach_edit_group (sigc::slot<void,RouteGroup*> sl) {
471                 for (list<RouteGroup *>::iterator i = edit_groups.begin(); i != edit_groups.end(); i++) {
472                         sl (*i);
473                 }
474         }
475
476         void foreach_mix_group (sigc::slot<void,RouteGroup*> sl) {
477                 for (list<RouteGroup *>::iterator i = mix_groups.begin(); i != mix_groups.end(); i++) {
478                         sl (*i);
479                 }
480         }
481
482         /* fundamental operations. duh. */
483
484         std::list<boost::shared_ptr<AudioTrack> > new_audio_track (int input_channels, int output_channels, TrackMode mode = Normal, uint32_t how_many = 1);
485         RouteList new_audio_route (int input_channels, int output_channels, uint32_t how_many);
486         
487         std::list<boost::shared_ptr<MidiTrack> > new_midi_track (TrackMode mode = Normal, uint32_t how_many = 1);
488         //boost::shared_ptr<Route>     new_midi_route (uint32_t how_many = 1);
489
490         void   remove_route (boost::shared_ptr<Route>);
491         void   resort_routes ();
492         void   resort_routes_using (boost::shared_ptr<RouteList>);
493         
494         void   set_remote_control_ids();
495
496         AudioEngine & engine() { return _engine; }
497         AudioEngine const & engine () const { return _engine; }
498
499         int32_t  max_level;
500         int32_t  min_level;
501
502         /* Time */
503
504         nframes_t transport_frame () const {return _transport_frame; }
505         nframes_t audible_frame () const;
506         nframes64_t requested_return_frame() const { return _requested_return_frame; }
507
508         enum PullupFormat {
509                 pullup_Plus4Plus1,
510                 pullup_Plus4,
511                 pullup_Plus4Minus1,
512                 pullup_Plus1,
513                 pullup_None,
514                 pullup_Minus1,
515                 pullup_Minus4Plus1,
516                 pullup_Minus4,
517                 pullup_Minus4Minus1
518         };
519
520         int  set_smpte_format (SmpteFormat);
521         void sync_time_vars();
522
523         void bbt_time (nframes_t when, BBT_Time&);
524         void smpte_to_sample( SMPTE::Time& smpte, nframes_t& sample, bool use_offset, bool use_subframes ) const;
525         void sample_to_smpte( nframes_t sample, SMPTE::Time& smpte, bool use_offset, bool use_subframes ) const;
526         void smpte_time (SMPTE::Time &);
527         void smpte_time (nframes_t when, SMPTE::Time&);
528         void smpte_time_subframes (nframes_t when, SMPTE::Time&);
529
530         void smpte_duration (nframes_t, SMPTE::Time&) const;
531         void smpte_duration_string (char *, nframes_t) const;
532
533         void           set_smpte_offset (nframes_t);
534         nframes_t smpte_offset () const { return _smpte_offset; }
535         void           set_smpte_offset_negative (bool);
536         bool           smpte_offset_negative () const { return _smpte_offset_negative; }
537
538         nframes_t convert_to_frames_at (nframes_t position, AnyTime&);
539
540         static sigc::signal<void> StartTimeChanged;
541         static sigc::signal<void> EndTimeChanged;
542         static sigc::signal<void> SMPTEOffsetChanged;
543
544         void        request_slave_source (SlaveSource);
545         bool        synced_to_jack() const { return Config->get_slave_source() == JACK; }
546
547         float       transport_speed() const { return _transport_speed; }
548         bool        transport_stopped() const { return _transport_speed == 0.0f; }
549         bool        transport_rolling() const { return _transport_speed != 0.0f; }
550         
551         void set_silent (bool yn);
552         bool silent () { return _silent; }
553
554         int jack_slave_sync (nframes_t);
555
556         TempoMap& tempo_map() { return *_tempo_map; }
557         
558         /* region info  */
559
560         sigc::signal<void,boost::shared_ptr<Region> > RegionAdded;
561         sigc::signal<void,boost::shared_ptr<Region> > RegionRemoved;
562
563         int region_name (string& result, string base = string(""), bool newlevel = false) const;
564         string new_region_name (string);
565         string path_from_region_name (DataType type, string name, string identifier);
566
567         boost::shared_ptr<Region> find_whole_file_parent (boost::shared_ptr<Region const>);
568         
569         void find_equivalent_playlist_regions (boost::shared_ptr<Region>, std::vector<boost::shared_ptr<Region> >& result);
570
571         boost::shared_ptr<Region>      XMLRegionFactory (const XMLNode&, bool full);
572         boost::shared_ptr<AudioRegion> XMLAudioRegionFactory (const XMLNode&, bool full);
573         boost::shared_ptr<MidiRegion>  XMLMidiRegionFactory (const XMLNode&, bool full);
574
575         template<class T> void foreach_region (T *obj, void (T::*func)(boost::shared_ptr<Region>));
576
577         /* source management */
578
579         struct import_status : public InterThreadInfo {
580             string doing_what;
581             
582             /* control info */
583             bool sample_convert;
584             SrcQuality quality;
585             volatile bool freeze;
586             std::vector<Glib::ustring> paths;
587             
588             /* result */
589             SourceList sources;
590             
591         };
592
593         void import_audiofiles (import_status&);
594         bool sample_rate_convert (import_status&, string infile, string& outfile);
595         string build_tmp_convert_name (string file);
596
597         SlaveSource post_export_slave;
598         nframes_t post_export_position;
599
600         int start_audio_export (ARDOUR::AudioExportSpecification&);
601         int stop_audio_export (ARDOUR::AudioExportSpecification&);
602         void finalize_audio_export ();
603
604         void add_source (boost::shared_ptr<Source>);
605         void remove_source (boost::weak_ptr<Source>);
606
607         struct cleanup_report {
608                 vector<string> paths;
609                 int64_t        space;
610         };
611
612         int  cleanup_sources (cleanup_report&);
613         int  cleanup_trash_sources (cleanup_report&);
614
615         int destroy_region (boost::shared_ptr<Region>);
616         int destroy_regions (std::list<boost::shared_ptr<Region> >);
617
618         int remove_last_capture ();
619
620         /* handlers should return -1 for "stop cleanup", 0 for
621            "yes, delete this playlist" and 1 for "no, don't delete
622            this playlist.
623         */
624         
625         sigc::signal<int,boost::shared_ptr<ARDOUR::Playlist> > AskAboutPlaylistDeletion;
626
627         /* handlers should return !0 for use pending state, 0 for
628            ignore it.
629         */
630
631         static sigc::signal<int> AskAboutPendingState;
632         
633         boost::shared_ptr<AudioFileSource> create_audio_source_for_session (ARDOUR::AudioDiskstream&, uint32_t which_channel, bool destructive);
634
635         boost::shared_ptr<MidiSource> create_midi_source_for_session (ARDOUR::MidiDiskstream&);
636
637         boost::shared_ptr<Source> source_by_id (const PBD::ID&);
638         boost::shared_ptr<Source> source_by_path_and_channel (const Glib::ustring&, uint16_t);
639
640         /* playlist management */
641
642         boost::shared_ptr<Playlist> playlist_by_name (string name);
643         void add_playlist (boost::shared_ptr<Playlist>);
644         sigc::signal<void,boost::shared_ptr<Playlist> > PlaylistAdded;
645         sigc::signal<void,boost::shared_ptr<Playlist> > PlaylistRemoved;
646
647         uint32_t n_playlists() const;
648
649         template<class T> void foreach_playlist (T *obj, void (T::*func)(boost::shared_ptr<Playlist>));
650         void get_playlists (std::vector<boost::shared_ptr<Playlist> >&);
651
652         /* named selections */
653
654         NamedSelection* named_selection_by_name (string name);
655         void add_named_selection (NamedSelection *);
656         void remove_named_selection (NamedSelection *);
657
658         template<class T> void foreach_named_selection (T& obj, void (T::*func)(NamedSelection&));
659         sigc::signal<void> NamedSelectionAdded;
660         sigc::signal<void> NamedSelectionRemoved;
661
662         /* Curves and AutomationLists (TODO when they go away) */
663         void add_curve(Curve*);
664         void add_automation_list(AutomationList*);
665         
666         /* fade curves */
667
668         float get_default_fade_length () const { return default_fade_msecs; }
669         float get_default_fade_steepness () const { return default_fade_steepness; }
670         void set_default_fade (float steepness, float msecs);
671
672         /* auditioning */
673
674         boost::shared_ptr<Auditioner> the_auditioner() { return auditioner; }
675         void audition_playlist ();
676         void audition_region (boost::shared_ptr<Region>);
677         void cancel_audition ();
678         bool is_auditioning () const;
679         
680         sigc::signal<void,bool> AuditionActive;
681
682         /* flattening stuff */
683
684         int write_one_audio_track (AudioTrack&, nframes_t start, nframes_t cnt, bool overwrite,
685                         vector<boost::shared_ptr<Source> >&, InterThreadInfo& wot);
686
687         int freeze (InterThreadInfo&);
688
689         /* session-wide solo/mute/rec-enable */
690         
691         bool soloing() const { return currently_soloing; }
692
693         void set_all_solo (bool);
694         void set_all_mute (bool);
695
696         sigc::signal<void,bool> SoloActive;
697         sigc::signal<void> SoloChanged;
698         
699         void record_disenable_all ();
700         void record_enable_all ();
701
702         /* control/master out */
703
704         boost::shared_ptr<IO> control_out() const { return _control_out; }
705         boost::shared_ptr<IO> master_out() const { return _master_out; }
706
707         /* insert/send management */
708         
709         uint32_t n_port_inserts() const { return _port_inserts.size(); }
710         uint32_t n_plugin_inserts() const { return _plugin_inserts.size(); }
711         uint32_t n_sends() const { return _sends.size(); }
712         
713         static void set_disable_all_loaded_plugins (bool yn) { 
714                 _disable_all_loaded_plugins = yn;
715         }
716         static bool get_disable_all_loaded_plugins() { 
717                 return _disable_all_loaded_plugins;
718         }
719
720         uint32_t next_send_id();
721         uint32_t next_insert_id();
722         void mark_send_id (uint32_t);
723         void mark_insert_id (uint32_t);
724
725         /* s/w "RAID" management */
726         
727         nframes_t available_capture_duration();
728
729         /* I/O bundles */
730
731         void foreach_bundle (sigc::slot<void, boost::shared_ptr<Bundle> >);
732         void add_bundle (boost::shared_ptr<Bundle>);
733         void remove_bundle (boost::shared_ptr<Bundle>);
734         boost::shared_ptr<Bundle> bundle_by_name (string) const;
735
736         sigc::signal<void,boost::shared_ptr<Bundle> > BundleAdded;
737         sigc::signal<void,boost::shared_ptr<Bundle> > BundleRemoved;
738
739         /* MIDI */
740
741         int set_mtc_port (string port_tag);
742         int set_mmc_port (string port_tag);
743         int set_midi_port (string port_tag);
744         MIDI::Port *mtc_port() const { return _mtc_port; }
745         MIDI::Port *mmc_port() const { return _mmc_port; }
746         MIDI::Port *midi_port() const { return _midi_port; }
747
748         sigc::signal<void> MTC_PortChanged;
749         sigc::signal<void> MMC_PortChanged;
750         sigc::signal<void> MIDI_PortChanged;
751
752         void set_trace_midi_input (bool, MIDI::Port* port = 0);
753         void set_trace_midi_output (bool, MIDI::Port* port = 0);
754
755         bool get_trace_midi_input(MIDI::Port *port = 0);
756         bool get_trace_midi_output(MIDI::Port *port = 0);
757         
758         void send_midi_message (MIDI::Port * port, MIDI::eventType ev, MIDI::channel_t, MIDI::EventTwoBytes);
759
760         void deliver_midi (MIDI::Port*, MIDI::byte*, int32_t size);
761
762         void set_mmc_receive_device_id (uint32_t id);
763         void set_mmc_send_device_id (uint32_t id);
764         
765         /* Scrubbing */
766
767         void start_scrub (nframes_t where);
768         void stop_scrub ();
769         void set_scrub_speed (float);
770         nframes_t scrub_buffer_size() const;
771         sigc::signal<void> ScrubReady;
772
773         /* History (for editors, mixers, UIs etc.) */
774
775         /** Undo some transactions.
776          * @param n Number of transactions to undo.
777          */
778         void undo (uint32_t n) {
779                 _history.undo (n);
780         }
781
782         void redo (uint32_t n) {
783                 _history.redo (n);
784         }
785
786         UndoHistory& history() { return _history; }
787         
788         uint32_t undo_depth() const { return _history.undo_depth(); }
789         uint32_t redo_depth() const { return _history.redo_depth(); }
790         string next_undo() const { return _history.next_undo(); }
791         string next_redo() const { return _history.next_redo(); }
792
793         void begin_reversible_command (const string& cmd_name);
794         void commit_reversible_command (Command* cmd = 0);
795
796         void add_command (Command *const cmd) {
797                 current_trans->add_command (cmd);
798         }
799
800         std::map<PBD::ID, PBD::StatefulThingWithGoingAway*> registry;
801
802         // these commands are implemented in libs/ardour/session_command.cc
803         Command* memento_command_factory(XMLNode* n);
804         void register_with_memento_command_factory(PBD::ID, PBD::StatefulThingWithGoingAway*);
805
806         Command* global_state_command_factory (const XMLNode& n);
807
808         class GlobalRouteStateCommand : public Command
809         {
810           public:
811                 GlobalRouteStateCommand (Session&, void*);
812                 GlobalRouteStateCommand (Session&, const XMLNode& node);
813                 int set_state (const XMLNode&);
814                 XMLNode& get_state ();
815
816           protected:
817                 GlobalRouteBooleanState before, after;
818                 Session& sess;
819                 void* src;
820                 
821         };
822
823         class GlobalSoloStateCommand : public GlobalRouteStateCommand
824         {
825           public:
826                 GlobalSoloStateCommand (Session &, void *src);
827                 GlobalSoloStateCommand (Session&, const XMLNode&);
828                 void operator()(); //redo
829                 void undo();
830                 XMLNode &get_state();
831                 void mark();
832         };
833
834         class GlobalMuteStateCommand : public GlobalRouteStateCommand
835         {
836           public:
837                 GlobalMuteStateCommand(Session &, void *src);
838                 GlobalMuteStateCommand (Session&, const XMLNode&);
839                 void operator()(); // redo
840                 void undo();
841                 XMLNode &get_state();
842                 void mark();
843         };
844
845         class GlobalRecordEnableStateCommand : public GlobalRouteStateCommand
846         {
847           public:
848                 GlobalRecordEnableStateCommand(Session &, void *src);
849                 GlobalRecordEnableStateCommand (Session&, const XMLNode&);
850                 void operator()(); // redo
851                 void undo();
852                 XMLNode &get_state();
853                 void mark();
854         };
855
856         class GlobalMeteringStateCommand : public Command
857         {
858           public:
859                 GlobalMeteringStateCommand(Session &, void *src);
860                 GlobalMeteringStateCommand (Session&, const XMLNode&);
861                 void operator()();
862                 void undo();
863                 XMLNode &get_state();
864                 int set_state (const XMLNode&);
865                 void mark();
866
867           protected:
868                 Session& sess;
869                 void* src;
870                 GlobalRouteMeterState before;
871                 GlobalRouteMeterState after;
872         };
873
874         /* clicking */
875
876         boost::shared_ptr<IO>  click_io() { return _click_io; }
877                 
878         /* disk, buffer loads */
879
880         uint32_t playback_load ();
881         uint32_t capture_load ();
882         uint32_t playback_load_min ();
883         uint32_t capture_load_min ();
884
885         void reset_playback_load_min ();
886         void reset_capture_load_min ();
887         
888         float read_data_rate () const;
889         float write_data_rate () const;
890
891         /* ranges */
892
893         void set_audio_range (list<AudioRange>&);
894         void set_music_range (list<MusicRange>&);
895
896         void request_play_range (bool yn);
897         bool get_play_range () const { return _play_range; }
898
899         /* buffers for gain and pan */
900
901         gain_t* gain_automation_buffer () const { return _gain_automation_buffer; }
902         pan_t** pan_automation_buffer () const  { return _pan_automation_buffer; }
903
904         /* buffers for conversion */
905         enum RunContext {
906                 ButlerContext = 0,
907                 TransportContext,
908                 ExportContext
909         };
910         
911         /* VST support */
912
913         static long vst_callback (AEffect* effect,
914                                   long opcode,
915                                   long index,
916                                   long value,
917                                   void* ptr,
918                                   float opt);
919         
920         typedef float (*compute_peak_t)          (Sample *, nframes_t, float);
921         typedef void  (*find_peaks_t)            (Sample *, nframes_t, float *, float*);
922         typedef void  (*apply_gain_to_buffer_t)  (Sample *, nframes_t, float);
923         typedef void  (*mix_buffers_with_gain_t) (Sample *, Sample *, nframes_t, float);
924         typedef void  (*mix_buffers_no_gain_t)   (Sample *, Sample *, nframes_t);
925
926         static compute_peak_t           compute_peak;
927         static find_peaks_t             find_peaks;
928         static apply_gain_to_buffer_t   apply_gain_to_buffer;
929         static mix_buffers_with_gain_t  mix_buffers_with_gain;
930         static mix_buffers_no_gain_t    mix_buffers_no_gain;
931
932         static sigc::signal<void> SendFeedback;
933
934         /* Controllables */
935
936         boost::shared_ptr<PBD::Controllable> controllable_by_id (const PBD::ID&);
937
938         void add_controllable (boost::shared_ptr<PBD::Controllable>);
939         void remove_controllable (PBD::Controllable*);
940
941   protected:
942         friend class AudioEngine;
943         void set_block_size (nframes_t nframes);
944         void set_frame_rate (nframes_t nframes);
945
946   protected:
947         friend class Diskstream;
948         void stop_butler ();
949         void wait_till_butler_finished();
950
951   protected:
952         friend class Route;
953         void schedule_curve_reallocation ();
954         void update_latency_compensation (bool, bool);
955         
956   private:
957         int  create (bool& new_session, const string& mix_template, nframes_t initial_length);
958         void destroy ();
959         
960         nframes_t compute_initial_length ();
961
962         enum SubState {
963                 PendingDeclickIn   = 0x1,
964                 PendingDeclickOut  = 0x2,
965                 StopPendingCapture = 0x4,
966                 AutoReturning      = 0x10,
967                 PendingLocate      = 0x20,
968                 PendingSetLoop     = 0x40
969         };
970
971         /* stuff used in process() should be close together to
972            maximise cache hits
973         */
974
975         typedef void (Session::*process_function_type)(nframes_t);
976         
977         AudioEngine&            _engine;
978         mutable gint             processing_prohibited;
979         process_function_type    process_function;
980         process_function_type    last_process_function;
981         bool                     waiting_for_sync_offset;
982         nframes_t               _base_frame_rate;
983         nframes_t               _current_frame_rate;  //this includes video pullup offset
984         int                      transport_sub_state;
985         mutable gint            _record_status;
986         volatile nframes_t      _transport_frame;
987         Location*                end_location;
988         Location*                start_location;
989         Slave*                  _slave;
990         bool                    _silent;
991         volatile float          _transport_speed;
992         volatile float          _desired_transport_speed;
993         float                   _last_transport_speed;
994         bool                     auto_play_legal;
995         nframes_t               _last_slave_transport_frame;
996         nframes_t                maximum_output_latency;
997         nframes_t                last_stop_frame;
998         volatile nframes64_t    _requested_return_frame;
999         BufferSet*              _scratch_buffers;
1000         BufferSet*              _silent_buffers;
1001         BufferSet*              _mix_buffers;
1002         nframes_t                current_block_size;
1003         nframes_t               _worst_output_latency;
1004         nframes_t               _worst_input_latency;
1005         nframes_t               _worst_track_latency;
1006         bool                    _have_captured;
1007         float                   _meter_hold;
1008         float                   _meter_falloff;
1009         bool                    _end_location_is_free;
1010
1011         void set_worst_io_latencies ();
1012         void set_worst_io_latencies_x (IOChange asifwecare, void *ignored) {
1013                 set_worst_io_latencies ();
1014         }
1015
1016         void update_latency_compensation_proxy (void* ignored);
1017
1018         void ensure_buffers (ChanCount howmany);
1019         
1020         void process_scrub          (nframes_t);
1021         void process_without_events (nframes_t);
1022         void process_with_events    (nframes_t);
1023         void process_audition       (nframes_t);
1024         int  process_export         (nframes_t, ARDOUR::AudioExportSpecification*);
1025         
1026         /* slave tracking */
1027
1028         static const int delta_accumulator_size = 25;
1029         int delta_accumulator_cnt;
1030         long delta_accumulator[delta_accumulator_size];
1031         long average_slave_delta;
1032         int  average_dir;
1033         bool have_first_delta_accumulator;
1034         
1035         enum SlaveState {
1036                 Stopped,
1037                 Waiting,
1038                 Running
1039         };
1040         
1041         SlaveState slave_state;
1042         nframes_t slave_wait_end;
1043
1044         void reset_slave_state ();
1045         bool follow_slave (nframes_t, nframes_t);
1046         void set_slave_source (SlaveSource);
1047
1048         bool _exporting;
1049         int prepare_to_export (ARDOUR::AudioExportSpecification&);
1050
1051         void prepare_diskstreams ();
1052         void commit_diskstreams (nframes_t, bool& session_requires_butler);
1053         int  process_routes (nframes_t, nframes_t);
1054         int  silent_process_routes (nframes_t, nframes_t);
1055
1056         bool get_rec_monitors_input () {
1057                 if (actively_recording()) {
1058                         return true;
1059                 } else {
1060                         if (Config->get_auto_input()) {
1061                                 return false;
1062                         } else {
1063                                 return true;
1064                         }
1065                 }
1066         }
1067
1068         int get_transport_declick_required () {
1069
1070                 if (transport_sub_state & PendingDeclickIn) {
1071                         transport_sub_state &= ~PendingDeclickIn;
1072                         return 1;
1073                 } else if (transport_sub_state & PendingDeclickOut) {
1074                         return -1;
1075                 } else {
1076                         return 0;
1077                 }
1078         }
1079
1080         bool maybe_stop (nframes_t limit) {
1081                 if ((_transport_speed > 0.0f && _transport_frame >= limit) || (_transport_speed < 0.0f && _transport_frame == 0)) {
1082                         stop_transport ();
1083                         return true;
1084                 }
1085                 return false;
1086         }
1087
1088         bool maybe_sync_start (nframes_t&, nframes_t&);
1089
1090         void check_declick_out ();
1091
1092         MIDI::MachineControl*    mmc;
1093         MIDI::Port*             _mmc_port;
1094         MIDI::Port*             _mtc_port;
1095         MIDI::Port*             _midi_port;
1096         string                  _path;
1097         string                  _name;
1098         bool                     session_send_mmc;
1099         bool                     session_send_mtc;
1100         bool                     session_midi_feedback;
1101         bool                     play_loop;
1102         bool                     loop_changing;
1103         nframes_t                last_loopend;
1104
1105         boost::scoped_ptr<SessionDirectory> _session_dir;
1106
1107         RingBuffer<Event*> pending_events;
1108
1109         void hookup_io ();
1110         void when_engine_running ();
1111         void graph_reordered ();
1112
1113         string _current_snapshot_name;
1114
1115         XMLTree* state_tree;
1116         bool     state_was_pending;
1117         StateOfTheState _state_of_the_state;
1118
1119         void     auto_save();
1120         int      load_options (const XMLNode&);
1121         XMLNode& get_options () const;
1122         int      load_state (string snapshot_name);
1123         bool     save_config_options_predicate (ConfigVariableBase::Owner owner) const;
1124
1125         nframes_t _last_roll_location;
1126         nframes_t _last_record_location;
1127
1128         bool              pending_locate_roll;
1129         nframes_t         pending_locate_frame;
1130         bool              pending_locate_flush;
1131         bool              pending_abort;
1132         bool              pending_auto_loop;
1133         
1134         Sample*           butler_mixdown_buffer;
1135         float*            butler_gain_buffer;
1136         pthread_t         butler_thread;
1137         Glib::Mutex       butler_request_lock;
1138         Glib::Cond        butler_paused;
1139         bool              butler_should_run;
1140         mutable gint      butler_should_do_transport_work;
1141         int               butler_request_pipe[2];
1142
1143         inline bool transport_work_requested() const { return g_atomic_int_get(&butler_should_do_transport_work); }
1144         
1145         struct ButlerRequest {
1146             enum Type {
1147                     Wake,
1148                     Run,
1149                     Pause,
1150                     Quit
1151             };
1152         };
1153
1154         enum PostTransportWork {
1155                 PostTransportStop               = 0x1,
1156                 PostTransportDisableRecord      = 0x2,
1157                 PostTransportPosition           = 0x8,
1158                 PostTransportDidRecord          = 0x20,
1159                 PostTransportDuration           = 0x40,
1160                 PostTransportLocate             = 0x80,
1161                 PostTransportRoll               = 0x200,
1162                 PostTransportAbort              = 0x800,
1163                 PostTransportOverWrite          = 0x1000,
1164                 PostTransportSpeed              = 0x2000,
1165                 PostTransportAudition           = 0x4000,
1166                 PostTransportScrub              = 0x8000,
1167                 PostTransportReverse            = 0x10000,
1168                 PostTransportInputChange        = 0x20000,
1169                 PostTransportCurveRealloc       = 0x40000
1170         };
1171         
1172         static const PostTransportWork ProcessCannotProceedMask = 
1173                 PostTransportWork (PostTransportInputChange|
1174                                    PostTransportSpeed|
1175                                    PostTransportReverse|
1176                                    PostTransportCurveRealloc|
1177                                    PostTransportScrub|
1178                                    PostTransportAudition|
1179                                    PostTransportLocate|
1180                                    PostTransportStop);
1181         
1182         PostTransportWork post_transport_work;
1183
1184         void             summon_butler ();
1185         void             schedule_butler_transport_work ();
1186         int              start_butler_thread ();
1187         void             terminate_butler_thread ();
1188         static void    *_butler_thread_work (void *arg);
1189         void*            butler_thread_work ();
1190
1191         uint32_t    cumulative_rf_motion;
1192         uint32_t    rf_scale;
1193
1194         void set_rf_speed (float speed);
1195         void reset_rf_scale (nframes_t frames_moved);
1196
1197         Locations        _locations;
1198         void              locations_changed ();
1199         void              locations_added (Location*);
1200         void              handle_locations_changed (Locations::LocationList&);
1201
1202         sigc::connection auto_punch_start_changed_connection;
1203         sigc::connection auto_punch_end_changed_connection;
1204         sigc::connection auto_punch_changed_connection;
1205         void             auto_punch_start_changed (Location *);
1206         void             auto_punch_end_changed (Location *);
1207         void             auto_punch_changed (Location *);
1208
1209         sigc::connection auto_loop_start_changed_connection;
1210         sigc::connection auto_loop_end_changed_connection;
1211         sigc::connection auto_loop_changed_connection;
1212         void             auto_loop_changed (Location *);
1213
1214         typedef list<Event *> Events;
1215         Events           events;
1216         Events           immediate_events;
1217         Events::iterator next_event;
1218
1219         /* there can only ever be one of each of these */
1220
1221         Event *auto_loop_event;
1222         Event *punch_out_event;
1223         Event *punch_in_event;
1224
1225         /* events */
1226
1227         void dump_events () const;
1228         void queue_event (Event *ev);
1229         void merge_event (Event*);
1230         void replace_event (Event::Type, nframes_t action_frame, nframes_t target = 0);
1231         bool _replace_event (Event*);
1232         bool _remove_event (Event *);
1233         void _clear_event_type (Event::Type);
1234
1235         void first_stage_init (string path, string snapshot_name);
1236         int  second_stage_init (bool new_tracks);
1237         void find_current_end ();
1238         void remove_empty_sounds ();
1239
1240         void setup_midi_control ();
1241         //int  midi_read (MIDI::Port *);
1242
1243         void enable_record ();
1244         
1245         void increment_transport_position (uint32_t val) {
1246                 if (max_frames - val < _transport_frame) {
1247                         _transport_frame = max_frames;
1248                 } else {
1249                         _transport_frame += val;
1250                 }
1251         }
1252
1253         void decrement_transport_position (uint32_t val) {
1254                 if (val < _transport_frame) {
1255                         _transport_frame -= val;
1256                 } else {
1257                         _transport_frame = 0;
1258                 }
1259         }
1260
1261         void post_transport_motion ();
1262         static void *session_loader_thread (void *arg);
1263
1264         void *do_work();
1265
1266         void set_next_event ();
1267         void process_event (Event *ev);
1268
1269         /* MIDI Machine Control */
1270
1271         void deliver_mmc (MIDI::MachineControl::Command, nframes_t);
1272         //void deliver_midi_message (MIDI::Port * port, MIDI::eventType ev, MIDI::channel_t, MIDI::EventTwoBytes);
1273         //void deliver_data (MIDI::Port* port, MIDI::byte*, int32_t size);
1274
1275         void spp_start (MIDI::Parser&);
1276         void spp_continue (MIDI::Parser&);
1277         void spp_stop (MIDI::Parser&);
1278
1279         void mmc_deferred_play (MIDI::MachineControl &);
1280         void mmc_stop (MIDI::MachineControl &);
1281         void mmc_step (MIDI::MachineControl &, int);
1282         void mmc_pause (MIDI::MachineControl &);
1283         void mmc_record_pause (MIDI::MachineControl &);
1284         void mmc_record_strobe (MIDI::MachineControl &);
1285         void mmc_record_exit (MIDI::MachineControl &);
1286         void mmc_track_record_status (MIDI::MachineControl &, uint32_t track, bool enabled);
1287         void mmc_fast_forward (MIDI::MachineControl &);
1288         void mmc_rewind (MIDI::MachineControl &);
1289         void mmc_locate (MIDI::MachineControl &, const MIDI::byte *);
1290         void mmc_shuttle (MIDI::MachineControl &mmc, float speed, bool forw);
1291         void mmc_record_enable (MIDI::MachineControl &mmc, size_t track, bool enabled);
1292
1293         struct timeval last_mmc_step;
1294         double step_speed;
1295
1296         typedef sigc::slot<bool> MidiTimeoutCallback;
1297         typedef list<MidiTimeoutCallback> MidiTimeoutList;
1298
1299         MidiTimeoutList midi_timeouts;
1300         bool mmc_step_timeout ();
1301
1302         MIDI::byte mmc_buffer[32];
1303         MIDI::byte mtc_msg[16];
1304         MIDI::byte mtc_smpte_bits;   /* encoding of SMTPE type for MTC */
1305         MIDI::byte midi_msg[16];
1306         nframes_t  outbound_mtc_smpte_frame;
1307         SMPTE::Time transmitting_smpte_time;
1308         int next_quarter_frame_to_send;
1309         
1310         double _frames_per_smpte_frame; /* has to be floating point because of drop frame */
1311         nframes_t _frames_per_hour;
1312         nframes_t _smpte_frames_per_hour;
1313         nframes_t _smpte_offset;
1314         bool _smpte_offset_negative;
1315
1316         /* cache the most-recently requested time conversions. This helps when we
1317          * have multiple clocks showing the same time (e.g. the transport frame) */
1318         bool           last_smpte_valid;
1319         nframes_t last_smpte_when;
1320         SMPTE::Time    last_smpte;
1321         
1322         bool _send_smpte_update; ///< Flag to send a full frame (SMPTE) MTC message this cycle
1323
1324         int send_full_time_code(nframes_t nframes);
1325         int send_midi_time_code_for_cycle(nframes_t nframes);
1326
1327         nframes_t adjust_apparent_position (nframes_t frames);
1328         
1329         void reset_record_status ();
1330         
1331         int no_roll (nframes_t nframes, nframes_t offset);
1332         
1333         bool non_realtime_work_pending() const { return static_cast<bool>(post_transport_work); }
1334         bool process_can_proceed() const { return !(post_transport_work & ProcessCannotProceedMask); }
1335
1336         struct MIDIRequest {
1337             
1338             enum Type {
1339                     SendFullMTC,
1340                     SendMTC,
1341                     SendMMC,
1342                     PortChange,
1343                     SendMessage,
1344                     Deliver,
1345                     Quit
1346             };
1347             
1348             Type type;
1349             MIDI::MachineControl::Command mmc_cmd;
1350             nframes_t locate_frame;
1351
1352             // for SendMessage type
1353
1354             MIDI::Port * port;
1355             MIDI::channel_t chan;
1356             union {
1357                 MIDI::EventTwoBytes data;
1358                 MIDI::byte* buf;
1359             };
1360
1361             union { 
1362                 MIDI::eventType ev;
1363                 int32_t size;
1364             };
1365
1366             MIDIRequest () {}
1367             
1368             void *operator new(size_t ignored) {
1369                     return pool.alloc ();
1370             };
1371
1372             void operator delete(void *ptr, size_t size) {
1373                     pool.release (ptr);
1374             }
1375
1376           private:
1377             static MultiAllocSingleReleasePool pool;
1378         };
1379
1380         mutable  gint   butler_active;
1381         
1382         void          change_midi_ports ();
1383         int           use_config_midi_ports ();
1384
1385         bool waiting_to_start;
1386
1387         void set_play_loop (bool yn);
1388         void overwrite_some_buffers (Diskstream*);
1389         void flush_all_inserts ();
1390         void locate (nframes_t, bool with_roll, bool with_flush, bool with_loop=false);
1391         void start_locate (nframes_t, bool with_roll, bool with_flush, bool with_loop=false);
1392         void force_locate (nframes_t frame, bool with_roll = false);
1393         void set_diskstream_speed (Diskstream*, float speed);
1394         void set_transport_speed (float speed, bool abort = false);
1395         void stop_transport (bool abort = false);
1396         void start_transport ();
1397         void actually_start_transport ();
1398         void realtime_stop (bool abort);
1399         void non_realtime_start_scrub ();
1400         void non_realtime_set_speed ();
1401         void non_realtime_locate ();
1402         void non_realtime_stop (bool abort, int entry_request_count, bool& finished);
1403         void non_realtime_overwrite (int entry_request_count, bool& finished);
1404         void butler_transport_work ();
1405         void post_transport ();
1406         void engine_halted ();
1407         void xrun_recovery ();
1408
1409         TempoMap    *_tempo_map;
1410         void          tempo_map_changed (Change);
1411
1412         /* edit/mix groups */
1413
1414         int load_route_groups (const XMLNode&, bool is_edit);
1415         int load_edit_groups (const XMLNode&);
1416         int load_mix_groups (const XMLNode&);
1417
1418
1419         list<RouteGroup *> edit_groups;
1420         list<RouteGroup *> mix_groups;
1421
1422         /* disk-streams */
1423
1424         SerializedRCUManager<DiskstreamList>  diskstreams; 
1425
1426         uint32_t dstream_buffer_size;
1427         int  load_diskstreams (const XMLNode&);
1428
1429         /* routes stuff */
1430
1431         SerializedRCUManager<RouteList>  routes;
1432
1433         void   add_routes (RouteList&, bool save);
1434         uint32_t destructive_index;
1435
1436         int load_routes (const XMLNode&);
1437         boost::shared_ptr<Route> XMLRouteFactory (const XMLNode&);
1438
1439         /* mixer stuff */
1440
1441         bool       solo_update_disabled;
1442         bool       currently_soloing;
1443         
1444         void route_mute_changed (void *src);
1445         void route_solo_changed (void *src, boost::weak_ptr<Route>);
1446         void catch_up_on_solo ();
1447         void update_route_solo_state ();
1448         void modify_solo_mute (bool, bool);
1449         void strip_portname_for_solo (string& portname);
1450
1451         /* REGION MANAGEMENT */
1452
1453         mutable Glib::Mutex region_lock;
1454         typedef map<PBD::ID,boost::shared_ptr<Region> > RegionList;
1455         RegionList regions;
1456         
1457         void add_region (boost::shared_ptr<Region>);
1458         void region_changed (Change, boost::weak_ptr<Region>);
1459         void remove_region (boost::weak_ptr<Region>);
1460
1461         int load_regions (const XMLNode& node);
1462
1463         /* SOURCES */
1464         
1465         mutable Glib::Mutex source_lock;
1466         typedef std::map<PBD::ID,boost::shared_ptr<Source> > SourceMap;
1467
1468         SourceMap sources;
1469
1470         int load_sources (const XMLNode& node);
1471         XMLNode& get_sources_as_xml ();
1472
1473         boost::shared_ptr<Source> XMLSourceFactory (const XMLNode&);
1474
1475         /* PLAYLISTS */
1476         
1477         mutable Glib::Mutex playlist_lock;
1478         typedef set<boost::shared_ptr<Playlist> > PlaylistList;
1479         PlaylistList playlists;
1480         PlaylistList unused_playlists;
1481
1482         int load_playlists (const XMLNode&);
1483         int load_unused_playlists (const XMLNode&);
1484         void remove_playlist (boost::weak_ptr<Playlist>);
1485         void track_playlist (bool, boost::weak_ptr<Playlist>);
1486
1487         boost::shared_ptr<Playlist> playlist_factory (string name);
1488         boost::shared_ptr<Playlist> XMLPlaylistFactory (const XMLNode&);
1489
1490         void playlist_length_changed ();
1491         void diskstream_playlist_changed (boost::shared_ptr<Diskstream>);
1492
1493         /* NAMED SELECTIONS */
1494
1495         mutable Glib::Mutex named_selection_lock;
1496         typedef set<NamedSelection *> NamedSelectionList;
1497         NamedSelectionList named_selections;
1498
1499         int load_named_selections (const XMLNode&);
1500
1501         NamedSelection *named_selection_factory (string name);
1502         NamedSelection *XMLNamedSelectionFactory (const XMLNode&);
1503
1504         /* CURVES and AUTOMATION LISTS */
1505         std::map<PBD::ID, Curve*> curves;
1506         std::map<PBD::ID, AutomationList*> automation_lists;
1507
1508         /* DEFAULT FADE CURVES */
1509
1510         float default_fade_steepness;
1511         float default_fade_msecs;
1512
1513         /* AUDITIONING */
1514
1515         boost::shared_ptr<Auditioner> auditioner;
1516         void set_audition (boost::shared_ptr<Region>);
1517         void non_realtime_set_audition ();
1518         boost::shared_ptr<Region> pending_audition_region;
1519
1520         /* EXPORT */
1521
1522         /* FLATTEN */
1523
1524         int flatten_one_track (AudioTrack&, nframes_t start, nframes_t cnt);
1525
1526         /* INSERT AND SEND MANAGEMENT */
1527         
1528         list<PortInsert *>              _port_inserts;
1529         list<PluginInsert *>            _plugin_inserts;
1530         list<Send *>                    _sends;
1531         boost::dynamic_bitset<uint32_t> send_bitset;
1532         boost::dynamic_bitset<uint32_t> insert_bitset;
1533         uint32_t                        send_cnt;
1534         uint32_t                        insert_cnt;
1535
1536
1537         void add_processor (Processor *);
1538         void remove_processor (Processor *);
1539
1540         /* S/W RAID */
1541
1542         struct space_and_path {
1543             uint32_t blocks; /* 4kB blocks */
1544             string path;
1545             
1546             space_and_path() { 
1547                     blocks = 0;
1548             }
1549         };
1550
1551         struct space_and_path_ascending_cmp {
1552             bool operator() (space_and_path a, space_and_path b) {
1553                     return a.blocks > b.blocks;
1554             }
1555         };
1556         
1557         void setup_raid_path (string path);
1558
1559         vector<space_and_path> session_dirs;
1560         vector<space_and_path>::iterator last_rr_session_dir;
1561         uint32_t _total_free_4k_blocks;
1562         Glib::Mutex space_lock;
1563         
1564         string get_best_session_directory_for_new_source ();
1565         void refresh_disk_space ();
1566
1567         mutable gint _playback_load;
1568         mutable gint _capture_load;
1569         mutable gint _playback_load_min;
1570         mutable gint _capture_load_min;
1571
1572         /* I/O bundles */
1573
1574         typedef list<boost::shared_ptr<Bundle> > BundleList;
1575         mutable Glib::Mutex bundle_lock;
1576         BundleList _bundles;
1577         XMLNode* _bundle_xml_node;
1578         int load_bundles (XMLNode const &);
1579
1580         void reverse_diskstream_buffers ();
1581
1582         UndoHistory _history;
1583         UndoTransaction* current_trans;
1584
1585         GlobalRouteBooleanState get_global_route_boolean (bool (Route::*method)(void) const);
1586         GlobalRouteMeterState get_global_route_metering ();
1587
1588         void set_global_route_boolean (GlobalRouteBooleanState s, void (Route::*method)(bool, void*), void *arg);
1589         void set_global_route_metering (GlobalRouteMeterState s, void *arg);
1590
1591         void set_global_mute (GlobalRouteBooleanState s, void *src);
1592         void set_global_solo (GlobalRouteBooleanState s, void *src);
1593         void set_global_record_enable (GlobalRouteBooleanState s, void *src);
1594
1595         void jack_timebase_callback (jack_transport_state_t, nframes_t, jack_position_t*, int);
1596         int  jack_sync_callback (jack_transport_state_t, jack_position_t*);
1597         void reset_jack_connection (jack_client_t* jack);
1598         void record_enable_change_all (bool yn);
1599
1600         XMLNode& state(bool);
1601
1602         /* click track */
1603
1604         struct Click {
1605             nframes_t start;
1606             nframes_t duration;
1607             nframes_t offset;
1608             const Sample *data;
1609
1610             Click (nframes_t s, nframes_t d, const Sample *b) 
1611                     : start (s), duration (d), data (b) { offset = 0; }
1612             
1613             void *operator new(size_t ignored) {
1614                     return pool.alloc ();
1615             };
1616
1617             void operator delete(void *ptr, size_t size) {
1618                     pool.release (ptr);
1619             }
1620
1621           private:
1622             static Pool pool;
1623         };
1624  
1625         typedef list<Click*> Clicks;
1626
1627         Clicks          clicks;
1628         bool           _clicking;
1629         boost::shared_ptr<IO> _click_io;
1630         Sample*         click_data;
1631         Sample*         click_emphasis_data;
1632         nframes_t  click_length;
1633         nframes_t  click_emphasis_length;
1634         mutable Glib::RWLock click_lock;
1635
1636         static const Sample    default_click[];
1637         static const nframes_t default_click_length;
1638         static const Sample    default_click_emphasis[];
1639         static const nframes_t default_click_emphasis_length;
1640
1641         Click *get_click();
1642         void   setup_click_sounds (int which);
1643         void   clear_clicks ();
1644         void   click (nframes_t start, nframes_t nframes, nframes_t offset);
1645
1646         vector<Route*> master_outs;
1647         
1648         /* range playback */
1649
1650         list<AudioRange> current_audio_range;
1651         bool _play_range;
1652         void set_play_range (bool yn);
1653         void setup_auto_play ();
1654
1655         /* main outs */
1656         uint32_t main_outs;
1657         
1658         boost::shared_ptr<IO> _master_out;
1659         boost::shared_ptr<IO> _control_out;
1660
1661         gain_t* _gain_automation_buffer;
1662         pan_t** _pan_automation_buffer;
1663         void allocate_pan_automation_buffers (nframes_t nframes, uint32_t howmany, bool force);
1664         uint32_t _npan_buffers;
1665
1666         /* VST support */
1667
1668         long _vst_callback (VSTPlugin*,
1669                             long opcode,
1670                             long index,
1671                             long value,
1672                             void* ptr,
1673                             float opt);
1674
1675         /* number of hardware ports we're using,
1676            based on max (requested,available)
1677         */
1678
1679         uint32_t n_physical_outputs;
1680         uint32_t n_physical_inputs;
1681
1682
1683         int find_all_sources (std::string path, std::set<std::string>& result);
1684         int find_all_sources_across_snapshots (std::set<std::string>& result, bool exclude_this_snapshot);
1685
1686         LayerModel layer_model;
1687         CrossfadeModel xfade_model;
1688
1689         typedef std::set<boost::shared_ptr<PBD::Controllable> > Controllables;
1690         Glib::Mutex controllables_lock;
1691         Controllables controllables;
1692
1693         void reset_native_file_format();
1694         bool first_file_data_format_reset;
1695         bool first_file_header_format_reset;
1696
1697         void config_changed (const char*);
1698
1699         XMLNode& get_control_protocol_state ();
1700         
1701         void set_history_depth (uint32_t depth);
1702         void sync_order_keys ();
1703         
1704         static bool _disable_all_loaded_plugins;
1705 };
1706
1707 } // namespace ARDOUR
1708
1709 #endif /* __ardour_session_h__ */