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