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/destructive_filesource.h>
52 #include <ardour/sndfilesource.h>
53 #include <ardour/auditioner.h>
54 #include <ardour/recent_sessions.h>
55 #include <ardour/redirect.h>
56 #include <ardour/send.h>
57 #include <ardour/insert.h>
58 #include <ardour/connection.h>
59 #include <ardour/slave.h>
60 #include <ardour/tempo.h>
61 #include <ardour/audio_track.h>
62 #include <ardour/cycle_timer.h>
63 #include <ardour/named_selection.h>
64 #include <ardour/crossfade.h>
65 #include <ardour/playlist.h>
66 #include <ardour/click.h>
67 #include <ardour/osc.h>
72 using namespace ARDOUR;
73 //using namespace sigc;
75 const char* Session::_template_suffix = X_(".template");
76 const char* Session::_statefile_suffix = X_(".ardour");
77 const char* Session::_pending_suffix = X_(".pending");
78 const char* Session::sound_dir_name = X_("sounds");
79 const char* Session::tape_dir_name = X_("tapes");
80 const char* Session::peak_dir_name = X_("peaks");
81 const char* Session::dead_sound_dir_name = X_("dead_sounds");
83 Session::compute_peak_t Session::compute_peak = 0;
84 Session::apply_gain_to_buffer_t Session::apply_gain_to_buffer = 0;
85 Session::mix_buffers_with_gain_t Session::mix_buffers_with_gain = 0;
86 Session::mix_buffers_no_gain_t Session::mix_buffers_no_gain = 0;
88 sigc::signal<int> Session::AskAboutPendingState;
91 Session::find_session (string str, string& path, string& snapshot, bool& isnew)
98 if (!realpath (str.c_str(), buf) && (errno != ENOENT && errno != ENOTDIR)) {
99 error << string_compose (_("Could not resolve path: %1 (%2)"), buf, strerror(errno)) << endmsg;
105 /* check to see if it exists, and what it is */
107 if (stat (str.c_str(), &statbuf)) {
108 if (errno == ENOENT) {
111 error << string_compose (_("cannot check session path %1 (%2)"), str, strerror (errno))
119 /* it exists, so it must either be the name
120 of the directory, or the name of the statefile
124 if (S_ISDIR (statbuf.st_mode)) {
126 string::size_type slash = str.find_last_of ('/');
128 if (slash == string::npos) {
130 /* a subdirectory of cwd, so statefile should be ... */
136 tmp += _statefile_suffix;
140 if (stat (tmp.c_str(), &statbuf)) {
141 error << string_compose (_("cannot check statefile %1 (%2)"), tmp, strerror (errno))
151 /* some directory someplace in the filesystem.
152 the snapshot name is the directory name
157 snapshot = str.substr (slash+1);
161 } else if (S_ISREG (statbuf.st_mode)) {
163 string::size_type slash = str.find_last_of ('/');
164 string::size_type suffix;
166 /* remove the suffix */
168 if (slash != string::npos) {
169 snapshot = str.substr (slash+1);
174 suffix = snapshot.find (_statefile_suffix);
176 if (suffix == string::npos) {
177 error << string_compose (_("%1 is not an Ardour snapshot file"), str) << endmsg;
183 snapshot = snapshot.substr (0, suffix);
185 if (slash == string::npos) {
187 /* we must be in the directory where the
188 statefile lives. get it using cwd().
191 char cwd[PATH_MAX+1];
193 if (getcwd (cwd, sizeof (cwd)) == 0) {
194 error << string_compose (_("cannot determine current working directory (%1)"), strerror (errno))
203 /* full path to the statefile */
205 path = str.substr (0, slash);
210 /* what type of file is it? */
211 error << string_compose (_("unknown file type for session %1"), str) << endmsg;
217 /* its the name of a new directory. get the name
221 string::size_type slash = str.find_last_of ('/');
223 if (slash == string::npos) {
225 /* no slash, just use the name, but clean it up */
227 path = legalize_for_path (str);
233 snapshot = str.substr (slash+1);
240 Session::Session (AudioEngine &eng,
242 string snapshot_name,
243 string* mix_template)
246 _mmc_port (default_mmc_port),
247 _mtc_port (default_mtc_port),
248 _midi_port (default_midi_port),
249 pending_events (2048),
250 //midi_requests (128), // the size of this should match the midi request pool size
251 _send_smpte_update (false),
256 cerr << "Loading session " << fullpath << " using snapshot " << snapshot_name << endl;
258 n_physical_outputs = _engine.n_physical_outputs();
259 n_physical_inputs = _engine.n_physical_inputs();
261 first_stage_init (fullpath, snapshot_name);
263 if (create (new_session, mix_template, _engine.frame_rate() * 60 * 5)) {
264 throw failed_constructor ();
267 if (second_stage_init (new_session)) {
268 throw failed_constructor ();
271 store_recent_sessions(_name, _path);
273 bool was_dirty = dirty();
275 _state_of_the_state = StateOfTheState (_state_of_the_state & ~Dirty);
278 DirtyChanged (); /* EMIT SIGNAL */
282 Session::Session (AudioEngine &eng,
284 string snapshot_name,
285 AutoConnectOption input_ac,
286 AutoConnectOption output_ac,
287 uint32_t control_out_channels,
288 uint32_t master_out_channels,
289 uint32_t requested_physical_in,
290 uint32_t requested_physical_out,
291 jack_nframes_t initial_length)
294 _mmc_port (default_mmc_port),
295 _mtc_port (default_mtc_port),
296 _midi_port (default_midi_port),
297 pending_events (2048),
298 //midi_requests (16),
304 cerr << "Loading session " << fullpath << " using snapshot " << snapshot_name << endl;
306 n_physical_outputs = max (requested_physical_out, _engine.n_physical_outputs());
307 n_physical_inputs = max (requested_physical_in, _engine.n_physical_inputs());
309 first_stage_init (fullpath, snapshot_name);
311 if (create (new_session, 0, initial_length)) {
312 throw failed_constructor ();
315 if (control_out_channels) {
317 r = new Route (*this, _("monitor"), -1, control_out_channels, -1, control_out_channels, Route::ControlOut);
322 if (master_out_channels) {
324 r = new Route (*this, _("master"), -1, master_out_channels, -1, master_out_channels, Route::MasterOut);
328 /* prohibit auto-connect to master, because there isn't one */
329 output_ac = AutoConnectOption (output_ac & ~AutoConnectMaster);
332 input_auto_connect = input_ac;
333 output_auto_connect = output_ac;
335 if (second_stage_init (new_session)) {
336 throw failed_constructor ();
339 store_recent_sessions(_name, _path);
341 bool was_dirty = dirty ();
343 _state_of_the_state = StateOfTheState (_state_of_the_state & ~Dirty);
346 DirtyChanged (); /* EMIT SIGNAL */
352 /* if we got to here, leaving pending capture state around
356 remove_pending_capture_state ();
358 _state_of_the_state = StateOfTheState (CannotSave|Deletion);
359 _engine.remove_session ();
361 going_away (); /* EMIT SIGNAL */
363 terminate_butler_thread ();
364 //terminate_midi_thread ();
366 if (click_data && click_data != default_click) {
367 delete [] click_data;
370 if (click_emphasis_data && click_emphasis_data != default_click_emphasis) {
371 delete [] click_emphasis_data;
385 for (vector<Sample*>::iterator i = _passthru_buffers.begin(); i != _passthru_buffers.end(); ++i) {
389 for (vector<Sample*>::iterator i = _silent_buffers.begin(); i != _silent_buffers.end(); ++i) {
393 for (vector<Sample*>::iterator i = _send_buffers.begin(); i != _send_buffers.end(); ++i) {
397 for (map<RunContext,char*>::iterator i = _conversion_buffers.begin(); i != _conversion_buffers.end(); ++i) {
398 delete [] (i->second);
401 #undef TRACK_DESTRUCTION
402 #ifdef TRACK_DESTRUCTION
403 cerr << "delete named selections\n";
404 #endif /* TRACK_DESTRUCTION */
405 for (NamedSelectionList::iterator i = named_selections.begin(); i != named_selections.end(); ) {
406 NamedSelectionList::iterator tmp;
415 #ifdef TRACK_DESTRUCTION
416 cerr << "delete playlists\n";
417 #endif /* TRACK_DESTRUCTION */
418 for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ) {
419 PlaylistList::iterator tmp;
429 #ifdef TRACK_DESTRUCTION
430 cerr << "delete audio regions\n";
431 #endif /* TRACK_DESTRUCTION */
432 for (AudioRegionList::iterator i = audio_regions.begin(); i != audio_regions.end(); ) {
433 AudioRegionList::iterator tmp;
443 #ifdef TRACK_DESTRUCTION
444 cerr << "delete routes\n";
445 #endif /* TRACK_DESTRUCTION */
446 for (RouteList::iterator i = routes.begin(); i != routes.end(); ) {
447 RouteList::iterator tmp;
454 #ifdef TRACK_DESTRUCTION
455 cerr << "delete diskstreams\n";
456 #endif /* TRACK_DESTRUCTION */
457 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ) {
458 DiskStreamList::iterator tmp;
468 #ifdef TRACK_DESTRUCTION
469 cerr << "delete sources\n";
470 #endif /* TRACK_DESTRUCTION */
471 for (SourceList::iterator i = sources.begin(); i != sources.end(); ) {
472 SourceList::iterator tmp;
482 #ifdef TRACK_DESTRUCTION
483 cerr << "delete mix groups\n";
484 #endif /* TRACK_DESTRUCTION */
485 for (list<RouteGroup *>::iterator i = mix_groups.begin(); i != mix_groups.end(); ) {
486 list<RouteGroup*>::iterator tmp;
496 #ifdef TRACK_DESTRUCTION
497 cerr << "delete edit groups\n";
498 #endif /* TRACK_DESTRUCTION */
499 for (list<RouteGroup *>::iterator i = edit_groups.begin(); i != edit_groups.end(); ) {
500 list<RouteGroup*>::iterator tmp;
510 #ifdef TRACK_DESTRUCTION
511 cerr << "delete connections\n";
512 #endif /* TRACK_DESTRUCTION */
513 for (ConnectionList::iterator i = _connections.begin(); i != _connections.end(); ) {
514 ConnectionList::iterator tmp;
524 if (butler_mixdown_buffer) {
525 delete [] butler_mixdown_buffer;
528 if (butler_gain_buffer) {
529 delete [] butler_gain_buffer;
532 Crossfade::set_buffer_size (0);
544 Session::set_worst_io_latencies (bool take_lock)
546 _worst_output_latency = 0;
547 _worst_input_latency = 0;
549 if (!_engine.connected()) {
554 route_lock.read_lock ();
557 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
558 _worst_output_latency = max (_worst_output_latency, (*i)->output_latency());
559 _worst_input_latency = max (_worst_input_latency, (*i)->input_latency());
563 route_lock.unlock ();
568 Session::when_engine_running ()
570 string first_physical_output;
572 /* we don't want to run execute this again */
574 first_time_running.disconnect ();
576 set_block_size (_engine.frames_per_cycle());
577 set_frame_rate (_engine.frame_rate());
579 /* every time we reconnect, recompute worst case output latencies */
581 _engine.Running.connect (sigc::bind (mem_fun (*this, &Session::set_worst_io_latencies), true));
583 if (synced_to_jack()) {
584 _engine.transport_stop ();
587 if (Config->get_jack_time_master()) {
588 _engine.transport_locate (_transport_frame);
596 _click_io = new ClickIO (*this, "click", 0, 0, -1, -1);
598 if (state_tree && (child = find_named_node (*state_tree->root(), "Click")) != 0) {
600 /* existing state for Click */
602 if (_click_io->set_state (*child->children().front()) == 0) {
604 _clicking = click_requested;
608 error << _("could not setup Click I/O") << endmsg;
614 /* default state for Click */
616 first_physical_output = _engine.get_nth_physical_output (0);
618 if (first_physical_output.length()) {
619 if (_click_io->add_output_port (first_physical_output, this)) {
620 // relax, even though its an error
622 _clicking = click_requested;
628 catch (failed_constructor& err) {
629 error << _("cannot setup Click I/O") << endmsg;
632 set_worst_io_latencies (true);
635 ControlChanged (Clicking); /* EMIT SIGNAL */
638 if (auditioner == 0) {
640 /* we delay creating the auditioner till now because
641 it makes its own connections to ports named
642 in the ARDOUR_RC config file. the engine has
643 to be running for this to work.
647 auditioner = new Auditioner (*this);
650 catch (failed_constructor& err) {
651 warning << _("cannot create Auditioner: no auditioning of regions possible") << endmsg;
655 /* Create a set of Connection objects that map
656 to the physical outputs currently available
661 for (uint32_t np = 0; np < n_physical_outputs; ++np) {
663 snprintf (buf, sizeof (buf), _("out %" PRIu32), np+1);
665 Connection* c = new OutputConnection (buf, true);
668 c->add_connection (0, _engine.get_nth_physical_output (np));
673 for (uint32_t np = 0; np < n_physical_inputs; ++np) {
675 snprintf (buf, sizeof (buf), _("in %" PRIu32), np+1);
677 Connection* c = new InputConnection (buf, true);
680 c->add_connection (0, _engine.get_nth_physical_input (np));
687 for (uint32_t np = 0; np < n_physical_outputs; np +=2) {
689 snprintf (buf, sizeof (buf), _("out %" PRIu32 "+%" PRIu32), np+1, np+2);
691 Connection* c = new OutputConnection (buf, true);
695 c->add_connection (0, _engine.get_nth_physical_output (np));
696 c->add_connection (1, _engine.get_nth_physical_output (np+1));
701 for (uint32_t np = 0; np < n_physical_inputs; np +=2) {
703 snprintf (buf, sizeof (buf), _("in %" PRIu32 "+%" PRIu32), np+1, np+2);
705 Connection* c = new InputConnection (buf, true);
709 c->add_connection (0, _engine.get_nth_physical_input (np));
710 c->add_connection (1, _engine.get_nth_physical_input (np+1));
719 /* create master/control ports */
724 /* force the master to ignore any later call to this */
726 if (_master_out->pending_state_node) {
727 _master_out->ports_became_legal();
730 /* no panner resets till we are through */
732 _master_out->defer_pan_reset ();
734 while ((int) _master_out->n_inputs() < _master_out->input_maximum()) {
735 if (_master_out->add_input_port ("", this)) {
736 error << _("cannot setup master inputs")
742 while ((int) _master_out->n_outputs() < _master_out->output_maximum()) {
743 if (_master_out->add_output_port (_engine.get_nth_physical_output (n), this)) {
744 error << _("cannot setup master outputs")
751 _master_out->allow_pan_reset ();
755 Connection* c = new OutputConnection (_("Master Out"), true);
757 for (uint32_t n = 0; n < _master_out->n_inputs (); ++n) {
759 c->add_connection ((int) n, _master_out->input(n)->name());
766 /* catch up on send+insert cnts */
770 for (list<PortInsert*>::iterator i = _port_inserts.begin(); i != _port_inserts.end(); ++i) {
773 if (sscanf ((*i)->name().c_str(), "%*s %u", &id) == 1) {
774 if (id > insert_cnt) {
782 for (list<Send*>::iterator i = _sends.begin(); i != _sends.end(); ++i) {
785 if (sscanf ((*i)->name().c_str(), "%*s %u", &id) == 1) {
792 _state_of_the_state = StateOfTheState (_state_of_the_state & ~(CannotSave|Dirty));
794 /* hook us up to the engine */
796 _engine.set_session (this);
800 osc->set_session (*this);
802 _state_of_the_state = Clean;
804 DirtyChanged (); /* EMIT SIGNAL */
808 Session::hookup_io ()
810 /* stop graph reordering notifications from
811 causing resorts, etc.
814 _state_of_the_state = StateOfTheState (_state_of_the_state | InitialConnecting);
816 /* Tell all IO objects to create their ports */
823 while ((int) _control_out->n_inputs() < _control_out->input_maximum()) {
824 if (_control_out->add_input_port ("", this)) {
825 error << _("cannot setup control inputs")
831 while ((int) _control_out->n_outputs() < _control_out->output_maximum()) {
832 if (_control_out->add_output_port (_engine.get_nth_physical_output (n), this)) {
833 error << _("cannot set up master outputs")
841 /* Tell all IO objects to connect themselves together */
843 IO::enable_connecting ();
845 /* Now reset all panners */
847 IO::reset_panners ();
849 /* Anyone who cares about input state, wake up and do something */
851 IOConnectionsComplete (); /* EMIT SIGNAL */
853 _state_of_the_state = StateOfTheState (_state_of_the_state & ~InitialConnecting);
855 /* now handle the whole enchilada as if it was one
861 /* update mixer solo state */
867 Session::playlist_length_changed (Playlist* pl)
869 /* we can't just increase end_location->end() if pl->get_maximum_extent()
870 if larger. if the playlist used to be the longest playlist,
871 and its now shorter, we have to decrease end_location->end(). hence,
872 we have to iterate over all diskstreams and check the
873 playlists currently in use.
879 Session::diskstream_playlist_changed (DiskStream* dstream)
883 if ((playlist = dstream->playlist()) != 0) {
884 playlist->LengthChanged.connect (sigc::bind (mem_fun (this, &Session::playlist_length_changed), playlist));
887 /* see comment in playlist_length_changed () */
892 Session::record_enabling_legal () const
894 /* this used to be in here, but survey says.... we don't need to restrict it */
895 // if (record_status() == Recording) {
906 Session::set_auto_play (bool yn)
908 if (auto_play != yn) {
911 ControlChanged (AutoPlay);
916 Session::set_auto_return (bool yn)
918 if (auto_return != yn) {
921 ControlChanged (AutoReturn);
926 Session::set_crossfades_active (bool yn)
928 if (crossfades_active != yn) {
929 crossfades_active = yn;
931 ControlChanged (CrossFadesActive);
936 Session::set_do_not_record_plugins (bool yn)
938 if (do_not_record_plugins != yn) {
939 do_not_record_plugins = yn;
941 ControlChanged (RecordingPlugins);
946 Session::set_auto_input (bool yn)
948 if (auto_input != yn) {
951 if (Config->get_use_hardware_monitoring() && transport_rolling()) {
952 /* auto-input only makes a difference if we're rolling */
954 /* Even though this can called from RT context we are using
955 a non-tentative rwlock here, because the action must occur.
956 The rarity and short potential lock duration makes this "OK"
958 RWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
959 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
960 if ((*i)->record_enabled ()) {
961 //cerr << "switching to input = " << !auto_input << __FILE__ << __LINE__ << endl << endl;
962 (*i)->monitor_input (!auto_input);
968 ControlChanged (AutoInput);
973 Session::reset_input_monitor_state ()
975 if (transport_rolling()) {
976 RWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
977 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
978 if ((*i)->record_enabled ()) {
979 //cerr << "switching to input = " << !auto_input << __FILE__ << __LINE__ << endl << endl;
980 (*i)->monitor_input (Config->get_use_hardware_monitoring() && !auto_input);
984 RWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
985 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
986 if ((*i)->record_enabled ()) {
987 //cerr << "switching to input = " << !auto_input << __FILE__ << __LINE__ << endl << endl;
988 (*i)->monitor_input (Config->get_use_hardware_monitoring());
996 Session::set_input_auto_connect (bool yn)
999 input_auto_connect = AutoConnectOption (input_auto_connect|AutoConnectPhysical);
1001 input_auto_connect = AutoConnectOption (input_auto_connect|~AutoConnectPhysical);
1007 Session::get_input_auto_connect () const
1009 return (input_auto_connect & AutoConnectPhysical);
1013 Session::set_output_auto_connect (AutoConnectOption aco)
1015 output_auto_connect = aco;
1020 Session::auto_punch_start_changed (Location* location)
1022 replace_event (Event::PunchIn, location->start());
1024 if (get_record_enabled() && get_punch_in()) {
1025 /* capture start has been changed, so save new pending state */
1026 save_state ("", true);
1031 Session::auto_punch_end_changed (Location* location)
1033 jack_nframes_t when_to_stop = location->end();
1034 // when_to_stop += _worst_output_latency + _worst_input_latency;
1035 replace_event (Event::PunchOut, when_to_stop);
1039 Session::auto_punch_changed (Location* location)
1041 jack_nframes_t when_to_stop = location->end();
1043 replace_event (Event::PunchIn, location->start());
1044 //when_to_stop += _worst_output_latency + _worst_input_latency;
1045 replace_event (Event::PunchOut, when_to_stop);
1049 Session::auto_loop_changed (Location* location)
1051 replace_event (Event::AutoLoop, location->end(), location->start());
1053 if (transport_rolling() && get_auto_loop()) {
1055 //if (_transport_frame < location->start() || _transport_frame > location->end()) {
1057 if (_transport_frame > location->end()) {
1058 // relocate to beginning of loop
1059 clear_events (Event::LocateRoll);
1061 request_locate (location->start(), true);
1064 else if (seamless_loop && !loop_changing) {
1066 // schedule a locate-roll to refill the diskstreams at the
1067 // previous loop end
1068 loop_changing = true;
1070 if (location->end() > last_loopend) {
1071 clear_events (Event::LocateRoll);
1072 Event *ev = new Event (Event::LocateRoll, Event::Add, last_loopend, last_loopend, 0, true);
1079 last_loopend = location->end();
1084 Session::set_auto_punch_location (Location* location)
1088 if ((existing = _locations.auto_punch_location()) != 0 && existing != location) {
1089 auto_punch_start_changed_connection.disconnect();
1090 auto_punch_end_changed_connection.disconnect();
1091 auto_punch_changed_connection.disconnect();
1092 existing->set_auto_punch (false, this);
1093 remove_event (existing->start(), Event::PunchIn);
1094 clear_events (Event::PunchOut);
1095 auto_punch_location_changed (0);
1100 if (location == 0) {
1104 if (location->end() <= location->start()) {
1105 error << _("Session: you can't use that location for auto punch (start <= end)") << endmsg;
1109 auto_punch_start_changed_connection.disconnect();
1110 auto_punch_end_changed_connection.disconnect();
1111 auto_punch_changed_connection.disconnect();
1113 auto_punch_start_changed_connection = location->start_changed.connect (mem_fun (this, &Session::auto_punch_start_changed));
1114 auto_punch_end_changed_connection = location->end_changed.connect (mem_fun (this, &Session::auto_punch_end_changed));
1115 auto_punch_changed_connection = location->changed.connect (mem_fun (this, &Session::auto_punch_changed));
1117 location->set_auto_punch (true, this);
1118 auto_punch_location_changed (location);
1122 Session::set_punch_in (bool yn)
1124 if (punch_in == yn) {
1130 if ((location = _locations.auto_punch_location()) != 0) {
1131 if ((punch_in = yn) == true) {
1132 replace_event (Event::PunchIn, location->start());
1134 remove_event (location->start(), Event::PunchIn);
1139 ControlChanged (PunchIn); /* EMIT SIGNAL */
1143 Session::set_punch_out (bool yn)
1145 if (punch_out == yn) {
1151 if ((location = _locations.auto_punch_location()) != 0) {
1152 if ((punch_out = yn) == true) {
1153 replace_event (Event::PunchOut, location->end());
1155 clear_events (Event::PunchOut);
1160 ControlChanged (PunchOut); /* EMIT SIGNAL */
1164 Session::set_auto_loop_location (Location* location)
1168 if ((existing = _locations.auto_loop_location()) != 0 && existing != location) {
1169 auto_loop_start_changed_connection.disconnect();
1170 auto_loop_end_changed_connection.disconnect();
1171 auto_loop_changed_connection.disconnect();
1172 existing->set_auto_loop (false, this);
1173 remove_event (existing->end(), Event::AutoLoop);
1174 auto_loop_location_changed (0);
1179 if (location == 0) {
1183 if (location->end() <= location->start()) {
1184 error << _("Session: you can't use a mark for auto loop") << endmsg;
1188 last_loopend = location->end();
1190 auto_loop_start_changed_connection.disconnect();
1191 auto_loop_end_changed_connection.disconnect();
1192 auto_loop_changed_connection.disconnect();
1194 auto_loop_start_changed_connection = location->start_changed.connect (mem_fun (this, &Session::auto_loop_changed));
1195 auto_loop_end_changed_connection = location->end_changed.connect (mem_fun (this, &Session::auto_loop_changed));
1196 auto_loop_changed_connection = location->changed.connect (mem_fun (this, &Session::auto_loop_changed));
1198 location->set_auto_loop (true, this);
1199 auto_loop_location_changed (location);
1203 Session::locations_added (Location* ignored)
1209 Session::locations_changed ()
1211 _locations.apply (*this, &Session::handle_locations_changed);
1215 Session::handle_locations_changed (Locations::LocationList& locations)
1217 Locations::LocationList::iterator i;
1219 bool set_loop = false;
1220 bool set_punch = false;
1222 for (i = locations.begin(); i != locations.end(); ++i) {
1226 if (location->is_auto_punch()) {
1227 set_auto_punch_location (location);
1230 if (location->is_auto_loop()) {
1231 set_auto_loop_location (location);
1238 set_auto_loop_location (0);
1241 set_auto_punch_location (0);
1248 Session::enable_record ()
1250 /* XXX really atomic compare+swap here */
1251 if (atomic_read (&_record_status) != Recording) {
1252 atomic_set (&_record_status, Recording);
1253 _last_record_location = _transport_frame;
1255 //send_mmc_in_another_thread (MIDI::MachineControl::cmdRecordStrobe);
1257 if (Config->get_use_hardware_monitoring() && auto_input) {
1258 /* Even though this can be called from RT context we are using
1259 a non-tentative rwlock here, because the action must occur.
1260 The rarity and short potential lock duration makes this "OK"
1262 RWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
1264 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
1265 if ((*i)->record_enabled ()) {
1266 (*i)->monitor_input (true);
1271 RecordStateChanged ();
1276 Session::disable_record (bool rt_context, bool force)
1280 if ((rs = (RecordState) atomic_read (&_record_status)) != Disabled) {
1282 if (!Config->get_latched_record_enable () || force) {
1283 atomic_set (&_record_status, Disabled);
1285 if (rs == Recording) {
1286 atomic_set (&_record_status, Enabled);
1291 //send_mmc_in_another_thread (MIDI::MachineControl::cmdRecordExit);
1293 if (Config->get_use_hardware_monitoring() && auto_input) {
1294 /* Even though this can be called from RT context we are using
1295 a non-tentative rwlock here, because the action must occur.
1296 The rarity and short potential lock duration makes this "OK"
1298 RWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
1300 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
1301 if ((*i)->record_enabled ()) {
1302 (*i)->monitor_input (false);
1307 RecordStateChanged (); /* emit signal */
1310 remove_pending_capture_state ();
1316 Session::step_back_from_record ()
1318 atomic_set (&_record_status, Enabled);
1320 if (Config->get_use_hardware_monitoring()) {
1321 /* Even though this can be called from RT context we are using
1322 a non-tentative rwlock here, because the action must occur.
1323 The rarity and short potential lock duration makes this "OK"
1325 RWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
1327 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
1328 if (auto_input && (*i)->record_enabled ()) {
1329 //cerr << "switching from input" << __FILE__ << __LINE__ << endl << endl;
1330 (*i)->monitor_input (false);
1337 Session::maybe_enable_record ()
1339 atomic_set (&_record_status, Enabled);
1341 /* XXX this save should really happen in another thread. its needed so that
1342 pending capture state can be recovered if we crash.
1345 save_state ("", true);
1347 if (_transport_speed) {
1353 //send_mmc_in_another_thread (MIDI::MachineControl::cmdRecordPause);
1354 RecordStateChanged (); /* EMIT SIGNAL */
1361 Session::audible_frame () const
1364 jack_nframes_t offset;
1367 /* the first of these two possible settings for "offset"
1368 mean that the audible frame is stationary until
1369 audio emerges from the latency compensation
1372 the second means that the audible frame is stationary
1373 until audio would emerge from a physical port
1374 in the absence of any plugin latency compensation
1377 offset = _worst_output_latency;
1379 if (offset > current_block_size) {
1380 offset -= current_block_size;
1382 /* XXX is this correct? if we have no external
1383 physical connections and everything is internal
1384 then surely this is zero? still, how
1385 likely is that anyway?
1387 offset = current_block_size;
1390 if (synced_to_jack()) {
1391 tf = _engine.transport_frame();
1393 tf = _transport_frame;
1396 if (_transport_speed == 0) {
1406 if (!non_realtime_work_pending()) {
1410 /* take latency into account */
1419 Session::set_frame_rate (jack_nframes_t frames_per_second)
1421 /** \fn void Session::set_frame_size(jack_nframes_t)
1422 the AudioEngine object that calls this guarantees
1423 that it will not be called while we are also in
1424 ::process(). Its fine to do things that block
1428 _current_frame_rate = frames_per_second;
1429 _frames_per_smpte_frame = (double) _current_frame_rate / (double) smpte_frames_per_second;
1431 Route::set_automation_interval ((jack_nframes_t) ceil ((double) frames_per_second * 0.25));
1435 /* XXX need to reset/reinstantiate all LADSPA plugins */
1439 Session::set_block_size (jack_nframes_t nframes)
1441 /* the AudioEngine guarantees
1442 that it will not be called while we are also in
1443 ::process(). It is therefore fine to do things that block
1448 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
1449 RWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
1450 vector<Sample*>::iterator i;
1453 current_block_size = nframes;
1455 for (np = 0, i = _passthru_buffers.begin(); i != _passthru_buffers.end(); ++i, ++np) {
1459 for (vector<Sample*>::iterator i = _silent_buffers.begin(); i != _silent_buffers.end(); ++i) {
1463 _passthru_buffers.clear ();
1464 _silent_buffers.clear ();
1466 ensure_passthru_buffers (np);
1468 for (vector<Sample*>::iterator i = _send_buffers.begin(); i != _send_buffers.end(); ++i) {
1472 #ifdef NO_POSIX_MEMALIGN
1473 buf = (Sample *) malloc(current_block_size * sizeof(Sample));
1475 posix_memalign((void **)&buf,16,current_block_size * 4);
1479 memset (*i, 0, sizeof (Sample) * current_block_size);
1483 if (_gain_automation_buffer) {
1484 delete [] _gain_automation_buffer;
1486 _gain_automation_buffer = new gain_t[nframes];
1488 allocate_pan_automation_buffers (nframes, _npan_buffers, true);
1490 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
1491 (*i)->set_block_size (nframes);
1494 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
1495 (*i)->set_block_size (nframes);
1498 set_worst_io_latencies (false);
1503 Session::set_default_fade (float steepness, float fade_msecs)
1506 jack_nframes_t fade_frames;
1508 /* Don't allow fade of less 1 frame */
1510 if (fade_msecs < (1000.0 * (1.0/_current_frame_rate))) {
1517 fade_frames = (jack_nframes_t) floor (fade_msecs * _current_frame_rate * 0.001);
1521 default_fade_msecs = fade_msecs;
1522 default_fade_steepness = steepness;
1525 // jlc, WTF is this!
1526 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
1527 AudioRegion::set_default_fade (steepness, fade_frames);
1532 /* XXX have to do this at some point */
1533 /* foreach region using default fade, reset, then
1534 refill_all_diskstream_buffers ();
1539 struct RouteSorter {
1540 bool operator() (Route* r1, Route* r2) {
1541 if (r1->fed_by.find (r2) != r1->fed_by.end()) {
1543 } else if (r2->fed_by.find (r1) != r2->fed_by.end()) {
1546 if (r1->fed_by.empty()) {
1547 if (r2->fed_by.empty()) {
1548 /* no ardour-based connections inbound to either route. just use signal order */
1549 return r1->order_key(N_("signal")) < r2->order_key(N_("signal"));
1551 /* r2 has connections, r1 does not; run r1 early */
1555 return r1->order_key(N_("signal")) < r2->order_key(N_("signal"));
1562 trace_terminal (Route* r1, Route* rbase)
1566 if ((r1->fed_by.find (rbase) != r1->fed_by.end()) && (rbase->fed_by.find (r1) != rbase->fed_by.end())) {
1567 info << string_compose(_("feedback loop setup between %1 and %2"), r1->name(), rbase->name()) << endmsg;
1571 /* make a copy of the existing list of routes that feed r1 */
1573 set<Route *> existing = r1->fed_by;
1575 /* for each route that feeds r1, recurse, marking it as feeding
1579 for (set<Route *>::iterator i = existing.begin(); i != existing.end(); ++i) {
1582 /* r2 is a route that feeds r1 which somehow feeds base. mark
1583 base as being fed by r2
1586 rbase->fed_by.insert (r2);
1590 /* 2nd level feedback loop detection. if r1 feeds or is fed by r2,
1594 if ((r1->fed_by.find (r2) != r1->fed_by.end()) && (r2->fed_by.find (r1) != r2->fed_by.end())) {
1598 /* now recurse, so that we can mark base as being fed by
1599 all routes that feed r2
1602 trace_terminal (r2, rbase);
1609 Session::resort_routes (void* src)
1611 /* don't do anything here with signals emitted
1612 by Routes while we are being destroyed.
1615 if (_state_of_the_state & Deletion) {
1619 /* Caller MUST hold the route_lock */
1621 RouteList::iterator i, j;
1623 for (i = routes.begin(); i != routes.end(); ++i) {
1625 (*i)->fed_by.clear ();
1627 for (j = routes.begin(); j != routes.end(); ++j) {
1629 /* although routes can feed themselves, it will
1630 cause an endless recursive descent if we
1631 detect it. so don't bother checking for
1639 if ((*j)->feeds (*i)) {
1640 (*i)->fed_by.insert (*j);
1645 for (i = routes.begin(); i != routes.end(); ++i) {
1646 trace_terminal (*i, *i);
1653 cerr << "finished route resort\n";
1655 for (i = routes.begin(); i != routes.end(); ++i) {
1656 cerr << " " << (*i)->name() << " signal order = " << (*i)->order_key ("signal") << endl;
1664 Session::new_audio_track (int input_channels, int output_channels, TrackMode mode)
1667 char track_name[32];
1669 uint32_t channels_used = 0;
1671 uint32_t nphysical_in;
1672 uint32_t nphysical_out;
1674 /* count existing audio tracks */
1677 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
1678 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
1679 if (dynamic_cast<AudioTrack*>(*i) != 0) {
1680 if (!(*i)->hidden()) {
1682 channels_used += (*i)->n_inputs();
1688 /* check for duplicate route names, since we might have pre-existing
1689 routes with this name (e.g. create Audio1, Audio2, delete Audio1,
1690 save, close,restart,add new route - first named route is now
1695 snprintf (track_name, sizeof(track_name), "Audio %" PRIu32, n+1);
1696 if (route_by_name (track_name) == 0) {
1701 } while (n < (UINT_MAX-1));
1703 if (input_auto_connect & AutoConnectPhysical) {
1704 nphysical_in = n_physical_inputs;
1709 if (output_auto_connect & AutoConnectPhysical) {
1710 nphysical_out = n_physical_outputs;
1716 track = new AudioTrack (*this, track_name, Route::Flag (0), mode);
1718 if (track->ensure_io (input_channels, output_channels, false, this)) {
1719 error << string_compose (_("cannot configure %1 in/%2 out configuration for new audio track"),
1720 input_channels, output_channels)
1725 for (uint32_t x = 0; x < track->n_inputs() && x < nphysical_in; ++x) {
1729 if (input_auto_connect & AutoConnectPhysical) {
1730 port = _engine.get_nth_physical_input ((channels_used+x)%nphysical_in);
1733 if (port.length() && track->connect_input (track->input (x), port, this)) {
1739 for (uint32_t x = 0; x < track->n_outputs(); ++x) {
1743 if (nphysical_out && (output_auto_connect & AutoConnectPhysical)) {
1744 port = _engine.get_nth_physical_output ((channels_used+x)%nphysical_out);
1745 } else if (output_auto_connect & AutoConnectMaster) {
1747 port = _master_out->input (x%_master_out->n_inputs())->name();
1751 if (port.length() && track->connect_output (track->output (x), port, this)) {
1757 vector<string> cports;
1758 uint32_t ni = _control_out->n_inputs();
1760 for (n = 0; n < ni; ++n) {
1761 cports.push_back (_control_out->input(n)->name());
1764 track->set_control_outs (cports);
1767 track->diskstream_changed.connect (mem_fun (this, &Session::resort_routes));
1771 track->set_remote_control_id (ntracks());
1774 catch (failed_constructor &err) {
1775 error << _("Session: could not create new audio track.") << endmsg;
1783 Session::new_audio_route (int input_channels, int output_channels)
1790 /* count existing audio busses */
1793 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
1794 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
1795 if (dynamic_cast<AudioTrack*>(*i) == 0) {
1796 if (!(*i)->hidden()) {
1804 snprintf (bus_name, sizeof(bus_name), "Bus %" PRIu32, n+1);
1805 if (route_by_name (bus_name) == 0) {
1810 } while (n < (UINT_MAX-1));
1813 bus = new Route (*this, bus_name, -1, -1, -1, -1);
1815 if (bus->ensure_io (input_channels, output_channels, false, this)) {
1816 error << string_compose (_("cannot configure %1 in/%2 out configuration for new audio track"),
1817 input_channels, output_channels)
1821 for (uint32_t x = 0; x < bus->n_inputs(); ++x) {
1825 if (input_auto_connect & AutoConnectPhysical) {
1826 port = _engine.get_nth_physical_input ((n+x)%n_physical_inputs);
1829 if (port.length() && bus->connect_input (bus->input (x), port, this)) {
1834 for (uint32_t x = 0; x < bus->n_outputs(); ++x) {
1838 if (output_auto_connect & AutoConnectPhysical) {
1839 port = _engine.get_nth_physical_input ((n+x)%n_physical_outputs);
1840 } else if (output_auto_connect & AutoConnectMaster) {
1842 port = _master_out->input (x%_master_out->n_inputs())->name();
1846 if (port.length() && bus->connect_output (bus->output (x), port, this)) {
1852 vector<string> cports;
1853 uint32_t ni = _control_out->n_inputs();
1855 for (uint32_t n = 0; n < ni; ++n) {
1856 cports.push_back (_control_out->input(n)->name());
1858 bus->set_control_outs (cports);
1864 catch (failed_constructor &err) {
1865 error << _("Session: could not create new route.") << endmsg;
1873 Session::add_route (Route* route)
1876 RWLockMonitor lm (route_lock, true, __LINE__, __FILE__);
1877 routes.push_front (route);
1881 route->solo_changed.connect (sigc::bind (mem_fun (*this, &Session::route_solo_changed), route));
1882 route->mute_changed.connect (mem_fun (*this, &Session::route_mute_changed));
1883 route->output_changed.connect (mem_fun (*this, &Session::set_worst_io_latencies_x));
1884 route->redirects_changed.connect (mem_fun (*this, &Session::update_latency_compensation_proxy));
1886 if (route->master()) {
1887 _master_out = route;
1890 if (route->control()) {
1891 _control_out = route;
1895 save_state (_current_snapshot_name);
1897 RouteAdded (route); /* EMIT SIGNAL */
1901 Session::add_diskstream (DiskStream* dstream)
1903 /* need to do this in case we're rolling at the time, to prevent false underruns */
1904 dstream->do_refill(0, 0, 0);
1907 RWLockMonitor lm (diskstream_lock, true, __LINE__, __FILE__);
1908 diskstreams.push_back (dstream);
1911 /* take a reference to the diskstream, preventing it from
1912 ever being deleted until the session itself goes away,
1913 or chooses to remove it for its own purposes.
1917 dstream->set_block_size (current_block_size);
1919 dstream->PlaylistChanged.connect (sigc::bind (mem_fun (*this, &Session::diskstream_playlist_changed), dstream));
1920 /* this will connect to future changes, and check the current length */
1921 diskstream_playlist_changed (dstream);
1923 dstream->prepare ();
1926 save_state (_current_snapshot_name);
1928 DiskStreamAdded (dstream); /* EMIT SIGNAL */
1932 Session::remove_route (Route& route)
1935 RWLockMonitor lm (route_lock, true, __LINE__, __FILE__);
1936 routes.remove (&route);
1938 /* deleting the master out seems like a dumb
1939 idea, but its more of a UI policy issue
1943 if (&route == _master_out) {
1947 if (&route == _control_out) {
1950 /* cancel control outs for all routes */
1952 vector<string> empty;
1954 for (RouteList::iterator r = routes.begin(); r != routes.end(); ++r) {
1955 (*r)->set_control_outs (empty);
1959 update_route_solo_state ();
1963 RWLockMonitor lm (diskstream_lock, true, __LINE__, __FILE__);
1967 if ((at = dynamic_cast<AudioTrack*>(&route)) != 0) {
1968 diskstreams.remove (&at->disk_stream());
1969 at->disk_stream().unref ();
1972 find_current_end ();
1975 update_latency_compensation (false, false);
1978 /* XXX should we disconnect from the Route's signals ? */
1980 save_state (_current_snapshot_name);
1986 Session::route_mute_changed (void* src)
1992 Session::route_solo_changed (void* src, Route* route)
1994 if (solo_update_disabled) {
1999 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
2002 is_track = (dynamic_cast<AudioTrack*>(route) != 0);
2004 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
2006 /* soloing a track mutes all other tracks, soloing a bus mutes all other busses */
2010 /* don't mess with busses */
2012 if (dynamic_cast<AudioTrack*>(*i) == 0) {
2018 /* don't mess with tracks */
2020 if (dynamic_cast<AudioTrack*>(*i) != 0) {
2025 if ((*i) != route &&
2026 ((*i)->mix_group () == 0 ||
2027 (*i)->mix_group () != route->mix_group () ||
2028 !route->mix_group ()->is_active())) {
2030 if ((*i)->soloed()) {
2032 /* if its already soloed, and solo latching is enabled,
2033 then leave it as it is.
2036 if (_solo_latched) {
2043 solo_update_disabled = true;
2044 (*i)->set_solo (false, src);
2045 solo_update_disabled = false;
2049 bool something_soloed = false;
2050 bool same_thing_soloed = false;
2051 bool signal = false;
2053 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
2054 if ((*i)->soloed()) {
2055 something_soloed = true;
2056 if (dynamic_cast<AudioTrack*>(*i)) {
2058 same_thing_soloed = true;
2063 same_thing_soloed = true;
2071 if (something_soloed != currently_soloing) {
2073 currently_soloing = something_soloed;
2076 modify_solo_mute (is_track, same_thing_soloed);
2079 SoloActive (currently_soloing);
2086 Session::set_solo_latched (bool yn)
2088 if (yn != _solo_latched) {
2091 ControlChanged (SoloLatch);
2096 Session::update_route_solo_state ()
2099 bool is_track = false;
2100 bool signal = false;
2102 /* caller must hold RouteLock */
2104 /* this is where we actually implement solo by changing
2105 the solo mute setting of each track.
2108 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
2109 if ((*i)->soloed()) {
2111 if (dynamic_cast<AudioTrack*>(*i)) {
2118 if (mute != currently_soloing) {
2120 currently_soloing = mute;
2123 if (!is_track && !mute) {
2125 /* nothing is soloed */
2127 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
2128 (*i)->set_solo_mute (false);
2138 modify_solo_mute (is_track, mute);
2141 SoloActive (currently_soloing);
2146 Session::modify_solo_mute (bool is_track, bool mute)
2148 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
2152 /* only alter track solo mute */
2154 if (dynamic_cast<AudioTrack*>(*i)) {
2155 if ((*i)->soloed()) {
2156 (*i)->set_solo_mute (!mute);
2158 (*i)->set_solo_mute (mute);
2164 /* only alter bus solo mute */
2166 if (!dynamic_cast<AudioTrack*>(*i)) {
2168 if ((*i)->soloed()) {
2170 (*i)->set_solo_mute (false);
2174 /* don't mute master or control outs
2175 in response to another bus solo
2178 if ((*i) != _master_out &&
2179 (*i) != _control_out) {
2180 (*i)->set_solo_mute (mute);
2191 Session::catch_up_on_solo ()
2193 /* this is called after set_state() to catch the full solo
2194 state, which can't be correctly determined on a per-route
2195 basis, but needs the global overview that only the session
2198 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
2199 update_route_solo_state();
2203 Session::route_by_name (string name)
2205 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
2207 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
2208 if ((*i)->name() == name) {
2217 Session::route_by_remote_id (uint32_t id)
2219 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
2221 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
2222 if ((*i)->remote_control_id() == id) {
2231 Session::find_current_end ()
2233 if (_state_of_the_state & Loading) {
2237 jack_nframes_t max = get_maximum_extent ();
2239 if (max > end_location->end()) {
2240 end_location->set_end (max);
2242 DurationChanged(); /* EMIT SIGNAL */
2247 Session::get_maximum_extent () const
2249 jack_nframes_t max = 0;
2252 /* Don't take the diskstream lock. Caller must have other ways to
2256 for (DiskStreamList::const_iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
2257 Playlist* pl = (*i)->playlist();
2258 if ((me = pl->get_maximum_extent()) > max) {
2267 Session::diskstream_by_name (string name)
2269 RWLockMonitor lm (diskstream_lock, false, __LINE__, __FILE__);
2271 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
2272 if ((*i)->name() == name) {
2281 Session::diskstream_by_id (id_t id)
2283 RWLockMonitor lm (diskstream_lock, false, __LINE__, __FILE__);
2285 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
2286 if ((*i)->id() == id) {
2294 /* AudioRegion management */
2297 Session::new_region_name (string old)
2299 string::size_type last_period;
2301 string::size_type len = old.length() + 64;
2304 if ((last_period = old.find_last_of ('.')) == string::npos) {
2306 /* no period present - add one explicitly */
2309 last_period = old.length() - 1;
2314 number = atoi (old.substr (last_period+1).c_str());
2318 while (number < (UINT_MAX-1)) {
2320 AudioRegionList::const_iterator i;
2325 snprintf (buf, len, "%s%" PRIu32, old.substr (0, last_period + 1).c_str(), number);
2328 for (i = audio_regions.begin(); i != audio_regions.end(); ++i) {
2329 if ((*i).second->name() == sbuf) {
2334 if (i == audio_regions.end()) {
2339 if (number != (UINT_MAX-1)) {
2343 error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
2348 Session::region_name (string& result, string base, bool newlevel) const
2355 LockMonitor lm (region_lock, __LINE__, __FILE__);
2357 snprintf (buf, sizeof (buf), "%d", (int)audio_regions.size() + 1);
2365 /* XXX this is going to be slow. optimize me later */
2370 string::size_type pos;
2372 pos = base.find_last_of ('.');
2374 /* pos may be npos, but then we just use entire base */
2376 subbase = base.substr (0, pos);
2380 bool name_taken = true;
2383 LockMonitor lm (region_lock, __LINE__, __FILE__);
2385 for (int n = 1; n < 5000; ++n) {
2388 snprintf (buf, sizeof (buf), ".%d", n);
2393 for (AudioRegionList::const_iterator i = audio_regions.begin(); i != audio_regions.end(); ++i) {
2394 if ((*i).second->name() == result) {
2407 fatal << string_compose(_("too many regions with names like %1"), base) << endmsg;
2415 Session::add_region (Region* region)
2417 AudioRegion* ar = 0;
2418 AudioRegion* oar = 0;
2422 LockMonitor lm (region_lock, __LINE__, __FILE__);
2424 if ((ar = dynamic_cast<AudioRegion*> (region)) != 0) {
2426 AudioRegionList::iterator x;
2428 for (x = audio_regions.begin(); x != audio_regions.end(); ++x) {
2430 oar = dynamic_cast<AudioRegion*> (x->second);
2432 if (ar->region_list_equivalent (*oar)) {
2437 if (x == audio_regions.end()) {
2439 pair<AudioRegionList::key_type, AudioRegionList::mapped_type> entry;
2441 entry.first = region->id();
2444 pair<AudioRegionList::iterator,bool> x = audio_regions.insert (entry);
2455 fatal << _("programming error: ")
2456 << X_("unknown region type passed to Session::add_region()")
2463 /* mark dirty because something has changed even if we didn't
2464 add the region to the region list.
2470 region->GoingAway.connect (mem_fun (*this, &Session::remove_region));
2471 region->StateChanged.connect (sigc::bind (mem_fun (*this, &Session::region_changed), region));
2472 AudioRegionAdded (ar); /* EMIT SIGNAL */
2477 Session::region_changed (Change what_changed, Region* region)
2479 if (what_changed & Region::HiddenChanged) {
2480 /* relay hidden changes */
2481 RegionHiddenChange (region);
2486 Session::region_renamed (Region* region)
2488 add_region (region);
2492 Session::remove_region (Region* region)
2494 AudioRegionList::iterator i;
2495 AudioRegion* ar = 0;
2496 bool removed = false;
2499 LockMonitor lm (region_lock, __LINE__, __FILE__);
2501 if ((ar = dynamic_cast<AudioRegion*> (region)) != 0) {
2502 if ((i = audio_regions.find (region->id())) != audio_regions.end()) {
2503 audio_regions.erase (i);
2507 fatal << _("programming error: ")
2508 << X_("unknown region type passed to Session::remove_region()")
2514 /* mark dirty because something has changed even if we didn't
2515 remove the region from the region list.
2521 AudioRegionRemoved(ar); /* EMIT SIGNAL */
2526 Session::find_whole_file_parent (AudioRegion& child)
2528 AudioRegionList::iterator i;
2529 AudioRegion* region;
2530 LockMonitor lm (region_lock, __LINE__, __FILE__);
2532 for (i = audio_regions.begin(); i != audio_regions.end(); ++i) {
2534 region = (*i).second;
2536 if (region->whole_file()) {
2538 if (child.source_equivalent (*region)) {
2548 Session::find_equivalent_playlist_regions (AudioRegion& region, vector<AudioRegion*>& result)
2550 for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ++i) {
2554 if ((pl = dynamic_cast<AudioPlaylist*>(*i)) == 0) {
2558 pl->get_region_list_equivalent_regions (region, result);
2563 Session::destroy_region (Region* region)
2565 AudioRegion* aregion;
2567 if ((aregion = dynamic_cast<AudioRegion*> (region)) == 0) {
2571 if (aregion->playlist()) {
2572 aregion->playlist()->destroy_region (region);
2575 vector<Source*> srcs;
2577 for (uint32_t n = 0; n < aregion->n_channels(); ++n) {
2578 srcs.push_back (&aregion->source (n));
2581 for (vector<Source*>::iterator i = srcs.begin(); i != srcs.end(); ++i) {
2583 if ((*i)->use_cnt() == 0) {
2584 (*i)->mark_for_remove ();
2593 Session::destroy_regions (list<Region*> regions)
2595 for (list<Region*>::iterator i = regions.begin(); i != regions.end(); ++i) {
2596 destroy_region (*i);
2602 Session::remove_last_capture ()
2606 RWLockMonitor lm (diskstream_lock, false, __LINE__, __FILE__);
2608 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
2609 list<Region*>& l = (*i)->last_capture_regions();
2612 r.insert (r.end(), l.begin(), l.end());
2617 destroy_regions (r);
2622 Session::remove_region_from_region_list (Region& r)
2628 /* Source Management */
2631 Session::add_source (Source* source)
2633 pair<SourceList::key_type, SourceList::mapped_type> entry;
2636 LockMonitor lm (source_lock, __LINE__, __FILE__);
2637 entry.first = source->id();
2638 entry.second = source;
2639 sources.insert (entry);
2642 source->GoingAway.connect (mem_fun (this, &Session::remove_source));
2645 SourceAdded (source); /* EMIT SIGNAL */
2649 Session::remove_source (Source* source)
2651 SourceList::iterator i;
2654 LockMonitor lm (source_lock, __LINE__, __FILE__);
2656 if ((i = sources.find (source->id())) != sources.end()) {
2661 if (!_state_of_the_state & InCleanup) {
2663 /* save state so we don't end up with a session file
2664 referring to non-existent sources.
2667 save_state (_current_snapshot_name);
2670 SourceRemoved(source); /* EMIT SIGNAL */
2674 Session::get_source (ARDOUR::id_t id)
2676 LockMonitor lm (source_lock, __LINE__, __FILE__);
2677 SourceList::iterator i;
2680 if ((i = sources.find (id)) != sources.end()) {
2681 source = (*i).second;
2688 Session::peak_path_from_audio_path (string audio_path)
2690 /* XXX hardly bombproof! fix me */
2694 res = PBD::dirname (audio_path);
2695 res = PBD::dirname (res);
2697 res += peak_dir_name;
2699 res += PBD::basename_nosuffix (audio_path);
2706 Session::change_audio_path_by_name (string path, string oldname, string newname, bool destructive)
2709 string old_basename = basename_nosuffix (oldname);
2710 string new_legalized = legalize_for_path (newname);
2712 /* note: we know (or assume) the old path is already valid */
2716 /* destructive file sources have a name of the form:
2718 /path/to/Tnnnn-NAME(%[LR])?.wav
2720 the task here is to replace NAME with the new name.
2723 /* find last slash */
2727 string::size_type slash;
2728 string::size_type dash;
2730 if ((slash = path.find_last_of ('/')) == string::npos) {
2734 dir = path.substr (0, slash+1);
2736 /* '-' is not a legal character for the NAME part of the path */
2738 if ((dash = path.find_last_of ('-')) == string::npos) {
2742 prefix = path.substr (slash+1, dash-(slash+1));
2747 path += new_legalized;
2748 path += ".wav"; /* XXX gag me with a spoon */
2752 /* non-destructive file sources have a name of the form:
2754 /path/to/NAME-nnnnn(%[LR])?.wav
2756 the task here is to replace NAME with the new name.
2759 /* find last slash */
2763 string::size_type slash;
2764 string::size_type dash;
2766 if ((slash = path.find_last_of ('/')) == string::npos) {
2770 dir = path.substr (0, slash+1);
2772 /* '-' is not a legal character for the NAME part of the path */
2774 if ((dash = path.find_last_of ('-')) == string::npos) {
2778 suffix = path.substr (dash);
2781 path += new_legalized;
2789 Session::audio_path_from_name (string name, uint32_t nchan, uint32_t chan, bool destructive)
2793 char buf[PATH_MAX+1];
2794 const uint32_t limit = 10000;
2798 legalized = legalize_for_path (name);
2800 /* find a "version" of the file name that doesn't exist in
2801 any of the possible directories.
2804 for (cnt = (destructive ? ++destructive_index : 1); cnt <= limit; ++cnt) {
2806 vector<space_and_path>::iterator i;
2807 uint32_t existing = 0;
2809 for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
2814 spath += tape_dir_name;
2816 spath += sound_dir_name;
2821 snprintf (buf, sizeof(buf), "%s/T%04d-%s.wav", spath.c_str(), cnt, legalized.c_str());
2822 } else if (nchan == 2) {
2824 snprintf (buf, sizeof(buf), "%s/T%04d-%s%%L.wav", spath.c_str(), cnt, legalized.c_str());
2826 snprintf (buf, sizeof(buf), "%s/T%04d-%s%%R.wav", spath.c_str(), cnt, legalized.c_str());
2828 } else if (nchan < 26) {
2829 snprintf (buf, sizeof(buf), "%s/T%04d-%s%%%c.wav", spath.c_str(), cnt, legalized.c_str(), 'a' + chan);
2831 snprintf (buf, sizeof(buf), "%s/T%04d-%s.wav", spath.c_str(), cnt, legalized.c_str());
2839 snprintf (buf, sizeof(buf), "%s-%u.wav", spath.c_str(), cnt);
2840 } else if (nchan == 2) {
2842 snprintf (buf, sizeof(buf), "%s-%u%%L.wav", spath.c_str(), cnt);
2844 snprintf (buf, sizeof(buf), "%s-%u%%R.wav", spath.c_str(), cnt);
2846 } else if (nchan < 26) {
2847 snprintf (buf, sizeof(buf), "%s-%u%%%c.wav", spath.c_str(), cnt, 'a' + chan);
2849 snprintf (buf, sizeof(buf), "%s-%u.wav", spath.c_str(), cnt);
2853 if (access (buf, F_OK) == 0) {
2858 if (existing == 0) {
2863 error << string_compose(_("There are already %1 recordings for %2, which I consider too many."), limit, name) << endmsg;
2864 throw failed_constructor();
2868 /* we now have a unique name for the file, but figure out where to
2875 spath = tape_dir ();
2877 spath = discover_best_sound_dir ();
2880 string::size_type pos = foo.find_last_of ('/');
2882 if (pos == string::npos) {
2885 spath += foo.substr (pos + 1);
2892 Session::create_file_source (DiskStream& ds, int32_t chan, bool destructive)
2894 string spath = audio_path_from_name (ds.name(), ds.n_channels(), chan, destructive);
2896 /* this might throw failed_constructor(), which is OK */
2899 return new DestructiveFileSource (spath, frame_rate(), false, Config->get_native_file_data_format());
2901 return new FileSource (spath, frame_rate(), false, Config->get_native_file_data_format());
2905 /* Playlist management */
2908 Session::get_playlist (string name)
2912 if ((ret = playlist_by_name (name)) == 0) {
2913 ret = new AudioPlaylist (*this, name);
2920 Session::playlist_by_name (string name)
2922 LockMonitor lm (playlist_lock, __LINE__, __FILE__);
2923 for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ++i) {
2924 if ((*i)->name() == name) {
2928 for (PlaylistList::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
2929 if ((*i)->name() == name) {
2937 Session::add_playlist (Playlist* playlist)
2939 if (playlist->hidden()) {
2944 LockMonitor lm (playlist_lock, __LINE__, __FILE__);
2945 if (find (playlists.begin(), playlists.end(), playlist) == playlists.end()) {
2946 playlists.insert (playlists.begin(), playlist);
2948 playlist->InUse.connect (mem_fun (*this, &Session::track_playlist));
2949 playlist->GoingAway.connect (mem_fun (*this, &Session::remove_playlist));
2955 PlaylistAdded (playlist); /* EMIT SIGNAL */
2959 Session::track_playlist (Playlist* pl, bool inuse)
2961 PlaylistList::iterator x;
2964 LockMonitor lm (playlist_lock, __LINE__, __FILE__);
2967 //cerr << "shifting playlist to unused: " << pl->name() << endl;
2969 unused_playlists.insert (pl);
2971 if ((x = playlists.find (pl)) != playlists.end()) {
2972 playlists.erase (x);
2977 //cerr << "shifting playlist to used: " << pl->name() << endl;
2979 playlists.insert (pl);
2981 if ((x = unused_playlists.find (pl)) != unused_playlists.end()) {
2982 unused_playlists.erase (x);
2989 Session::remove_playlist (Playlist* playlist)
2991 if (_state_of_the_state & Deletion) {
2996 LockMonitor lm (playlist_lock, __LINE__, __FILE__);
2997 // cerr << "removing playlist: " << playlist->name() << endl;
2999 PlaylistList::iterator i;
3001 i = find (playlists.begin(), playlists.end(), playlist);
3003 if (i != playlists.end()) {
3004 playlists.erase (i);
3007 i = find (unused_playlists.begin(), unused_playlists.end(), playlist);
3008 if (i != unused_playlists.end()) {
3009 unused_playlists.erase (i);
3016 PlaylistRemoved (playlist); /* EMIT SIGNAL */
3020 Session::set_audition (AudioRegion* r)
3022 pending_audition_region = r;
3023 post_transport_work = PostTransportWork (post_transport_work | PostTransportAudition);
3024 schedule_butler_transport_work ();
3028 Session::non_realtime_set_audition ()
3030 if (pending_audition_region == (AudioRegion*) 0xfeedface) {
3031 auditioner->audition_current_playlist ();
3032 } else if (pending_audition_region) {
3033 auditioner->audition_region (*pending_audition_region);
3035 pending_audition_region = 0;
3036 AuditionActive (true); /* EMIT SIGNAL */
3040 Session::audition_playlist ()
3042 Event* ev = new Event (Event::Audition, Event::Add, Event::Immediate, 0, 0.0);
3043 ev->set_ptr ((void*) 0xfeedface);
3048 Session::audition_region (AudioRegion& r)
3050 Event* ev = new Event (Event::Audition, Event::Add, Event::Immediate, 0, 0.0);
3056 Session::cancel_audition ()
3058 if (auditioner->active()) {
3059 auditioner->cancel_audition ();
3060 AuditionActive (false); /* EMIT SIGNAL */
3065 Session::RoutePublicOrderSorter::operator() (Route* a, Route* b)
3067 return a->order_key(N_("signal")) < b->order_key(N_("signal"));
3071 Session::remove_empty_sounds ()
3074 PathScanner scanner;
3079 vector<string *>* possible_audiofiles = scanner (dir, "\\.wav$", false, true);
3081 for (vector<string *>::iterator i = possible_audiofiles->begin(); i != possible_audiofiles->end(); ++i) {
3083 if (FileSource::is_empty (*(*i))) {
3085 unlink ((*i)->c_str());
3087 string peak_path = peak_path_from_audio_path (**i);
3088 unlink (peak_path.c_str());
3094 delete possible_audiofiles;
3098 Session::is_auditioning () const
3100 /* can be called before we have an auditioner object */
3102 return auditioner->active();
3109 Session::set_all_solo (bool yn)
3112 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
3114 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
3115 if (!(*i)->hidden()) {
3116 (*i)->set_solo (yn, this);
3125 Session::set_all_mute (bool yn)
3128 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
3130 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
3131 if (!(*i)->hidden()) {
3132 (*i)->set_mute (yn, this);
3141 Session::n_diskstreams () const
3143 RWLockMonitor lm (diskstream_lock, false, __LINE__, __FILE__);
3146 for (DiskStreamList::const_iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
3147 if (!(*i)->hidden()) {
3155 Session::foreach_diskstream (void (DiskStream::*func)(void))
3157 RWLockMonitor lm (diskstream_lock, false, __LINE__, __FILE__);
3158 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
3159 if (!(*i)->hidden()) {
3166 Session::graph_reordered ()
3168 /* don't do this stuff if we are setting up connections
3169 from a set_state() call.
3172 if (_state_of_the_state & InitialConnecting) {
3176 RWLockMonitor lm1 (route_lock, true, __LINE__, __FILE__);
3177 RWLockMonitor lm2 (diskstream_lock, false, __LINE__, __FILE__);
3181 /* force all diskstreams to update their capture offset values to
3182 reflect any changes in latencies within the graph.
3185 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
3186 (*i)->set_capture_offset ();
3191 Session::record_disenable_all ()
3193 record_enable_change_all (false);
3197 Session::record_enable_all ()
3199 record_enable_change_all (true);
3203 Session::record_enable_change_all (bool yn)
3205 RWLockMonitor lm1 (route_lock, false, __LINE__, __FILE__);
3207 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
3210 if ((at = dynamic_cast<AudioTrack*>(*i)) != 0) {
3211 at->set_record_enable (yn, this);
3215 /* since we don't keep rec-enable state, don't mark session dirty */
3219 Session::add_redirect (Redirect* redirect)
3223 PortInsert* port_insert;
3224 PluginInsert* plugin_insert;
3226 if ((insert = dynamic_cast<Insert *> (redirect)) != 0) {
3227 if ((port_insert = dynamic_cast<PortInsert *> (insert)) != 0) {
3228 _port_inserts.insert (_port_inserts.begin(), port_insert);
3229 } else if ((plugin_insert = dynamic_cast<PluginInsert *> (insert)) != 0) {
3230 _plugin_inserts.insert (_plugin_inserts.begin(), plugin_insert);
3232 fatal << _("programming error: unknown type of Insert created!") << endmsg;
3235 } else if ((send = dynamic_cast<Send *> (redirect)) != 0) {
3236 _sends.insert (_sends.begin(), send);
3238 fatal << _("programming error: unknown type of Redirect created!") << endmsg;
3242 redirect->GoingAway.connect (mem_fun (*this, &Session::remove_redirect));
3248 Session::remove_redirect (Redirect* redirect)
3252 PortInsert* port_insert;
3253 PluginInsert* plugin_insert;
3255 if ((insert = dynamic_cast<Insert *> (redirect)) != 0) {
3256 if ((port_insert = dynamic_cast<PortInsert *> (insert)) != 0) {
3257 _port_inserts.remove (port_insert);
3258 } else if ((plugin_insert = dynamic_cast<PluginInsert *> (insert)) != 0) {
3259 _plugin_inserts.remove (plugin_insert);
3261 fatal << _("programming error: unknown type of Insert deleted!") << endmsg;
3264 } else if ((send = dynamic_cast<Send *> (redirect)) != 0) {
3265 _sends.remove (send);
3267 fatal << _("programming error: unknown type of Redirect deleted!") << endmsg;
3275 Session::available_capture_duration ()
3277 const double scale = 4096.0 / sizeof (Sample);
3279 if (_total_free_4k_blocks * scale > (double) max_frames) {
3283 return (jack_nframes_t) floor (_total_free_4k_blocks * scale);
3287 Session::add_connection (ARDOUR::Connection* connection)
3290 LockMonitor (connection_lock, __LINE__, __FILE__);
3291 _connections.push_back (connection);
3294 ConnectionAdded (connection); /* EMIT SIGNAL */
3300 Session::remove_connection (ARDOUR::Connection* connection)
3302 bool removed = false;
3305 LockMonitor (connection_lock, __LINE__, __FILE__);
3306 ConnectionList::iterator i = find (_connections.begin(), _connections.end(), connection);
3308 if (i != _connections.end()) {
3309 _connections.erase (i);
3315 ConnectionRemoved (connection); /* EMIT SIGNAL */
3321 ARDOUR::Connection *
3322 Session::connection_by_name (string name) const
3324 LockMonitor lm (connection_lock, __LINE__, __FILE__);
3326 for (ConnectionList::const_iterator i = _connections.begin(); i != _connections.end(); ++i) {
3327 if ((*i)->name() == name) {
3336 Session::set_edit_mode (EditMode mode)
3341 LockMonitor lm (playlist_lock, __LINE__, __FILE__);
3343 for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ++i) {
3344 (*i)->set_edit_mode (mode);
3349 ControlChanged (EditingMode); /* EMIT SIGNAL */
3353 Session::tempo_map_changed (Change ignored)
3360 Session::ensure_passthru_buffers (uint32_t howmany)
3362 while (howmany > _passthru_buffers.size()) {
3364 #ifdef NO_POSIX_MEMALIGN
3365 p = (Sample *) malloc(current_block_size * sizeof(Sample));
3367 posix_memalign((void **)&p,16,current_block_size * 4);
3369 _passthru_buffers.push_back (p);
3373 #ifdef NO_POSIX_MEMALIGN
3374 p = (Sample *) malloc(current_block_size * sizeof(Sample));
3376 posix_memalign((void **)&p,16,current_block_size * 4);
3378 memset (p, 0, sizeof (Sample) * current_block_size);
3379 _silent_buffers.push_back (p);
3383 #ifdef NO_POSIX_MEMALIGN
3384 p = (Sample *) malloc(current_block_size * sizeof(Sample));
3386 posix_memalign((void **)&p,16,current_block_size * 4);
3388 memset (p, 0, sizeof (Sample) * current_block_size);
3389 _send_buffers.push_back (p);
3392 allocate_pan_automation_buffers (current_block_size, howmany, false);
3396 Session::next_send_name ()
3399 snprintf (buf, sizeof (buf), "send %" PRIu32, ++send_cnt);
3404 Session::next_insert_name ()
3407 snprintf (buf, sizeof (buf), "insert %" PRIu32, ++insert_cnt);
3411 /* Named Selection management */
3414 Session::named_selection_by_name (string name)
3416 LockMonitor lm (named_selection_lock, __LINE__, __FILE__);
3417 for (NamedSelectionList::iterator i = named_selections.begin(); i != named_selections.end(); ++i) {
3418 if ((*i)->name == name) {
3426 Session::add_named_selection (NamedSelection* named_selection)
3429 LockMonitor lm (named_selection_lock, __LINE__, __FILE__);
3430 named_selections.insert (named_selections.begin(), named_selection);
3435 NamedSelectionAdded (); /* EMIT SIGNAL */
3439 Session::remove_named_selection (NamedSelection* named_selection)
3441 bool removed = false;
3444 LockMonitor lm (named_selection_lock, __LINE__, __FILE__);
3446 NamedSelectionList::iterator i = find (named_selections.begin(), named_selections.end(), named_selection);
3448 if (i != named_selections.end()) {
3450 named_selections.erase (i);
3457 NamedSelectionRemoved (); /* EMIT SIGNAL */
3462 Session::reset_native_file_format ()
3464 // jlc - WHY take routelock?
3465 //RWLockMonitor lm1 (route_lock, true, __LINE__, __FILE__);
3466 RWLockMonitor lm2 (diskstream_lock, false, __LINE__, __FILE__);
3468 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
3469 (*i)->reset_write_sources (false);
3474 Session::route_name_unique (string n) const
3476 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
3478 for (RouteList::const_iterator i = routes.begin(); i != routes.end(); ++i) {
3479 if ((*i)->name() == n) {
3488 Session::remove_file_source (FileSource& fs)
3490 return fs.move_to_trash (dead_sound_dir_name);
3494 Session::n_playlists () const
3496 LockMonitor lm (playlist_lock, __LINE__, __FILE__);
3497 return playlists.size();
3501 Session::set_solo_model (SoloModel sm)
3503 if (sm != _solo_model) {
3505 ControlChanged (SoloingModel);
3511 Session::allocate_pan_automation_buffers (jack_nframes_t nframes, uint32_t howmany, bool force)
3513 if (!force && howmany <= _npan_buffers) {
3517 if (_pan_automation_buffer) {
3519 for (uint32_t i = 0; i < _npan_buffers; ++i) {
3520 delete [] _pan_automation_buffer[i];
3523 delete [] _pan_automation_buffer;
3526 _pan_automation_buffer = new pan_t*[howmany];
3528 for (uint32_t i = 0; i < howmany; ++i) {
3529 _pan_automation_buffer[i] = new pan_t[nframes];
3532 _npan_buffers = howmany;
3536 Session::add_instant_xml (XMLNode& node, const std::string& dir)
3538 Stateful::add_instant_xml (node, dir);
3539 Config->add_instant_xml (node, get_user_ardour_path());
3543 Session::freeze (InterThreadInfo& itt)
3545 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
3547 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
3551 if ((at = dynamic_cast<AudioTrack*>(*i)) != 0) {
3552 /* XXX this is wrong because itt.progress will keep returning to zero at the start
3563 Session::write_one_track (AudioTrack& track, jack_nframes_t start, jack_nframes_t len, bool overwrite, vector<Source*>& srcs,
3564 InterThreadInfo& itt)
3568 FileSource* fsource;
3570 char buf[PATH_MAX+1];
3573 jack_nframes_t position;
3574 jack_nframes_t this_chunk;
3575 jack_nframes_t to_do;
3576 vector<Sample*> buffers;
3578 const jack_nframes_t chunk_size = (256 * 1024)/4;
3580 atomic_set (&processing_prohibited, 1);
3582 /* call tree *MUST* hold route_lock */
3584 if ((playlist = track.disk_stream().playlist()) == 0) {
3588 /* external redirects will be a problem */
3590 if (track.has_external_redirects()) {
3594 nchans = track.disk_stream().n_channels();
3596 dir = discover_best_sound_dir ();
3598 for (uint32_t chan_n=0; chan_n < nchans; ++chan_n) {
3600 for (x = 0; x < 99999; ++x) {
3601 snprintf (buf, sizeof(buf), "%s/%s-%d-bounce-%" PRIu32 ".wav", dir.c_str(), playlist->name().c_str(), chan_n, x+1);
3602 if (access (buf, F_OK) != 0) {
3608 error << string_compose (_("too many bounced versions of playlist \"%1\""), playlist->name()) << endmsg;
3613 fsource = new FileSource (buf, frame_rate(), false, Config->get_native_file_data_format());
3616 catch (failed_constructor& err) {
3617 error << string_compose (_("cannot create new audio file \"%1\" for %2"), buf, track.name()) << endmsg;
3621 srcs.push_back(fsource);
3624 /* XXX need to flush all redirects */
3629 /* create a set of reasonably-sized buffers */
3631 for (vector<Sample*>::iterator i = _passthru_buffers.begin(); i != _passthru_buffers.end(); ++i) {
3633 #ifdef NO_POSIX_MEMALIGN
3634 b = (Sample *) malloc(chunk_size * sizeof(Sample));
3636 posix_memalign((void **)&b,16,chunk_size * 4);
3638 buffers.push_back (b);
3641 workbuf = new char[chunk_size * 4];
3643 while (to_do && !itt.cancel) {
3645 this_chunk = min (to_do, chunk_size);
3647 if (track.export_stuff (buffers, workbuf, nchans, start, this_chunk)) {
3652 for (vector<Source*>::iterator src=srcs.begin(); src != srcs.end(); ++src, ++n) {
3653 if ((*src)->write (buffers[n], this_chunk, workbuf) != this_chunk) {
3658 start += this_chunk;
3659 to_do -= this_chunk;
3661 itt.progress = (float) (1.0 - ((double) to_do / len));
3670 xnow = localtime (&now);
3672 for (vector<Source*>::iterator src=srcs.begin(); src != srcs.end(); ++src) {
3673 dynamic_cast<FileSource*>((*src))->update_header (position, *xnow, now);
3676 /* build peakfile for new source */
3678 for (vector<Source*>::iterator src=srcs.begin(); src != srcs.end(); ++src) {
3679 dynamic_cast<FileSource*>(*src)->build_peaks ();
3687 for (vector<Source*>::iterator src=srcs.begin(); src != srcs.end(); ++src) {
3688 dynamic_cast<FileSource*>(*src)->mark_for_remove ();
3693 for (vector<Sample*>::iterator i = buffers.begin(); i != buffers.end(); ++i) {
3701 atomic_set (&processing_prohibited, 0);
3709 Session::get_silent_buffers (uint32_t howmany)
3711 for (uint32_t i = 0; i < howmany; ++i) {
3712 memset (_silent_buffers[i], 0, sizeof (Sample) * current_block_size);
3714 return _silent_buffers;
3718 Session::ntracks () const
3721 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
3723 for (RouteList::const_iterator i = routes.begin(); i != routes.end(); ++i) {
3724 if (dynamic_cast<AudioTrack*> (*i)) {
3733 Session::nbusses () const
3736 RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
3738 for (RouteList::const_iterator i = routes.begin(); i != routes.end(); ++i) {
3739 if (dynamic_cast<AudioTrack*> (*i) == 0) {
3748 Session::set_layer_model (LayerModel lm)
3750 if (lm != layer_model) {
3753 ControlChanged (LayeringModel);
3758 Session::set_xfade_model (CrossfadeModel xm)
3760 if (xm != xfade_model) {
3763 ControlChanged (CrossfadingModel);