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