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