2 Copyright (C) 2000 Paul Davis
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.
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.
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.
20 #ifndef __ardour_session_h__
21 #define __ardour_session_h__
31 #include <boost/weak_ptr.hpp>
32 #include <boost/dynamic_bitset.hpp>
38 #include <glibmm/thread.h>
40 #include <pbd/error.h>
44 #include <pbd/statefuldestructible.h>
46 #include <midi++/types.h>
47 #include <midi++/mmc.h>
49 #include <pbd/stateful.h>
50 #include <pbd/destructible.h>
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>
76 class AudioDiskstream;
81 class AudioFileSource;
96 class ControlProtocolInfo;
98 struct AudioExportSpecification;
106 class Session : public PBD::StatefulDestructible
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;
136 InputConfigurationChange,
140 /* only one of each of these events
141 can be queued at any one time
157 nframes_t action_frame;
158 nframes_t target_frame;
167 boost::shared_ptr<Region> region;
169 list<AudioRange> audio_range;
170 list<MusicRange> music_range;
172 Event(Type t, Action a, nframes_t when, nframes_t where, float spd, bool yn = false)
176 target_frame (where),
180 void set_ptr (void* p) {
184 bool before (const Event& other) const {
185 return action_frame < other.action_frame;
188 bool after (const Event& other) const {
189 return action_frame > other.action_frame;
192 static bool compare (const Event *e1, const Event *e2) {
193 return e1->before (*e2);
196 void *operator new (size_t ignored) {
197 return pool.alloc ();
200 void operator delete(void *ptr, size_t size) {
204 static const nframes_t Immediate = 0;
207 static MultiAllocSingleReleasePool pool;
210 /* creating from an XML file */
212 Session (AudioEngine&,
214 string snapshot_name,
215 string* mix_template = 0);
217 /* creating a new Session */
219 Session (AudioEngine&,
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);
233 static int find_session (string str, string& path, string& snapshot, bool& isnew);
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;
241 void set_snap_name ();
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;
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;
255 string peak_path_from_audio_path (string) const;
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>&);
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);
266 void process (nframes_t nframes);
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; }
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);
276 bool have_captured() const { return _have_captured; }
278 void refill_all_diskstream_buffers ();
279 uint32_t diskstream_buffer_size() const { return dstream_buffer_size; }
281 uint32_t get_next_diskstream_id() const { return n_diskstreams(); }
282 uint32_t n_diskstreams() const;
284 typedef std::list<boost::shared_ptr<Diskstream> > DiskstreamList;
285 typedef std::list<boost::shared_ptr<Route> > RouteList;
287 boost::shared_ptr<RouteList> get_routes() const {
288 return routes.reader ();
291 uint32_t nroutes() const { return routes.reader()->size(); }
292 uint32_t ntracks () const;
293 uint32_t nbusses () const;
295 struct RoutePublicOrderSorter {
296 bool operator() (boost::shared_ptr<Route>, boost::shared_ptr<Route> b);
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);
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);
307 bool route_name_unique (string) const;
309 bool get_record_enabled() const {
310 return (record_status () >= Enabled);
313 RecordState record_status() const {
314 return (RecordState) g_atomic_int_get (&_record_status);
317 bool actively_recording () {
318 return record_status() == Recording;
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 ();
326 void maybe_write_autosave ();
328 /* Proxy signal for region hidden changes */
330 sigc::signal<void,boost::shared_ptr<Region> > RegionHiddenChange;
332 /* Emitted when all i/o connections are complete */
334 sigc::signal<void> IOConnectionsComplete;
336 /* Record status signals */
338 sigc::signal<void> RecordStateChanged;
340 /* Transport mechanism signals */
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;
347 sigc::signal<void,RouteList&> RouteAdded;
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);
354 void request_play_loop (bool yn);
355 bool get_play_loop () const { return play_loop; }
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 ();
369 bool locate_pending() const { return static_cast<bool>(post_transport_work&PostTransportLocate); }
370 bool transport_locked () const;
374 int remove_region_from_region_list (boost::shared_ptr<Region>);
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; }
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; }
385 float smpte_frames_per_second() const;
386 bool smpte_drop_frames() const;
390 Locations *locations() { return &_locations; }
392 sigc::signal<void,Location*> auto_loop_location_changed;
393 sigc::signal<void,Location*> auto_punch_location_changed;
394 sigc::signal<void> locations_modified;
396 void set_auto_punch_location (Location *);
397 void set_auto_loop_location (Location *);
398 int location_name(string& result, string base = string(""));
400 void reset_input_monitor_state ();
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);
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; }
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);
419 static int rename_template (string old_name, string new_name);
421 static int delete_template (string name);
423 sigc::signal<void,string> StateSaved;
424 sigc::signal<void> StateReady;
426 vector<string*>* possible_states() const;
427 static vector<string*>* possible_states(string path);
429 XMLNode& get_state();
430 int set_state(const XMLNode& node); // not idempotent
431 XMLNode& get_template();
433 void add_instant_xml (XMLNode&, const std::string& dir);
435 enum StateOfTheState {
440 InitialConnecting = 0x8,
445 StateOfTheState state_of_the_state() const { return _state_of_the_state; }
447 RouteGroup* add_edit_group (string);
448 RouteGroup* add_mix_group (string);
450 void remove_edit_group (RouteGroup&);
451 void remove_mix_group (RouteGroup&);
453 RouteGroup *mix_group_by_name (string);
454 RouteGroup *edit_group_by_name (string);
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;
461 void foreach_edit_group (sigc::slot<void,RouteGroup*> sl) {
462 for (list<RouteGroup *>::iterator i = edit_groups.begin(); i != edit_groups.end(); i++) {
467 void foreach_mix_group (sigc::slot<void,RouteGroup*> sl) {
468 for (list<RouteGroup *>::iterator i = mix_groups.begin(); i != mix_groups.end(); i++) {
473 /* fundamental operations. duh. */
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);
478 void remove_route (boost::shared_ptr<Route>);
480 void resort_routes ();
481 void resort_routes_using (boost::shared_ptr<RouteList>);
483 void set_remote_control_ids();
485 AudioEngine &engine() { return _engine; };
492 nframes_t transport_frame () const {return _transport_frame; }
493 nframes_t audible_frame () const;
507 int set_smpte_format (SmpteFormat);
508 void sync_time_vars();
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&);
517 void smpte_duration (nframes_t, SMPTE::Time&) const;
518 void smpte_duration_string (char *, nframes_t) const;
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; }
525 nframes_t convert_to_frames_at (nframes_t position, AnyTime&);
527 static sigc::signal<void> StartTimeChanged;
528 static sigc::signal<void> EndTimeChanged;
529 static sigc::signal<void> SMPTEOffsetChanged;
531 void request_slave_source (SlaveSource);
532 bool synced_to_jack() const { return Config->get_slave_source() == JACK; }
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; }
538 int jack_slave_sync (nframes_t);
540 TempoMap& tempo_map() { return *_tempo_map; }
544 sigc::signal<void,boost::weak_ptr<AudioRegion> > AudioRegionAdded;
545 sigc::signal<void,boost::weak_ptr<AudioRegion> > AudioRegionRemoved;
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);
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);
554 boost::shared_ptr<AudioRegion> XMLRegionFactory (const XMLNode&, bool full);
556 template<class T> void foreach_audio_region (T *obj, void (T::*func)(boost::shared_ptr<AudioRegion>));
558 /* source management */
560 struct import_status : public InterThreadInfo {
566 volatile bool freeze;
567 std::vector<Glib::ustring> paths;
570 std::vector<boost::shared_ptr<AudioRegion> > new_regions;
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);
578 SlaveSource post_export_slave;
579 nframes_t post_export_position;
581 int start_audio_export (ARDOUR::AudioExportSpecification&);
582 int stop_audio_export (ARDOUR::AudioExportSpecification&);
583 void finalize_audio_export ();
585 void add_source (boost::shared_ptr<Source>);
586 void remove_source (boost::weak_ptr<Source>);
588 struct cleanup_report {
589 vector<string> paths;
593 int cleanup_sources (cleanup_report&);
594 int cleanup_trash_sources (cleanup_report&);
596 int destroy_region (boost::shared_ptr<Region>);
597 int destroy_regions (std::list<boost::shared_ptr<Region> >);
599 int remove_last_capture ();
601 /* handlers should return -1 for "stop cleanup", 0 for
602 "yes, delete this playlist" and 1 for "no, don't delete
606 sigc::signal<int,boost::shared_ptr<ARDOUR::Playlist> > AskAboutPlaylistDeletion;
608 /* handlers should return !0 for use pending state, 0 for
612 static sigc::signal<int> AskAboutPendingState;
614 boost::shared_ptr<AudioFileSource> create_audio_source_for_session (ARDOUR::AudioDiskstream&, uint32_t which_channel, bool destructive);
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);
619 /* playlist management */
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;
626 uint32_t n_playlists() const;
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> >&);
631 /* named selections */
633 NamedSelection* named_selection_by_name (string name);
634 void add_named_selection (NamedSelection *);
635 void remove_named_selection (NamedSelection *);
637 template<class T> void foreach_named_selection (T& obj, void (T::*func)(NamedSelection&));
638 sigc::signal<void> NamedSelectionAdded;
639 sigc::signal<void> NamedSelectionRemoved;
641 /* Curves and AutomationLists (TODO when they go away) */
642 void add_curve(Curve*);
643 void add_automation_list(AutomationList*);
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);
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;
659 sigc::signal<void,bool> AuditionActive;
661 /* flattening stuff */
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&);
667 /* session-wide solo/mute/rec-enable */
669 bool soloing() const { return currently_soloing; }
671 void set_all_solo (bool);
672 void set_all_mute (bool);
674 sigc::signal<void,bool> SoloActive;
675 sigc::signal<void> SoloChanged;
677 void record_disenable_all ();
678 void record_enable_all ();
680 /* control/master out */
682 boost::shared_ptr<IO> control_out() const { return _control_out; }
683 boost::shared_ptr<IO> master_out() const { return _master_out; }
685 /* insert/send management */
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(); }
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);
696 /* s/w "RAID" management */
698 nframes_t available_capture_duration();
700 /* I/O Connections */
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;
707 sigc::signal<void,Connection *> ConnectionAdded;
708 sigc::signal<void,Connection *> ConnectionRemoved;
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; }
719 sigc::signal<void> MTC_PortChanged;
720 sigc::signal<void> MMC_PortChanged;
721 sigc::signal<void> MIDI_PortChanged;
723 void set_trace_midi_input (bool, MIDI::Port* port = 0);
724 void set_trace_midi_output (bool, MIDI::Port* port = 0);
726 bool get_trace_midi_input(MIDI::Port *port = 0);
727 bool get_trace_midi_output(MIDI::Port *port = 0);
729 void send_midi_message (MIDI::Port * port, MIDI::eventType ev, MIDI::channel_t, MIDI::EventTwoBytes);
731 void deliver_midi (MIDI::Port*, MIDI::byte*, int32_t size);
733 void set_mmc_device_id (uint32_t id);
737 void start_scrub (nframes_t where);
739 void set_scrub_speed (float);
740 nframes_t scrub_buffer_size() const;
741 sigc::signal<void> ScrubReady;
743 /* History (for editors, mixers, UIs etc.) */
745 void undo (uint32_t n) {
749 void redo (uint32_t n) {
753 UndoHistory& history() { return _history; }
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(); }
760 void begin_reversible_command (string cmd_name);
761 void commit_reversible_command (Command* cmd = 0);
763 void add_command (Command *const cmd) {
764 current_trans->add_command (cmd);
767 std::map<PBD::ID, PBD::StatefulThingWithGoingAway*> registry;
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*);
773 Command* global_state_command_factory (const XMLNode& n);
775 class GlobalRouteStateCommand : public Command
778 GlobalRouteStateCommand (Session&, void*);
779 GlobalRouteStateCommand (Session&, const XMLNode& node);
780 int set_state (const XMLNode&);
781 XMLNode& get_state ();
784 GlobalRouteBooleanState before, after;
790 class GlobalSoloStateCommand : public GlobalRouteStateCommand
793 GlobalSoloStateCommand (Session &, void *src);
794 GlobalSoloStateCommand (Session&, const XMLNode&);
795 void operator()(); //redo
797 XMLNode &get_state();
801 class GlobalMuteStateCommand : public GlobalRouteStateCommand
804 GlobalMuteStateCommand(Session &, void *src);
805 GlobalMuteStateCommand (Session&, const XMLNode&);
806 void operator()(); // redo
808 XMLNode &get_state();
812 class GlobalRecordEnableStateCommand : public GlobalRouteStateCommand
815 GlobalRecordEnableStateCommand(Session &, void *src);
816 GlobalRecordEnableStateCommand (Session&, const XMLNode&);
817 void operator()(); // redo
819 XMLNode &get_state();
823 class GlobalMeteringStateCommand : public Command
826 GlobalMeteringStateCommand(Session &, void *src);
827 GlobalMeteringStateCommand (Session&, const XMLNode&);
830 XMLNode &get_state();
831 int set_state (const XMLNode&);
837 GlobalRouteMeterState before;
838 GlobalRouteMeterState after;
843 boost::shared_ptr<IO> click_io() { return _click_io; }
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 */
855 TimeStretchRequest () {}
858 boost::shared_ptr<AudioRegion> tempoize_region (TimeStretchRequest&);
860 /* disk, buffer loads */
862 uint32_t playback_load ();
863 uint32_t capture_load ();
864 uint32_t playback_load_min ();
865 uint32_t capture_load_min ();
867 void reset_playback_load_min ();
868 void reset_capture_load_min ();
870 float read_data_rate () const;
871 float write_data_rate () const;
875 void set_audio_range (list<AudioRange>&);
876 void set_music_range (list<MusicRange>&);
878 void request_play_range (bool yn);
879 bool get_play_range () const { return _play_range; }
882 typedef vector<string> FavoriteDirs;
884 static int read_favorite_dirs (FavoriteDirs&);
886 static int write_favorite_dirs (FavoriteDirs&);
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; }
894 /* buffers for gain and pan */
896 gain_t* gain_automation_buffer () const { return _gain_automation_buffer; }
897 pan_t** pan_automation_buffer() const { return _pan_automation_buffer; }
899 /* buffers for conversion */
908 static long vst_callback (AEffect* effect,
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);
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;
927 static sigc::signal<void> SendFeedback;
931 PBD::Controllable* controllable_by_id (const PBD::ID&);
933 void add_controllable (PBD::Controllable*);
934 void remove_controllable (PBD::Controllable*);
937 friend class AudioEngine;
938 void set_block_size (nframes_t nframes);
939 void set_frame_rate (nframes_t nframes);
942 friend class Diskstream;
944 void wait_till_butler_finished();
948 void schedule_curve_reallocation ();
949 void update_latency_compensation (bool, bool);
952 int create (bool& new_session, string* mix_template, nframes_t initial_length);
955 nframes_t compute_initial_length ();
957 static const char* _template_suffix;
958 static const char* _statefile_suffix;
959 static const char* _pending_suffix;
962 PendingDeclickIn = 0x1,
963 PendingDeclickOut = 0x2,
964 StopPendingCapture = 0x4,
965 AutoReturning = 0x10,
966 PendingLocate = 0x20,
967 PendingSetLoop = 0x40
970 /* stuff used in process() should be close together to
974 typedef void (Session::*process_function_type)(nframes_t);
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;
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;
1005 float _meter_falloff;
1006 bool _end_location_is_free;
1008 void set_worst_io_latencies ();
1009 void set_worst_io_latencies_x (IOChange asifwecare, void *ignored) {
1010 set_worst_io_latencies ();
1013 void update_latency_compensation_proxy (void* ignored);
1015 void ensure_passthru_buffers (uint32_t howmany);
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*);
1023 /* slave tracking */
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;
1030 bool have_first_delta_accumulator;
1038 SlaveState slave_state;
1039 nframes_t slave_wait_end;
1041 void reset_slave_state ();
1042 bool follow_slave (nframes_t, nframes_t);
1043 void set_slave_source (SlaveSource);
1046 int prepare_to_export (ARDOUR::AudioExportSpecification&);
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);
1053 bool get_rec_monitors_input () {
1054 if (actively_recording()) {
1057 if (Config->get_auto_input()) {
1065 int get_transport_declick_required () {
1067 if (transport_sub_state & PendingDeclickIn) {
1068 transport_sub_state &= ~PendingDeclickIn;
1070 } else if (transport_sub_state & PendingDeclickOut) {
1077 bool maybe_stop (nframes_t limit) {
1078 if ((_transport_speed > 0.0f && _transport_frame >= limit) || (_transport_speed < 0.0f && _transport_frame == 0)) {
1085 bool maybe_sync_start (nframes_t&, nframes_t&);
1087 void check_declick_out ();
1089 MIDI::MachineControl* mmc;
1090 MIDI::Port* _mmc_port;
1091 MIDI::Port* _mtc_port;
1092 MIDI::Port* _midi_port;
1095 bool session_send_mmc;
1096 bool session_send_mtc;
1097 bool session_midi_feedback;
1100 nframes_t last_loopend;
1102 RingBuffer<Event*> pending_events;
1105 void when_engine_running ();
1106 void graph_reordered ();
1108 string _current_snapshot_name;
1110 XMLTree* state_tree;
1111 bool state_was_pending;
1112 StateOfTheState _state_of_the_state;
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;
1120 nframes_t _last_roll_location;
1121 nframes_t _last_record_location;
1122 bool pending_locate_roll;
1123 nframes_t pending_locate_frame;
1125 bool pending_locate_flush;
1127 bool pending_auto_loop;
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];
1138 inline bool transport_work_requested() const { return g_atomic_int_get(&butler_should_do_transport_work); }
1140 struct ButlerRequest {
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
1167 static const PostTransportWork ProcessCannotProceedMask =
1168 PostTransportWork (PostTransportInputChange|
1170 PostTransportReverse|
1171 PostTransportCurveRealloc|
1173 PostTransportAudition|
1174 PostTransportLocate|
1177 PostTransportWork post_transport_work;
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 ();
1186 uint32_t cumulative_rf_motion;
1189 void set_rf_speed (float speed);
1190 void reset_rf_scale (nframes_t frames_moved);
1192 Locations _locations;
1193 void locations_changed ();
1194 void locations_added (Location*);
1195 void handle_locations_changed (Locations::LocationList&);
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 *);
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 *);
1209 typedef list<Event *> Events;
1211 Events immediate_events;
1212 Events::iterator next_event;
1214 /* there can only ever be one of each of these */
1216 Event *auto_loop_event;
1217 Event *punch_out_event;
1218 Event *punch_in_event;
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);
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 ();
1235 void setup_midi_control ();
1236 int midi_read (MIDI::Port *);
1238 void enable_record ();
1240 void increment_transport_position (uint32_t val) {
1241 if (max_frames - val < _transport_frame) {
1242 _transport_frame = max_frames;
1244 _transport_frame += val;
1248 void decrement_transport_position (uint32_t val) {
1249 if (val < _transport_frame) {
1250 _transport_frame -= val;
1252 _transport_frame = 0;
1256 void post_transport_motion ();
1257 static void *session_loader_thread (void *arg);
1261 void set_next_event ();
1262 void process_event (Event *);
1264 /* MIDI Machine Control */
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);
1270 void spp_start (MIDI::Parser&);
1271 void spp_continue (MIDI::Parser&);
1272 void spp_stop (MIDI::Parser&);
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);
1288 struct timeval last_mmc_step;
1291 typedef sigc::slot<bool> MidiTimeoutCallback;
1292 typedef list<MidiTimeoutCallback> MidiTimeoutList;
1294 MidiTimeoutList midi_timeouts;
1295 bool mmc_step_timeout ();
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;
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;
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)
1316 bool last_smpte_valid;
1317 nframes_t last_smpte_when;
1318 SMPTE::Time last_smpte;
1320 int send_full_time_code ();
1321 int send_midi_time_code ();
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);
1328 nframes_t adjust_apparent_position (nframes_t frames);
1330 void reset_record_status ();
1332 int no_roll (nframes_t nframes, nframes_t offset);
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); }
1337 struct MIDIRequest {
1350 MIDI::MachineControl::Command mmc_cmd;
1351 nframes_t locate_frame;
1353 // for SendMessage type
1356 MIDI::channel_t chan;
1358 MIDI::EventTwoBytes data;
1369 void *operator new(size_t ignored) {
1370 return pool.alloc ();
1373 void operator delete(void *ptr, size_t size) {
1378 static MultiAllocSingleReleasePool pool;
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;
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 ();
1395 bool waiting_to_start;
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 ();
1418 TempoMap *_tempo_map;
1419 void tempo_map_changed (Change);
1421 /* edit/mix groups */
1423 int load_route_groups (const XMLNode&, bool is_edit);
1424 int load_edit_groups (const XMLNode&);
1425 int load_mix_groups (const XMLNode&);
1428 list<RouteGroup *> edit_groups;
1429 list<RouteGroup *> mix_groups;
1433 SerializedRCUManager<DiskstreamList> diskstreams;
1435 uint32_t dstream_buffer_size;
1436 int load_diskstreams (const XMLNode&);
1440 SerializedRCUManager<RouteList> routes;
1442 void add_routes (RouteList&, bool save = true);
1443 uint32_t destructive_index;
1445 int load_routes (const XMLNode&);
1446 boost::shared_ptr<Route> XMLRouteFactory (const XMLNode&);
1450 bool solo_update_disabled;
1451 bool currently_soloing;
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);
1460 /* REGION MANAGEMENT */
1462 mutable Glib::Mutex region_lock;
1463 typedef map<PBD::ID,boost::shared_ptr<AudioRegion> > AudioRegionList;
1464 AudioRegionList audio_regions;
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>);
1470 int load_regions (const XMLNode& node);
1474 mutable Glib::Mutex audio_source_lock;
1475 typedef std::map<PBD::ID,boost::shared_ptr<AudioSource> > AudioSourceList;
1477 AudioSourceList audio_sources;
1479 int load_sources (const XMLNode& node);
1480 XMLNode& get_sources_as_xml ();
1482 boost::shared_ptr<Source> XMLSourceFactory (const XMLNode&);
1486 mutable Glib::Mutex playlist_lock;
1487 typedef set<boost::shared_ptr<Playlist> > PlaylistList;
1488 PlaylistList playlists;
1489 PlaylistList unused_playlists;
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>);
1496 boost::shared_ptr<Playlist> playlist_factory (string name);
1497 boost::shared_ptr<Playlist> XMLPlaylistFactory (const XMLNode&);
1499 void playlist_length_changed ();
1500 void diskstream_playlist_changed (boost::weak_ptr<Diskstream>);
1502 /* NAMED SELECTIONS */
1504 mutable Glib::Mutex named_selection_lock;
1505 typedef set<NamedSelection *> NamedSelectionList;
1506 NamedSelectionList named_selections;
1508 int load_named_selections (const XMLNode&);
1510 NamedSelection *named_selection_factory (string name);
1511 NamedSelection *XMLNamedSelectionFactory (const XMLNode&);
1513 /* CURVES and AUTOMATION LISTS */
1514 std::map<PBD::ID, Curve*> curves;
1515 std::map<PBD::ID, AutomationList*> automation_lists;
1517 /* DEFAULT FADE CURVES */
1519 float default_fade_steepness;
1520 float default_fade_msecs;
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;
1533 int flatten_one_track (AudioTrack&, nframes_t start, nframes_t cnt);
1535 /* INSERT AND SEND MANAGEMENT */
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;
1543 uint32_t insert_cnt;
1546 void add_redirect (Redirect *);
1547 void remove_redirect (Redirect *);
1551 struct space_and_path {
1552 uint32_t blocks; /* 4kB blocks */
1560 struct space_and_path_ascending_cmp {
1561 bool operator() (space_and_path a, space_and_path b) {
1562 return a.blocks > b.blocks;
1566 void setup_raid_path (string path);
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;
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;
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 ();
1585 mutable gint _playback_load;
1586 mutable gint _capture_load;
1587 mutable gint _playback_load_min;
1588 mutable gint _capture_load_min;
1590 /* I/O Connections */
1592 typedef list<Connection *> ConnectionList;
1593 mutable Glib::Mutex connection_lock;
1594 ConnectionList _connections;
1595 int load_connections (const XMLNode&);
1597 void reverse_diskstream_buffers ();
1599 UndoHistory _history;
1600 UndoTransaction* current_trans;
1602 GlobalRouteBooleanState get_global_route_boolean (bool (Route::*method)(void) const);
1603 GlobalRouteMeterState get_global_route_metering ();
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);
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);
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);
1617 XMLNode& state(bool);
1627 Click (nframes_t s, nframes_t d, const Sample *b)
1628 : start (s), duration (d), data (b) { offset = 0; }
1630 void *operator new(size_t ignored) {
1631 return pool.alloc ();
1634 void operator delete(void *ptr, size_t size) {
1642 typedef list<Click*> Clicks;
1646 boost::shared_ptr<IO> _click_io;
1648 Sample* click_emphasis_data;
1649 nframes_t click_length;
1650 nframes_t click_emphasis_length;
1651 mutable Glib::RWLock click_lock;
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;
1659 void setup_click_sounds (int which);
1660 void clear_clicks ();
1661 void click (nframes_t start, nframes_t nframes, nframes_t offset);
1663 vector<Route*> master_outs;
1665 /* range playback */
1667 list<AudioRange> current_audio_range;
1669 void set_play_range (bool yn);
1670 void setup_auto_play ();
1675 boost::shared_ptr<IO> _master_out;
1676 boost::shared_ptr<IO> _control_out;
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;
1685 long _vst_callback (VSTPlugin*,
1692 /* number of hardware audio ports we're using,
1693 based on max (requested,available)
1696 uint32_t n_physical_outputs;
1697 uint32_t n_physical_inputs;
1699 void remove_pending_capture_state ();
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);
1704 LayerModel layer_model;
1705 CrossfadeModel xfade_model;
1707 typedef std::set<PBD::Controllable*> Controllables;
1708 Glib::Mutex controllables_lock;
1709 Controllables controllables;
1711 void reset_native_file_format();
1712 bool first_file_data_format_reset;
1713 bool first_file_header_format_reset;
1715 void config_changed (const char*);
1717 XMLNode& get_control_protocol_state ();
1721 } // namespace ARDOUR
1723 #endif /* __ardour_session_h__ */