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