2 Copyright (C) 2000-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.
20 /* Note: public Editor methods are documented in public_editor.h */
30 #include "pbd/error.h"
31 #include "pbd/basename.h"
32 #include "pbd/pthread_utils.h"
33 #include "pbd/memento_command.h"
34 #include "pbd/unwind.h"
35 #include "pbd/whitespace.h"
36 #include "pbd/stateful_diff_command.h"
38 #include <gtkmm2ext/utils.h>
39 #include <gtkmm2ext/choice.h>
40 #include <gtkmm2ext/popup.h>
42 #include "ardour/audio_track.h"
43 #include "ardour/audioregion.h"
44 #include "ardour/dB.h"
45 #include "ardour/location.h"
46 #include "ardour/midi_region.h"
47 #include "ardour/operations.h"
48 #include "ardour/playlist_factory.h"
49 #include "ardour/quantize.h"
50 #include "ardour/region_factory.h"
51 #include "ardour/reverse.h"
52 #include "ardour/route_group.h"
53 #include "ardour/session.h"
54 #include "ardour/session_playlists.h"
55 #include "ardour/strip_silence.h"
56 #include "ardour/transient_detector.h"
57 #include "ardour/utils.h"
59 #include "ardour_ui.h"
62 #include "time_axis_view.h"
63 #include "route_time_axis.h"
64 #include "audio_time_axis.h"
65 #include "automation_time_axis.h"
66 #include "streamview.h"
67 #include "audio_streamview.h"
68 #include "audio_region_view.h"
69 #include "midi_region_view.h"
70 #include "rgb_macros.h"
71 #include "selection_templates.h"
72 #include "selection.h"
74 #include "gtk-custom-hruler.h"
75 #include "gui_thread.h"
78 #include "editor_drag.h"
79 #include "strip_silence_dialog.h"
80 #include "editor_routes.h"
81 #include "editor_regions.h"
82 #include "quantize_dialog.h"
83 #include "interthread_progress_window.h"
84 #include "insert_time_dialog.h"
85 #include "normalize_dialog.h"
86 #include "editor_cursors.h"
87 #include "mouse_cursors.h"
88 #include "patch_change_dialog.h"
89 #include "transpose_dialog.h"
94 using namespace ARDOUR;
97 using namespace Gtkmm2ext;
98 using namespace Editing;
99 using Gtkmm2ext::Keyboard;
101 /***********************************************************************
103 ***********************************************************************/
106 Editor::undo (uint32_t n)
108 if (_drags->active ()) {
118 Editor::redo (uint32_t n)
120 if (_drags->active ()) {
130 Editor::split_regions_at (framepos_t where, RegionSelection& regions)
134 list <boost::shared_ptr<Playlist > > used_playlists;
136 if (regions.empty()) {
140 begin_reversible_command (_("split"));
142 // if splitting a single region, and snap-to is using
143 // region boundaries, don't pay attention to them
145 if (regions.size() == 1) {
146 switch (_snap_type) {
147 case SnapToRegionStart:
148 case SnapToRegionSync:
149 case SnapToRegionEnd:
158 EditorFreeze(); /* Emit Signal */
161 for (RegionSelection::iterator a = regions.begin(); a != regions.end(); ) {
163 RegionSelection::iterator tmp;
165 /* XXX this test needs to be more complicated, to make sure we really
166 have something to split.
169 if (!(*a)->region()->covers (where)) {
177 boost::shared_ptr<Playlist> pl = (*a)->region()->playlist();
185 /* we haven't seen this playlist before */
187 /* remember used playlists so we can thaw them later */
188 used_playlists.push_back(pl);
193 pl->clear_changes ();
194 pl->split_region ((*a)->region(), where);
195 _session->add_command (new StatefulDiffCommand (pl));
201 while (used_playlists.size() > 0) {
202 list <boost::shared_ptr<Playlist > >::iterator i = used_playlists.begin();
204 used_playlists.pop_front();
207 commit_reversible_command ();
210 EditorThaw(); /* Emit Signal */
214 /** Move one extreme of the current range selection. If more than one range is selected,
215 * the start of the earliest range or the end of the latest range is moved.
217 * @param move_end true to move the end of the current range selection, false to move
219 * @param next true to move the extreme to the next region boundary, false to move to
223 Editor::move_range_selection_start_or_end_to_region_boundary (bool move_end, bool next)
225 if (selection->time.start() == selection->time.end_frame()) {
229 framepos_t start = selection->time.start ();
230 framepos_t end = selection->time.end_frame ();
232 /* the position of the thing we may move */
233 framepos_t pos = move_end ? end : start;
234 int dir = next ? 1 : -1;
236 /* so we don't find the current region again */
237 if (dir > 0 || pos > 0) {
241 framepos_t const target = get_region_boundary (pos, dir, true, false);
256 begin_reversible_command (_("alter selection"));
257 selection->set_preserving_all_ranges (start, end);
258 commit_reversible_command ();
262 Editor::nudge_forward_release (GdkEventButton* ev)
264 if (ev->state & Keyboard::PrimaryModifier) {
265 nudge_forward (false, true);
267 nudge_forward (false, false);
273 Editor::nudge_backward_release (GdkEventButton* ev)
275 if (ev->state & Keyboard::PrimaryModifier) {
276 nudge_backward (false, true);
278 nudge_backward (false, false);
285 Editor::nudge_forward (bool next, bool force_playhead)
288 framepos_t next_distance;
294 RegionSelection rs = get_regions_from_selection_and_entered ();
296 if (!force_playhead && !rs.empty()) {
298 begin_reversible_command (_("nudge regions forward"));
300 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
301 boost::shared_ptr<Region> r ((*i)->region());
303 distance = get_nudge_distance (r->position(), next_distance);
306 distance = next_distance;
310 r->set_position (r->position() + distance);
311 _session->add_command (new StatefulDiffCommand (r));
314 commit_reversible_command ();
317 } else if (!force_playhead && !selection->markers.empty()) {
321 begin_reversible_command (_("nudge location forward"));
323 for (MarkerSelection::iterator i = selection->markers.begin(); i != selection->markers.end(); ++i) {
325 Location* loc = find_location_from_marker ((*i), is_start);
329 XMLNode& before (loc->get_state());
332 distance = get_nudge_distance (loc->start(), next_distance);
334 distance = next_distance;
336 if (max_framepos - distance > loc->start() + loc->length()) {
337 loc->set_start (loc->start() + distance);
339 loc->set_start (max_framepos - loc->length());
342 distance = get_nudge_distance (loc->end(), next_distance);
344 distance = next_distance;
346 if (max_framepos - distance > loc->end()) {
347 loc->set_end (loc->end() + distance);
349 loc->set_end (max_framepos);
352 XMLNode& after (loc->get_state());
353 _session->add_command (new MementoCommand<Location>(*loc, &before, &after));
357 commit_reversible_command ();
360 distance = get_nudge_distance (playhead_cursor->current_frame, next_distance);
361 _session->request_locate (playhead_cursor->current_frame + distance);
366 Editor::nudge_backward (bool next, bool force_playhead)
369 framepos_t next_distance;
375 RegionSelection rs = get_regions_from_selection_and_entered ();
377 if (!force_playhead && !rs.empty()) {
379 begin_reversible_command (_("nudge regions backward"));
381 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
382 boost::shared_ptr<Region> r ((*i)->region());
384 distance = get_nudge_distance (r->position(), next_distance);
387 distance = next_distance;
392 if (r->position() > distance) {
393 r->set_position (r->position() - distance);
397 _session->add_command (new StatefulDiffCommand (r));
400 commit_reversible_command ();
402 } else if (!force_playhead && !selection->markers.empty()) {
406 begin_reversible_command (_("nudge location forward"));
408 for (MarkerSelection::iterator i = selection->markers.begin(); i != selection->markers.end(); ++i) {
410 Location* loc = find_location_from_marker ((*i), is_start);
414 XMLNode& before (loc->get_state());
417 distance = get_nudge_distance (loc->start(), next_distance);
419 distance = next_distance;
421 if (distance < loc->start()) {
422 loc->set_start (loc->start() - distance);
427 distance = get_nudge_distance (loc->end(), next_distance);
430 distance = next_distance;
433 if (distance < loc->end() - loc->length()) {
434 loc->set_end (loc->end() - distance);
436 loc->set_end (loc->length());
440 XMLNode& after (loc->get_state());
441 _session->add_command (new MementoCommand<Location>(*loc, &before, &after));
445 commit_reversible_command ();
449 distance = get_nudge_distance (playhead_cursor->current_frame, next_distance);
451 if (playhead_cursor->current_frame > distance) {
452 _session->request_locate (playhead_cursor->current_frame - distance);
454 _session->goto_start();
460 Editor::nudge_forward_capture_offset ()
462 RegionSelection rs = get_regions_from_selection_and_entered ();
464 if (!_session || rs.empty()) {
468 begin_reversible_command (_("nudge forward"));
470 framepos_t const distance = _session->worst_output_latency();
472 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
473 boost::shared_ptr<Region> r ((*i)->region());
476 r->set_position (r->position() + distance);
477 _session->add_command(new StatefulDiffCommand (r));
480 commit_reversible_command ();
484 Editor::nudge_backward_capture_offset ()
486 RegionSelection rs = get_regions_from_selection_and_entered ();
488 if (!_session || rs.empty()) {
492 begin_reversible_command (_("nudge forward"));
494 framepos_t const distance = _session->worst_output_latency();
496 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
497 boost::shared_ptr<Region> r ((*i)->region());
501 if (r->position() > distance) {
502 r->set_position (r->position() - distance);
506 _session->add_command(new StatefulDiffCommand (r));
509 commit_reversible_command ();
515 Editor::move_to_start ()
517 _session->goto_start ();
521 Editor::move_to_end ()
524 _session->request_locate (_session->current_end_frame());
528 Editor::build_region_boundary_cache ()
531 vector<RegionPoint> interesting_points;
532 boost::shared_ptr<Region> r;
533 TrackViewList tracks;
536 region_boundary_cache.clear ();
542 switch (_snap_type) {
543 case SnapToRegionStart:
544 interesting_points.push_back (Start);
546 case SnapToRegionEnd:
547 interesting_points.push_back (End);
549 case SnapToRegionSync:
550 interesting_points.push_back (SyncPoint);
552 case SnapToRegionBoundary:
553 interesting_points.push_back (Start);
554 interesting_points.push_back (End);
557 fatal << string_compose (_("build_region_boundary_cache called with snap_type = %1"), _snap_type) << endmsg;
562 TimeAxisView *ontrack = 0;
565 if (!selection->tracks.empty()) {
566 tlist = selection->tracks.filter_to_unique_playlists ();
568 tlist = track_views.filter_to_unique_playlists ();
571 while (pos < _session->current_end_frame() && !at_end) {
574 framepos_t lpos = max_framepos;
576 for (vector<RegionPoint>::iterator p = interesting_points.begin(); p != interesting_points.end(); ++p) {
578 if ((r = find_next_region (pos, *p, 1, tlist, &ontrack)) == 0) {
579 if (*p == interesting_points.back()) {
582 /* move to next point type */
588 rpos = r->first_frame();
592 rpos = r->last_frame();
596 rpos = r->sync_position ();
604 RouteTimeAxisView *rtav;
606 if (ontrack != 0 && (rtav = dynamic_cast<RouteTimeAxisView*>(ontrack)) != 0 ) {
607 if (rtav->track() != 0) {
608 speed = rtav->track()->speed();
612 rpos = track_frame_to_session_frame (rpos, speed);
618 /* prevent duplicates, but we don't use set<> because we want to be able
622 vector<framepos_t>::iterator ri;
624 for (ri = region_boundary_cache.begin(); ri != region_boundary_cache.end(); ++ri) {
630 if (ri == region_boundary_cache.end()) {
631 region_boundary_cache.push_back (rpos);
638 /* finally sort to be sure that the order is correct */
640 sort (region_boundary_cache.begin(), region_boundary_cache.end());
643 boost::shared_ptr<Region>
644 Editor::find_next_region (framepos_t frame, RegionPoint point, int32_t dir, TrackViewList& tracks, TimeAxisView **ontrack)
646 TrackViewList::iterator i;
647 framepos_t closest = max_framepos;
648 boost::shared_ptr<Region> ret;
652 framepos_t track_frame;
653 RouteTimeAxisView *rtav;
655 for (i = tracks.begin(); i != tracks.end(); ++i) {
658 boost::shared_ptr<Region> r;
661 if ( (rtav = dynamic_cast<RouteTimeAxisView*>(*i)) != 0 ) {
662 if (rtav->track()!=0)
663 track_speed = rtav->track()->speed();
666 track_frame = session_frame_to_track_frame(frame, track_speed);
668 if ((r = (*i)->find_next_region (track_frame, point, dir)) == 0) {
674 rpos = r->first_frame ();
678 rpos = r->last_frame ();
682 rpos = r->sync_position ();
686 // rpos is a "track frame", converting it to "_session frame"
687 rpos = track_frame_to_session_frame(rpos, track_speed);
690 distance = rpos - frame;
692 distance = frame - rpos;
695 if (distance < closest) {
707 Editor::find_next_region_boundary (framepos_t pos, int32_t dir, const TrackViewList& tracks)
709 framecnt_t distance = max_framepos;
710 framepos_t current_nearest = -1;
712 for (TrackViewList::const_iterator i = tracks.begin(); i != tracks.end(); ++i) {
713 framepos_t contender;
716 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*i);
722 if ((contender = rtv->find_next_region_boundary (pos, dir)) < 0) {
726 d = ::llabs (pos - contender);
729 current_nearest = contender;
734 return current_nearest;
738 Editor::get_region_boundary (framepos_t pos, int32_t dir, bool with_selection, bool only_onscreen)
743 if (with_selection && Config->get_region_boundaries_from_selected_tracks()) {
745 if (!selection->tracks.empty()) {
747 target = find_next_region_boundary (pos, dir, selection->tracks);
751 if (only_onscreen || Config->get_region_boundaries_from_onscreen_tracks()) {
752 get_onscreen_tracks (tvl);
753 target = find_next_region_boundary (pos, dir, tvl);
755 target = find_next_region_boundary (pos, dir, track_views);
761 if (only_onscreen || Config->get_region_boundaries_from_onscreen_tracks()) {
762 get_onscreen_tracks (tvl);
763 target = find_next_region_boundary (pos, dir, tvl);
765 target = find_next_region_boundary (pos, dir, track_views);
773 Editor::cursor_to_region_boundary (bool with_selection, int32_t dir)
775 framepos_t pos = playhead_cursor->current_frame;
782 // so we don't find the current region again..
783 if (dir > 0 || pos > 0) {
787 if ((target = get_region_boundary (pos, dir, with_selection, false)) < 0) {
791 _session->request_locate (target);
795 Editor::cursor_to_next_region_boundary (bool with_selection)
797 cursor_to_region_boundary (with_selection, 1);
801 Editor::cursor_to_previous_region_boundary (bool with_selection)
803 cursor_to_region_boundary (with_selection, -1);
807 Editor::cursor_to_region_point (EditorCursor* cursor, RegionPoint point, int32_t dir)
809 boost::shared_ptr<Region> r;
810 framepos_t pos = cursor->current_frame;
816 TimeAxisView *ontrack = 0;
818 // so we don't find the current region again..
822 if (!selection->tracks.empty()) {
824 r = find_next_region (pos, point, dir, selection->tracks, &ontrack);
826 } else if (clicked_axisview) {
829 t.push_back (clicked_axisview);
831 r = find_next_region (pos, point, dir, t, &ontrack);
835 r = find_next_region (pos, point, dir, track_views, &ontrack);
844 pos = r->first_frame ();
848 pos = r->last_frame ();
852 pos = r->sync_position ();
857 RouteTimeAxisView *rtav;
859 if ( ontrack != 0 && (rtav = dynamic_cast<RouteTimeAxisView*>(ontrack)) != 0 ) {
860 if (rtav->track() != 0) {
861 speed = rtav->track()->speed();
865 pos = track_frame_to_session_frame(pos, speed);
867 if (cursor == playhead_cursor) {
868 _session->request_locate (pos);
870 cursor->set_position (pos);
875 Editor::cursor_to_next_region_point (EditorCursor* cursor, RegionPoint point)
877 cursor_to_region_point (cursor, point, 1);
881 Editor::cursor_to_previous_region_point (EditorCursor* cursor, RegionPoint point)
883 cursor_to_region_point (cursor, point, -1);
887 Editor::cursor_to_selection_start (EditorCursor *cursor)
891 switch (mouse_mode) {
893 if (!selection->regions.empty()) {
894 pos = selection->regions.start();
899 if (!selection->time.empty()) {
900 pos = selection->time.start ();
908 if (cursor == playhead_cursor) {
909 _session->request_locate (pos);
911 cursor->set_position (pos);
916 Editor::cursor_to_selection_end (EditorCursor *cursor)
920 switch (mouse_mode) {
922 if (!selection->regions.empty()) {
923 pos = selection->regions.end_frame();
928 if (!selection->time.empty()) {
929 pos = selection->time.end_frame ();
937 if (cursor == playhead_cursor) {
938 _session->request_locate (pos);
940 cursor->set_position (pos);
945 Editor::selected_marker_to_region_boundary (bool with_selection, int32_t dir)
955 if (selection->markers.empty()) {
959 if (!mouse_frame (mouse, ignored)) {
963 add_location_mark (mouse);
966 if ((loc = find_location_from_marker (selection->markers.front(), ignored)) == 0) {
970 framepos_t pos = loc->start();
972 // so we don't find the current region again..
973 if (dir > 0 || pos > 0) {
977 if ((target = get_region_boundary (pos, dir, with_selection, false)) < 0) {
981 loc->move_to (target);
985 Editor::selected_marker_to_next_region_boundary (bool with_selection)
987 selected_marker_to_region_boundary (with_selection, 1);
991 Editor::selected_marker_to_previous_region_boundary (bool with_selection)
993 selected_marker_to_region_boundary (with_selection, -1);
997 Editor::selected_marker_to_region_point (RegionPoint point, int32_t dir)
999 boost::shared_ptr<Region> r;
1004 if (!_session || selection->markers.empty()) {
1008 if ((loc = find_location_from_marker (selection->markers.front(), ignored)) == 0) {
1012 TimeAxisView *ontrack = 0;
1016 // so we don't find the current region again..
1020 if (!selection->tracks.empty()) {
1022 r = find_next_region (pos, point, dir, selection->tracks, &ontrack);
1026 r = find_next_region (pos, point, dir, track_views, &ontrack);
1035 pos = r->first_frame ();
1039 pos = r->last_frame ();
1043 pos = r->adjust_to_sync (r->first_frame());
1048 RouteTimeAxisView *rtav;
1050 if (ontrack != 0 && (rtav = dynamic_cast<RouteTimeAxisView*>(ontrack)) != 0) {
1051 if (rtav->track() != 0) {
1052 speed = rtav->track()->speed();
1056 pos = track_frame_to_session_frame(pos, speed);
1062 Editor::selected_marker_to_next_region_point (RegionPoint point)
1064 selected_marker_to_region_point (point, 1);
1068 Editor::selected_marker_to_previous_region_point (RegionPoint point)
1070 selected_marker_to_region_point (point, -1);
1074 Editor::selected_marker_to_selection_start ()
1080 if (!_session || selection->markers.empty()) {
1084 if ((loc = find_location_from_marker (selection->markers.front(), ignored)) == 0) {
1088 switch (mouse_mode) {
1090 if (!selection->regions.empty()) {
1091 pos = selection->regions.start();
1096 if (!selection->time.empty()) {
1097 pos = selection->time.start ();
1109 Editor::selected_marker_to_selection_end ()
1115 if (!_session || selection->markers.empty()) {
1119 if ((loc = find_location_from_marker (selection->markers.front(), ignored)) == 0) {
1123 switch (mouse_mode) {
1125 if (!selection->regions.empty()) {
1126 pos = selection->regions.end_frame();
1131 if (!selection->time.empty()) {
1132 pos = selection->time.end_frame ();
1144 Editor::scroll_playhead (bool forward)
1146 framepos_t pos = playhead_cursor->current_frame;
1147 framecnt_t delta = (framecnt_t) floor (current_page_frames() / 0.8);
1150 if (pos == max_framepos) {
1154 if (pos < max_framepos - delta) {
1173 _session->request_locate (pos);
1177 Editor::cursor_align (bool playhead_to_edit)
1183 if (playhead_to_edit) {
1185 if (selection->markers.empty()) {
1189 _session->request_locate (selection->markers.front()->position(), _session->transport_rolling());
1192 /* move selected markers to playhead */
1194 for (MarkerSelection::iterator i = selection->markers.begin(); i != selection->markers.end(); ++i) {
1197 Location* loc = find_location_from_marker (*i, ignored);
1199 if (loc->is_mark()) {
1200 loc->set_start (playhead_cursor->current_frame);
1202 loc->set (playhead_cursor->current_frame,
1203 playhead_cursor->current_frame + loc->length());
1210 Editor::scroll_backward (float pages)
1212 framepos_t const one_page = (framepos_t) rint (_canvas_width * frames_per_unit);
1213 framepos_t const cnt = (framepos_t) floor (pages * one_page);
1216 if (leftmost_frame < cnt) {
1219 frame = leftmost_frame - cnt;
1222 reset_x_origin (frame);
1226 Editor::scroll_forward (float pages)
1228 framepos_t const one_page = (framepos_t) rint (_canvas_width * frames_per_unit);
1229 framepos_t const cnt = (framepos_t) floor (pages * one_page);
1232 if (max_framepos - cnt < leftmost_frame) {
1233 frame = max_framepos - cnt;
1235 frame = leftmost_frame + cnt;
1238 reset_x_origin (frame);
1242 Editor::scroll_tracks_down ()
1244 double vert_value = vertical_adjustment.get_value() + vertical_adjustment.get_page_size();
1245 if (vert_value > vertical_adjustment.get_upper() - _canvas_height) {
1246 vert_value = vertical_adjustment.get_upper() - _canvas_height;
1249 vertical_adjustment.set_value (vert_value);
1253 Editor::scroll_tracks_up ()
1255 vertical_adjustment.set_value (vertical_adjustment.get_value() - vertical_adjustment.get_page_size());
1259 Editor::scroll_tracks_down_line ()
1261 double vert_value = vertical_adjustment.get_value() + 60;
1263 if (vert_value > vertical_adjustment.get_upper() - _canvas_height) {
1264 vert_value = vertical_adjustment.get_upper() - _canvas_height;
1267 vertical_adjustment.set_value (vert_value);
1271 Editor::scroll_tracks_up_line ()
1273 reset_y_origin (vertical_adjustment.get_value() - 60);
1279 Editor::tav_zoom_step (bool coarser)
1281 ENSURE_GUI_THREAD (*this, &Editor::temporal_zoom_step, coarser)
1283 _routes->suspend_redisplay ();
1285 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
1286 TimeAxisView *tv = (static_cast<TimeAxisView*>(*i));
1287 tv->step_height (coarser);
1290 _routes->resume_redisplay ();
1294 Editor::temporal_zoom_step (bool coarser)
1296 ENSURE_GUI_THREAD (*this, &Editor::temporal_zoom_step, coarser)
1300 nfpu = frames_per_unit;
1305 nfpu = max(1.0,(nfpu/1.61803399));
1308 temporal_zoom (nfpu);
1312 Editor::temporal_zoom (gdouble fpu)
1314 if (!_session) return;
1316 framepos_t current_page = current_page_frames();
1317 framepos_t current_leftmost = leftmost_frame;
1318 framepos_t current_rightmost;
1319 framepos_t current_center;
1320 framepos_t new_page_size;
1321 framepos_t half_page_size;
1322 framepos_t leftmost_after_zoom = 0;
1324 bool in_track_canvas;
1328 /* XXX this limit is also in ::set_frames_per_unit() */
1330 if (frames_per_unit <= 1.0 && fpu <= frames_per_unit) {
1336 new_page_size = (framepos_t) floor (_canvas_width * nfpu);
1337 half_page_size = new_page_size / 2;
1339 switch (zoom_focus) {
1341 leftmost_after_zoom = current_leftmost;
1344 case ZoomFocusRight:
1345 current_rightmost = leftmost_frame + current_page;
1346 if (current_rightmost < new_page_size) {
1347 leftmost_after_zoom = 0;
1349 leftmost_after_zoom = current_rightmost - new_page_size;
1353 case ZoomFocusCenter:
1354 current_center = current_leftmost + (current_page/2);
1355 if (current_center < half_page_size) {
1356 leftmost_after_zoom = 0;
1358 leftmost_after_zoom = current_center - half_page_size;
1362 case ZoomFocusPlayhead:
1363 /* centre playhead */
1364 l = playhead_cursor->current_frame - (new_page_size * 0.5);
1367 leftmost_after_zoom = 0;
1368 } else if (l > max_framepos) {
1369 leftmost_after_zoom = max_framepos - new_page_size;
1371 leftmost_after_zoom = (framepos_t) l;
1375 case ZoomFocusMouse:
1376 /* try to keep the mouse over the same point in the display */
1378 if (!mouse_frame (where, in_track_canvas)) {
1379 /* use playhead instead */
1380 where = playhead_cursor->current_frame;
1382 if (where < half_page_size) {
1383 leftmost_after_zoom = 0;
1385 leftmost_after_zoom = where - half_page_size;
1390 l = - ((new_page_size * ((where - current_leftmost)/(double)current_page)) - where);
1393 leftmost_after_zoom = 0;
1394 } else if (l > max_framepos) {
1395 leftmost_after_zoom = max_framepos - new_page_size;
1397 leftmost_after_zoom = (framepos_t) l;
1404 /* try to keep the edit point in the same place */
1405 where = get_preferred_edit_position ();
1409 double l = - ((new_page_size * ((where - current_leftmost)/(double)current_page)) - where);
1412 leftmost_after_zoom = 0;
1413 } else if (l > max_framepos) {
1414 leftmost_after_zoom = max_framepos - new_page_size;
1416 leftmost_after_zoom = (framepos_t) l;
1420 /* edit point not defined */
1427 // leftmost_after_zoom = min (leftmost_after_zoom, _session->current_end_frame());
1429 reposition_and_zoom (leftmost_after_zoom, nfpu);
1433 Editor::temporal_zoom_region (bool both_axes)
1435 framepos_t start = max_framepos;
1437 set<TimeAxisView*> tracks;
1439 RegionSelection rs = get_regions_from_selection_and_entered ();
1445 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
1447 if ((*i)->region()->position() < start) {
1448 start = (*i)->region()->position();
1451 if ((*i)->region()->last_frame() + 1 > end) {
1452 end = (*i)->region()->last_frame() + 1;
1455 tracks.insert (&((*i)->get_time_axis_view()));
1458 /* now comes an "interesting" hack ... make sure we leave a little space
1459 at each end of the editor so that the zoom doesn't fit the region
1460 precisely to the screen.
1463 GdkScreen* screen = gdk_screen_get_default ();
1464 gint pixwidth = gdk_screen_get_width (screen);
1465 gint mmwidth = gdk_screen_get_width_mm (screen);
1466 double pix_per_mm = (double) pixwidth/ (double) mmwidth;
1467 double one_centimeter_in_pixels = pix_per_mm * 10.0;
1469 if ((start == 0 && end == 0) || end < start) {
1473 framepos_t range = end - start;
1474 double new_fpu = (double)range / (double)_canvas_width;
1475 framepos_t extra_samples = (framepos_t) floor (one_centimeter_in_pixels * new_fpu);
1477 if (start > extra_samples) {
1478 start -= extra_samples;
1483 if (max_framepos - extra_samples > end) {
1484 end += extra_samples;
1489 /* if we're zooming on both axes we need to save track heights etc.
1492 undo_visual_stack.push_back (current_visual_state (both_axes));
1494 PBD::Unwinder<bool> nsv (no_save_visual, true);
1496 temporal_zoom_by_frame (start, end, "zoom to region");
1499 uint32_t per_track_height = (uint32_t) floor ((_canvas_height - canvas_timebars_vsize - 10.0) / tracks.size());
1501 /* set visible track heights appropriately */
1503 for (set<TimeAxisView*>::iterator t = tracks.begin(); t != tracks.end(); ++t) {
1504 (*t)->set_height (per_track_height);
1507 /* hide irrelevant tracks */
1509 _routes->suspend_redisplay ();
1511 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
1512 if (find (tracks.begin(), tracks.end(), (*i)) == tracks.end()) {
1513 hide_track_in_display (*i);
1517 _routes->resume_redisplay ();
1519 vertical_adjustment.set_value (0.0);
1522 redo_visual_stack.push_back (current_visual_state (both_axes));
1526 Editor::zoom_to_region (bool both_axes)
1528 temporal_zoom_region (both_axes);
1532 Editor::temporal_zoom_selection ()
1534 if (!selection) return;
1536 if (selection->time.empty()) {
1540 framepos_t start = selection->time[clicked_selection].start;
1541 framepos_t end = selection->time[clicked_selection].end;
1543 temporal_zoom_by_frame (start, end, "zoom to selection");
1547 Editor::temporal_zoom_session ()
1549 ENSURE_GUI_THREAD (*this, &Editor::temporal_zoom_session)
1552 framecnt_t const l = _session->current_end_frame() - _session->current_start_frame();
1553 double s = _session->current_start_frame() - l * 0.01;
1557 framecnt_t const e = _session->current_end_frame() + l * 0.01;
1558 temporal_zoom_by_frame (framecnt_t (s), e, "zoom to _session");
1563 Editor::temporal_zoom_by_frame (framepos_t start, framepos_t end, const string & /*op*/)
1565 if (!_session) return;
1567 if ((start == 0 && end == 0) || end < start) {
1571 framepos_t range = end - start;
1573 double new_fpu = (double)range / (double)_canvas_width;
1575 framepos_t new_page = (framepos_t) floor (_canvas_width * new_fpu);
1576 framepos_t middle = (framepos_t) floor( (double)start + ((double)range / 2.0f ));
1577 framepos_t new_leftmost = (framepos_t) floor( (double)middle - ((double)new_page/2.0f));
1579 if (new_leftmost > middle) {
1583 if (new_leftmost < 0) {
1587 reposition_and_zoom (new_leftmost, new_fpu);
1591 Editor::temporal_zoom_to_frame (bool coarser, framepos_t frame)
1596 double range_before = frame - leftmost_frame;
1599 new_fpu = frames_per_unit;
1602 new_fpu *= 1.61803399;
1603 range_before *= 1.61803399;
1605 new_fpu = max(1.0,(new_fpu/1.61803399));
1606 range_before /= 1.61803399;
1609 if (new_fpu == frames_per_unit) {
1613 framepos_t new_leftmost = frame - (framepos_t)range_before;
1615 if (new_leftmost > frame) {
1619 if (new_leftmost < 0) {
1623 reposition_and_zoom (new_leftmost, new_fpu);
1628 Editor::choose_new_marker_name(string &name) {
1630 if (!Config->get_name_new_markers()) {
1631 /* don't prompt user for a new name */
1635 ArdourPrompter dialog (true);
1637 dialog.set_prompt (_("New Name:"));
1639 dialog.set_title (_("New Location Marker"));
1641 dialog.set_name ("MarkNameWindow");
1642 dialog.set_size_request (250, -1);
1643 dialog.set_position (Gtk::WIN_POS_MOUSE);
1645 dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
1646 dialog.set_initial_text (name);
1650 switch (dialog.run ()) {
1651 case RESPONSE_ACCEPT:
1657 dialog.get_result(name);
1664 Editor::add_location_from_selection ()
1668 if (selection->time.empty()) {
1672 if (_session == 0 || clicked_axisview == 0) {
1676 framepos_t start = selection->time[clicked_selection].start;
1677 framepos_t end = selection->time[clicked_selection].end;
1679 _session->locations()->next_available_name(rangename,"selection");
1680 Location *location = new Location (*_session, start, end, rangename, Location::IsRangeMarker);
1682 _session->begin_reversible_command (_("add marker"));
1683 XMLNode &before = _session->locations()->get_state();
1684 _session->locations()->add (location, true);
1685 XMLNode &after = _session->locations()->get_state();
1686 _session->add_command(new MementoCommand<Locations>(*(_session->locations()), &before, &after));
1687 _session->commit_reversible_command ();
1691 Editor::add_location_mark (framepos_t where)
1695 select_new_marker = true;
1697 _session->locations()->next_available_name(markername,"mark");
1698 if (!choose_new_marker_name(markername)) {
1701 Location *location = new Location (*_session, where, where, markername, Location::IsMark);
1702 _session->begin_reversible_command (_("add marker"));
1703 XMLNode &before = _session->locations()->get_state();
1704 _session->locations()->add (location, true);
1705 XMLNode &after = _session->locations()->get_state();
1706 _session->add_command(new MementoCommand<Locations>(*(_session->locations()), &before, &after));
1707 _session->commit_reversible_command ();
1711 Editor::add_location_from_playhead_cursor ()
1713 add_location_mark (_session->audible_frame());
1716 /** Add a range marker around each selected region */
1718 Editor::add_locations_from_region ()
1720 RegionSelection rs = get_regions_from_selection_and_entered ();
1726 _session->begin_reversible_command (selection->regions.size () > 1 ? _("add markers") : _("add marker"));
1727 XMLNode &before = _session->locations()->get_state();
1729 for (RegionSelection::iterator i = rs.begin (); i != rs.end (); ++i) {
1731 boost::shared_ptr<Region> region = (*i)->region ();
1733 Location *location = new Location (*_session, region->position(), region->last_frame(), region->name(), Location::IsRangeMarker);
1735 _session->locations()->add (location, true);
1738 XMLNode &after = _session->locations()->get_state();
1739 _session->add_command (new MementoCommand<Locations>(*(_session->locations()), &before, &after));
1740 _session->commit_reversible_command ();
1743 /** Add a single range marker around all selected regions */
1745 Editor::add_location_from_region ()
1747 RegionSelection rs = get_regions_from_selection_and_entered ();
1753 _session->begin_reversible_command (_("add marker"));
1754 XMLNode &before = _session->locations()->get_state();
1758 if (rs.size() > 1) {
1759 _session->locations()->next_available_name(markername, "regions");
1761 RegionView* rv = *(rs.begin());
1762 boost::shared_ptr<Region> region = rv->region();
1763 markername = region->name();
1766 if (!choose_new_marker_name(markername)) {
1770 // single range spanning all selected
1771 Location *location = new Location (*_session, selection->regions.start(), selection->regions.end_frame(), markername, Location::IsRangeMarker);
1772 _session->locations()->add (location, true);
1774 XMLNode &after = _session->locations()->get_state();
1775 _session->add_command (new MementoCommand<Locations>(*(_session->locations()), &before, &after));
1776 _session->commit_reversible_command ();
1782 Editor::jump_forward_to_mark ()
1788 Location *location = _session->locations()->first_location_after (playhead_cursor->current_frame);
1791 _session->request_locate (location->start(), _session->transport_rolling());
1793 _session->request_locate (_session->current_end_frame());
1798 Editor::jump_backward_to_mark ()
1804 Location *location = _session->locations()->first_location_before (playhead_cursor->current_frame);
1807 _session->request_locate (location->start(), _session->transport_rolling());
1809 _session->goto_start ();
1816 framepos_t const pos = _session->audible_frame ();
1819 _session->locations()->next_available_name (markername, "mark");
1821 if (!choose_new_marker_name (markername)) {
1825 _session->locations()->add (new Location (*_session, pos, 0, markername, Location::IsMark), true);
1829 Editor::clear_markers ()
1832 _session->begin_reversible_command (_("clear markers"));
1833 XMLNode &before = _session->locations()->get_state();
1834 _session->locations()->clear_markers ();
1835 XMLNode &after = _session->locations()->get_state();
1836 _session->add_command(new MementoCommand<Locations>(*(_session->locations()), &before, &after));
1837 _session->commit_reversible_command ();
1842 Editor::clear_ranges ()
1845 _session->begin_reversible_command (_("clear ranges"));
1846 XMLNode &before = _session->locations()->get_state();
1848 Location * looploc = _session->locations()->auto_loop_location();
1849 Location * punchloc = _session->locations()->auto_punch_location();
1850 Location * sessionloc = _session->locations()->session_range_location();
1852 _session->locations()->clear_ranges ();
1854 if (looploc) _session->locations()->add (looploc);
1855 if (punchloc) _session->locations()->add (punchloc);
1856 if (sessionloc) _session->locations()->add (sessionloc);
1858 XMLNode &after = _session->locations()->get_state();
1859 _session->add_command(new MementoCommand<Locations>(*(_session->locations()), &before, &after));
1860 _session->commit_reversible_command ();
1865 Editor::clear_locations ()
1867 _session->begin_reversible_command (_("clear locations"));
1868 XMLNode &before = _session->locations()->get_state();
1869 _session->locations()->clear ();
1870 XMLNode &after = _session->locations()->get_state();
1871 _session->add_command(new MementoCommand<Locations>(*(_session->locations()), &before, &after));
1872 _session->commit_reversible_command ();
1873 _session->locations()->clear ();
1877 Editor::unhide_markers ()
1879 for (LocationMarkerMap::iterator i = location_markers.begin(); i != location_markers.end(); ++i) {
1880 Location *l = (*i).first;
1881 if (l->is_hidden() && l->is_mark()) {
1882 l->set_hidden(false, this);
1888 Editor::unhide_ranges ()
1890 for (LocationMarkerMap::iterator i = location_markers.begin(); i != location_markers.end(); ++i) {
1891 Location *l = (*i).first;
1892 if (l->is_hidden() && l->is_range_marker()) {
1893 l->set_hidden(false, this);
1898 /* INSERT/REPLACE */
1901 Editor::insert_region_list_drag (boost::shared_ptr<Region> region, int x, int y)
1906 RouteTimeAxisView *rtv = 0;
1907 boost::shared_ptr<Playlist> playlist;
1909 track_canvas->window_to_world (x, y, wx, wy);
1912 event.type = GDK_BUTTON_RELEASE;
1913 event.button.x = wx;
1914 event.button.y = wy;
1916 where = event_frame (&event, &cx, &cy);
1918 if (where < leftmost_frame || where > leftmost_frame + current_page_frames()) {
1919 /* clearly outside canvas area */
1923 std::pair<TimeAxisView*, int> tv = trackview_by_y_position (cy);
1924 if (tv.first == 0) {
1928 if ((rtv = dynamic_cast<RouteTimeAxisView*> (tv.first)) == 0) {
1932 if ((playlist = rtv->playlist()) == 0) {
1938 begin_reversible_command (_("insert dragged region"));
1939 playlist->clear_changes ();
1940 playlist->add_region (RegionFactory::create (region, true), where, 1.0);
1941 _session->add_command(new StatefulDiffCommand (playlist));
1942 commit_reversible_command ();
1946 Editor::insert_route_list_drag (boost::shared_ptr<Route> route, int x, int y)
1950 RouteTimeAxisView *dest_rtv = 0;
1951 RouteTimeAxisView *source_rtv = 0;
1953 track_canvas->window_to_world (x, y, wx, wy);
1954 wx += horizontal_position ();
1955 wy += vertical_adjustment.get_value();
1958 event.type = GDK_BUTTON_RELEASE;
1959 event.button.x = wx;
1960 event.button.y = wy;
1962 event_frame (&event, &cx, &cy);
1964 std::pair<TimeAxisView*, int> const tv = trackview_by_y_position (cy);
1965 if (tv.first == 0) {
1969 if ((dest_rtv = dynamic_cast<RouteTimeAxisView*> (tv.first)) == 0) {
1973 /* use this drag source to add underlay to a track. But we really don't care
1974 about the Route, only the view of the route, so find it first */
1975 for(TrackViewList::iterator it = track_views.begin(); it != track_views.end(); ++it) {
1976 if((source_rtv = dynamic_cast<RouteTimeAxisView*>(*it)) == 0) {
1980 if(source_rtv->route() == route && source_rtv != dest_rtv) {
1981 dest_rtv->add_underlay(source_rtv->view());
1988 Editor::insert_region_list_selection (float times)
1990 RouteTimeAxisView *tv = 0;
1991 boost::shared_ptr<Playlist> playlist;
1993 if (clicked_routeview != 0) {
1994 tv = clicked_routeview;
1995 } else if (!selection->tracks.empty()) {
1996 if ((tv = dynamic_cast<RouteTimeAxisView*>(selection->tracks.front())) == 0) {
1999 } else if (entered_track != 0) {
2000 if ((tv = dynamic_cast<RouteTimeAxisView*>(entered_track)) == 0) {
2007 if ((playlist = tv->playlist()) == 0) {
2011 boost::shared_ptr<Region> region = _regions->get_single_selection ();
2016 begin_reversible_command (_("insert region"));
2017 playlist->clear_changes ();
2018 playlist->add_region ((RegionFactory::create (region, true)), get_preferred_edit_position(), times);
2019 _session->add_command(new StatefulDiffCommand (playlist));
2020 commit_reversible_command ();
2023 /* BUILT-IN EFFECTS */
2026 Editor::reverse_selection ()
2031 /* GAIN ENVELOPE EDITING */
2034 Editor::edit_envelope ()
2041 Editor::transition_to_rolling (bool fwd)
2047 if (_session->config.get_external_sync()) {
2048 switch (_session->config.get_sync_source()) {
2052 /* transport controlled by the master */
2057 if (_session->is_auditioning()) {
2058 _session->cancel_audition ();
2062 _session->request_transport_speed (fwd ? 1.0f : -1.0f);
2066 Editor::play_from_start ()
2068 _session->request_locate (_session->current_start_frame(), true);
2072 Editor::play_from_edit_point ()
2074 _session->request_locate (get_preferred_edit_position(), true);
2078 Editor::play_from_edit_point_and_return ()
2080 framepos_t start_frame;
2081 framepos_t return_frame;
2083 start_frame = get_preferred_edit_position (true);
2085 if (_session->transport_rolling()) {
2086 _session->request_locate (start_frame, false);
2090 /* don't reset the return frame if its already set */
2092 if ((return_frame = _session->requested_return_frame()) < 0) {
2093 return_frame = _session->audible_frame();
2096 if (start_frame >= 0) {
2097 _session->request_roll_at_and_return (start_frame, return_frame);
2102 Editor::play_selection ()
2104 if (selection->time.empty()) {
2108 _session->request_play_range (&selection->time, true);
2112 Editor::play_location (Location& location)
2114 if (location.start() <= location.end()) {
2118 _session->request_bounded_roll (location.start(), location.end());
2122 Editor::loop_location (Location& location)
2124 if (location.start() <= location.end()) {
2130 if ((tll = transport_loop_location()) != 0) {
2131 tll->set (location.start(), location.end());
2133 // enable looping, reposition and start rolling
2134 _session->request_play_loop (true);
2135 _session->request_locate (tll->start(), true);
2140 Editor::do_layer_operation (LayerOperation op)
2142 if (selection->regions.empty ()) {
2146 bool const multiple = selection->regions.size() > 1;
2150 begin_reversible_command (_("raise regions"));
2152 begin_reversible_command (_("raise region"));
2158 begin_reversible_command (_("raise regions to top"));
2160 begin_reversible_command (_("raise region to top"));
2166 begin_reversible_command (_("lower regions"));
2168 begin_reversible_command (_("lower region"));
2174 begin_reversible_command (_("lower regions to bottom"));
2176 begin_reversible_command (_("lower region"));
2181 set<boost::shared_ptr<Playlist> > playlists = selection->regions.playlists ();
2182 for (set<boost::shared_ptr<Playlist> >::iterator i = playlists.begin(); i != playlists.end(); ++i) {
2183 (*i)->clear_owned_changes ();
2186 for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
2187 boost::shared_ptr<Region> r = (*i)->region ();
2199 r->lower_to_bottom ();
2203 for (set<boost::shared_ptr<Playlist> >::iterator i = playlists.begin(); i != playlists.end(); ++i) {
2204 vector<Command*> cmds;
2206 _session->add_commands (cmds);
2209 commit_reversible_command ();
2213 Editor::raise_region ()
2215 do_layer_operation (Raise);
2219 Editor::raise_region_to_top ()
2221 do_layer_operation (RaiseToTop);
2225 Editor::lower_region ()
2227 do_layer_operation (Lower);
2231 Editor::lower_region_to_bottom ()
2233 do_layer_operation (LowerToBottom);
2236 /** Show the region editor for the selected regions */
2238 Editor::show_region_properties ()
2240 selection->foreach_regionview (&RegionView::show_region_editor);
2243 /** Show the midi list editor for the selected MIDI regions */
2245 Editor::show_midi_list_editor ()
2247 selection->foreach_midi_regionview (&MidiRegionView::show_list_editor);
2251 Editor::rename_region ()
2253 RegionSelection rs = get_regions_from_selection_and_entered ();
2259 ArdourDialog d (*this, _("Rename Region"), true, false);
2261 Label label (_("New name:"));
2264 hbox.set_spacing (6);
2265 hbox.pack_start (label, false, false);
2266 hbox.pack_start (entry, true, true);
2268 d.get_vbox()->set_border_width (12);
2269 d.get_vbox()->pack_start (hbox, false, false);
2271 d.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
2272 d.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
2274 d.set_size_request (300, -1);
2275 d.set_position (Gtk::WIN_POS_MOUSE);
2277 entry.set_text (rs.front()->region()->name());
2278 entry.select_region (0, -1);
2280 entry.signal_activate().connect (sigc::bind (sigc::mem_fun (d, &Dialog::response), RESPONSE_OK));
2286 int const ret = d.run();
2290 if (ret != RESPONSE_OK) {
2294 std::string str = entry.get_text();
2295 strip_whitespace_edges (str);
2297 rs.front()->region()->set_name (str);
2298 _regions->redisplay ();
2303 Editor::audition_playlist_region_via_route (boost::shared_ptr<Region> region, Route& route)
2305 if (_session->is_auditioning()) {
2306 _session->cancel_audition ();
2309 // note: some potential for creativity here, because region doesn't
2310 // have to belong to the playlist that Route is handling
2312 // bool was_soloed = route.soloed();
2314 route.set_solo (true, this);
2316 _session->request_bounded_roll (region->position(), region->position() + region->length());
2318 /* XXX how to unset the solo state ? */
2321 /** Start an audition of the first selected region */
2323 Editor::play_edit_range ()
2325 framepos_t start, end;
2327 if (get_edit_op_range (start, end)) {
2328 _session->request_bounded_roll (start, end);
2333 Editor::play_selected_region ()
2335 framepos_t start = max_framepos;
2338 RegionSelection rs = get_regions_from_selection_and_entered ();
2344 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
2345 if ((*i)->region()->position() < start) {
2346 start = (*i)->region()->position();
2348 if ((*i)->region()->last_frame() + 1 > end) {
2349 end = (*i)->region()->last_frame() + 1;
2353 _session->request_bounded_roll (start, end);
2357 Editor::audition_playlist_region_standalone (boost::shared_ptr<Region> region)
2359 _session->audition_region (region);
2363 Editor::region_from_selection ()
2365 if (clicked_axisview == 0) {
2369 if (selection->time.empty()) {
2373 framepos_t start = selection->time[clicked_selection].start;
2374 framepos_t end = selection->time[clicked_selection].end;
2376 TrackViewList tracks = get_tracks_for_range_action ();
2378 framepos_t selection_cnt = end - start + 1;
2380 for (TrackSelection::iterator i = tracks.begin(); i != tracks.end(); ++i) {
2381 boost::shared_ptr<Region> current;
2382 boost::shared_ptr<Playlist> pl;
2383 framepos_t internal_start;
2386 if ((pl = (*i)->playlist()) == 0) {
2390 if ((current = pl->top_region_at (start)) == 0) {
2394 internal_start = start - current->position();
2395 RegionFactory::region_name (new_name, current->name(), true);
2399 plist.add (ARDOUR::Properties::start, current->start() + internal_start);
2400 plist.add (ARDOUR::Properties::length, selection_cnt);
2401 plist.add (ARDOUR::Properties::name, new_name);
2402 plist.add (ARDOUR::Properties::layer, 0);
2404 boost::shared_ptr<Region> region (RegionFactory::create (current, plist));
2409 Editor::create_region_from_selection (vector<boost::shared_ptr<Region> >& new_regions)
2411 if (selection->time.empty() || selection->tracks.empty()) {
2415 framepos_t start = selection->time[clicked_selection].start;
2416 framepos_t end = selection->time[clicked_selection].end;
2418 TrackViewList ts = selection->tracks.filter_to_unique_playlists ();
2419 sort_track_selection (ts);
2421 for (TrackSelection::iterator i = ts.begin(); i != ts.end(); ++i) {
2422 boost::shared_ptr<Region> current;
2423 boost::shared_ptr<Playlist> playlist;
2424 framepos_t internal_start;
2427 if ((playlist = (*i)->playlist()) == 0) {
2431 if ((current = playlist->top_region_at(start)) == 0) {
2435 internal_start = start - current->position();
2436 RegionFactory::region_name (new_name, current->name(), true);
2440 plist.add (ARDOUR::Properties::start, current->start() + internal_start);
2441 plist.add (ARDOUR::Properties::length, end - start + 1);
2442 plist.add (ARDOUR::Properties::name, new_name);
2444 new_regions.push_back (RegionFactory::create (current, plist));
2449 Editor::split_multichannel_region ()
2451 RegionSelection rs = get_regions_from_selection_and_entered ();
2457 vector< boost::shared_ptr<Region> > v;
2459 for (list<RegionView*>::iterator x = rs.begin(); x != rs.end(); ++x) {
2460 (*x)->region()->separate_by_channel (*_session, v);
2465 Editor::new_region_from_selection ()
2467 region_from_selection ();
2468 cancel_selection ();
2472 add_if_covered (RegionView* rv, const AudioRange* ar, RegionSelection* rs)
2474 switch (rv->region()->coverage (ar->start, ar->end - 1)) {
2483 * - selected tracks, or if there are none...
2484 * - tracks containing selected regions, or if there are none...
2489 Editor::get_tracks_for_range_action () const
2493 if (selection->tracks.empty()) {
2495 /* use tracks with selected regions */
2497 RegionSelection rs = selection->regions;
2499 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
2500 TimeAxisView* tv = &(*i)->get_time_axis_view();
2502 if (!t.contains (tv)) {
2508 /* no regions and no tracks: use all tracks */
2514 t = selection->tracks;
2517 return t.filter_to_unique_playlists();
2521 Editor::separate_regions_between (const TimeSelection& ts)
2523 bool in_command = false;
2524 boost::shared_ptr<Playlist> playlist;
2525 RegionSelection new_selection;
2527 TrackViewList tmptracks = get_tracks_for_range_action ();
2528 sort_track_selection (tmptracks);
2530 for (TrackSelection::iterator i = tmptracks.begin(); i != tmptracks.end(); ++i) {
2532 RouteTimeAxisView* rtv;
2534 if ((rtv = dynamic_cast<RouteTimeAxisView*> ((*i))) != 0) {
2536 if (rtv->is_track()) {
2538 /* no edits to destructive tracks */
2540 if (rtv->track()->destructive()) {
2544 if ((playlist = rtv->playlist()) != 0) {
2546 playlist->clear_changes ();
2548 /* XXX need to consider musical time selections here at some point */
2550 double speed = rtv->track()->speed();
2553 for (list<AudioRange>::const_iterator t = ts.begin(); t != ts.end(); ++t) {
2555 sigc::connection c = rtv->view()->RegionViewAdded.connect (
2556 sigc::mem_fun(*this, &Editor::collect_new_region_view));
2558 latest_regionviews.clear ();
2560 playlist->partition ((framepos_t)((*t).start * speed),
2561 (framepos_t)((*t).end * speed), false);
2565 if (!latest_regionviews.empty()) {
2567 rtv->view()->foreach_regionview (sigc::bind (
2568 sigc::ptr_fun (add_if_covered),
2569 &(*t), &new_selection));
2572 begin_reversible_command (_("separate"));
2576 /* pick up changes to existing regions */
2578 vector<Command*> cmds;
2579 playlist->rdiff (cmds);
2580 _session->add_commands (cmds);
2582 /* pick up changes to the playlist itself (adds/removes)
2585 _session->add_command(new StatefulDiffCommand (playlist));
2594 selection->set (new_selection);
2595 set_mouse_mode (MouseObject);
2597 commit_reversible_command ();
2601 struct PlaylistState {
2602 boost::shared_ptr<Playlist> playlist;
2606 /** Take tracks from get_tracks_for_range_action and cut any regions
2607 * on those tracks so that the tracks are empty over the time
2611 Editor::separate_region_from_selection ()
2613 /* preferentially use *all* ranges in the time selection if we're in range mode
2614 to allow discontiguous operation, since get_edit_op_range() currently
2615 returns a single range.
2618 if (mouse_mode == MouseRange && !selection->time.empty()) {
2620 separate_regions_between (selection->time);
2627 if (get_edit_op_range (start, end)) {
2629 AudioRange ar (start, end, 1);
2633 separate_regions_between (ts);
2639 Editor::separate_region_from_punch ()
2641 Location* loc = _session->locations()->auto_punch_location();
2643 separate_regions_using_location (*loc);
2648 Editor::separate_region_from_loop ()
2650 Location* loc = _session->locations()->auto_loop_location();
2652 separate_regions_using_location (*loc);
2657 Editor::separate_regions_using_location (Location& loc)
2659 if (loc.is_mark()) {
2663 AudioRange ar (loc.start(), loc.end(), 1);
2668 separate_regions_between (ts);
2671 /** Separate regions under the selected region */
2673 Editor::separate_under_selected_regions ()
2675 vector<PlaylistState> playlists;
2679 rs = get_regions_from_selection_and_entered();
2681 if (!_session || rs.empty()) {
2685 begin_reversible_command (_("separate region under"));
2687 list<boost::shared_ptr<Region> > regions_to_remove;
2689 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
2690 // we can't just remove the region(s) in this loop because
2691 // this removes them from the RegionSelection, and they thus
2692 // disappear from underneath the iterator, and the ++i above
2693 // SEGVs in a puzzling fashion.
2695 // so, first iterate over the regions to be removed from rs and
2696 // add them to the regions_to_remove list, and then
2697 // iterate over the list to actually remove them.
2699 regions_to_remove.push_back ((*i)->region());
2702 for (list<boost::shared_ptr<Region> >::iterator rl = regions_to_remove.begin(); rl != regions_to_remove.end(); ++rl) {
2704 boost::shared_ptr<Playlist> playlist = (*rl)->playlist();
2707 // is this check necessary?
2711 vector<PlaylistState>::iterator i;
2713 //only take state if this is a new playlist.
2714 for (i = playlists.begin(); i != playlists.end(); ++i) {
2715 if ((*i).playlist == playlist) {
2720 if (i == playlists.end()) {
2722 PlaylistState before;
2723 before.playlist = playlist;
2724 before.before = &playlist->get_state();
2726 playlist->freeze ();
2727 playlists.push_back(before);
2730 //Partition on the region bounds
2731 playlist->partition ((*rl)->first_frame() - 1, (*rl)->last_frame() + 1, true);
2733 //Re-add region that was just removed due to the partition operation
2734 playlist->add_region( (*rl), (*rl)->first_frame() );
2737 vector<PlaylistState>::iterator pl;
2739 for (pl = playlists.begin(); pl != playlists.end(); ++pl) {
2740 (*pl).playlist->thaw ();
2741 _session->add_command(new MementoCommand<Playlist>(*(*pl).playlist, (*pl).before, &(*pl).playlist->get_state()));
2744 commit_reversible_command ();
2748 Editor::crop_region_to_selection ()
2750 if (!selection->time.empty()) {
2752 crop_region_to (selection->time.start(), selection->time.end_frame());
2759 if (get_edit_op_range (start, end)) {
2760 crop_region_to (start, end);
2767 Editor::crop_region_to (framepos_t start, framepos_t end)
2769 vector<boost::shared_ptr<Playlist> > playlists;
2770 boost::shared_ptr<Playlist> playlist;
2773 if (selection->tracks.empty()) {
2774 ts = track_views.filter_to_unique_playlists();
2776 ts = selection->tracks.filter_to_unique_playlists ();
2779 sort_track_selection (ts);
2781 for (TrackSelection::iterator i = ts.begin(); i != ts.end(); ++i) {
2783 RouteTimeAxisView* rtv;
2785 if ((rtv = dynamic_cast<RouteTimeAxisView*> ((*i))) != 0) {
2787 boost::shared_ptr<Track> t = rtv->track();
2789 if (t != 0 && ! t->destructive()) {
2791 if ((playlist = rtv->playlist()) != 0) {
2792 playlists.push_back (playlist);
2798 if (playlists.empty()) {
2802 framepos_t the_start;
2806 begin_reversible_command (_("trim to selection"));
2808 for (vector<boost::shared_ptr<Playlist> >::iterator i = playlists.begin(); i != playlists.end(); ++i) {
2810 boost::shared_ptr<Region> region;
2814 if ((region = (*i)->top_region_at(the_start)) == 0) {
2818 /* now adjust lengths to that we do the right thing
2819 if the selection extends beyond the region
2822 the_start = max (the_start, (framepos_t) region->position());
2823 if (max_framepos - the_start < region->length()) {
2824 the_end = the_start + region->length() - 1;
2826 the_end = max_framepos;
2828 the_end = min (end, the_end);
2829 cnt = the_end - the_start + 1;
2831 region->clear_changes ();
2832 region->trim_to (the_start, cnt);
2833 _session->add_command (new StatefulDiffCommand (region));
2836 commit_reversible_command ();
2840 Editor::region_fill_track ()
2842 RegionSelection rs = get_regions_from_selection_and_entered ();
2844 if (!_session || rs.empty()) {
2848 framepos_t const end = _session->current_end_frame ();
2850 begin_reversible_command (Operations::region_fill);
2852 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
2854 boost::shared_ptr<Region> region ((*i)->region());
2856 boost::shared_ptr<Playlist> pl = region->playlist();
2858 if (end <= region->last_frame()) {
2862 double times = (double) (end - region->last_frame()) / (double) region->length();
2868 pl->clear_changes ();
2869 pl->add_region (RegionFactory::create (region, true), region->last_frame(), times);
2870 _session->add_command (new StatefulDiffCommand (pl));
2873 commit_reversible_command ();
2877 Editor::region_fill_selection ()
2879 if (clicked_routeview == 0 || !clicked_routeview->is_audio_track()) {
2883 if (selection->time.empty()) {
2887 boost::shared_ptr<Region> region = _regions->get_single_selection ();
2892 framepos_t start = selection->time[clicked_selection].start;
2893 framepos_t end = selection->time[clicked_selection].end;
2895 boost::shared_ptr<Playlist> playlist;
2897 if (selection->tracks.empty()) {
2901 framepos_t selection_length = end - start;
2902 float times = (float)selection_length / region->length();
2904 begin_reversible_command (Operations::fill_selection);
2906 TrackViewList ts = selection->tracks.filter_to_unique_playlists ();
2908 for (TrackViewList::iterator i = ts.begin(); i != ts.end(); ++i) {
2910 if ((playlist = (*i)->playlist()) == 0) {
2914 playlist->clear_changes ();
2915 playlist->add_region (RegionFactory::create (region, true), start, times);
2916 _session->add_command (new StatefulDiffCommand (playlist));
2919 commit_reversible_command ();
2923 Editor::set_region_sync_position ()
2925 set_sync_point (get_preferred_edit_position (), get_regions_from_selection_and_edit_point ());
2929 Editor::set_sync_point (framepos_t where, const RegionSelection& rs)
2931 bool in_command = false;
2933 for (RegionSelection::const_iterator r = rs.begin(); r != rs.end(); ++r) {
2935 if (!(*r)->region()->covers (where)) {
2939 boost::shared_ptr<Region> region ((*r)->region());
2942 begin_reversible_command (_("set sync point"));
2946 region->clear_changes ();
2947 region->set_sync_position (where);
2948 _session->add_command(new StatefulDiffCommand (region));
2952 commit_reversible_command ();
2956 /** Remove the sync positions of the selection */
2958 Editor::remove_region_sync ()
2960 RegionSelection rs = get_regions_from_selection_and_entered ();
2966 begin_reversible_command (_("remove region sync"));
2968 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
2970 (*i)->region()->clear_changes ();
2971 (*i)->region()->clear_sync_position ();
2972 _session->add_command(new StatefulDiffCommand ((*i)->region()));
2975 commit_reversible_command ();
2979 Editor::naturalize_region ()
2981 RegionSelection rs = get_regions_from_selection_and_entered ();
2987 if (rs.size() > 1) {
2988 begin_reversible_command (_("move regions to original position"));
2990 begin_reversible_command (_("move region to original position"));
2993 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
2994 (*i)->region()->clear_changes ();
2995 (*i)->region()->move_to_natural_position ();
2996 _session->add_command (new StatefulDiffCommand ((*i)->region()));
2999 commit_reversible_command ();
3003 Editor::align_regions (RegionPoint what)
3005 RegionSelection const rs = get_regions_from_selection_and_edit_point ();
3011 begin_reversible_command (_("align selection"));
3013 framepos_t const position = get_preferred_edit_position ();
3015 for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
3016 align_region_internal ((*i)->region(), what, position);
3019 commit_reversible_command ();
3022 struct RegionSortByTime {
3023 bool operator() (const RegionView* a, const RegionView* b) {
3024 return a->region()->position() < b->region()->position();
3029 Editor::align_regions_relative (RegionPoint point)
3031 RegionSelection const rs = get_regions_from_selection_and_edit_point ();
3037 framepos_t const position = get_preferred_edit_position ();
3039 framepos_t distance = 0;
3043 list<RegionView*> sorted;
3044 rs.by_position (sorted);
3046 boost::shared_ptr<Region> r ((*sorted.begin())->region());
3051 if (position > r->position()) {
3052 distance = position - r->position();
3054 distance = r->position() - position;
3060 if (position > r->last_frame()) {
3061 distance = position - r->last_frame();
3062 pos = r->position() + distance;
3064 distance = r->last_frame() - position;
3065 pos = r->position() - distance;
3071 pos = r->adjust_to_sync (position);
3072 if (pos > r->position()) {
3073 distance = pos - r->position();
3075 distance = r->position() - pos;
3081 if (pos == r->position()) {
3085 begin_reversible_command (_("align selection (relative)"));
3087 /* move first one specially */
3089 r->clear_changes ();
3090 r->set_position (pos);
3091 _session->add_command(new StatefulDiffCommand (r));
3093 /* move rest by the same amount */
3097 for (list<RegionView*>::iterator i = sorted.begin(); i != sorted.end(); ++i) {
3099 boost::shared_ptr<Region> region ((*i)->region());
3101 region->clear_changes ();
3104 region->set_position (region->position() + distance);
3106 region->set_position (region->position() - distance);
3109 _session->add_command(new StatefulDiffCommand (region));
3113 commit_reversible_command ();
3117 Editor::align_region (boost::shared_ptr<Region> region, RegionPoint point, framepos_t position)
3119 begin_reversible_command (_("align region"));
3120 align_region_internal (region, point, position);
3121 commit_reversible_command ();
3125 Editor::align_region_internal (boost::shared_ptr<Region> region, RegionPoint point, framepos_t position)
3127 region->clear_changes ();
3131 region->set_position (region->adjust_to_sync (position));
3135 if (position > region->length()) {
3136 region->set_position (position - region->length());
3141 region->set_position (position);
3145 _session->add_command(new StatefulDiffCommand (region));
3149 Editor::trim_region_front ()
3155 Editor::trim_region_back ()
3157 trim_region (false);
3161 Editor::trim_region (bool front)
3163 framepos_t where = get_preferred_edit_position();
3164 RegionSelection rs = get_regions_from_selection_and_edit_point ();
3166 cerr << "trim regions\n";
3169 cerr << " no regions\n";
3173 cerr << "where = " << where << endl;
3175 begin_reversible_command (front ? _("trim front") : _("trim back"));
3177 for (list<RegionView*>::const_iterator i = rs.by_layer().begin(); i != rs.by_layer().end(); ++i) {
3178 if (!(*i)->region()->locked()) {
3180 (*i)->region()->clear_changes ();
3183 (*i)->region()->trim_front (where);
3185 (*i)->region()->trim_end (where);
3188 _session->add_command (new StatefulDiffCommand ((*i)->region()));
3192 commit_reversible_command ();
3195 /** Trim the end of the selected regions to the position of the edit cursor */
3197 Editor::trim_region_to_loop ()
3199 Location* loc = _session->locations()->auto_loop_location();
3203 trim_region_to_location (*loc, _("trim to loop"));
3207 Editor::trim_region_to_punch ()
3209 Location* loc = _session->locations()->auto_punch_location();
3213 trim_region_to_location (*loc, _("trim to punch"));
3217 Editor::trim_region_to_location (const Location& loc, const char* str)
3219 RegionSelection rs = get_regions_from_selection_and_entered ();
3221 begin_reversible_command (str);
3223 for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
3224 RegionView* rv = (*x);
3226 /* require region to span proposed trim */
3227 switch (rv->region()->coverage (loc.start(), loc.end())) {
3228 case OverlapInternal:
3234 RouteTimeAxisView* tav = dynamic_cast<RouteTimeAxisView*> (&rv->get_time_axis_view());
3243 if (tav->track() != 0) {
3244 speed = tav->track()->speed();
3247 start = session_frame_to_track_frame (loc.start(), speed);
3248 end = session_frame_to_track_frame (loc.end(), speed);
3250 rv->region()->clear_changes ();
3251 rv->region()->trim_to (start, (end - start));
3252 _session->add_command(new StatefulDiffCommand (rv->region()));
3255 commit_reversible_command ();
3259 Editor::trim_region_to_previous_region_end ()
3261 return trim_to_region(false);
3265 Editor::trim_region_to_next_region_start ()
3267 return trim_to_region(true);
3271 Editor::trim_to_region(bool forward)
3273 RegionSelection rs = get_regions_from_selection_and_entered ();
3275 begin_reversible_command (_("trim to region"));
3277 boost::shared_ptr<Region> next_region;
3279 for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
3281 AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*x);
3287 AudioTimeAxisView* atav = dynamic_cast<AudioTimeAxisView*> (&arv->get_time_axis_view());
3295 if (atav->track() != 0) {
3296 speed = atav->track()->speed();
3300 boost::shared_ptr<Region> region = arv->region();
3301 boost::shared_ptr<Playlist> playlist (region->playlist());
3303 region->clear_changes ();
3307 next_region = playlist->find_next_region (region->first_frame(), Start, 1);
3313 region->trim_end((framepos_t) ( (next_region->first_frame() - 1) * speed));
3314 arv->region_changed (PropertyChange (ARDOUR::Properties::length));
3318 next_region = playlist->find_next_region (region->first_frame(), Start, 0);
3324 region->trim_front((framepos_t) ((next_region->last_frame() + 1) * speed));
3326 arv->region_changed (ARDOUR::bounds_change);
3329 _session->add_command(new StatefulDiffCommand (region));
3332 commit_reversible_command ();
3336 Editor::unfreeze_route ()
3338 if (clicked_routeview == 0 || !clicked_routeview->is_track()) {
3342 clicked_routeview->track()->unfreeze ();
3346 Editor::_freeze_thread (void* arg)
3348 SessionEvent::create_per_thread_pool ("freeze events", 64);
3350 return static_cast<Editor*>(arg)->freeze_thread ();
3354 Editor::freeze_thread ()
3356 clicked_routeview->audio_track()->freeze_me (*current_interthread_info);
3357 current_interthread_info->done = true;
3362 Editor::freeze_route ()
3368 /* stop transport before we start. this is important */
3370 _session->request_transport_speed (0.0);
3372 if (clicked_routeview == 0 || !clicked_routeview->is_audio_track()) {
3376 if (!clicked_routeview->track()->bounceable()) {
3377 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (clicked_routeview);
3378 if (rtv && !rtv->track()->bounceable()) {
3380 _("This route cannot be frozen because it has more outputs than inputs. "
3381 "You can fix this by increasing the number of inputs.")
3383 d.set_title (_("Cannot freeze"));
3389 InterThreadInfo itt;
3390 current_interthread_info = &itt;
3392 InterthreadProgressWindow ipw (current_interthread_info, _("Freeze"), _("Cancel Freeze"));
3394 pthread_create_and_store (X_("freezer"), &itt.thread, _freeze_thread, this);
3396 set_canvas_cursor (_cursors->wait);
3398 while (!itt.done && !itt.cancel) {
3399 gtk_main_iteration ();
3402 current_interthread_info = 0;
3403 set_canvas_cursor (current_canvas_cursor);
3407 Editor::bounce_range_selection (bool replace, bool enable_processing)
3409 if (selection->time.empty()) {
3413 TrackSelection views = selection->tracks;
3415 for (TrackViewList::iterator i = views.begin(); i != views.end(); ++i) {
3416 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*i);
3417 if (rtv && rtv->track() && replace && enable_processing && !rtv->track()->bounceable()) {
3419 _("You can't perform this operation because the processing of the signal "
3420 "will cause one or more of the tracks will end up with a region with more channels than this track has inputs.\n\n"
3421 "You can do this without processing, which is a different operation.")
3423 d.set_title (_("Cannot bounce"));
3429 framepos_t start = selection->time[clicked_selection].start;
3430 framepos_t end = selection->time[clicked_selection].end;
3431 framepos_t cnt = end - start + 1;
3433 begin_reversible_command (_("bounce range"));
3435 for (TrackViewList::iterator i = views.begin(); i != views.end(); ++i) {
3437 RouteTimeAxisView* rtv;
3439 if ((rtv = dynamic_cast<RouteTimeAxisView*> (*i)) == 0) {
3443 boost::shared_ptr<Playlist> playlist;
3445 if ((playlist = rtv->playlist()) == 0) {
3449 InterThreadInfo itt;
3451 playlist->clear_changes ();
3452 playlist->clear_owned_changes ();
3454 boost::shared_ptr<Region> r = rtv->track()->bounce_range (start, start+cnt, itt, enable_processing);
3461 list<AudioRange> ranges;
3462 ranges.push_back (AudioRange (start, start+cnt, 0));
3463 playlist->cut (ranges); // discard result
3464 playlist->add_region (r, start);
3467 vector<Command*> cmds;
3468 playlist->rdiff (cmds);
3469 _session->add_commands (cmds);
3471 _session->add_command (new StatefulDiffCommand (playlist));
3474 commit_reversible_command ();
3477 /** Delete selected regions, automation points or a time range */
3484 /** Cut selected regions, automation points or a time range */
3491 /** Copy selected regions, automation points or a time range */
3499 /** @return true if a Cut, Copy or Clear is possible */
3501 Editor::can_cut_copy () const
3503 switch (current_mouse_mode()) {
3506 if (!selection->regions.empty() || !selection->points.empty()) {
3512 if (!selection->time.empty()) {
3525 /** Cut, copy or clear selected regions, automation points or a time range.
3526 * @param op Operation (Cut, Copy or Clear)
3529 Editor::cut_copy (CutCopyOp op)
3531 /* only cancel selection if cut/copy is successful.*/
3537 opname = _("delete");
3546 opname = _("clear");
3550 /* if we're deleting something, and the mouse is still pressed,
3551 the thing we started a drag for will be gone when we release
3552 the mouse button(s). avoid this. see part 2 at the end of
3556 if (op == Delete || op == Cut || op == Clear) {
3557 if (_drags->active ()) {
3562 cut_buffer->clear ();
3564 if (entered_marker) {
3566 /* cut/delete op while pointing at a marker */
3569 Location* loc = find_location_from_marker (entered_marker, ignored);
3571 if (_session && loc) {
3572 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::really_remove_marker), loc));
3579 if (internal_editing()) {
3581 switch (current_mouse_mode()) {
3594 /* we only want to cut regions if some are selected */
3596 if (doing_object_stuff()) {
3597 rs = get_regions_from_selection ();
3598 if (!rs.empty() || !selection->points.empty()) {
3600 begin_reversible_command (opname + _(" objects"));
3603 cut_copy_regions (op, rs);
3605 if (op == Cut || op == Delete) {
3606 selection->clear_regions ();
3610 if (!selection->points.empty()) {
3611 cut_copy_points (op);
3613 if (op == Cut || op == Delete) {
3614 selection->clear_points ();
3617 commit_reversible_command ();
3620 if (!selection->time.empty() && (_join_object_range_state == JOIN_OBJECT_RANGE_NONE)) {
3621 /* don't cause suprises */
3626 if (doing_range_stuff()) {
3627 if (selection->time.empty()) {
3628 framepos_t start, end;
3629 if (!get_edit_op_range (start, end)) {
3632 selection->set (start, end);
3635 begin_reversible_command (opname + _(" range"));
3636 cut_copy_ranges (op);
3637 commit_reversible_command ();
3639 if (op == Cut || op == Delete) {
3640 selection->clear_time ();
3646 if (op == Delete || op == Cut || op == Clear) {
3651 /** Cut, copy or clear selected automation points.
3652 * @param op Operation (Cut, Copy or Clear)
3655 Editor::cut_copy_points (CutCopyOp op)
3657 for (PointSelection::iterator i = selection->points.begin(); i != selection->points.end(); ++i) {
3659 AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*>((*i).track);
3660 _last_cut_copy_source_track = atv;
3663 atv->cut_copy_clear_objects (selection->points, op);
3668 /** Cut, copy or clear selected automation points.
3669 * @param op Operation (Cut, Copy or Clear)
3672 Editor::cut_copy_midi (CutCopyOp op)
3674 for (MidiRegionSelection::iterator i = selection->midi_regions.begin(); i != selection->midi_regions.end(); ++i) {
3675 MidiRegionView* mrv = *i;
3676 mrv->cut_copy_clear (op);
3682 struct lt_playlist {
3683 bool operator () (const PlaylistState& a, const PlaylistState& b) {
3684 return a.playlist < b.playlist;
3688 struct PlaylistMapping {
3690 boost::shared_ptr<Playlist> pl;
3692 PlaylistMapping (TimeAxisView* tvp) : tv (tvp) {}
3695 /** Remove `clicked_regionview' */
3697 Editor::remove_clicked_region ()
3699 if (clicked_routeview == 0 || clicked_regionview == 0) {
3703 boost::shared_ptr<Playlist> playlist = clicked_routeview->playlist();
3705 begin_reversible_command (_("remove region"));
3706 playlist->clear_changes ();
3707 playlist->clear_owned_changes ();
3708 playlist->remove_region (clicked_regionview->region());
3710 /* We might have removed regions, which alters other regions' layering_index,
3711 so we need to do a recursive diff here.
3713 vector<Command*> cmds;
3714 playlist->rdiff (cmds);
3715 _session->add_commands (cmds);
3717 _session->add_command(new StatefulDiffCommand (playlist));
3718 commit_reversible_command ();
3722 /** Remove the selected regions */
3724 Editor::remove_selected_regions ()
3726 RegionSelection rs = get_regions_from_selection_and_entered ();
3728 if (!_session || rs.empty()) {
3732 begin_reversible_command (_("remove region"));
3734 list<boost::shared_ptr<Region> > regions_to_remove;
3736 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
3737 // we can't just remove the region(s) in this loop because
3738 // this removes them from the RegionSelection, and they thus
3739 // disappear from underneath the iterator, and the ++i above
3740 // SEGVs in a puzzling fashion.
3742 // so, first iterate over the regions to be removed from rs and
3743 // add them to the regions_to_remove list, and then
3744 // iterate over the list to actually remove them.
3746 regions_to_remove.push_back ((*i)->region());
3749 vector<boost::shared_ptr<Playlist> > playlists;
3751 for (list<boost::shared_ptr<Region> >::iterator rl = regions_to_remove.begin(); rl != regions_to_remove.end(); ++rl) {
3753 boost::shared_ptr<Playlist> playlist = (*rl)->playlist();
3756 // is this check necessary?
3760 /* get_regions_from_selection_and_entered() guarantees that
3761 the playlists involved are unique, so there is no need
3765 playlists.push_back (playlist);
3767 playlist->clear_changes ();
3768 playlist->clear_owned_changes ();
3769 playlist->freeze ();
3770 playlist->remove_region (*rl);
3773 vector<boost::shared_ptr<Playlist> >::iterator pl;
3775 for (pl = playlists.begin(); pl != playlists.end(); ++pl) {
3778 /* We might have removed regions, which alters other regions' layering_index,
3779 so we need to do a recursive diff here.
3781 vector<Command*> cmds;
3782 (*pl)->rdiff (cmds);
3783 _session->add_commands (cmds);
3785 _session->add_command(new StatefulDiffCommand (*pl));
3788 commit_reversible_command ();
3791 /** Cut, copy or clear selected regions.
3792 * @param op Operation (Cut, Copy or Clear)
3795 Editor::cut_copy_regions (CutCopyOp op, RegionSelection& rs)
3797 /* we can't use a std::map here because the ordering is important, and we can't trivially sort
3798 a map when we want ordered access to both elements. i think.
3801 vector<PlaylistMapping> pmap;
3803 framepos_t first_position = max_framepos;
3805 typedef set<boost::shared_ptr<Playlist> > FreezeList;
3806 FreezeList freezelist;
3808 /* get ordering correct before we cut/copy */
3810 rs.sort_by_position_and_track ();
3812 for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
3814 first_position = min ((framepos_t) (*x)->region()->position(), first_position);
3816 if (op == Cut || op == Clear || op == Delete) {
3817 boost::shared_ptr<Playlist> pl = (*x)->region()->playlist();
3820 FreezeList::iterator fl;
3822 // only take state if this is a new playlist.
3823 for (fl = freezelist.begin(); fl != freezelist.end(); ++fl) {
3829 if (fl == freezelist.end()) {
3830 pl->clear_changes();
3831 pl->clear_owned_changes ();
3833 freezelist.insert (pl);
3838 TimeAxisView* tv = &(*x)->get_time_axis_view();
3839 vector<PlaylistMapping>::iterator z;
3841 for (z = pmap.begin(); z != pmap.end(); ++z) {
3842 if ((*z).tv == tv) {
3847 if (z == pmap.end()) {
3848 pmap.push_back (PlaylistMapping (tv));
3852 for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ) {
3854 boost::shared_ptr<Playlist> pl = (*x)->region()->playlist();
3857 /* region not yet associated with a playlist (e.g. unfinished
3864 TimeAxisView& tv = (*x)->get_time_axis_view();
3865 boost::shared_ptr<Playlist> npl;
3866 RegionSelection::iterator tmp;
3873 vector<PlaylistMapping>::iterator z;
3875 for (z = pmap.begin(); z != pmap.end(); ++z) {
3876 if ((*z).tv == &tv) {
3881 assert (z != pmap.end());
3884 npl = PlaylistFactory::create (pl->data_type(), *_session, "cutlist", true);
3892 boost::shared_ptr<Region> r = (*x)->region();
3893 boost::shared_ptr<Region> _xx;
3899 pl->remove_region (r);
3903 _xx = RegionFactory::create (r);
3904 npl->add_region (_xx, r->position() - first_position);
3905 pl->remove_region (r);
3909 /* copy region before adding, so we're not putting same object into two different playlists */
3910 npl->add_region (RegionFactory::create (r), r->position() - first_position);
3914 pl->remove_region (r);
3923 list<boost::shared_ptr<Playlist> > foo;
3925 /* the pmap is in the same order as the tracks in which selected regions occured */
3927 for (vector<PlaylistMapping>::iterator i = pmap.begin(); i != pmap.end(); ++i) {
3930 foo.push_back ((*i).pl);
3935 cut_buffer->set (foo);
3939 _last_cut_copy_source_track = 0;
3941 _last_cut_copy_source_track = pmap.front().tv;
3945 for (FreezeList::iterator pl = freezelist.begin(); pl != freezelist.end(); ++pl) {
3948 /* We might have removed regions, which alters other regions' layering_index,
3949 so we need to do a recursive diff here.
3951 vector<Command*> cmds;
3952 (*pl)->rdiff (cmds);
3953 _session->add_commands (cmds);
3955 _session->add_command (new StatefulDiffCommand (*pl));
3960 Editor::cut_copy_ranges (CutCopyOp op)
3962 TrackViewList ts = selection->tracks.filter_to_unique_playlists ();
3964 /* Sort the track selection now, so that it if is used, the playlists
3965 selected by the calls below to cut_copy_clear are in the order that
3966 their tracks appear in the editor. This makes things like paste
3967 of ranges work properly.
3970 sort_track_selection (ts);
3973 if (!entered_track) {
3976 ts.push_back (entered_track);
3979 for (TrackViewList::iterator i = ts.begin(); i != ts.end(); ++i) {
3980 (*i)->cut_copy_clear (*selection, op);
3985 Editor::paste (float times, bool from_context)
3987 DEBUG_TRACE (DEBUG::CutNPaste, "paste to preferred edit pos\n");
3989 paste_internal (get_preferred_edit_position (false, from_context), times);
3993 Editor::mouse_paste ()
3998 if (!mouse_frame (where, ignored)) {
4003 paste_internal (where, 1);
4007 Editor::paste_internal (framepos_t position, float times)
4009 DEBUG_TRACE (DEBUG::CutNPaste, string_compose ("apparent paste position is %1\n", position));
4011 if (internal_editing()) {
4012 if (cut_buffer->midi_notes.empty()) {
4016 if (cut_buffer->empty()) {
4021 if (position == max_framepos) {
4022 position = get_preferred_edit_position();
4023 DEBUG_TRACE (DEBUG::CutNPaste, string_compose ("preferred edit position is %1\n", position));
4027 TrackViewList::iterator i;
4030 /* get everything in the correct order */
4032 if (!selection->tracks.empty()) {
4033 /* there are some selected tracks, so paste to them */
4034 ts = selection->tracks.filter_to_unique_playlists ();
4035 sort_track_selection (ts);
4036 } else if (_last_cut_copy_source_track) {
4037 /* otherwise paste to the track that the cut/copy came from;
4038 see discussion in mantis #3333.
4040 ts.push_back (_last_cut_copy_source_track);
4043 if (internal_editing ()) {
4045 /* undo/redo is handled by individual tracks/regions */
4047 for (nth = 0, i = ts.begin(); i != ts.end(); ++i, ++nth) {
4050 RegionSelection::iterator r;
4051 MidiNoteSelection::iterator cb;
4053 get_regions_at (rs, position, ts);
4055 for (cb = cut_buffer->midi_notes.begin(), r = rs.begin();
4056 cb != cut_buffer->midi_notes.end() && r != rs.end(); ++r) {
4057 MidiRegionView* mrv = dynamic_cast<MidiRegionView*> (*r);
4059 mrv->paste (position, times, **cb);
4067 /* we do redo (do you do voodoo?) */
4069 begin_reversible_command (Operations::paste);
4071 for (nth = 0, i = ts.begin(); i != ts.end(); ++i, ++nth) {
4072 (*i)->paste (position, times, *cut_buffer, nth);
4075 commit_reversible_command ();
4080 Editor::duplicate_some_regions (RegionSelection& regions, float times)
4082 boost::shared_ptr<Playlist> playlist;
4083 RegionSelection sel = regions; // clear (below) may clear the argument list if its the current region selection
4084 RegionSelection foo;
4086 framepos_t const start_frame = regions.start ();
4087 framepos_t const end_frame = regions.end_frame ();
4089 begin_reversible_command (Operations::duplicate_region);
4091 selection->clear_regions ();
4093 for (RegionSelection::iterator i = sel.begin(); i != sel.end(); ++i) {
4095 boost::shared_ptr<Region> r ((*i)->region());
4097 TimeAxisView& tv = (*i)->get_time_axis_view();
4098 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (&tv);
4099 latest_regionviews.clear ();
4100 sigc::connection c = rtv->view()->RegionViewAdded.connect (sigc::mem_fun(*this, &Editor::collect_new_region_view));
4102 playlist = (*i)->region()->playlist();
4103 playlist->clear_changes ();
4104 playlist->duplicate (r, end_frame + (r->first_frame() - start_frame), times);
4105 _session->add_command(new StatefulDiffCommand (playlist));
4109 foo.insert (foo.end(), latest_regionviews.begin(), latest_regionviews.end());
4112 commit_reversible_command ();
4115 selection->set (foo);
4120 Editor::duplicate_selection (float times)
4122 if (selection->time.empty() || selection->tracks.empty()) {
4126 boost::shared_ptr<Playlist> playlist;
4127 vector<boost::shared_ptr<Region> > new_regions;
4128 vector<boost::shared_ptr<Region> >::iterator ri;
4130 create_region_from_selection (new_regions);
4132 if (new_regions.empty()) {
4136 begin_reversible_command (_("duplicate selection"));
4138 ri = new_regions.begin();
4140 TrackViewList ts = selection->tracks.filter_to_unique_playlists ();
4142 for (TrackViewList::iterator i = ts.begin(); i != ts.end(); ++i) {
4143 if ((playlist = (*i)->playlist()) == 0) {
4146 playlist->clear_changes ();
4147 playlist->duplicate (*ri, selection->time[clicked_selection].end, times);
4148 _session->add_command (new StatefulDiffCommand (playlist));
4151 if (ri == new_regions.end()) {
4156 commit_reversible_command ();
4160 Editor::reset_point_selection ()
4162 /* reset all selected points to the relevant default value */
4164 for (PointSelection::iterator i = selection->points.begin(); i != selection->points.end(); ++i) {
4166 AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*>((*i).track);
4169 atv->reset_objects (selection->points);
4175 Editor::center_playhead ()
4177 float page = _canvas_width * frames_per_unit;
4178 center_screen_internal (playhead_cursor->current_frame, page);
4182 Editor::center_edit_point ()
4184 float page = _canvas_width * frames_per_unit;
4185 center_screen_internal (get_preferred_edit_position(), page);
4188 /** Caller must begin and commit a reversible command */
4190 Editor::clear_playlist (boost::shared_ptr<Playlist> playlist)
4192 playlist->clear_changes ();
4194 _session->add_command (new StatefulDiffCommand (playlist));
4198 Editor::nudge_track (bool use_edit, bool forwards)
4200 boost::shared_ptr<Playlist> playlist;
4201 framepos_t distance;
4202 framepos_t next_distance;
4206 start = get_preferred_edit_position();
4211 if ((distance = get_nudge_distance (start, next_distance)) == 0) {
4215 if (selection->tracks.empty()) {
4219 begin_reversible_command (_("nudge track"));
4221 TrackViewList ts = selection->tracks.filter_to_unique_playlists ();
4223 for (TrackViewList::iterator i = ts.begin(); i != ts.end(); ++i) {
4225 if ((playlist = (*i)->playlist()) == 0) {
4229 playlist->clear_changes ();
4230 playlist->clear_owned_changes ();
4232 playlist->nudge_after (start, distance, forwards);
4234 vector<Command*> cmds;
4236 playlist->rdiff (cmds);
4237 _session->add_commands (cmds);
4239 _session->add_command (new StatefulDiffCommand (playlist));
4242 commit_reversible_command ();
4246 Editor::remove_last_capture ()
4248 vector<string> choices;
4255 if (Config->get_verify_remove_last_capture()) {
4256 prompt = _("Do you really want to destroy the last capture?"
4257 "\n(This is destructive and cannot be undone)");
4259 choices.push_back (_("No, do nothing."));
4260 choices.push_back (_("Yes, destroy it."));
4262 Gtkmm2ext::Choice prompter (_("Destroy last capture"), prompt, choices);
4264 if (prompter.run () == 1) {
4265 _session->remove_last_capture ();
4266 _regions->redisplay ();
4270 _session->remove_last_capture();
4271 _regions->redisplay ();
4276 Editor::normalize_region ()
4282 RegionSelection rs = get_regions_from_selection_and_entered ();
4288 NormalizeDialog dialog (rs.size() > 1);
4290 if (dialog.run () == RESPONSE_CANCEL) {
4294 set_canvas_cursor (_cursors->wait);
4297 /* XXX: should really only count audio regions here */
4298 int const regions = rs.size ();
4300 /* Make a list of the selected audio regions' maximum amplitudes, and also
4301 obtain the maximum amplitude of them all.
4303 list<double> max_amps;
4305 for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
4306 AudioRegionView const * arv = dynamic_cast<AudioRegionView const *> (*i);
4308 dialog.descend (1.0 / regions);
4309 double const a = arv->audio_region()->maximum_amplitude (&dialog);
4312 /* the user cancelled the operation */
4313 set_canvas_cursor (current_canvas_cursor);
4317 max_amps.push_back (a);
4318 max_amp = max (max_amp, a);
4323 begin_reversible_command (_("normalize"));
4325 list<double>::const_iterator a = max_amps.begin ();
4327 for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
4328 AudioRegionView* const arv = dynamic_cast<AudioRegionView*> (*r);
4333 arv->region()->clear_changes ();
4335 double const amp = dialog.normalize_individually() ? *a : max_amp;
4337 arv->audio_region()->normalize (amp, dialog.target ());
4338 _session->add_command (new StatefulDiffCommand (arv->region()));
4343 commit_reversible_command ();
4344 set_canvas_cursor (current_canvas_cursor);
4349 Editor::reset_region_scale_amplitude ()
4355 RegionSelection rs = get_regions_from_selection_and_entered ();
4361 begin_reversible_command ("reset gain");
4363 for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
4364 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*r);
4367 arv->region()->clear_changes ();
4368 arv->audio_region()->set_scale_amplitude (1.0f);
4369 _session->add_command (new StatefulDiffCommand (arv->region()));
4372 commit_reversible_command ();
4376 Editor::adjust_region_gain (bool up)
4378 RegionSelection rs = get_regions_from_selection_and_entered ();
4380 if (!_session || rs.empty()) {
4384 begin_reversible_command ("adjust region gain");
4386 for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
4387 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*r);
4392 arv->region()->clear_changes ();
4394 double dB = accurate_coefficient_to_dB (arv->audio_region()->scale_amplitude ());
4402 arv->audio_region()->set_scale_amplitude (dB_to_coefficient (dB));
4403 _session->add_command (new StatefulDiffCommand (arv->region()));
4406 commit_reversible_command ();
4411 Editor::reverse_region ()
4417 Reverse rev (*_session);
4418 apply_filter (rev, _("reverse regions"));
4422 Editor::strip_region_silence ()
4428 RegionSelection rs = get_regions_from_selection_and_entered ();
4434 std::list<RegionView*> audio_only;
4436 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
4437 AudioRegionView* const arv = dynamic_cast<AudioRegionView*> (*i);
4439 audio_only.push_back (arv);
4443 StripSilenceDialog d (_session, audio_only);
4444 int const r = d.run ();
4448 if (r == Gtk::RESPONSE_OK) {
4449 ARDOUR::AudioIntervalMap silences;
4450 d.silences (silences);
4451 StripSilence s (*_session, silences, d.fade_length());
4452 apply_filter (s, _("strip silence"), &d);
4457 Editor::apply_midi_note_edit_op_to_region (MidiOperator& op, MidiRegionView& mrv)
4459 Evoral::Sequence<Evoral::MusicalTime>::Notes selected;
4460 mrv.selection_as_notelist (selected, true);
4462 vector<Evoral::Sequence<Evoral::MusicalTime>::Notes> v;
4463 v.push_back (selected);
4465 framepos_t pos_frames = mrv.midi_region()->position();
4466 double pos_beats = _session->tempo_map().framewalk_to_beats(0, pos_frames);
4468 return op (mrv.midi_region()->model(), pos_beats, v);
4472 Editor::apply_midi_note_edit_op (MidiOperator& op)
4476 RegionSelection rs = get_regions_from_selection_and_entered ();
4482 begin_reversible_command (op.name ());
4484 for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ) {
4485 RegionSelection::iterator tmp = r;
4488 MidiRegionView* const mrv = dynamic_cast<MidiRegionView*> (*r);
4491 cmd = apply_midi_note_edit_op_to_region (op, *mrv);
4494 _session->add_command (cmd);
4501 commit_reversible_command ();
4505 Editor::fork_region ()
4507 RegionSelection rs = get_regions_from_selection_and_entered ();
4513 begin_reversible_command (_("Fork Region(s)"));
4515 set_canvas_cursor (_cursors->wait);
4518 for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ) {
4519 RegionSelection::iterator tmp = r;
4522 MidiRegionView* const mrv = dynamic_cast<MidiRegionView*>(*r);
4525 boost::shared_ptr<Playlist> playlist = mrv->region()->playlist();
4526 boost::shared_ptr<MidiRegion> newregion = mrv->midi_region()->clone ();
4528 playlist->clear_changes ();
4529 playlist->replace_region (mrv->region(), newregion, mrv->region()->position());
4530 _session->add_command(new StatefulDiffCommand (playlist));
4536 commit_reversible_command ();
4538 set_canvas_cursor (current_canvas_cursor);
4542 Editor::quantize_region ()
4544 int selected_midi_region_cnt = 0;
4550 RegionSelection rs = get_regions_from_selection_and_entered ();
4556 for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
4557 MidiRegionView* const mrv = dynamic_cast<MidiRegionView*> (*r);
4559 selected_midi_region_cnt++;
4563 if (selected_midi_region_cnt == 0) {
4567 QuantizeDialog* qd = new QuantizeDialog (*this);
4570 const int r = qd->run ();
4573 if (r == Gtk::RESPONSE_OK) {
4574 Quantize quant (*_session, Plain,
4575 qd->snap_start(), qd->snap_end(),
4576 qd->start_grid_size(), qd->end_grid_size(),
4577 qd->strength(), qd->swing(), qd->threshold());
4579 apply_midi_note_edit_op (quant);
4584 Editor::insert_patch_change (bool from_context)
4586 RegionSelection rs = get_regions_from_selection_and_entered ();
4592 const framepos_t p = get_preferred_edit_position (false, from_context);
4594 Evoral::PatchChange<Evoral::MusicalTime> empty (0, 0, 0, 0);
4595 PatchChangeDialog d (0, _session, empty, Gtk::Stock::ADD);
4597 if (d.run() == RESPONSE_CANCEL) {
4601 for (RegionSelection::iterator i = rs.begin (); i != rs.end(); ++i) {
4602 MidiRegionView* const mrv = dynamic_cast<MidiRegionView*> (*i);
4604 if (p >= mrv->region()->first_frame() && p <= mrv->region()->last_frame()) {
4605 mrv->add_patch_change (p - mrv->region()->position(), d.patch ());
4612 Editor::apply_filter (Filter& filter, string command, ProgressReporter* progress)
4614 RegionSelection rs = get_regions_from_selection_and_entered ();
4620 begin_reversible_command (command);
4622 set_canvas_cursor (_cursors->wait);
4626 int const N = rs.size ();
4628 for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ) {
4629 RegionSelection::iterator tmp = r;
4632 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*r);
4634 boost::shared_ptr<Playlist> playlist = arv->region()->playlist();
4637 progress->descend (1.0 / N);
4640 if (arv->audio_region()->apply (filter, progress) == 0) {
4642 playlist->clear_changes ();
4643 playlist->clear_owned_changes ();
4645 if (filter.results.empty ()) {
4647 /* no regions returned; remove the old one */
4648 playlist->remove_region (arv->region ());
4652 std::vector<boost::shared_ptr<Region> >::iterator res = filter.results.begin ();
4654 /* first region replaces the old one */
4655 playlist->replace_region (arv->region(), *res, (*res)->position());
4659 while (res != filter.results.end()) {
4660 playlist->add_region (*res, (*res)->position());
4666 /* We might have removed regions, which alters other regions' layering_index,
4667 so we need to do a recursive diff here.
4669 vector<Command*> cmds;
4670 playlist->rdiff (cmds);
4671 _session->add_commands (cmds);
4673 _session->add_command(new StatefulDiffCommand (playlist));
4679 progress->ascend ();
4687 commit_reversible_command ();
4690 set_canvas_cursor (current_canvas_cursor);
4694 Editor::external_edit_region ()
4700 Editor::reset_region_gain_envelopes ()
4702 RegionSelection rs = get_regions_from_selection_and_entered ();
4704 if (!_session || rs.empty()) {
4708 _session->begin_reversible_command (_("reset region gain"));
4710 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
4711 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
4713 boost::shared_ptr<AutomationList> alist (arv->audio_region()->envelope());
4714 XMLNode& before (alist->get_state());
4716 arv->audio_region()->set_default_envelope ();
4717 _session->add_command (new MementoCommand<AutomationList>(*arv->audio_region()->envelope().get(), &before, &alist->get_state()));
4721 _session->commit_reversible_command ();
4725 Editor::set_region_gain_visibility (RegionView* rv, bool yn)
4727 AudioRegionView* arv = dynamic_cast<AudioRegionView*> (rv);
4729 arv->set_envelope_visible (yn);
4734 Editor::set_gain_envelope_visibility (bool yn)
4740 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
4741 AudioTimeAxisView* v = dynamic_cast<AudioTimeAxisView*>(*i);
4743 v->audio_view()->foreach_regionview (sigc::bind (sigc::mem_fun (this, &Editor::set_region_gain_visibility), yn));
4749 Editor::toggle_gain_envelope_active ()
4751 if (_ignore_region_action) {
4755 RegionSelection rs = get_regions_from_selection_and_entered ();
4757 if (!_session || rs.empty()) {
4761 _session->begin_reversible_command (_("region gain envelope active"));
4763 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
4764 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
4766 arv->region()->clear_changes ();
4767 arv->audio_region()->set_envelope_active (!arv->audio_region()->envelope_active());
4768 _session->add_command (new StatefulDiffCommand (arv->region()));
4772 _session->commit_reversible_command ();
4776 Editor::toggle_region_lock ()
4778 if (_ignore_region_action) {
4782 RegionSelection rs = get_regions_from_selection_and_entered ();
4784 if (!_session || rs.empty()) {
4788 _session->begin_reversible_command (_("toggle region lock"));
4790 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
4791 (*i)->region()->clear_changes ();
4792 (*i)->region()->set_locked (!(*i)->region()->locked());
4793 _session->add_command (new StatefulDiffCommand ((*i)->region()));
4796 _session->commit_reversible_command ();
4800 Editor::toggle_region_lock_style ()
4802 if (_ignore_region_action) {
4806 RegionSelection rs = get_regions_from_selection_and_entered ();
4808 if (!_session || rs.empty()) {
4812 _session->begin_reversible_command (_("region lock style"));
4814 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
4815 (*i)->region()->clear_changes ();
4816 PositionLockStyle const ns = (*i)->region()->position_lock_style() == AudioTime ? MusicTime : AudioTime;
4817 (*i)->region()->set_position_lock_style (ns);
4818 _session->add_command (new StatefulDiffCommand ((*i)->region()));
4821 _session->commit_reversible_command ();
4825 Editor::toggle_opaque_region ()
4827 if (_ignore_region_action) {
4831 RegionSelection rs = get_regions_from_selection_and_entered ();
4833 if (!_session || rs.empty()) {
4837 _session->begin_reversible_command (_("change region opacity"));
4839 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
4840 (*i)->region()->clear_changes ();
4841 (*i)->region()->set_opaque (!(*i)->region()->opaque());
4842 _session->add_command (new StatefulDiffCommand ((*i)->region()));
4845 _session->commit_reversible_command ();
4849 Editor::toggle_record_enable ()
4851 bool new_state = false;
4853 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
4854 RouteTimeAxisView *rtav = dynamic_cast<RouteTimeAxisView *>(*i);
4857 if (!rtav->is_track())
4861 new_state = !rtav->track()->record_enabled();
4865 rtav->track()->set_record_enabled (new_state, this);
4870 Editor::toggle_solo ()
4872 bool new_state = false;
4874 boost::shared_ptr<RouteList> rl (new RouteList);
4876 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
4877 RouteTimeAxisView *rtav = dynamic_cast<RouteTimeAxisView *>(*i);
4884 new_state = !rtav->route()->soloed ();
4888 rl->push_back (rtav->route());
4891 _session->set_solo (rl, new_state, Session::rt_cleanup, true);
4895 Editor::toggle_mute ()
4897 bool new_state = false;
4899 boost::shared_ptr<RouteList> rl (new RouteList);
4901 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
4902 RouteTimeAxisView *rtav = dynamic_cast<RouteTimeAxisView *>(*i);
4909 new_state = !rtav->route()->muted();
4913 rl->push_back (rtav->route());
4916 _session->set_mute (rl, new_state, Session::rt_cleanup, true);
4920 Editor::toggle_solo_isolate ()
4925 Editor::set_fade_length (bool in)
4927 RegionSelection rs = get_regions_from_selection_and_entered ();
4933 /* we need a region to measure the offset from the start */
4935 RegionView* rv = rs.front ();
4937 framepos_t pos = get_preferred_edit_position();
4941 if (pos > rv->region()->last_frame() || pos < rv->region()->first_frame()) {
4942 /* edit point is outside the relevant region */
4947 if (pos <= rv->region()->position()) {
4951 len = pos - rv->region()->position();
4952 cmd = _("set fade in length");
4954 if (pos >= rv->region()->last_frame()) {
4958 len = rv->region()->last_frame() - pos;
4959 cmd = _("set fade out length");
4962 begin_reversible_command (cmd);
4964 for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
4965 AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
4971 boost::shared_ptr<AutomationList> alist;
4973 alist = tmp->audio_region()->fade_in();
4975 alist = tmp->audio_region()->fade_out();
4978 XMLNode &before = alist->get_state();
4981 tmp->audio_region()->set_fade_in_length (len);
4982 tmp->audio_region()->set_fade_in_active (true);
4984 tmp->audio_region()->set_fade_out_length (len);
4985 tmp->audio_region()->set_fade_out_active (true);
4988 XMLNode &after = alist->get_state();
4989 _session->add_command(new MementoCommand<AutomationList>(*alist, &before, &after));
4992 commit_reversible_command ();
4996 Editor::set_fade_in_shape (FadeShape shape)
4998 RegionSelection rs = get_regions_from_selection_and_entered ();
5004 begin_reversible_command (_("set fade in shape"));
5006 for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
5007 AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
5013 boost::shared_ptr<AutomationList> alist = tmp->audio_region()->fade_in();
5014 XMLNode &before = alist->get_state();
5016 tmp->audio_region()->set_fade_in_shape (shape);
5018 XMLNode &after = alist->get_state();
5019 _session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &after));
5022 commit_reversible_command ();
5027 Editor::set_fade_out_shape (FadeShape shape)
5029 RegionSelection rs = get_regions_from_selection_and_entered ();
5035 begin_reversible_command (_("set fade out shape"));
5037 for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
5038 AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
5044 boost::shared_ptr<AutomationList> alist = tmp->audio_region()->fade_out();
5045 XMLNode &before = alist->get_state();
5047 tmp->audio_region()->set_fade_out_shape (shape);
5049 XMLNode &after = alist->get_state();
5050 _session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &after));
5053 commit_reversible_command ();
5057 Editor::set_fade_in_active (bool yn)
5059 RegionSelection rs = get_regions_from_selection_and_entered ();
5065 begin_reversible_command (_("set fade in active"));
5067 for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
5068 AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
5075 boost::shared_ptr<AudioRegion> ar (tmp->audio_region());
5077 ar->clear_changes ();
5078 ar->set_fade_in_active (yn);
5079 _session->add_command (new StatefulDiffCommand (ar));
5082 commit_reversible_command ();
5086 Editor::set_fade_out_active (bool yn)
5088 RegionSelection rs = get_regions_from_selection_and_entered ();
5094 begin_reversible_command (_("set fade out active"));
5096 for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
5097 AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
5103 boost::shared_ptr<AudioRegion> ar (tmp->audio_region());
5105 ar->clear_changes ();
5106 ar->set_fade_out_active (yn);
5107 _session->add_command(new StatefulDiffCommand (ar));
5110 commit_reversible_command ();
5114 Editor::toggle_region_fades (int dir)
5116 boost::shared_ptr<AudioRegion> ar;
5119 RegionSelection rs = get_regions_from_selection_and_entered ();
5125 RegionSelection::iterator i;
5126 for (i = rs.begin(); i != rs.end(); ++i) {
5127 if ((ar = boost::dynamic_pointer_cast<AudioRegion>((*i)->region())) != 0) {
5129 yn = ar->fade_out_active ();
5131 yn = ar->fade_in_active ();
5137 if (i == rs.end()) {
5141 /* XXX should this undo-able? */
5143 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
5144 if ((ar = boost::dynamic_pointer_cast<AudioRegion>((*i)->region())) == 0) {
5147 if (dir == 1 || dir == 0) {
5148 ar->set_fade_in_active (!yn);
5151 if (dir == -1 || dir == 0) {
5152 ar->set_fade_out_active (!yn);
5158 /** Update region fade visibility after its configuration has been changed */
5160 Editor::update_region_fade_visibility ()
5162 bool _fade_visibility = _session->config.get_show_region_fades ();
5164 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
5165 AudioTimeAxisView* v = dynamic_cast<AudioTimeAxisView*>(*i);
5167 if (_fade_visibility) {
5168 v->audio_view()->show_all_fades ();
5170 v->audio_view()->hide_all_fades ();
5176 /** Update crossfade visibility after its configuration has been changed */
5178 Editor::update_xfade_visibility ()
5180 _xfade_visibility = _session->config.get_xfades_visible ();
5182 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
5183 AudioTimeAxisView* v = dynamic_cast<AudioTimeAxisView*>(*i);
5185 if (_xfade_visibility) {
5186 v->show_all_xfades ();
5188 v->hide_all_xfades ();
5195 Editor::set_edit_point ()
5200 if (!mouse_frame (where, ignored)) {
5206 if (selection->markers.empty()) {
5208 mouse_add_new_marker (where);
5213 Location* loc = find_location_from_marker (selection->markers.front(), ignored);
5216 loc->move_to (where);
5222 Editor::set_playhead_cursor ()
5224 if (entered_marker) {
5225 _session->request_locate (entered_marker->position(), _session->transport_rolling());
5230 if (!mouse_frame (where, ignored)) {
5237 _session->request_locate (where, _session->transport_rolling());
5243 Editor::split_region ()
5245 if (((mouse_mode == MouseRange) ||
5246 (mouse_mode != MouseObject && _join_object_range_state == JOIN_OBJECT_RANGE_RANGE)) &&
5247 !selection->time.empty()) {
5248 separate_regions_between (selection->time);
5252 RegionSelection rs = get_regions_from_selection_and_edit_point ();
5254 framepos_t where = get_preferred_edit_position ();
5260 split_regions_at (where, rs);
5264 Editor::ensure_entered_track_selected (bool op_really_wants_one_track_if_none_are_selected)
5266 if (entered_track && mouse_mode == MouseObject) {
5267 if (!selection->tracks.empty()) {
5268 if (!selection->selected (entered_track)) {
5269 selection->add (entered_track);
5272 /* there is no selection, but this operation requires/prefers selected objects */
5274 if (op_really_wants_one_track_if_none_are_selected) {
5275 selection->set (entered_track);
5281 struct EditorOrderRouteSorter {
5282 bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
5283 /* use of ">" forces the correct sort order */
5284 return a->order_key ("editor") < b->order_key ("editor");
5289 Editor::select_next_route()
5291 if (selection->tracks.empty()) {
5292 selection->set (track_views.front());
5296 TimeAxisView* current = selection->tracks.front();
5300 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
5301 if (*i == current) {
5303 if (i != track_views.end()) {
5306 current = (*(track_views.begin()));
5307 //selection->set (*(track_views.begin()));
5312 rui = dynamic_cast<RouteUI *>(current);
5313 } while ( current->hidden() || (rui != NULL && !rui->route()->active()));
5315 selection->set(current);
5317 ensure_track_visible(current);
5321 Editor::select_prev_route()
5323 if (selection->tracks.empty()) {
5324 selection->set (track_views.front());
5328 TimeAxisView* current = selection->tracks.front();
5332 for (TrackViewList::reverse_iterator i = track_views.rbegin(); i != track_views.rend(); ++i) {
5333 if (*i == current) {
5335 if (i != track_views.rend()) {
5338 current = *(track_views.rbegin());
5343 rui = dynamic_cast<RouteUI *>(current);
5344 } while ( current->hidden() || (rui != NULL && !rui->route()->active()));
5346 selection->set (current);
5348 ensure_track_visible(current);
5352 Editor::ensure_track_visible(TimeAxisView *track)
5354 if (track->hidden())
5357 double const current_view_min_y = vertical_adjustment.get_value();
5358 double const current_view_max_y = vertical_adjustment.get_value() + vertical_adjustment.get_page_size() - canvas_timebars_vsize;
5360 double const track_min_y = track->y_position ();
5361 double const track_max_y = track->y_position () + track->effective_height ();
5363 if (track_min_y >= current_view_min_y &&
5364 track_max_y <= current_view_max_y) {
5370 if (track_min_y < current_view_min_y) {
5371 // Track is above the current view
5372 new_value = track_min_y;
5374 // Track is below the current view
5375 new_value = track->y_position () + track->effective_height() + canvas_timebars_vsize - vertical_adjustment.get_page_size();
5378 vertical_adjustment.set_value(new_value);
5382 Editor::set_loop_from_selection (bool play)
5384 if (_session == 0 || selection->time.empty()) {
5388 framepos_t start = selection->time[clicked_selection].start;
5389 framepos_t end = selection->time[clicked_selection].end;
5391 set_loop_range (start, end, _("set loop range from selection"));
5394 _session->request_play_loop (true);
5395 _session->request_locate (start, true);
5400 Editor::set_loop_from_edit_range (bool play)
5402 if (_session == 0) {
5409 if (!get_edit_op_range (start, end)) {
5413 set_loop_range (start, end, _("set loop range from edit range"));
5416 _session->request_play_loop (true);
5417 _session->request_locate (start, true);
5422 Editor::set_loop_from_region (bool play)
5424 framepos_t start = max_framepos;
5427 RegionSelection rs = get_regions_from_selection_and_entered ();
5433 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
5434 if ((*i)->region()->position() < start) {
5435 start = (*i)->region()->position();
5437 if ((*i)->region()->last_frame() + 1 > end) {
5438 end = (*i)->region()->last_frame() + 1;
5442 set_loop_range (start, end, _("set loop range from region"));
5445 _session->request_play_loop (true);
5446 _session->request_locate (start, true);
5451 Editor::set_punch_from_selection ()
5453 if (_session == 0 || selection->time.empty()) {
5457 framepos_t start = selection->time[clicked_selection].start;
5458 framepos_t end = selection->time[clicked_selection].end;
5460 set_punch_range (start, end, _("set punch range from selection"));
5464 Editor::set_punch_from_edit_range ()
5466 if (_session == 0) {
5473 if (!get_edit_op_range (start, end)) {
5477 set_punch_range (start, end, _("set punch range from edit range"));
5481 Editor::set_punch_from_region ()
5483 framepos_t start = max_framepos;
5486 RegionSelection rs = get_regions_from_selection_and_entered ();
5492 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
5493 if ((*i)->region()->position() < start) {
5494 start = (*i)->region()->position();
5496 if ((*i)->region()->last_frame() + 1 > end) {
5497 end = (*i)->region()->last_frame() + 1;
5501 set_punch_range (start, end, _("set punch range from region"));
5505 Editor::pitch_shift_region ()
5507 RegionSelection rs = get_regions_from_selection_and_entered ();
5509 RegionSelection audio_rs;
5510 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
5511 if (dynamic_cast<AudioRegionView*> (*i)) {
5512 audio_rs.push_back (*i);
5516 if (audio_rs.empty()) {
5520 pitch_shift (audio_rs, 1.2);
5524 Editor::transpose_region ()
5526 RegionSelection rs = get_regions_from_selection_and_entered ();
5528 list<MidiRegionView*> midi_region_views;
5529 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
5530 MidiRegionView* mrv = dynamic_cast<MidiRegionView*> (*i);
5532 midi_region_views.push_back (mrv);
5537 int const r = d.run ();
5538 if (r != RESPONSE_ACCEPT) {
5542 for (list<MidiRegionView*>::iterator i = midi_region_views.begin(); i != midi_region_views.end(); ++i) {
5543 (*i)->midi_region()->transpose (d.semitones ());
5548 Editor::set_tempo_from_region ()
5550 RegionSelection rs = get_regions_from_selection_and_entered ();
5552 if (!_session || rs.empty()) {
5556 RegionView* rv = rs.front();
5558 define_one_bar (rv->region()->position(), rv->region()->last_frame() + 1);
5562 Editor::use_range_as_bar ()
5564 framepos_t start, end;
5565 if (get_edit_op_range (start, end)) {
5566 define_one_bar (start, end);
5571 Editor::define_one_bar (framepos_t start, framepos_t end)
5573 framepos_t length = end - start;
5575 const Meter& m (_session->tempo_map().meter_at (start));
5577 /* length = 1 bar */
5579 /* now we want frames per beat.
5580 we have frames per bar, and beats per bar, so ...
5583 /* XXXX METER MATH */
5585 double frames_per_beat = length / m.divisions_per_bar();
5587 /* beats per minute = */
5589 double beats_per_minute = (_session->frame_rate() * 60.0) / frames_per_beat;
5591 /* now decide whether to:
5593 (a) set global tempo
5594 (b) add a new tempo marker
5598 const TempoSection& t (_session->tempo_map().tempo_section_at (start));
5600 bool do_global = false;
5602 if ((_session->tempo_map().n_tempos() == 1) && (_session->tempo_map().n_meters() == 1)) {
5604 /* only 1 tempo & 1 meter: ask if the user wants to set the tempo
5605 at the start, or create a new marker
5608 vector<string> options;
5609 options.push_back (_("Cancel"));
5610 options.push_back (_("Add new marker"));
5611 options.push_back (_("Set global tempo"));
5614 _("Define one bar"),
5615 _("Do you want to set the global tempo or add a new tempo marker?"),
5619 c.set_default_response (2);
5635 /* more than 1 tempo and/or meter section already, go ahead do the "usual":
5636 if the marker is at the region starter, change it, otherwise add
5641 begin_reversible_command (_("set tempo from region"));
5642 XMLNode& before (_session->tempo_map().get_state());
5645 _session->tempo_map().change_initial_tempo (beats_per_minute, t.note_type());
5646 } else if (t.frame() == start) {
5647 _session->tempo_map().change_existing_tempo_at (start, beats_per_minute, t.note_type());
5649 _session->tempo_map().add_tempo (Tempo (beats_per_minute, t.note_type()), start);
5652 XMLNode& after (_session->tempo_map().get_state());
5654 _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
5655 commit_reversible_command ();
5659 Editor::split_region_at_transients ()
5661 AnalysisFeatureList positions;
5663 RegionSelection rs = get_regions_from_selection_and_entered ();
5665 if (!_session || rs.empty()) {
5669 _session->begin_reversible_command (_("split regions"));
5671 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ) {
5673 RegionSelection::iterator tmp;
5678 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> ((*i)->region());
5680 if (ar && (ar->get_transients (positions) == 0)) {
5681 split_region_at_points ((*i)->region(), positions, true);
5688 _session->commit_reversible_command ();
5693 Editor::split_region_at_points (boost::shared_ptr<Region> r, AnalysisFeatureList& positions, bool can_ferret, bool select_new)
5695 bool use_rhythmic_rodent = false;
5697 boost::shared_ptr<Playlist> pl = r->playlist();
5699 list<boost::shared_ptr<Region> > new_regions;
5705 if (positions.empty()) {
5710 if (positions.size() > 20 && can_ferret) {
5711 std::string msgstr = string_compose (_("You are about to split\n%1\ninto %2 pieces.\nThis could take a long time."), r->name(), positions.size() + 1);
5712 MessageDialog msg (msgstr,
5715 Gtk::BUTTONS_OK_CANCEL);
5718 msg.add_button (_("Call for the Ferret!"), RESPONSE_APPLY);
5719 msg.set_secondary_text (_("Press OK to continue with this split operation\nor ask the Ferret dialog to tune the analysis"));
5721 msg.set_secondary_text (_("Press OK to continue with this split operation"));
5724 msg.set_title (_("Excessive split?"));
5727 int response = msg.run();
5733 case RESPONSE_APPLY:
5734 use_rhythmic_rodent = true;
5741 if (use_rhythmic_rodent) {
5742 show_rhythm_ferret ();
5746 AnalysisFeatureList::const_iterator x;
5748 pl->clear_changes ();
5749 pl->clear_owned_changes ();
5751 x = positions.begin();
5753 if (x == positions.end()) {
5758 pl->remove_region (r);
5762 while (x != positions.end()) {
5764 /* deal with positons that are out of scope of present region bounds */
5765 if (*x <= 0 || *x > r->length()) {
5770 /* file start = original start + how far we from the initial position ?
5773 framepos_t file_start = r->start() + pos;
5775 /* length = next position - current position
5778 framepos_t len = (*x) - pos;
5780 /* XXX we do we really want to allow even single-sample regions?
5781 shouldn't we have some kind of lower limit on region size?
5790 if (RegionFactory::region_name (new_name, r->name())) {
5794 /* do NOT announce new regions 1 by one, just wait till they are all done */
5798 plist.add (ARDOUR::Properties::start, file_start);
5799 plist.add (ARDOUR::Properties::length, len);
5800 plist.add (ARDOUR::Properties::name, new_name);
5801 plist.add (ARDOUR::Properties::layer, 0);
5803 boost::shared_ptr<Region> nr = RegionFactory::create (r->sources(), plist, false);
5805 pl->add_region (nr, r->position() + pos);
5808 new_regions.push_front(nr);
5817 RegionFactory::region_name (new_name, r->name());
5819 /* Add the final region */
5822 plist.add (ARDOUR::Properties::start, r->start() + pos);
5823 plist.add (ARDOUR::Properties::length, r->last_frame() - (r->position() + pos) + 1);
5824 plist.add (ARDOUR::Properties::name, new_name);
5825 plist.add (ARDOUR::Properties::layer, 0);
5827 boost::shared_ptr<Region> nr = RegionFactory::create (r->sources(), plist, false);
5828 pl->add_region (nr, r->position() + pos);
5831 new_regions.push_front(nr);
5836 /* We might have removed regions, which alters other regions' layering_index,
5837 so we need to do a recursive diff here.
5839 vector<Command*> cmds;
5841 _session->add_commands (cmds);
5843 _session->add_command (new StatefulDiffCommand (pl));
5847 for (list<boost::shared_ptr<Region> >::iterator i = new_regions.begin(); i != new_regions.end(); ++i){
5848 set_selected_regionview_from_region_list ((*i), Selection::Add);
5854 Editor::place_transient()
5860 RegionSelection rs = get_regions_from_selection_and_edit_point ();
5866 framepos_t where = get_preferred_edit_position();
5868 _session->begin_reversible_command (_("place transient"));
5870 for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
5871 framepos_t position = (*r)->region()->position();
5872 (*r)->region()->add_transient(where - position);
5875 _session->commit_reversible_command ();
5879 Editor::remove_transient(ArdourCanvas::Item* item)
5885 ArdourCanvas::Line* _line = reinterpret_cast<ArdourCanvas::Line*> (item);
5888 AudioRegionView* _arv = reinterpret_cast<AudioRegionView*> (item->get_data ("regionview"));
5889 _arv->remove_transient (*(float*) _line->get_data ("position"));
5893 Editor::snap_regions_to_grid ()
5895 list <boost::shared_ptr<Playlist > > used_playlists;
5897 RegionSelection rs = get_regions_from_selection_and_entered ();
5899 if (!_session || rs.empty()) {
5903 _session->begin_reversible_command (_("snap regions to grid"));
5905 for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
5907 boost::shared_ptr<Playlist> pl = (*r)->region()->playlist();
5909 if (!pl->frozen()) {
5910 /* we haven't seen this playlist before */
5912 /* remember used playlists so we can thaw them later */
5913 used_playlists.push_back(pl);
5917 framepos_t start_frame = (*r)->region()->first_frame ();
5918 snap_to (start_frame);
5919 (*r)->region()->set_position (start_frame);
5922 while (used_playlists.size() > 0) {
5923 list <boost::shared_ptr<Playlist > >::iterator i = used_playlists.begin();
5925 used_playlists.pop_front();
5928 _session->commit_reversible_command ();
5932 Editor::close_region_gaps ()
5934 list <boost::shared_ptr<Playlist > > used_playlists;
5936 RegionSelection rs = get_regions_from_selection_and_entered ();
5938 if (!_session || rs.empty()) {
5942 Dialog dialog (_("Close Region Gaps"));
5945 table.set_spacings (12);
5946 table.set_border_width (12);
5947 Label* l = manage (new Label (_("Crossfade length")));
5948 l->set_alignment (0, 0.5);
5949 table.attach (*l, 0, 1, 0, 1);
5951 SpinButton spin_crossfade (1, 0);
5952 spin_crossfade.set_range (0, 15);
5953 spin_crossfade.set_increments (1, 1);
5954 spin_crossfade.set_value (5);
5955 table.attach (spin_crossfade, 1, 2, 0, 1);
5957 table.attach (*manage (new Label (_("ms"))), 2, 3, 0, 1);
5959 l = manage (new Label (_("Pull-back length")));
5960 l->set_alignment (0, 0.5);
5961 table.attach (*l, 0, 1, 1, 2);
5963 SpinButton spin_pullback (1, 0);
5964 spin_pullback.set_range (0, 100);
5965 spin_pullback.set_increments (1, 1);
5966 spin_pullback.set_value(30);
5967 table.attach (spin_pullback, 1, 2, 1, 2);
5969 table.attach (*manage (new Label (_("ms"))), 2, 3, 1, 2);
5971 dialog.get_vbox()->pack_start (table);
5972 dialog.add_button (Stock::CANCEL, RESPONSE_CANCEL);
5973 dialog.add_button (_("Ok"), RESPONSE_ACCEPT);
5976 if (dialog.run () == RESPONSE_CANCEL) {
5980 framepos_t crossfade_len = spin_crossfade.get_value();
5981 framepos_t pull_back_frames = spin_pullback.get_value();
5983 crossfade_len = lrintf (crossfade_len * _session->frame_rate()/1000);
5984 pull_back_frames = lrintf (pull_back_frames * _session->frame_rate()/1000);
5986 /* Iterate over the region list and make adjacent regions overlap by crossfade_len_ms */
5988 _session->begin_reversible_command (_("close region gaps"));
5991 boost::shared_ptr<Region> last_region;
5993 rs.sort_by_position_and_track();
5995 for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
5997 boost::shared_ptr<Playlist> pl = (*r)->region()->playlist();
5999 if (!pl->frozen()) {
6000 /* we haven't seen this playlist before */
6002 /* remember used playlists so we can thaw them later */
6003 used_playlists.push_back(pl);
6007 framepos_t position = (*r)->region()->position();
6009 if (idx == 0 || position < last_region->position()){
6010 last_region = (*r)->region();
6015 (*r)->region()->trim_front( (position - pull_back_frames));
6016 last_region->trim_end( (position - pull_back_frames + crossfade_len));
6018 last_region = (*r)->region();
6023 while (used_playlists.size() > 0) {
6024 list <boost::shared_ptr<Playlist > >::iterator i = used_playlists.begin();
6026 used_playlists.pop_front();
6029 _session->commit_reversible_command ();
6033 Editor::tab_to_transient (bool forward)
6035 AnalysisFeatureList positions;
6037 RegionSelection rs = get_regions_from_selection_and_entered ();
6043 framepos_t pos = _session->audible_frame ();
6045 if (!selection->tracks.empty()) {
6047 /* don't waste time searching for transients in duplicate playlists.
6050 TrackViewList ts = selection->tracks.filter_to_unique_playlists ();
6052 for (TrackViewList::iterator t = ts.begin(); t != ts.end(); ++t) {
6054 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*t);
6057 boost::shared_ptr<Track> tr = rtv->track();
6059 boost::shared_ptr<Playlist> pl = tr->playlist ();
6061 framepos_t result = pl->find_next_transient (pos, forward ? 1 : -1);
6064 positions.push_back (result);
6077 for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
6078 (*r)->region()->get_transients (positions);
6082 TransientDetector::cleanup_transients (positions, _session->frame_rate(), 3.0);
6085 AnalysisFeatureList::iterator x;
6087 for (x = positions.begin(); x != positions.end(); ++x) {
6093 if (x != positions.end ()) {
6094 _session->request_locate (*x);
6098 AnalysisFeatureList::reverse_iterator x;
6100 for (x = positions.rbegin(); x != positions.rend(); ++x) {
6106 if (x != positions.rend ()) {
6107 _session->request_locate (*x);
6113 Editor::playhead_forward_to_grid ()
6115 if (!_session) return;
6116 framepos_t pos = playhead_cursor->current_frame;
6117 if (pos < max_framepos - 1) {
6119 snap_to_internal (pos, 1, false);
6120 _session->request_locate (pos);
6126 Editor::playhead_backward_to_grid ()
6128 if (!_session) return;
6129 framepos_t pos = playhead_cursor->current_frame;
6132 snap_to_internal (pos, -1, false);
6133 _session->request_locate (pos);
6138 Editor::set_track_height (Height h)
6140 TrackSelection& ts (selection->tracks);
6142 for (TrackSelection::iterator x = ts.begin(); x != ts.end(); ++x) {
6143 (*x)->set_height_enum (h);
6148 Editor::toggle_tracks_active ()
6150 TrackSelection& ts (selection->tracks);
6152 bool target = false;
6158 for (TrackSelection::iterator x = ts.begin(); x != ts.end(); ++x) {
6159 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(*x);
6163 target = !rtv->_route->active();
6166 rtv->_route->set_active (target, this);
6172 Editor::remove_tracks ()
6174 TrackSelection& ts (selection->tracks);
6180 vector<string> choices;
6184 const char* trackstr;
6186 vector<boost::shared_ptr<Route> > routes;
6187 bool special_bus = false;
6189 for (TrackSelection::iterator x = ts.begin(); x != ts.end(); ++x) {
6190 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*x);
6192 if (rtv->is_track()) {
6198 routes.push_back (rtv->_route);
6200 if (rtv->route()->is_master() || rtv->route()->is_monitor()) {
6205 if (special_bus && !Config->get_allow_special_bus_removal()) {
6206 MessageDialog msg (_("That would be bad news ...."),
6210 msg.set_secondary_text (string_compose (_(
6211 "Removing the master or monitor bus is such a bad idea\n\
6212 that %1 is not going to allow it.\n\
6214 If you really want to do this sort of thing\n\
6215 edit your ardour.rc file to set the\n\
6216 \"allow-special-bus-removal\" option to be \"yes\""), PROGRAM_NAME));
6223 if (ntracks + nbusses == 0) {
6228 trackstr = _("tracks");
6230 trackstr = _("track");
6234 busstr = _("busses");
6241 prompt = string_compose (_("Do you really want to remove %1 %2 and %3 %4?\n"
6242 "(You may also lose the playlists associated with the %2)\n\n"
6243 "This action cannot be undone, and the session file will be overwritten!"),
6244 ntracks, trackstr, nbusses, busstr);
6246 prompt = string_compose (_("Do you really want to remove %1 %2?\n"
6247 "(You may also lose the playlists associated with the %2)\n\n"
6248 "This action cannot be undone, and the session file will be overwritten!"),
6251 } else if (nbusses) {
6252 prompt = string_compose (_("Do you really want to remove %1 %2?\n\n"
6253 "This action cannot be undon, and the session file will be overwritten"),
6257 choices.push_back (_("No, do nothing."));
6258 if (ntracks + nbusses > 1) {
6259 choices.push_back (_("Yes, remove them."));
6261 choices.push_back (_("Yes, remove it."));
6266 title = string_compose (_("Remove %1"), trackstr);
6268 title = string_compose (_("Remove %1"), busstr);
6271 Choice prompter (title, prompt, choices);
6273 if (prompter.run () != 1) {
6277 for (vector<boost::shared_ptr<Route> >::iterator x = routes.begin(); x != routes.end(); ++x) {
6278 _session->remove_route (*x);
6283 Editor::do_insert_time ()
6285 if (selection->tracks.empty()) {
6289 InsertTimeDialog d (*this);
6290 int response = d.run ();
6292 if (response != RESPONSE_OK) {
6296 if (d.distance() == 0) {
6300 InsertTimeOption opt = d.intersected_region_action ();
6303 get_preferred_edit_position(),
6309 d.move_glued_markers(),
6310 d.move_locked_markers(),
6316 Editor::insert_time (
6317 framepos_t pos, framecnt_t frames, InsertTimeOption opt,
6318 bool all_playlists, bool ignore_music_glue, bool markers_too, bool glued_markers_too, bool locked_markers_too, bool tempo_too
6321 bool commit = false;
6323 if (Config->get_edit_mode() == Lock) {
6327 begin_reversible_command (_("insert time"));
6329 TrackViewList ts = selection->tracks.filter_to_unique_playlists ();
6331 for (TrackViewList::iterator x = ts.begin(); x != ts.end(); ++x) {
6335 /* don't operate on any playlist more than once, which could
6336 * happen if "all playlists" is enabled, but there is more
6337 * than 1 track using playlists "from" a given track.
6340 set<boost::shared_ptr<Playlist> > pl;
6342 if (all_playlists) {
6343 RouteTimeAxisView* rtav = dynamic_cast<RouteTimeAxisView*> (*x);
6345 vector<boost::shared_ptr<Playlist> > all = _session->playlists->playlists_for_track (rtav->track ());
6346 for (vector<boost::shared_ptr<Playlist> >::iterator p = all.begin(); p != all.end(); ++p) {
6351 if ((*x)->playlist ()) {
6352 pl.insert ((*x)->playlist ());
6356 for (set<boost::shared_ptr<Playlist> >::iterator i = pl.begin(); i != pl.end(); ++i) {
6358 (*i)->clear_changes ();
6359 (*i)->clear_owned_changes ();
6361 if (opt == SplitIntersected) {
6365 (*i)->shift (pos, frames, (opt == MoveIntersected), ignore_music_glue);
6367 vector<Command*> cmds;
6369 _session->add_commands (cmds);
6371 _session->add_command (new StatefulDiffCommand (*i));
6376 RouteTimeAxisView* rtav = dynamic_cast<RouteTimeAxisView*> (*x);
6378 rtav->route ()->shift (pos, frames);
6386 XMLNode& before (_session->locations()->get_state());
6387 Locations::LocationList copy (_session->locations()->list());
6389 for (Locations::LocationList::iterator i = copy.begin(); i != copy.end(); ++i) {
6391 Locations::LocationList::const_iterator tmp;
6393 bool const was_locked = (*i)->locked ();
6394 if (locked_markers_too) {
6398 if ((*i)->position_lock_style() == AudioTime || glued_markers_too) {
6400 if ((*i)->start() >= pos) {
6401 (*i)->set_start ((*i)->start() + frames);
6402 if (!(*i)->is_mark()) {
6403 (*i)->set_end ((*i)->end() + frames);
6416 XMLNode& after (_session->locations()->get_state());
6417 _session->add_command (new MementoCommand<Locations>(*_session->locations(), &before, &after));
6422 _session->tempo_map().insert_time (pos, frames);
6426 commit_reversible_command ();
6431 Editor::fit_selected_tracks ()
6433 if (!selection->tracks.empty()) {
6434 fit_tracks (selection->tracks);
6438 /* no selected tracks - use tracks with selected regions */
6440 if (!selection->regions.empty()) {
6441 for (RegionSelection::iterator r = selection->regions.begin(); r != selection->regions.end(); ++r) {
6442 tvl.push_back (&(*r)->get_time_axis_view ());
6448 } else if (internal_editing()) {
6449 /* no selected tracks, or regions, but in internal edit mode, so follow the mouse and use
6452 if (entered_track) {
6453 tvl.push_back (entered_track);
6461 Editor::fit_tracks (TrackViewList & tracks)
6463 if (tracks.empty()) {
6467 uint32_t child_heights = 0;
6468 int visible_tracks = 0;
6470 for (TrackSelection::iterator t = tracks.begin(); t != tracks.end(); ++t) {
6472 if (!(*t)->marked_for_display()) {
6476 child_heights += (*t)->effective_height() - (*t)->current_height();
6480 uint32_t h = (uint32_t) floor ((_canvas_height - child_heights - canvas_timebars_vsize) / visible_tracks);
6481 double first_y_pos = DBL_MAX;
6483 if (h < TimeAxisView::preset_height (HeightSmall)) {
6484 MessageDialog msg (*this, _("There are too many tracks to fit in the current window"));
6485 /* too small to be displayed */
6489 undo_visual_stack.push_back (current_visual_state (true));
6490 no_save_visual = true;
6492 /* build a list of all tracks, including children */
6495 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
6497 TimeAxisView::Children c = (*i)->get_child_list ();
6498 for (TimeAxisView::Children::iterator j = c.begin(); j != c.end(); ++j) {
6499 all.push_back (j->get());
6503 /* operate on all tracks, hide unselected ones that are in the middle of selected ones */
6505 bool prev_was_selected = false;
6506 bool is_selected = tracks.contains (all.front());
6507 bool next_is_selected;
6509 for (TrackViewList::iterator t = all.begin(); t != all.end(); ++t) {
6511 TrackViewList::iterator next;
6516 if (next != all.end()) {
6517 next_is_selected = tracks.contains (*next);
6519 next_is_selected = false;
6522 if ((*t)->marked_for_display ()) {
6524 (*t)->set_height (h);
6525 first_y_pos = std::min ((*t)->y_position (), first_y_pos);
6527 if (prev_was_selected && next_is_selected) {
6528 hide_track_in_display (*t);
6533 prev_was_selected = is_selected;
6534 is_selected = next_is_selected;
6538 set the controls_layout height now, because waiting for its size
6539 request signal handler will cause the vertical adjustment setting to fail
6542 controls_layout.property_height () = full_canvas_height - canvas_timebars_vsize;
6543 vertical_adjustment.set_value (first_y_pos);
6545 redo_visual_stack.push_back (current_visual_state (true));
6549 Editor::save_visual_state (uint32_t n)
6551 while (visual_states.size() <= n) {
6552 visual_states.push_back (0);
6555 delete visual_states[n];
6557 visual_states[n] = current_visual_state (true);
6562 Editor::goto_visual_state (uint32_t n)
6564 if (visual_states.size() <= n) {
6568 if (visual_states[n] == 0) {
6572 use_visual_state (*visual_states[n]);
6576 Editor::start_visual_state_op (uint32_t n)
6578 if (visual_state_op_connection.empty()) {
6579 visual_state_op_connection = Glib::signal_timeout().connect (sigc::bind (sigc::mem_fun (*this, &Editor::end_visual_state_op), n), 1000);
6584 Editor::cancel_visual_state_op (uint32_t n)
6586 if (!visual_state_op_connection.empty()) {
6587 visual_state_op_connection.disconnect();
6588 goto_visual_state (n);
6590 //we land here if called from the menu OR if end_visual_state_op has been called
6591 //so check if we are already in visual state n
6592 // XXX not yet checking it at all, but redoing does not hurt
6593 goto_visual_state (n);
6598 Editor::end_visual_state_op (uint32_t n)
6600 visual_state_op_connection.disconnect();
6601 save_visual_state (n);
6603 PopUp* pup = new PopUp (WIN_POS_MOUSE, 1000, true);
6605 snprintf (buf, sizeof (buf), _("Saved view %u"), n+1);
6606 pup->set_text (buf);
6609 return false; // do not call again
6613 Editor::toggle_region_mute ()
6615 if (_ignore_region_action) {
6619 RegionSelection rs = get_regions_from_selection_and_entered ();
6625 if (rs.size() > 1) {
6626 begin_reversible_command (_("mute regions"));
6628 begin_reversible_command (_("mute region"));
6631 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
6633 (*i)->region()->playlist()->clear_changes ();
6634 (*i)->region()->set_muted (!(*i)->region()->muted ());
6635 _session->add_command (new StatefulDiffCommand ((*i)->region()->playlist()));
6639 commit_reversible_command ();
6643 Editor::combine_regions ()
6645 /* foreach track with selected regions, take all selected regions
6646 and join them into a new region containing the subregions (as a
6650 typedef set<RouteTimeAxisView*> RTVS;
6653 if (selection->regions.empty()) {
6657 for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
6658 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
6661 tracks.insert (rtv);
6665 begin_reversible_command (_("combine regions"));
6667 vector<RegionView*> new_selection;
6669 for (RTVS::iterator i = tracks.begin(); i != tracks.end(); ++i) {
6672 if ((rv = (*i)->combine_regions ()) != 0) {
6673 new_selection.push_back (rv);
6677 selection->clear_regions ();
6678 for (vector<RegionView*>::iterator i = new_selection.begin(); i != new_selection.end(); ++i) {
6679 selection->add (*i);
6682 commit_reversible_command ();
6686 Editor::uncombine_regions ()
6688 typedef set<RouteTimeAxisView*> RTVS;
6691 if (selection->regions.empty()) {
6695 for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
6696 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
6699 tracks.insert (rtv);
6703 begin_reversible_command (_("uncombine regions"));
6705 for (RTVS::iterator i = tracks.begin(); i != tracks.end(); ++i) {
6706 (*i)->uncombine_regions ();
6709 commit_reversible_command ();