2 Copyright (C) 1999-2004 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.
26 #include <cstdio> /* sprintf(3) ... grrr */
32 #include <sigc++/bind.h>
33 #include <sigc++/retype.h>
35 #include <pbd/error.h>
36 #include <pbd/lockmonitor.h>
37 #include <pbd/pathscanner.h>
38 #include <pbd/stl_delete.h>
39 #include <pbd/basename.h>
40 #include <pbd/dirname.h>
42 #include <ardour/audioengine.h>
43 #include <ardour/configuration.h>
44 #include <ardour/session.h>
45 #include <ardour/diskstream.h>
46 #include <ardour/utils.h>
47 #include <ardour/audioplaylist.h>
48 #include <ardour/audioregion.h>
49 #include <ardour/source.h>
50 #include <ardour/filesource.h>
51 #include <ardour/sndfilesource.h>
52 #include <ardour/auditioner.h>
53 #include <ardour/recent_sessions.h>
54 #include <ardour/redirect.h>
55 #include <ardour/send.h>
56 #include <ardour/insert.h>
57 #include <ardour/connection.h>
58 #include <ardour/slave.h>
59 #include <ardour/tempo.h>
60 #include <ardour/audio_track.h>
61 #include <ardour/cycle_timer.h>
62 #include <ardour/named_selection.h>
63 #include <ardour/crossfade.h>
64 #include <ardour/playlist.h>
65 #include <ardour/click.h>
66 #include <ardour/timestamps.h>
71 using namespace ARDOUR;
72 //using namespace sigc;
74 const char* Session::_template_suffix = X_(".template");
75 const char* Session::_statefile_suffix = X_(".ardour");
76 const char* Session::_pending_suffix = X_(".pending");
77 const char* Session::sound_dir_name = X_("sounds");
78 const char* Session::peak_dir_name = X_("peaks");
79 const char* Session::dead_sound_dir_name = X_("dead_sounds");
81 Session::compute_peak_t Session::compute_peak = 0;
82 Session::apply_gain_to_buffer_t Session::apply_gain_to_buffer = 0;
83 Session::mix_buffers_with_gain_t Session::mix_buffers_with_gain = 0;
84 Session::mix_buffers_no_gain_t Session::mix_buffers_no_gain = 0;
86 sigc::signal<int> Session::AskAboutPendingState;
89 Session::find_session (string str, string& path, string& snapshot, bool& isnew)
96 if (!realpath (str.c_str(), buf) && (errno != ENOENT && errno != ENOTDIR)) {
97 error << string_compose (_("Could not resolve path: %1 (%2)"), buf, strerror(errno)) << endmsg;
103 /* check to see if it exists, and what it is */
105 if (stat (str.c_str(), &statbuf)) {
106 if (errno == ENOENT) {
109 error << string_compose (_("cannot check session path %1 (%2)"), str, strerror (errno))
117 /* it exists, so it must either be the name
118 of the directory, or the name of the statefile
122 if (S_ISDIR (statbuf.st_mode)) {
124 string::size_type slash = str.find_last_of ('/');
126 if (slash == string::npos) {
128 /* a subdirectory of cwd, so statefile should be ... */
134 tmp += _statefile_suffix;
138 if (stat (tmp.c_str(), &statbuf)) {
139 error << string_compose (_("cannot check statefile %1 (%2)"), tmp, strerror (errno))
149 /* some directory someplace in the filesystem.
150 the snapshot name is the directory name
155 snapshot = str.substr (slash+1);
159 } else if (S_ISREG (statbuf.st_mode)) {
161 string::size_type slash = str.find_last_of ('/');
162 string::size_type suffix;
164 /* remove the suffix */
166 if (slash != string::npos) {
167 snapshot = str.substr (slash+1);
172 suffix = snapshot.find (_statefile_suffix);
174 if (suffix == string::npos) {
175 error << string_compose (_("%1 is not an Ardour snapshot file"), str) << endmsg;
181 snapshot = snapshot.substr (0, suffix);
183 if (slash == string::npos) {
185 /* we must be in the directory where the
186 statefile lives. get it using cwd().
189 char cwd[PATH_MAX+1];
191 if (getcwd (cwd, sizeof (cwd)) == 0) {
192 error << string_compose (_("cannot determine current working directory (%1)"), strerror (errno))
201 /* full path to the statefile */
203 path = str.substr (0, slash);
208 /* what type of file is it? */
209 error << string_compose (_("unknown file type for session %1"), str) << endmsg;
215 /* its the name of a new directory. get the name
219 string::size_type slash = str.find_last_of ('/');
221 if (slash == string::npos) {
223 /* no slash, just use the name, but clean it up */
225 path = legalize_for_path (str);
231 snapshot = str.substr (slash+1);
238 Session::Session (AudioEngine &eng,
240 string snapshot_name,
241 string* mix_template)
244 _mmc_port (default_mmc_port),
245 _mtc_port (default_mtc_port),
246 _midi_port (default_midi_port),
247 pending_events (2048),
248 midi_requests (128), // the size of this should match the midi request pool size
253 cerr << "Loading session " << fullpath << " using snapshot " << snapshot_name << endl;
255 n_physical_outputs = _engine.n_physical_outputs();
256 n_physical_inputs = _engine.n_physical_inputs();
258 first_stage_init (fullpath, snapshot_name);
260 if (create (new_session, mix_template, _engine.frame_rate() * 60 * 5)) {
261 throw failed_constructor ();
264 if (second_stage_init (new_session)) {
265 throw failed_constructor ();
268 store_recent_sessions(_name, _path);
270 bool was_dirty = dirty();
272 _state_of_the_state = StateOfTheState (_state_of_the_state & ~Dirty);
275 DirtyChanged (); /* EMIT SIGNAL */
279 Session::Session (AudioEngine &eng,
281 string snapshot_name,
282 AutoConnectOption input_ac,
283 AutoConnectOption output_ac,
284 uint32_t control_out_channels,
285 uint32_t master_out_channels,
286 uint32_t requested_physical_in,
287 uint32_t requested_physical_out,
288 jack_nframes_t initial_length)
291 _mmc_port (default_mmc_port),
292 _mtc_port (default_mtc_port),
293 _midi_port (default_midi_port),
294 pending_events (2048),
301 cerr << "Loading session " << fullpath << " using snapshot " << snapshot_name << endl;
303 n_physical_outputs = max (requested_physical_out, _engine.n_physical_outputs());
304 n_physical_inputs = max (requested_physical_in, _engine.n_physical_inputs());
306 first_stage_init (fullpath, snapshot_name);
308 if (create (new_session, 0, initial_length)) {
309 throw failed_constructor ();
312 if (control_out_channels) {
314 r = new Route (*this, _("monitor"), -1, control_out_channels, -1, control_out_channels, Route::ControlOut);
319 if (master_out_channels) {
321 r = new Route (*this, _("master"), -1, master_out_channels, -1, master_out_channels, Route::MasterOut);
325 /* prohibit auto-connect to master, because there isn't one */
326 output_ac = AutoConnectOption (output_ac & ~AutoConnectMaster);
329 input_auto_connect = input_ac;
330 output_auto_connect = output_ac;
332 if (second_stage_init (new_session)) {
333 throw failed_constructor ();
336 store_recent_sessions(_name, _path);
338 bool was_dirty = dirty ();
340 _state_of_the_state = StateOfTheState (_state_of_the_state & ~Dirty);
343 DirtyChanged (); /* EMIT SIGNAL */
349 /* if we got to here, leaving pending capture state around
353 remove_pending_capture_state ();
355 _state_of_the_state = StateOfTheState (CannotSave|Deletion);
356 _engine.remove_session ();
358 going_away (); /* EMIT SIGNAL */
360 terminate_butler_thread ();
361 terminate_midi_thread ();
362 terminate_feedback ();
364 if (click_data && click_data != default_click) {
365 delete [] click_data;
368 if (click_emphasis_data && click_emphasis_data != default_click_emphasis) {
369 delete [] click_emphasis_data;
382 for (vector<Sample*>::iterator i = _passthru_buffers.begin(); i != _passthru_buffers.end(); ++i) {
386 for (vector<Sample*>::iterator i = _silent_buffers.begin(); i != _silent_buffers.end(); ++i) {
390 #undef TRACK_DESTRUCTION
391 #ifdef TRACK_DESTRUCTION
392 cerr << "delete named selections\n";
393 #endif /* TRACK_DESTRUCTION */
394 for (NamedSelectionList::iterator i = named_selections.begin(); i != named_selections.end(); ) {
395 NamedSelectionList::iterator tmp;
404 #ifdef TRACK_DESTRUCTION
405 cerr << "delete playlists\n";
406 #endif /* TRACK_DESTRUCTION */
407 for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ) {
408 PlaylistList::iterator tmp;
418 #ifdef TRACK_DESTRUCTION
419 cerr << "delete audio regions\n";
420 #endif /* TRACK_DESTRUCTION */
421 for (AudioRegionList::iterator i = audio_regions.begin(); i != audio_regions.end(); ) {
422 AudioRegionList::iterator tmp;
432 #ifdef TRACK_DESTRUCTION
433 cerr << "delete routes\n";
434 #endif /* TRACK_DESTRUCTION */
435 for (RouteList::iterator i = routes.begin(); i != routes.end(); ) {
436 RouteList::iterator tmp;
443 #ifdef TRACK_DESTRUCTION
444 cerr << "delete diskstreams\n";
445 #endif /* TRACK_DESTRUCTION */
446 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ) {
447 DiskStreamList::iterator tmp;
457 #ifdef TRACK_DESTRUCTION
458 cerr << "delete sources\n";
459 #endif /* TRACK_DESTRUCTION */
460 for (SourceList::iterator i = sources.begin(); i != sources.end(); ) {
461 SourceList::iterator tmp;
471 #ifdef TRACK_DESTRUCTION
472 cerr << "delete mix groups\n";
473 #endif /* TRACK_DESTRUCTION */
474 for (list<RouteGroup *>::iterator i = mix_groups.begin(); i != mix_groups.end(); ) {
475 list<RouteGroup*>::iterator tmp;
485 #ifdef TRACK_DESTRUCTION
486 cerr << "delete edit groups\n";
487 #endif /* TRACK_DESTRUCTION */
488 for (list<RouteGroup *>::iterator i = edit_groups.begin(); i != edit_groups.end(); ) {
489 list<RouteGroup*>::iterator tmp;
499 #ifdef TRACK_DESTRUCTION
500 cerr << "delete connections\n";
501 #endif /* TRACK_DESTRUCTION */
502 for (ConnectionList::iterator i = _connections.begin(); i != _connections.end(); ) {
503 ConnectionList::iterator tmp;
513 if (butler_mixdown_buffer) {
514 delete [] butler_mixdown_buffer;
517 if (butler_gain_buffer) {
518 delete [] butler_gain_buffer;
521 Crossfade::set_buffer_size (0);
533 Session::set_worst_io_latencies (bool take_lock)
535 _worst_output_latency = 0;
536 _worst_input_latency = 0;
538 if (!_engine.connected()) {
546 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
547 _worst_output_latency = max (_worst_output_latency, (*i)->output_latency());
548 _worst_input_latency = max (_worst_input_latency, (*i)->input_latency());
552 route_lock.unlock ();
557 Session::when_engine_running ()
559 string first_physical_output;
561 /* we don't want to run execute this again */
563 first_time_running.disconnect ();
565 /* every time we reconnect, recompute worst case output latencies */
567 _engine.Running.connect (sigc::bind (mem_fun (*this, &Session::set_worst_io_latencies), true));
569 if (synced_to_jack()) {
570 _engine.transport_stop ();
573 if (Config->get_jack_time_master()) {
574 _engine.transport_locate (_transport_frame);
582 _click_io = new ClickIO (*this, "click", 0, 0, -1, -1);
584 if (state_tree && (child = find_named_node (*state_tree->root(), "Click")) != 0) {
586 /* existing state for Click */
588 if (_click_io->set_state (*child->children().front()) == 0) {
590 _clicking = click_requested;
594 error << _("could not setup Click I/O") << endmsg;
600 /* default state for Click */
602 first_physical_output = _engine.get_nth_physical_output (0);
604 if (first_physical_output.length()) {
605 if (_click_io->add_output_port (first_physical_output, this)) {
606 // relax, even though its an error
608 _clicking = click_requested;
614 catch (failed_constructor& err) {
615 error << _("cannot setup Click I/O") << endmsg;
618 set_worst_io_latencies (true);
621 ControlChanged (Clicking); /* EMIT SIGNAL */
624 if (auditioner == 0) {
626 /* we delay creating the auditioner till now because
627 it makes its own connections to ports named
628 in the ARDOUR_RC config file. the engine has
629 to be running for this to work.
633 auditioner = new Auditioner (*this);
636 catch (failed_constructor& err) {
637 warning << _("cannot create Auditioner: no auditioning of regions possible") << endmsg;
641 /* Create a set of Connection objects that map
642 to the physical outputs currently available
647 for (uint32_t np = 0; np < n_physical_outputs; ++np) {
649 snprintf (buf, sizeof (buf), _("out %" PRIu32), np+1);
651 Connection* c = new OutputConnection (buf, true);
654 c->add_connection (0, _engine.get_nth_physical_output (np));
659 for (uint32_t np = 0; np < n_physical_inputs; ++np) {
661 snprintf (buf, sizeof (buf), _("in %" PRIu32), np+1);
663 Connection* c = new InputConnection (buf, true);
666 c->add_connection (0, _engine.get_nth_physical_input (np));
673 for (uint32_t np = 0; np < n_physical_outputs; np +=2) {
675 snprintf (buf, sizeof (buf), _("out %" PRIu32 "+%" PRIu32), np+1, np+2);
677 Connection* c = new OutputConnection (buf, true);
681 c->add_connection (0, _engine.get_nth_physical_output (np));
682 c->add_connection (1, _engine.get_nth_physical_output (np+1));
687 for (uint32_t np = 0; np < n_physical_inputs; np +=2) {
689 snprintf (buf, sizeof (buf), _("in %" PRIu32 "+%" PRIu32), np+1, np+2);
691 Connection* c = new InputConnection (buf, true);
695 c->add_connection (0, _engine.get_nth_physical_input (np));
696 c->add_connection (1, _engine.get_nth_physical_input (np+1));
705 /* create master/control ports */
710 /* force the master to ignore any later call to this */
712 if (_master_out->pending_state_node) {
713 _master_out->ports_became_legal();
716 /* no panner resets till we are through */
718 _master_out->defer_pan_reset ();
720 while ((int) _master_out->n_inputs() < _master_out->input_maximum()) {
721 if (_master_out->add_input_port ("", this)) {
722 error << _("cannot setup master inputs")
728 while ((int) _master_out->n_outputs() < _master_out->output_maximum()) {
729 if (_master_out->add_output_port (_engine.get_nth_physical_output (n), this)) {
730 error << _("cannot setup master outputs")
737 _master_out->allow_pan_reset ();
741 Connection* c = new OutputConnection (_("Master Out"), true);
743 for (uint32_t n = 0; n < _master_out->n_inputs (); ++n) {
745 c->add_connection ((int) n, _master_out->input(n)->name());
752 /* catch up on send+insert cnts */
756 for (slist<PortInsert*>::iterator i = _port_inserts.begin(); i != _port_inserts.end(); ++i) {
759 if (sscanf ((*i)->name().c_str(), "%*s %u", &id) == 1) {
760 if (id > insert_cnt) {
768 for (slist<Send*>::iterator i = _sends.begin(); i != _sends.end(); ++i) {
771 if (sscanf ((*i)->name().c_str(), "%*s %u", &id) == 1) {
778 _state_of_the_state = StateOfTheState (_state_of_the_state & ~(CannotSave|Dirty));
780 /* hook us up to the engine */
782 _engine.set_session (this);
784 _state_of_the_state = Clean;
786 DirtyChanged (); /* EMIT SIGNAL */
790 Session::hookup_io ()
792 /* stop graph reordering notifications from
793 causing resorts, etc.
796 _state_of_the_state = StateOfTheState (_state_of_the_state | InitialConnecting);
798 /* Tell all IO objects to create their ports */
805 while ((int) _control_out->n_inputs() < _control_out->input_maximum()) {
806 if (_control_out->add_input_port ("", this)) {
807 error << _("cannot setup control inputs")
813 while ((int) _control_out->n_outputs() < _control_out->output_maximum()) {
814 if (_control_out->add_output_port (_engine.get_nth_physical_output (n), this)) {
815 error << _("cannot set up master outputs")
823 /* Tell all IO objects to connect themselves together */
825 IO::enable_connecting ();
827 /* Now reset all panners */
829 IO::reset_panners ();
831 /* Anyone who cares about input state, wake up and do something */
833 IOConnectionsComplete (); /* EMIT SIGNAL */
835 _state_of_the_state = StateOfTheState (_state_of_the_state & ~InitialConnecting);
837 /* now handle the whole enchilada as if it was one
843 /* update mixer solo state */
849 Session::playlist_length_changed (Playlist* pl)
851 /* we can't just increase end_location->end() if pl->get_maximum_extent()
852 if larger. if the playlist used to be the longest playlist,
853 and its now shorter, we have to decrease end_location->end(). hence,
854 we have to iterate over all diskstreams and check the
855 playlists currently in use.
861 Session::diskstream_playlist_changed (DiskStream* dstream)
865 if ((playlist = dstream->playlist()) != 0) {
866 playlist->LengthChanged.connect (sigc::bind (mem_fun (this, &Session::playlist_length_changed), playlist));
869 /* see comment in playlist_length_changed () */
874 Session::record_enabling_legal () const
876 /* this used to be in here, but survey says.... we don't need to restrict it */
877 // if (record_status() == Recording) {
888 Session::set_auto_play (bool yn)
890 if (auto_play != yn) {
893 ControlChanged (AutoPlay);
898 Session::set_auto_return (bool yn)
900 if (auto_return != yn) {
903 ControlChanged (AutoReturn);
908 Session::set_crossfades_active (bool yn)
910 if (crossfades_active != yn) {
911 crossfades_active = yn;
913 ControlChanged (CrossFadesActive);
918 Session::set_recording_plugins (bool yn)
920 if (recording_plugins != yn) {
921 recording_plugins = yn;
923 ControlChanged (RecordingPlugins);
928 Session::set_auto_input (bool yn)
930 if (auto_input != yn) {
933 ControlChanged (AutoInput);
938 Session::set_input_auto_connect (bool yn)
941 input_auto_connect = AutoConnectOption (input_auto_connect|AutoConnectPhysical);
943 input_auto_connect = AutoConnectOption (input_auto_connect|~AutoConnectPhysical);
949 Session::set_output_auto_connect (AutoConnectOption aco)
951 output_auto_connect = aco;
956 Session::auto_punch_start_changed (Location* location)
958 replace_event (Event::PunchIn, location->start());
960 if (get_record_enabled() && get_punch_in()) {
961 /* capture start has been changed, so save new pending state */
962 save_state ("", true);
967 Session::auto_punch_end_changed (Location* location)
969 jack_nframes_t when_to_stop = location->end();
970 // when_to_stop += _worst_output_latency + _worst_input_latency;
971 replace_event (Event::PunchOut, when_to_stop);
975 Session::auto_punch_changed (Location* location)
977 jack_nframes_t when_to_stop = location->end();
979 replace_event (Event::PunchIn, location->start());
980 //when_to_stop += _worst_output_latency + _worst_input_latency;
981 replace_event (Event::PunchOut, when_to_stop);
985 Session::auto_loop_changed (Location* location)
987 replace_event (Event::AutoLoop, location->end(), location->start());
989 if (transport_rolling() && get_auto_loop()) {
991 //if (_transport_frame < location->start() || _transport_frame > location->end()) {
993 if (_transport_frame > location->end()) {
994 // relocate to beginning of loop
995 clear_events (Event::LocateRoll);
997 request_locate (location->start(), true);
1000 else if (seamless_loop && !loop_changing) {
1002 // schedule a locate-roll to refill the diskstreams at the
1003 // previous loop end
1004 loop_changing = true;
1006 if (location->end() > last_loopend) {
1007 clear_events (Event::LocateRoll);
1008 Event *ev = new Event (Event::LocateRoll, Event::Add, last_loopend, last_loopend, 0, true);
1015 last_loopend = location->end();
1020 Session::set_auto_punch_location (Location* location)
1024 if ((existing = _locations.auto_punch_location()) != 0 && existing != location) {
1025 auto_punch_start_changed_connection.disconnect();
1026 auto_punch_end_changed_connection.disconnect();
1027 auto_punch_changed_connection.disconnect();
1028 existing->set_auto_punch (false, this);
1029 remove_event (existing->start(), Event::PunchIn);
1030 clear_events (Event::PunchOut);
1031 auto_punch_location_changed (0);
1036 if (location == 0) {
1040 if (location->end() <= location->start()) {
1041 error << _("Session: you can't use that location for auto punch (start <= end)") << endmsg;
1045 auto_punch_start_changed_connection.disconnect();
1046 auto_punch_end_changed_connection.disconnect();
1047 auto_punch_changed_connection.disconnect();
1049 auto_punch_start_changed_connection = location->start_changed.connect (mem_fun (this, &Session::auto_punch_start_changed));
1050 auto_punch_end_changed_connection = location->end_changed.connect (mem_fun (this, &Session::auto_punch_end_changed));
1051 auto_punch_changed_connection = location->changed.connect (mem_fun (this, &Session::auto_punch_changed));
1053 location->set_auto_punch (true, this);
1054 auto_punch_location_changed (location);
1058 Session::set_punch_in (bool yn)
1060 if (punch_in == yn) {
1066 if ((location = _locations.auto_punch_location()) != 0) {
1067 if ((punch_in = yn) == true) {
1068 replace_event (Event::PunchIn, location->start());
1070 remove_event (location->start(), Event::PunchIn);
1075 ControlChanged (PunchIn); /* EMIT SIGNAL */
1079 Session::set_punch_out (bool yn)
1081 if (punch_out == yn) {
1087 if ((location = _locations.auto_punch_location()) != 0) {
1088 if ((punch_out = yn) == true) {
1089 replace_event (Event::PunchOut, location->end());
1091 clear_events (Event::PunchOut);
1096 ControlChanged (PunchOut); /* EMIT SIGNAL */
1100 Session::set_auto_loop_location (Location* location)
1104 if ((existing = _locations.auto_loop_location()) != 0 && existing != location) {
1105 auto_loop_start_changed_connection.disconnect();
1106 auto_loop_end_changed_connection.disconnect();
1107 auto_loop_changed_connection.disconnect();
1108 existing->set_auto_loop (false, this);
1109 remove_event (existing->end(), Event::AutoLoop);
1110 auto_loop_location_changed (0);
1115 if (location == 0) {
1119 if (location->end() <= location->start()) {
1120 error << _("Session: you can't use a mark for auto loop") << endmsg;
1124 last_loopend = location->end();
1126 auto_loop_start_changed_connection.disconnect();
1127 auto_loop_end_changed_connection.disconnect();
1128 auto_loop_changed_connection.disconnect();
1130 auto_loop_start_changed_connection = location->start_changed.connect (mem_fun (this, &Session::auto_loop_changed));
1131 auto_loop_end_changed_connection = location->end_changed.connect (mem_fun (this, &Session::auto_loop_changed));
1132 auto_loop_changed_connection = location->changed.connect (mem_fun (this, &Session::auto_loop_changed));
1134 location->set_auto_loop (true, this);
1135 auto_loop_location_changed (location);
1139 Session::locations_added (Location* ignored)
1145 Session::locations_changed ()
1147 _locations.apply (*this, &Session::handle_locations_changed);
1151 Session::handle_locations_changed (Locations::LocationList& locations)
1153 Locations::LocationList::iterator i;
1155 bool set_loop = false;
1156 bool set_punch = false;
1158 for (i = locations.begin(); i != locations.end(); ++i) {
1162 if (location->is_auto_punch()) {
1163 set_auto_punch_location (location);
1166 if (location->is_auto_loop()) {
1167 set_auto_loop_location (location);
1174 set_auto_loop_location (0);
1177 set_auto_punch_location (0);
1184 Session::enable_record ()
1186 /* XXX really atomic compare+swap here */
1187 if (atomic_read (&_record_status) != Recording) {
1188 atomic_set (&_record_status, Recording);
1189 _last_record_location = _transport_frame;
1190 send_mmc_in_another_thread (MIDI::MachineControl::cmdRecordStrobe);
1196 Session::disable_record ()
1198 if (atomic_read (&_record_status) != Disabled) {
1199 atomic_set (&_record_status, Disabled);
1200 send_mmc_in_another_thread (MIDI::MachineControl::cmdRecordExit);
1202 remove_pending_capture_state ();
1207 Session::step_back_from_record ()
1209 atomic_set (&_record_status, Enabled);
1213 Session::maybe_enable_record ()
1215 atomic_set (&_record_status, Enabled);
1217 save_state ("", true);
1219 if (_transport_speed) {
1224 send_mmc_in_another_thread (MIDI::MachineControl::cmdRecordPause);
1225 RecordEnabled (); /* EMIT SIGNAL */
1232 Session::audible_frame () const
1235 jack_nframes_t offset;
1238 /* the first of these two possible settings for "offset"
1239 mean that the audible frame is stationary until
1240 audio emerges from the latency compensation
1243 the second means that the audible frame is stationary
1244 until audio would emerge from a physical port
1245 in the absence of any plugin latency compensation
1248 offset = _worst_output_latency;
1250 if (offset > current_block_size) {
1251 offset -= current_block_size;
1253 /* XXX is this correct? if we have no external
1254 physical connections and everything is internal
1255 then surely this is zero? still, how
1256 likely is that anyway?
1258 offset = current_block_size;
1261 if (synced_to_jack()) {
1262 tf = _engine.transport_frame();
1264 tf = _transport_frame;
1267 if (_transport_speed == 0) {
1277 if (!non_realtime_work_pending()) {
1281 /* take latency into account */
1290 Session::set_frame_rate (jack_nframes_t frames_per_second)
1292 /** \fn void Session::set_frame_size(jack_nframes_t)
1293 the AudioEngine object that calls this guarantees
1294 that it will not be called while we are also in
1295 ::process(). Its fine to do things that block
1299 _current_frame_rate = frames_per_second;
1300 _frames_per_smpte_frame = (double) _current_frame_rate / (double) smpte_frames_per_second;
1302 Route::set_automation_interval ((jack_nframes_t) ceil ((double) frames_per_second * 0.25));
1306 /* XXX need to reset/reinstantiate all LADSPA plugins */
1310 Session::set_block_size (jack_nframes_t nframes)
1312 /** \fn void Session::set_block_size(jack_nframes_t)
1313 the AudioEngine object that calls this guarantees
1314 that it will not be called while we are also in
1315 ::process(). Its also fine to do things that block
1320 LockMonitor lm (route_lock, __LINE__, __FILE__);
1322 current_block_size = nframes;
1324 for (vector<Sample*>::iterator i = _passthru_buffers.begin(); i != _passthru_buffers.end(); ++i) {
1328 #ifdef NO_POSIX_MEMALIGN
1329 buf = (Sample *) malloc(current_block_size * sizeof(Sample));
1331 posix_memalign((void **)&buf,16,current_block_size * 4);
1335 memset (*i, 0, sizeof (Sample) * current_block_size);
1338 for (vector<Sample*>::iterator i = _silent_buffers.begin(); i != _silent_buffers.end(); ++i) {
1342 #ifdef NO_POSIX_MEMALIGN
1343 buf = (Sample *) malloc(current_block_size * sizeof(Sample));
1345 posix_memalign((void **)&buf,16,current_block_size * 4);
1349 memset (*i, 0, sizeof (Sample) * current_block_size);
1352 if (_gain_automation_buffer) {
1353 delete [] _gain_automation_buffer;
1355 _gain_automation_buffer = new gain_t[nframes];
1357 allocate_pan_automation_buffers (nframes, _npan_buffers, true);
1359 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
1360 (*i)->set_block_size (nframes);
1363 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
1364 (*i)->set_block_size (nframes);
1367 set_worst_io_latencies (false);
1372 Session::set_default_fade (float steepness, float fade_msecs)
1375 jack_nframes_t fade_frames;
1377 /* Don't allow fade of less 1 frame */
1379 if (fade_msecs < (1000.0 * (1.0/_current_frame_rate))) {
1386 fade_frames = (jack_nframes_t) floor (fade_msecs * _current_frame_rate * 0.001);
1390 default_fade_msecs = fade_msecs;
1391 default_fade_steepness = steepness;
1394 LockMonitor lm (route_lock, __LINE__, __FILE__);
1395 AudioRegion::set_default_fade (steepness, fade_frames);
1400 /* XXX have to do this at some point */
1401 /* foreach region using default fade, reset, then
1402 refill_all_diskstream_buffers ();
1407 struct RouteSorter {
1408 bool operator() (Route* r1, Route* r2) {
1409 if (r1->fed_by.find (r2) != r1->fed_by.end()) {
1411 } else if (r2->fed_by.find (r1) != r2->fed_by.end()) {
1414 if (r1->fed_by.empty()) {
1415 if (r2->fed_by.empty()) {
1416 /* no ardour-based connections inbound to either route. just use signal order */
1417 return r1->order_key(N_("signal")) < r2->order_key(N_("signal"));
1419 /* r2 has connections, r1 does not; run r1 early */
1423 return r1->order_key(N_("signal")) < r2->order_key(N_("signal"));
1430 trace_terminal (Route* r1, Route* rbase)
1434 if ((r1->fed_by.find (rbase) != r1->fed_by.end()) && (rbase->fed_by.find (r1) != rbase->fed_by.end())) {
1435 info << string_compose(_("feedback loop setup between %1 and %2"), r1->name(), rbase->name()) << endmsg;
1439 /* make a copy of the existing list of routes that feed r1 */
1441 set<Route *> existing = r1->fed_by;
1443 /* for each route that feeds r1, recurse, marking it as feeding
1447 for (set<Route *>::iterator i = existing.begin(); i != existing.end(); ++i) {
1450 /* r2 is a route that feeds r1 which somehow feeds base. mark
1451 base as being fed by r2
1454 rbase->fed_by.insert (r2);
1458 /* 2nd level feedback loop detection. if r1 feeds or is fed by r2,
1462 if ((r1->fed_by.find (r2) != r1->fed_by.end()) && (r2->fed_by.find (r1) != r2->fed_by.end())) {
1466 /* now recurse, so that we can mark base as being fed by
1467 all routes that feed r2
1470 trace_terminal (r2, rbase);
1477 Session::resort_routes (void* src)
1479 /* don't do anything here with signals emitted
1480 by Routes while we are being destroyed.
1483 if (_state_of_the_state & Deletion) {
1487 /* Caller MUST hold the route_lock */
1489 RouteList::iterator i, j;
1491 for (i = routes.begin(); i != routes.end(); ++i) {
1493 (*i)->fed_by.clear ();
1495 for (j = routes.begin(); j != routes.end(); ++j) {
1497 /* although routes can feed themselves, it will
1498 cause an endless recursive descent if we
1499 detect it. so don't bother checking for
1507 if ((*j)->feeds (*i)) {
1508 (*i)->fed_by.insert (*j);
1513 for (i = routes.begin(); i != routes.end(); ++i) {
1514 trace_terminal (*i, *i);
1521 cerr << "finished route resort\n";
1523 for (i = routes.begin(); i != routes.end(); ++i) {
1524 cerr << " " << (*i)->name() << " signal order = " << (*i)->order_key ("signal") << endl;
1532 Session::new_audio_track (int input_channels, int output_channels)
1535 char track_name[32];
1537 uint32_t channels_used = 0;
1539 uint32_t nphysical_in;
1540 uint32_t nphysical_out;
1542 /* count existing audio tracks */
1545 LockMonitor lm (route_lock, __LINE__, __FILE__);
1546 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
1547 if (dynamic_cast<AudioTrack*>(*i) != 0) {
1548 if (!(*i)->hidden()) {
1550 channels_used += (*i)->n_inputs();
1556 /* check for duplicate route names, since we might have pre-existing
1557 routes with this name (e.g. create Audio1, Audio2, delete Audio1,
1558 save, close,restart,add new route - first named route is now
1563 snprintf (track_name, sizeof(track_name), "Audio %" PRIu32, n+1);
1564 if (route_by_name (track_name) == 0) {
1569 } while (n < (UINT_MAX-1));
1571 if (input_auto_connect & AutoConnectPhysical) {
1572 nphysical_in = n_physical_inputs;
1577 if (output_auto_connect & AutoConnectPhysical) {
1578 nphysical_out = n_physical_outputs;
1584 track = new AudioTrack (*this, track_name);
1586 if (track->ensure_io (input_channels, output_channels, false, this)) {
1587 error << string_compose (_("cannot configure %1 in/%2 out configuration for new audio track"),
1588 input_channels, output_channels)
1593 for (uint32_t x = 0; x < track->n_inputs() && x < nphysical_in; ++x) {
1597 if (input_auto_connect & AutoConnectPhysical) {
1598 port = _engine.get_nth_physical_input ((channels_used+x)%nphysical_in);
1601 if (port.length() && track->connect_input (track->input (x), port, this)) {
1607 for (uint32_t x = 0; x < track->n_outputs(); ++x) {
1611 if (nphysical_out && (output_auto_connect & AutoConnectPhysical)) {
1612 port = _engine.get_nth_physical_output ((channels_used+x)%nphysical_out);
1613 } else if (output_auto_connect & AutoConnectMaster) {
1615 port = _master_out->input (x%_master_out->n_inputs())->name();
1619 if (port.length() && track->connect_output (track->output (x), port, this)) {
1625 vector<string> cports;
1626 uint32_t ni = _control_out->n_inputs();
1628 for (n = 0; n < ni; ++n) {
1629 cports.push_back (_control_out->input(n)->name());
1632 track->set_control_outs (cports);
1635 track->diskstream_changed.connect (mem_fun (this, &Session::resort_routes));
1640 catch (failed_constructor &err) {
1641 error << _("Session: could not create new audio track.") << endmsg;
1649 Session::new_audio_route (int input_channels, int output_channels)
1656 /* count existing audio busses */
1659 LockMonitor lm (route_lock, __LINE__, __FILE__);
1660 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
1661 if (dynamic_cast<AudioTrack*>(*i) == 0) {
1662 if (!(*i)->hidden()) {
1670 snprintf (bus_name, sizeof(bus_name), "Bus %" PRIu32, n+1);
1671 if (route_by_name (bus_name) == 0) {
1676 } while (n < (UINT_MAX-1));
1679 bus = new Route (*this, bus_name, -1, -1, -1, -1);
1681 if (bus->ensure_io (input_channels, output_channels, false, this)) {
1682 error << string_compose (_("cannot configure %1 in/%2 out configuration for new audio track"),
1683 input_channels, output_channels)
1687 for (uint32_t x = 0; x < bus->n_inputs(); ++x) {
1691 if (input_auto_connect & AutoConnectPhysical) {
1692 port = _engine.get_nth_physical_input ((n+x)%n_physical_inputs);
1695 if (port.length() && bus->connect_input (bus->input (x), port, this)) {
1700 for (uint32_t x = 0; x < bus->n_outputs(); ++x) {
1704 if (output_auto_connect & AutoConnectPhysical) {
1705 port = _engine.get_nth_physical_input ((n+x)%n_physical_outputs);
1706 } else if (output_auto_connect & AutoConnectMaster) {
1708 port = _master_out->input (x%_master_out->n_inputs())->name();
1712 if (port.length() && bus->connect_output (bus->output (x), port, this)) {
1718 vector<string> cports;
1719 uint32_t ni = _control_out->n_inputs();
1721 for (uint32_t n = 0; n < ni; ++n) {
1722 cports.push_back (_control_out->input(n)->name());
1724 bus->set_control_outs (cports);
1730 catch (failed_constructor &err) {
1731 error << _("Session: could not create new route.") << endmsg;
1739 Session::add_route (Route* route)
1742 LockMonitor lm (route_lock, __LINE__, __FILE__);
1743 routes.push_front (route);
1747 route->solo_changed.connect (sigc::bind (mem_fun (*this, &Session::route_solo_changed), route));
1748 route->mute_changed.connect (mem_fun (*this, &Session::route_mute_changed));
1749 route->output_changed.connect (mem_fun (*this, &Session::set_worst_io_latencies_x));
1750 route->redirects_changed.connect (mem_fun (*this, &Session::update_latency_compensation_proxy));
1752 if (route->master()) {
1753 _master_out = route;
1756 if (route->control()) {
1757 _control_out = route;
1761 save_state (_current_snapshot_name);
1763 RouteAdded (route); /* EMIT SIGNAL */
1767 Session::add_diskstream (DiskStream* dstream)
1770 LockMonitor lm (diskstream_lock, __LINE__, __FILE__);
1771 diskstreams.push_back (dstream);
1774 /* take a reference to the diskstream, preventing it from
1775 ever being deleted until the session itself goes away,
1776 or chooses to remove it for its own purposes.
1780 dstream->set_block_size (current_block_size);
1782 dstream->PlaylistChanged.connect (sigc::bind (mem_fun (*this, &Session::diskstream_playlist_changed), dstream));
1783 /* this will connect to future changes, and check the current length */
1784 diskstream_playlist_changed (dstream);
1786 dstream->prepare ();
1789 save_state (_current_snapshot_name);
1791 DiskStreamAdded (dstream); /* EMIT SIGNAL */
1795 Session::remove_route (Route& route)
1798 LockMonitor lm (route_lock, __LINE__, __FILE__);
1799 routes.remove (&route);
1801 /* deleting the master out seems like a dumb
1802 idea, but its more of a UI policy issue
1806 if (&route == _master_out) {
1810 if (&route == _control_out) {
1813 /* cancel control outs for all routes */
1815 vector<string> empty;
1817 for (RouteList::iterator r = routes.begin(); r != routes.end(); ++r) {
1818 (*r)->set_control_outs (empty);
1822 update_route_solo_state ();
1826 LockMonitor lm (diskstream_lock, __LINE__, __FILE__);
1830 if ((at = dynamic_cast<AudioTrack*>(&route)) != 0) {
1831 diskstreams.remove (&at->disk_stream());
1832 at->disk_stream().unref ();
1835 find_current_end ();
1838 update_latency_compensation (false, false);
1841 /* XXX should we disconnect from the Route's signals ? */
1843 save_state (_current_snapshot_name);
1849 Session::route_mute_changed (void* src)
1855 Session::route_solo_changed (void* src, Route* route)
1857 if (solo_update_disabled) {
1862 LockMonitor lm (route_lock, __LINE__, __FILE__);
1865 is_track = (dynamic_cast<AudioTrack*>(route) != 0);
1867 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
1869 /* soloing a track mutes all other tracks, soloing a bus mutes all other busses */
1873 /* don't mess with busses */
1875 if (dynamic_cast<AudioTrack*>(*i) == 0) {
1881 /* don't mess with tracks */
1883 if (dynamic_cast<AudioTrack*>(*i) != 0) {
1888 if ((*i) != route &&
1889 ((*i)->mix_group () == 0 ||
1890 (*i)->mix_group () != route->mix_group () ||
1891 !route->mix_group ()->is_active())) {
1893 if ((*i)->soloed()) {
1895 /* if its already soloed, and solo latching is enabled,
1896 then leave it as it is.
1899 if (_solo_latched) {
1906 solo_update_disabled = true;
1907 (*i)->set_solo (false, src);
1908 solo_update_disabled = false;
1912 bool something_soloed = false;
1913 bool same_thing_soloed = false;
1914 bool signal = false;
1916 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
1917 if ((*i)->soloed()) {
1918 something_soloed = true;
1919 if (dynamic_cast<AudioTrack*>(*i)) {
1921 same_thing_soloed = true;
1926 same_thing_soloed = true;
1934 if (something_soloed != currently_soloing) {
1936 currently_soloing = something_soloed;
1939 modify_solo_mute (is_track, same_thing_soloed);
1942 SoloActive (currently_soloing);
1949 Session::set_solo_latched (bool yn)
1956 Session::update_route_solo_state ()
1959 bool is_track = false;
1960 bool signal = false;
1962 /* caller must hold RouteLock */
1964 /* this is where we actually implement solo by changing
1965 the solo mute setting of each track.
1968 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
1969 if ((*i)->soloed()) {
1971 if (dynamic_cast<AudioTrack*>(*i)) {
1978 if (mute != currently_soloing) {
1980 currently_soloing = mute;
1983 if (!is_track && !mute) {
1985 /* nothing is soloed */
1987 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
1988 (*i)->set_solo_mute (false);
1998 modify_solo_mute (is_track, mute);
2001 SoloActive (currently_soloing);
2006 Session::modify_solo_mute (bool is_track, bool mute)
2008 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
2012 /* only alter track solo mute */
2014 if (dynamic_cast<AudioTrack*>(*i)) {
2015 if ((*i)->soloed()) {
2016 (*i)->set_solo_mute (!mute);
2018 (*i)->set_solo_mute (mute);
2024 /* only alter bus solo mute */
2026 if (!dynamic_cast<AudioTrack*>(*i)) {
2028 if ((*i)->soloed()) {
2030 (*i)->set_solo_mute (false);
2034 /* don't mute master or control outs
2035 in response to another bus solo
2038 if ((*i) != _master_out &&
2039 (*i) != _control_out) {
2040 (*i)->set_solo_mute (mute);
2051 Session::catch_up_on_solo ()
2053 /* this is called after set_state() to catch the full solo
2054 state, which can't be correctly determined on a per-route
2055 basis, but needs the global overview that only the session
2058 LockMonitor lm (route_lock, __LINE__, __FILE__);
2059 update_route_solo_state();
2063 Session::route_by_name (string name)
2065 LockMonitor lm (route_lock, __LINE__, __FILE__);
2067 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
2068 if ((*i)->name() == name) {
2077 Session::find_current_end ()
2079 jack_nframes_t max = 0;
2082 if (_state_of_the_state & Loading) {
2086 /* Don't take the diskstream lock. Caller must have other ways to
2090 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
2091 Playlist* pl = (*i)->playlist();
2092 if ((me = pl->get_maximum_extent()) > max) {
2097 if (max > end_location->end()) {
2098 end_location->set_end (max);
2100 DurationChanged(); /* EMIT SIGNAL */
2105 Session::diskstream_by_name (string name)
2107 LockMonitor lm (diskstream_lock, __LINE__, __FILE__);
2109 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
2110 if ((*i)->name() == name) {
2119 Session::diskstream_by_id (id_t id)
2121 LockMonitor lm (diskstream_lock, __LINE__, __FILE__);
2123 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
2124 if ((*i)->id() == id) {
2132 /* AudioRegion management */
2135 Session::new_region_name (string old)
2137 string::size_type last_period;
2139 string::size_type len = old.length() + 64;
2142 if ((last_period = old.find_last_of ('.')) == string::npos) {
2144 /* no period present - add one explicitly */
2147 last_period = old.length() - 1;
2152 number = atoi (old.substr (last_period+1).c_str());
2156 while (number < (UINT_MAX-1)) {
2158 AudioRegionList::const_iterator i;
2163 snprintf (buf, len, "%s%" PRIu32, old.substr (0, last_period + 1).c_str(), number);
2166 for (i = audio_regions.begin(); i != audio_regions.end(); ++i) {
2167 if ((*i).second->name() == sbuf) {
2172 if (i == audio_regions.end()) {
2177 if (number != (UINT_MAX-1)) {
2181 error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
2186 Session::region_name (string& result, string base, bool newlevel) const
2193 LockMonitor lm (region_lock, __LINE__, __FILE__);
2195 snprintf (buf, sizeof (buf), "%d", (int)audio_regions.size() + 1);
2203 /* XXX this is going to be slow. optimize me later */
2208 string::size_type pos;
2210 if ((pos = base.find_last_of ('-')) == string::npos) {
2211 pos = base.find_last_of ('.');
2214 /* pos may be npos, but then we just use entire base */
2216 subbase = base.substr (0, pos);
2219 bool name_taken = true;
2222 LockMonitor lm (region_lock, __LINE__, __FILE__);
2224 for (int n = 1; n < 5000; ++n) {
2227 snprintf (buf, sizeof (buf), ".%d", n);
2232 for (AudioRegionList::const_iterator i = audio_regions.begin(); i != audio_regions.end(); ++i) {
2233 if ((*i).second->name() == result) {
2246 fatal << string_compose(_("too many regions with names like %1"), base) << endmsg;
2255 Session::add_region (Region* region)
2257 AudioRegion* ar = 0;
2258 AudioRegion* oar = 0;
2262 LockMonitor lm (region_lock, __LINE__, __FILE__);
2264 if ((ar = dynamic_cast<AudioRegion*> (region)) != 0) {
2266 AudioRegionList::iterator x;
2268 for (x = audio_regions.begin(); x != audio_regions.end(); ++x) {
2270 oar = dynamic_cast<AudioRegion*> (x->second);
2272 if (ar->region_list_equivalent (*oar)) {
2277 if (x == audio_regions.end()) {
2279 pair<AudioRegionList::key_type, AudioRegionList::mapped_type> entry;
2281 entry.first = region->id();
2284 pair<AudioRegionList::iterator,bool> x = audio_regions.insert (entry);
2295 fatal << _("programming error: ")
2296 << X_("unknown region type passed to Session::add_region()")
2303 /* mark dirty because something has changed even if we didn't
2304 add the region to the region list.
2310 region->GoingAway.connect (mem_fun (*this, &Session::remove_region));
2311 region->StateChanged.connect (sigc::bind (mem_fun (*this, &Session::region_changed), region));
2312 AudioRegionAdded (ar); /* EMIT SIGNAL */
2317 Session::region_changed (Change what_changed, Region* region)
2319 if (what_changed & Region::HiddenChanged) {
2320 /* relay hidden changes */
2321 RegionHiddenChange (region);
2326 Session::region_renamed (Region* region)
2328 add_region (region);
2332 Session::remove_region (Region* region)
2334 AudioRegionList::iterator i;
2335 AudioRegion* ar = 0;
2336 bool removed = false;
2339 LockMonitor lm (region_lock, __LINE__, __FILE__);
2341 if ((ar = dynamic_cast<AudioRegion*> (region)) != 0) {
2342 if ((i = audio_regions.find (region->id())) != audio_regions.end()) {
2343 audio_regions.erase (i);
2347 fatal << _("programming error: ")
2348 << X_("unknown region type passed to Session::remove_region()")
2354 /* mark dirty because something has changed even if we didn't
2355 remove the region from the region list.
2361 AudioRegionRemoved(ar); /* EMIT SIGNAL */
2366 Session::find_whole_file_parent (AudioRegion& child)
2368 AudioRegionList::iterator i;
2369 AudioRegion* region;
2370 LockMonitor lm (region_lock, __LINE__, __FILE__);
2372 for (i = audio_regions.begin(); i != audio_regions.end(); ++i) {
2374 region = (*i).second;
2376 if (region->whole_file()) {
2378 if (child.source_equivalent (*region)) {
2388 Session::find_equivalent_playlist_regions (AudioRegion& region, vector<AudioRegion*>& result)
2390 for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ++i) {
2394 if ((pl = dynamic_cast<AudioPlaylist*>(*i)) == 0) {
2398 pl->get_region_list_equivalent_regions (region, result);
2403 Session::destroy_region (Region* region)
2405 AudioRegion* aregion;
2407 if ((aregion = dynamic_cast<AudioRegion*> (region)) == 0) {
2411 if (aregion->playlist()) {
2412 aregion->playlist()->destroy_region (region);
2415 vector<Source*> srcs;
2417 for (uint32_t n = 0; n < aregion->n_channels(); ++n) {
2418 srcs.push_back (&aregion->source (n));
2421 for (vector<Source*>::iterator i = srcs.begin(); i != srcs.end(); ++i) {
2423 if ((*i)->use_cnt() == 0) {
2424 (*i)->mark_for_remove ();
2433 Session::destroy_regions (list<Region*> regions)
2435 for (list<Region*>::iterator i = regions.begin(); i != regions.end(); ++i) {
2436 destroy_region (*i);
2442 Session::remove_last_capture ()
2446 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
2447 list<Region*>& l = (*i)->last_capture_regions();
2450 r.insert (r.end(), l.begin(), l.end());
2455 destroy_regions (r);
2460 Session::remove_region_from_region_list (Region& r)
2466 /* Source Management */
2469 Session::add_source (Source* source)
2471 pair<SourceList::key_type, SourceList::mapped_type> entry;
2474 LockMonitor lm (source_lock, __LINE__, __FILE__);
2475 entry.first = source->id();
2476 entry.second = source;
2477 sources.insert (entry);
2480 source->GoingAway.connect (mem_fun (this, &Session::remove_source));
2483 SourceAdded (source); /* EMIT SIGNAL */
2487 Session::remove_source (Source* source)
2489 SourceList::iterator i;
2492 LockMonitor lm (source_lock, __LINE__, __FILE__);
2494 if ((i = sources.find (source->id())) != sources.end()) {
2499 if (!_state_of_the_state & InCleanup) {
2501 /* save state so we don't end up with a session file
2502 referring to non-existent sources.
2505 save_state (_current_snapshot_name);
2508 SourceRemoved(source); /* EMIT SIGNAL */
2512 Session::get_source (ARDOUR::id_t id)
2514 LockMonitor lm (source_lock, __LINE__, __FILE__);
2515 SourceList::iterator i;
2518 if ((i = sources.find (id)) != sources.end()) {
2519 source = (*i).second;
2526 Session::create_file_source (DiskStream& ds, int32_t chan)
2530 char buf[PATH_MAX+1];
2531 const uint32_t limit = 10000;
2535 legalized = legalize_for_path (ds.name());
2537 /* find a "version" of the file name that doesn't exist in
2538 any of the possible directories.
2541 for (cnt = 1; cnt <= limit; ++cnt) {
2543 vector<space_and_path>::iterator i;
2544 uint32_t existing = 0;
2546 for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
2549 spath += sound_dir_name;
2553 if (ds.n_channels() < 2) {
2554 snprintf (buf, sizeof(buf), "%s-%u.wav", spath.c_str(), cnt);
2555 } else if (ds.n_channels() == 2) {
2557 snprintf (buf, sizeof(buf), "%s-%u%%L.wav", spath.c_str(), cnt);
2559 snprintf (buf, sizeof(buf), "%s-%u%%R.wav", spath.c_str(), cnt);
2561 } else if (ds.n_channels() < 26) {
2562 snprintf (buf, sizeof(buf), "%s-%u%%%c.wav", spath.c_str(), cnt, 'a' + chan);
2564 snprintf (buf, sizeof(buf), "%s-%u.wav", spath.c_str(), cnt);
2567 if (access (buf, F_OK) == 0) {
2572 if (existing == 0) {
2578 error << string_compose(_("There are already %1 recordings for %2, which I consider too many."), limit, ds.name()) << endmsg;
2579 throw failed_constructor();
2582 /* we now have a unique name for the file, but figure out where to
2588 spath = discover_best_sound_dir ();
2590 string::size_type pos = foo.find_last_of ('/');
2592 if (pos == string::npos) {
2595 spath += foo.substr (pos + 1);
2598 /* this might throw failed_constructor(), which is OK */
2600 return new FileSource (spath, frame_rate());
2603 /* Playlist management */
2606 Session::get_playlist (string name)
2610 if ((ret = playlist_by_name (name)) == 0) {
2611 ret = new AudioPlaylist (*this, name);
2618 Session::playlist_by_name (string name)
2620 LockMonitor lm (playlist_lock, __LINE__, __FILE__);
2621 for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ++i) {
2622 if ((*i)->name() == name) {
2626 for (PlaylistList::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
2627 if ((*i)->name() == name) {
2635 Session::add_playlist (Playlist* playlist)
2637 if (playlist->hidden()) {
2642 LockMonitor lm (playlist_lock, __LINE__, __FILE__);
2643 if (find (playlists.begin(), playlists.end(), playlist) == playlists.end()) {
2644 playlists.insert (playlists.begin(), playlist);
2646 playlist->InUse.connect (mem_fun (*this, &Session::track_playlist));
2647 playlist->GoingAway.connect (mem_fun (*this, &Session::remove_playlist));
2653 PlaylistAdded (playlist); /* EMIT SIGNAL */
2657 Session::track_playlist (Playlist* pl, bool inuse)
2659 PlaylistList::iterator x;
2662 LockMonitor lm (playlist_lock, __LINE__, __FILE__);
2665 //cerr << "shifting playlist to unused: " << pl->name() << endl;
2667 unused_playlists.insert (pl);
2669 if ((x = playlists.find (pl)) != playlists.end()) {
2670 playlists.erase (x);
2675 //cerr << "shifting playlist to used: " << pl->name() << endl;
2677 playlists.insert (pl);
2679 if ((x = unused_playlists.find (pl)) != unused_playlists.end()) {
2680 unused_playlists.erase (x);
2687 Session::remove_playlist (Playlist* playlist)
2689 if (_state_of_the_state & Deletion) {
2694 LockMonitor lm (playlist_lock, __LINE__, __FILE__);
2695 // cerr << "removing playlist: " << playlist->name() << endl;
2697 PlaylistList::iterator i;
2699 i = find (playlists.begin(), playlists.end(), playlist);
2701 if (i != playlists.end()) {
2702 playlists.erase (i);
2705 i = find (unused_playlists.begin(), unused_playlists.end(), playlist);
2706 if (i != unused_playlists.end()) {
2707 unused_playlists.erase (i);
2714 PlaylistRemoved (playlist); /* EMIT SIGNAL */
2718 Session::set_audition (AudioRegion* r)
2720 pending_audition_region = r;
2721 post_transport_work = PostTransportWork (post_transport_work | PostTransportAudition);
2722 schedule_butler_transport_work ();
2726 Session::non_realtime_set_audition ()
2728 if (pending_audition_region == (AudioRegion*) 0xfeedface) {
2729 auditioner->audition_current_playlist ();
2730 } else if (pending_audition_region) {
2731 auditioner->audition_region (*pending_audition_region);
2733 pending_audition_region = 0;
2734 AuditionActive (true); /* EMIT SIGNAL */
2738 Session::audition_playlist ()
2740 Event* ev = new Event (Event::Audition, Event::Add, Event::Immediate, 0, 0.0);
2741 ev->set_ptr ((void*) 0xfeedface);
2746 Session::audition_region (AudioRegion& r)
2748 Event* ev = new Event (Event::Audition, Event::Add, Event::Immediate, 0, 0.0);
2754 Session::cancel_audition ()
2756 if (auditioner->active()) {
2757 auditioner->cancel_audition ();
2758 AuditionActive (false); /* EMIT SIGNAL */
2763 Session::RoutePublicOrderSorter::operator() (Route* a, Route* b)
2765 return a->order_key(N_("signal")) < b->order_key(N_("signal"));
2769 Session::remove_empty_sounds ()
2772 PathScanner scanner;
2777 vector<string *>* possible_audiofiles = scanner (dir, "\\.wav$", false, true);
2779 for (vector<string *>::iterator i = possible_audiofiles->begin(); i != possible_audiofiles->end(); ++i) {
2781 if (FileSource::is_empty (*(*i))) {
2783 unlink ((*i)->c_str());
2785 string peak_path = peak_path_from_audio_path (**i);
2786 unlink (peak_path.c_str());
2792 delete possible_audiofiles;
2796 Session::is_auditioning () const
2798 /* can be called before we have an auditioner object */
2800 return auditioner->active();
2808 Session::peak_path_from_audio_path (string audio_path)
2810 /* XXX hardly bombproof! fix me */
2814 res = PBD::dirname (audio_path);
2815 res = PBD::dirname (res);
2817 res += peak_dir_name;
2819 res += PBD::basename_nosuffix (audio_path);
2826 Session::old_peak_path_from_audio_path (string audio_path)
2828 /* This is a hangover from when audio and peak files
2829 lived in the same directory. We need it to to
2830 be able to open old sessions.
2833 /* XXX hardly bombproof! fix me */
2835 string res = audio_path.substr (0, audio_path.find_last_of ('.'));
2841 Session::set_all_solo (bool yn)
2843 /* XXX this copy is not safe: the Routes within the list
2844 can still be deleted after the Route lock is released.
2849 LockMonitor lm (route_lock, __LINE__, __FILE__);
2853 for (RouteList::iterator i = copy.begin(); i != copy.end(); ++i) {
2854 if (!(*i)->hidden()) {
2855 (*i)->set_solo (yn, this);
2863 Session::set_all_mute (bool yn)
2867 LockMonitor lm (route_lock, __LINE__, __FILE__);
2871 for (RouteList::iterator i = copy.begin(); i != copy.end(); ++i) {
2872 if (!(*i)->hidden()) {
2873 (*i)->set_mute (yn, this);
2881 Session::n_diskstreams () const
2883 LockMonitor lm (diskstream_lock, __LINE__, __FILE__);
2886 for (DiskStreamList::const_iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
2887 if (!(*i)->hidden()) {
2895 Session::foreach_diskstream (void (DiskStream::*func)(void))
2897 LockMonitor lm (diskstream_lock, __LINE__, __FILE__);
2898 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
2899 if (!(*i)->hidden()) {
2906 Session::graph_reordered ()
2908 /* don't do this stuff if we are setting up connections
2909 from a set_state() call.
2912 if (_state_of_the_state & InitialConnecting) {
2916 LockMonitor lm1 (route_lock, __LINE__, __FILE__);
2917 LockMonitor lm2 (diskstream_lock, __LINE__, __FILE__);
2921 /* force all diskstreams to update their capture offset values to
2922 reflect any changes in latencies within the graph.
2925 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
2926 (*i)->set_capture_offset ();
2931 Session::record_disenable_all ()
2933 record_enable_change_all (false);
2937 Session::record_enable_all ()
2939 record_enable_change_all (true);
2943 Session::record_enable_change_all (bool yn)
2945 LockMonitor lm1 (route_lock, __LINE__, __FILE__);
2947 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
2950 if ((at = dynamic_cast<AudioTrack*>(*i)) != 0) {
2951 at->set_record_enable (yn, this);
2955 /* since we don't keep rec-enable state, don't mark session dirty */
2959 Session::add_redirect (Redirect* redirect)
2963 PortInsert* port_insert;
2964 PluginInsert* plugin_insert;
2966 if ((insert = dynamic_cast<Insert *> (redirect)) != 0) {
2967 if ((port_insert = dynamic_cast<PortInsert *> (insert)) != 0) {
2968 _port_inserts.insert (_port_inserts.begin(), port_insert);
2969 } else if ((plugin_insert = dynamic_cast<PluginInsert *> (insert)) != 0) {
2970 _plugin_inserts.insert (_plugin_inserts.begin(), plugin_insert);
2972 fatal << _("programming error: unknown type of Insert created!") << endmsg;
2975 } else if ((send = dynamic_cast<Send *> (redirect)) != 0) {
2976 _sends.insert (_sends.begin(), send);
2978 fatal << _("programming error: unknown type of Redirect created!") << endmsg;
2982 redirect->GoingAway.connect (mem_fun (*this, &Session::remove_redirect));
2988 Session::remove_redirect (Redirect* redirect)
2992 PortInsert* port_insert;
2993 PluginInsert* plugin_insert;
2995 if ((insert = dynamic_cast<Insert *> (redirect)) != 0) {
2996 if ((port_insert = dynamic_cast<PortInsert *> (insert)) != 0) {
2997 _port_inserts.remove (port_insert);
2998 } else if ((plugin_insert = dynamic_cast<PluginInsert *> (insert)) != 0) {
2999 _plugin_inserts.remove (plugin_insert);
3001 fatal << _("programming error: unknown type of Insert deleted!") << endmsg;
3004 } else if ((send = dynamic_cast<Send *> (redirect)) != 0) {
3005 _sends.remove (send);
3007 fatal << _("programming error: unknown type of Redirect deleted!") << endmsg;
3015 Session::available_capture_duration ()
3017 const double scale = 4096.0 / sizeof (Sample);
3019 if (_total_free_4k_blocks * scale > (double) max_frames) {
3023 return (jack_nframes_t) floor (_total_free_4k_blocks * scale);
3027 Session::add_connection (ARDOUR::Connection* connection)
3030 LockMonitor (connection_lock, __LINE__, __FILE__);
3031 _connections.push_back (connection);
3034 ConnectionAdded (connection); /* EMIT SIGNAL */
3040 Session::remove_connection (ARDOUR::Connection* connection)
3042 bool removed = false;
3045 LockMonitor (connection_lock, __LINE__, __FILE__);
3046 ConnectionList::iterator i = find (_connections.begin(), _connections.end(), connection);
3048 if (i != _connections.end()) {
3049 _connections.erase (i);
3055 ConnectionRemoved (connection); /* EMIT SIGNAL */
3061 ARDOUR::Connection *
3062 Session::connection_by_name (string name) const
3064 LockMonitor lm (connection_lock, __LINE__, __FILE__);
3066 for (ConnectionList::const_iterator i = _connections.begin(); i != _connections.end(); ++i) {
3067 if ((*i)->name() == name) {
3076 Session::set_edit_mode (EditMode mode)
3081 LockMonitor lm (playlist_lock, __LINE__, __FILE__);
3083 for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ++i) {
3084 (*i)->set_edit_mode (mode);
3089 ControlChanged (EditingMode); /* EMIT SIGNAL */
3093 Session::tempo_map_changed (Change ignored)
3100 Session::ensure_passthru_buffers (uint32_t howmany)
3102 while (howmany > _passthru_buffers.size()) {
3104 #ifdef NO_POSIX_MEMALIGN
3105 p = (Sample *) malloc(current_block_size * sizeof(Sample));
3107 posix_memalign((void **)&p,16,current_block_size * 4);
3109 _passthru_buffers.push_back (p);
3113 #ifdef NO_POSIX_MEMALIGN
3114 p = (Sample *) malloc(current_block_size * sizeof(Sample));
3116 posix_memalign((void **)&p,16,current_block_size * 4);
3118 memset (p, 0, sizeof (Sample) * current_block_size);
3119 _silent_buffers.push_back (p);
3122 allocate_pan_automation_buffers (current_block_size, howmany, false);
3126 Session::next_send_name ()
3129 snprintf (buf, sizeof (buf), "send %" PRIu32, ++send_cnt);
3134 Session::next_insert_name ()
3137 snprintf (buf, sizeof (buf), "insert %" PRIu32, ++insert_cnt);
3141 /* Named Selection management */
3144 Session::named_selection_by_name (string name)
3146 LockMonitor lm (named_selection_lock, __LINE__, __FILE__);
3147 for (NamedSelectionList::iterator i = named_selections.begin(); i != named_selections.end(); ++i) {
3148 if ((*i)->name == name) {
3156 Session::add_named_selection (NamedSelection* named_selection)
3159 LockMonitor lm (named_selection_lock, __LINE__, __FILE__);
3160 named_selections.insert (named_selections.begin(), named_selection);
3165 NamedSelectionAdded (); /* EMIT SIGNAL */
3169 Session::remove_named_selection (NamedSelection* named_selection)
3171 bool removed = false;
3174 LockMonitor lm (named_selection_lock, __LINE__, __FILE__);
3176 NamedSelectionList::iterator i = find (named_selections.begin(), named_selections.end(), named_selection);
3178 if (i != named_selections.end()) {
3180 named_selections.erase (i);
3187 NamedSelectionRemoved (); /* EMIT SIGNAL */
3192 Session::reset_native_file_format ()
3194 LockMonitor lm1 (route_lock, __LINE__, __FILE__);
3195 LockMonitor lm2 (diskstream_lock, __LINE__, __FILE__);
3197 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
3198 (*i)->reset_write_sources (false);
3203 Session::route_name_unique (string n) const
3205 LockMonitor lm (route_lock, __LINE__, __FILE__);
3207 for (RouteList::const_iterator i = routes.begin(); i != routes.end(); ++i) {
3208 if ((*i)->name() == n) {
3217 Session::remove_file_source (FileSource& fs)
3219 return fs.move_to_trash (dead_sound_dir_name);
3223 Session::n_playlists () const
3225 LockMonitor lm (playlist_lock, __LINE__, __FILE__);
3226 return playlists.size();
3230 Session::set_align_style (AlignStyle style)
3232 align_style = style;
3234 foreach_diskstream (&DiskStream::set_capture_offset);
3237 ControlChanged (AlignChoice);
3241 Session::set_solo_model (SoloModel sm)
3248 Session::allocate_pan_automation_buffers (jack_nframes_t nframes, uint32_t howmany, bool force)
3250 if (!force && howmany <= _npan_buffers) {
3254 if (_pan_automation_buffer) {
3256 for (uint32_t i = 0; i < _npan_buffers; ++i) {
3257 delete [] _pan_automation_buffer[i];
3260 delete [] _pan_automation_buffer;
3263 _pan_automation_buffer = new pan_t*[howmany];
3265 for (uint32_t i = 0; i < howmany; ++i) {
3266 _pan_automation_buffer[i] = new pan_t[nframes];
3269 _npan_buffers = howmany;
3273 Session::add_instant_xml (XMLNode& node, const std::string& dir)
3275 Stateful::add_instant_xml (node, dir);
3276 Config->add_instant_xml (node, Config->get_user_ardour_path());
3280 Session::freeze (InterThreadInfo& itt)
3282 LockMonitor lm (route_lock, __LINE__, __FILE__);
3284 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
3288 if ((at = dynamic_cast<AudioTrack*>(*i)) != 0) {
3289 /* XXX this is wrong because itt.progress will keep returning to zero at the start
3300 Session::write_one_track (AudioTrack& track, jack_nframes_t start, jack_nframes_t len, bool overwrite, vector<Source*>& srcs,
3301 InterThreadInfo& itt)
3305 FileSource* fsource;
3307 char buf[PATH_MAX+1];
3310 jack_nframes_t position;
3311 jack_nframes_t this_chunk;
3312 jack_nframes_t to_do;
3313 vector<Sample*> buffers;
3314 const jack_nframes_t chunk_size = (256 * 1024)/4;
3316 atomic_set (&processing_prohibited, 1);
3318 /* call tree *MUST* hold route_lock */
3320 if ((playlist = track.disk_stream().playlist()) == 0) {
3324 /* external redirects will be a problem */
3326 if (track.has_external_redirects()) {
3330 nchans = track.disk_stream().n_channels();
3332 dir = discover_best_sound_dir ();
3334 for (uint32_t chan_n=0; chan_n < nchans; ++chan_n) {
3336 for (x = 0; x < 99999; ++x) {
3337 snprintf (buf, sizeof(buf), "%s/%s-%d-bounce-%" PRIu32 ".wav", dir.c_str(), playlist->name().c_str(), chan_n, x+1);
3338 if (access (buf, F_OK) != 0) {
3344 error << string_compose (_("too many bounced versions of playlist \"%1\""), playlist->name()) << endmsg;
3349 fsource = new FileSource (buf, frame_rate());
3352 catch (failed_constructor& err) {
3353 error << string_compose (_("cannot create new audio file \"%1\" for %2"), buf, track.name()) << endmsg;
3357 srcs.push_back(fsource);
3360 /* XXX need to flush all redirects */
3365 /* create a set of reasonably-sized buffers */
3367 for (vector<Sample*>::iterator i = _passthru_buffers.begin(); i != _passthru_buffers.end(); ++i) {
3369 #ifdef NO_POSIX_MEMALIGN
3370 b = (Sample *) malloc(chunk_size * sizeof(Sample));
3372 posix_memalign((void **)&b,16,chunk_size * 4);
3374 buffers.push_back (b);
3377 while (to_do && !itt.cancel) {
3379 this_chunk = min (to_do, chunk_size);
3381 if (track.export_stuff (buffers, nchans, start, this_chunk)) {
3386 for (vector<Source*>::iterator src=srcs.begin(); src != srcs.end(); ++src, ++n) {
3387 if ((*src)->write (buffers[n], this_chunk) != this_chunk) {
3392 start += this_chunk;
3393 to_do -= this_chunk;
3395 itt.progress = (float) (1.0 - ((double) to_do / len));
3404 xnow = localtime (&now);
3406 for (vector<Source*>::iterator src=srcs.begin(); src != srcs.end(); ++src) {
3407 dynamic_cast<FileSource*>((*src))->update_header (position, *xnow, now);
3410 /* build peakfile for new source */
3412 for (vector<Source*>::iterator src=srcs.begin(); src != srcs.end(); ++src) {
3413 dynamic_cast<FileSource*>(*src)->build_peaks ();
3421 for (vector<Source*>::iterator src=srcs.begin(); src != srcs.end(); ++src) {
3422 dynamic_cast<FileSource*>(*src)->mark_for_remove ();
3427 for (vector<Sample*>::iterator i = buffers.begin(); i != buffers.end(); ++i) {
3431 atomic_set (&processing_prohibited, 0);
3439 Session::get_silent_buffers (uint32_t howmany)
3441 for (uint32_t i = 0; i < howmany; ++i) {
3442 memset (_silent_buffers[i], 0, sizeof (Sample) * current_block_size);
3444 return _silent_buffers;
3448 Session::ntracks () const
3451 LockMonitor lm (route_lock, __LINE__, __FILE__);
3453 for (RouteList::const_iterator i = routes.begin(); i != routes.end(); ++i) {
3454 if (dynamic_cast<AudioTrack*> (*i)) {
3463 Session::nbusses () const
3466 LockMonitor lm (route_lock, __LINE__, __FILE__);
3468 for (RouteList::const_iterator i = routes.begin(); i != routes.end(); ++i) {
3469 if (dynamic_cast<AudioTrack*> (*i) == 0) {
3478 Session::set_layer_model (LayerModel lm)
3481 LayerModelChanged (); /* EMIT SIGNAL */
3486 Session::set_xfade_model (CrossfadeModel xm)