+
+void
+Editor::set_fade_length (bool in)
+{
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ /* we need a region to measure the offset from the start */
+
+ RegionView* rv;
+
+ if (!rs.empty()) {
+ rv = rs.front();
+ } else if (entered_regionview) {
+ rv = entered_regionview;
+ } else {
+ return;
+ }
+
+ nframes64_t pos = get_preferred_edit_position();
+ nframes64_t len;
+ char* cmd;
+
+ if (pos > rv->region()->last_frame() || pos < rv->region()->first_frame()) {
+ /* edit point is outside the relevant region */
+ return;
+ }
+
+ if (in) {
+ if (pos <= rv->region()->position()) {
+ /* can't do it */
+ return;
+ }
+ len = pos - rv->region()->position();
+ cmd = _("set fade in length");
+ } else {
+ if (pos >= rv->region()->last_frame()) {
+ /* can't do it */
+ return;
+ }
+ len = rv->region()->last_frame() - pos;
+ cmd = _("set fade out length");
+ }
+
+ begin_reversible_command (cmd);
+
+ for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+ AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
+
+ if (!tmp) {
+ return;
+ }
+
+ boost::shared_ptr<AutomationList> alist;
+ if (in) {
+ alist = tmp->audio_region()->fade_in();
+ } else {
+ alist = tmp->audio_region()->fade_out();
+ }
+
+ XMLNode &before = alist->get_state();
+
+ if (in) {
+ tmp->audio_region()->set_fade_in_length (len);
+ tmp->audio_region()->set_fade_in_active (true);
+ } else {
+ tmp->audio_region()->set_fade_out_length (len);
+ tmp->audio_region()->set_fade_out_active (true);
+ }
+
+ XMLNode &after = alist->get_state();
+ session->add_command(new MementoCommand<AutomationList>(*alist, &before, &after));
+ }
+
+ commit_reversible_command ();
+}
+
+void
+Editor::toggle_fade_active (bool in)
+{
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ if (rs.empty()) {
+ return;
+ }
+
+ const char* cmd = (in ? _("toggle fade in active") : _("toggle fade out active"));
+ bool have_switch = false;
+ bool yn = false;
+
+ begin_reversible_command (cmd);
+
+ for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+ AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
+
+ if (!tmp) {
+ return;
+ }
+
+ boost::shared_ptr<AudioRegion> region (tmp->audio_region());
+
+ /* make the behaviour consistent across all regions */
+
+ if (!have_switch) {
+ if (in) {
+ yn = region->fade_in_active();
+ } else {
+ yn = region->fade_out_active();
+ }
+ have_switch = true;
+ }
+
+ XMLNode &before = region->get_state();
+ if (in) {
+ region->set_fade_in_active (!yn);
+ } else {
+ region->set_fade_out_active (!yn);
+ }
+ XMLNode &after = region->get_state();
+ session->add_command(new MementoCommand<AudioRegion>(*region.get(), &before, &after));
+ }
+
+ commit_reversible_command ();
+}
+
+void
+Editor::set_fade_in_shape (AudioRegion::FadeShape shape)
+{
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ if (rs.empty()) {
+ return;
+ }
+
+ begin_reversible_command (_("set fade in shape"));
+
+ for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+ AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
+
+ if (!tmp) {
+ return;
+ }
+
+ boost::shared_ptr<AutomationList> alist = tmp->audio_region()->fade_in();
+ XMLNode &before = alist->get_state();
+
+ tmp->audio_region()->set_fade_in_shape (shape);
+
+ XMLNode &after = alist->get_state();
+ session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &after));
+ }
+
+ commit_reversible_command ();
+
+}
+
+void
+Editor::set_fade_out_shape (AudioRegion::FadeShape shape)
+{
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ if (rs.empty()) {
+ return;
+ }
+
+ begin_reversible_command (_("set fade out shape"));
+
+ for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+ AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
+
+ if (!tmp) {
+ return;
+ }
+
+ boost::shared_ptr<AutomationList> alist = tmp->audio_region()->fade_out();
+ XMLNode &before = alist->get_state();
+
+ tmp->audio_region()->set_fade_out_shape (shape);
+
+ XMLNode &after = alist->get_state();
+ session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &after));
+ }
+
+ commit_reversible_command ();
+}
+
+void
+Editor::set_fade_in_active (bool yn)
+{
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ if (rs.empty()) {
+ return;
+ }
+
+ begin_reversible_command (_("set fade in active"));
+
+ for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+ AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
+
+ if (!tmp) {
+ return;
+ }
+
+
+ boost::shared_ptr<AudioRegion> ar (tmp->audio_region());
+
+ XMLNode &before = ar->get_state();
+
+ ar->set_fade_in_active (yn);
+
+ XMLNode &after = ar->get_state();
+ session->add_command(new MementoCommand<AudioRegion>(*ar, &before, &after));
+ }
+
+ commit_reversible_command ();
+}
+
+void
+Editor::set_fade_out_active (bool yn)
+{
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ if (rs.empty()) {
+ return;
+ }
+
+ begin_reversible_command (_("set fade out active"));
+
+ for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+ AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
+
+ if (!tmp) {
+ return;
+ }
+
+ boost::shared_ptr<AudioRegion> ar (tmp->audio_region());
+
+ XMLNode &before = ar->get_state();
+
+ ar->set_fade_out_active (yn);
+
+ XMLNode &after = ar->get_state();
+ session->add_command(new MementoCommand<AudioRegion>(*ar, &before, &after));
+ }
+
+ commit_reversible_command ();
+}
+
+void
+Editor::toggle_selected_region_fades (int dir)
+{
+ RegionSelection rs;
+ RegionSelection::iterator i;
+ boost::shared_ptr<AudioRegion> ar;
+ bool yn;
+
+ get_regions_for_action (rs);
+
+ if (rs.empty()) {
+ return;
+ }
+
+ for (i = rs.begin(); i != rs.end(); ++i) {
+ if ((ar = boost::dynamic_pointer_cast<AudioRegion>((*i)->region())) != 0) {
+ if (dir == -1) {
+ yn = ar->fade_out_active ();
+ } else {
+ yn = ar->fade_in_active ();
+ }
+ break;
+ }
+ }
+
+ if (i == rs.end()) {
+ return;
+ }
+
+ /* XXX should this undo-able? */
+
+ for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+ if ((ar = boost::dynamic_pointer_cast<AudioRegion>((*i)->region())) == 0) {
+ continue;
+ }
+ if (dir == 1 || dir == 0) {
+ ar->set_fade_in_active (!yn);
+ }
+
+ if (dir == -1 || dir == 0) {
+ ar->set_fade_out_active (!yn);
+ }
+ }
+}
+
+
+/** Update region fade visibility after its configuration has been changed */
+void
+Editor::update_region_fade_visibility ()
+{
+ bool _fade_visibility = Config->get_show_region_fades ();
+
+ for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
+ AudioTimeAxisView* v = dynamic_cast<AudioTimeAxisView*>(*i);
+ if (v) {
+ if (_fade_visibility) {
+ v->audio_view()->show_all_fades ();
+ } else {
+ v->audio_view()->hide_all_fades ();
+ }
+ }
+ }
+}
+
+/** Update crossfade visibility after its configuration has been changed */
+void
+Editor::update_xfade_visibility ()
+{
+ _xfade_visibility = Config->get_xfades_visible ();
+
+ for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
+ AudioTimeAxisView* v = dynamic_cast<AudioTimeAxisView*>(*i);
+ if (v) {
+ if (_xfade_visibility) {
+ v->show_all_xfades ();
+ } else {
+ v->hide_all_xfades ();
+ }
+ }
+ }
+}
+
+void
+Editor::set_edit_point ()
+{
+ nframes64_t where;
+ bool ignored;
+
+ if (!mouse_frame (where, ignored)) {
+ return;
+ }
+
+ snap_to (where);
+
+ if (selection->markers.empty()) {
+
+ mouse_add_new_marker (where);
+
+ } else {
+ bool ignored;
+
+ Location* loc = find_location_from_marker (selection->markers.front(), ignored);
+
+ if (loc) {
+ loc->move_to (where);
+ }
+ }
+}
+
+void
+Editor::set_playhead_cursor ()
+{
+ if (entered_marker) {
+ session->request_locate (entered_marker->position(), session->transport_rolling());
+ } else {
+ nframes64_t where;
+ bool ignored;
+
+ if (!mouse_frame (where, ignored)) {
+ return;
+ }
+
+ snap_to (where);
+
+ if (session) {
+ session->request_locate (where, session->transport_rolling());
+ }
+ }
+}
+
+void
+Editor::split ()
+{
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ nframes64_t where = get_preferred_edit_position();
+
+ if (rs.empty()) {
+ return;
+ }
+
+ split_regions_at (where, rs);
+}
+
+void
+Editor::ensure_entered_track_selected (bool op_really_wants_one_track_if_none_are_selected)
+{
+ if (entered_track && mouse_mode == MouseObject) {
+ if (!selection->tracks.empty()) {
+ if (!selection->selected (entered_track)) {
+ selection->add (entered_track);
+ }
+ } else {
+ /* there is no selection, but this operation requires/prefers selected objects */
+
+ if (op_really_wants_one_track_if_none_are_selected) {
+ selection->set (entered_track);
+ }
+ }
+ }
+}
+
+void
+Editor::trim_region_front ()
+{
+ trim_region (true);
+}
+
+void
+Editor::trim_region_back ()
+{
+ trim_region (false);
+}
+
+void
+Editor::trim_region (bool front)
+{
+ nframes64_t where = get_preferred_edit_position();
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ if (rs.empty()) {
+ return;
+ }
+
+ begin_reversible_command (front ? _("trim front") : _("trim back"));
+
+ for (list<RegionView*>::const_iterator i = rs.by_layer().begin(); i != rs.by_layer().end(); ++i) {
+ if (!(*i)->region()->locked()) {
+ boost::shared_ptr<Playlist> pl = (*i)->region()->playlist();
+ XMLNode &before = pl->get_state();
+ if (front) {
+ (*i)->region()->trim_front (where, this);
+ } else {
+ (*i)->region()->trim_end (where, this);
+ }
+ XMLNode &after = pl->get_state();
+ session->add_command(new MementoCommand<Playlist>(*pl.get(), &before, &after));
+ }
+ }
+
+ commit_reversible_command ();
+}
+
+struct EditorOrderRouteSorter {
+ bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
+ /* use of ">" forces the correct sort order */
+ return a->order_key ("editor") < b->order_key ("editor");
+ }
+};
+
+void
+Editor::select_next_route()
+{
+ if (selection->tracks.empty()) {
+ selection->set (track_views.front());
+ return;
+ }
+
+ TimeAxisView* current = selection->tracks.front();
+
+ for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
+ if (*i == current) {
+ ++i;
+ if (i != track_views.end()) {
+ selection->set (*i);
+ } else {
+ selection->set (*(track_views.begin()));
+ }
+ break;
+ }
+ }
+}
+
+void
+Editor::select_prev_route()
+{
+ if (selection->tracks.empty()) {
+ selection->set (track_views.front());
+ return;
+ }
+
+ TimeAxisView* current = selection->tracks.front();
+
+ for (TrackViewList::reverse_iterator i = track_views.rbegin(); i != track_views.rend(); ++i) {
+ if (*i == current) {
+ ++i;
+ if (i != track_views.rend()) {
+ selection->set (*i);
+ } else {
+ selection->set (*(track_views.rbegin()));
+ }
+ break;
+ }
+ }
+}
+
+void
+Editor::set_loop_from_selection (bool play)
+{
+ if (session == 0 || selection->time.empty()) {
+ return;
+ }
+
+ nframes64_t start = selection->time[clicked_selection].start;
+ nframes64_t end = selection->time[clicked_selection].end;
+
+ set_loop_range (start, end, _("set loop range from selection"));
+
+ if (play) {
+ session->request_play_loop (true);
+ session->request_locate (start, true);
+ }
+}
+
+void
+Editor::set_loop_from_edit_range (bool play)
+{
+ if (session == 0) {
+ return;
+ }
+
+ nframes64_t start;
+ nframes64_t end;
+
+ if (!get_edit_op_range (start, end)) {
+ return;
+ }
+
+ set_loop_range (start, end, _("set loop range from edit range"));
+
+ if (play) {
+ session->request_play_loop (true);
+ session->request_locate (start, true);
+ }
+}
+
+void
+Editor::set_loop_from_region (bool play)
+{
+ nframes64_t start = max_frames;
+ nframes64_t end = 0;
+
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ if (rs.empty()) {
+ return;
+ }
+
+ for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+ if ((*i)->region()->position() < start) {
+ start = (*i)->region()->position();
+ }
+ if ((*i)->region()->last_frame() + 1 > end) {
+ end = (*i)->region()->last_frame() + 1;
+ }
+ }
+
+ set_loop_range (start, end, _("set loop range from region"));
+
+ if (play) {
+ session->request_play_loop (true);
+ session->request_locate (start, true);
+ }
+}
+
+void
+Editor::set_punch_from_selection ()
+{
+ if (session == 0 || selection->time.empty()) {
+ return;
+ }
+
+ nframes64_t start = selection->time[clicked_selection].start;
+ nframes64_t end = selection->time[clicked_selection].end;
+
+ set_punch_range (start, end, _("set punch range from selection"));
+}
+
+void
+Editor::set_punch_from_edit_range ()
+{
+ if (session == 0) {
+ return;
+ }
+
+ nframes64_t start;
+ nframes64_t end;
+
+ if (!get_edit_op_range (start, end)) {
+ return;
+ }
+
+ set_punch_range (start, end, _("set punch range from edit range"));
+}
+
+void
+Editor::set_punch_from_region ()
+{
+ nframes64_t start = max_frames;
+ nframes64_t end = 0;
+
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ if (rs.empty()) {
+ return;
+ }
+
+ for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+ if ((*i)->region()->position() < start) {
+ start = (*i)->region()->position();
+ }
+ if ((*i)->region()->last_frame() + 1 > end) {
+ end = (*i)->region()->last_frame() + 1;
+ }
+ }
+
+ set_punch_range (start, end, _("set punch range from region"));
+}
+
+void
+Editor::pitch_shift_regions ()
+{
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ if (rs.empty()) {
+ return;
+ }
+
+ pitch_shift (rs, 1.2);
+}
+
+void
+Editor::use_region_as_bar ()
+{
+ if (!session) {
+ return;
+ }
+
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ if (rs.empty()) {
+ return;
+ }
+
+ RegionView* rv = rs.front();
+
+ define_one_bar (rv->region()->position(), rv->region()->last_frame() + 1);
+}
+
+void
+Editor::use_range_as_bar ()
+{
+ nframes64_t start, end;
+ if (get_edit_op_range (start, end)) {
+ define_one_bar (start, end);
+ }
+}
+
+void
+Editor::define_one_bar (nframes64_t start, nframes64_t end)
+{
+ nframes64_t length = end - start;
+
+ const Meter& m (session->tempo_map().meter_at (start));
+
+ /* length = 1 bar */
+
+ /* now we want frames per beat.
+ we have frames per bar, and beats per bar, so ...
+ */
+
+ double frames_per_beat = length / m.beats_per_bar();
+
+ /* beats per minute = */
+
+ double beats_per_minute = (session->frame_rate() * 60.0) / frames_per_beat;
+
+ /* now decide whether to:
+
+ (a) set global tempo
+ (b) add a new tempo marker
+
+ */
+
+ const TempoSection& t (session->tempo_map().tempo_section_at (start));
+
+ bool do_global = false;
+
+ if ((session->tempo_map().n_tempos() == 1) && (session->tempo_map().n_meters() == 1)) {
+
+ /* only 1 tempo & 1 meter: ask if the user wants to set the tempo
+ at the start, or create a new marker
+ */
+
+ vector<string> options;
+ options.push_back (_("Cancel"));
+ options.push_back (_("Add new marker"));
+ options.push_back (_("Set global tempo"));
+ Choice c (_("Do you want to set the global tempo or add new tempo marker?"),
+ options);
+ c.set_default_response (2);
+
+ switch (c.run()) {
+ case 0:
+ return;
+
+ case 2:
+ do_global = true;
+ break;
+
+ default:
+ do_global = false;
+ }
+
+ } else {
+
+ /* more than 1 tempo and/or meter section already, go ahead do the "usual":
+ if the marker is at the region starter, change it, otherwise add
+ a new tempo marker
+ */
+ }
+
+ begin_reversible_command (_("set tempo from region"));
+ XMLNode& before (session->tempo_map().get_state());
+
+ if (do_global) {
+ session->tempo_map().change_initial_tempo (beats_per_minute, t.note_type());
+ } else if (t.frame() == start) {
+ session->tempo_map().change_existing_tempo_at (start, beats_per_minute, t.note_type());
+ } else {
+ session->tempo_map().add_tempo (Tempo (beats_per_minute, t.note_type()), start);
+ }
+
+ XMLNode& after (session->tempo_map().get_state());
+
+ session->add_command (new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
+ commit_reversible_command ();
+}
+
+void
+Editor::split_region_at_transients ()
+{
+ AnalysisFeatureList positions;
+
+ if (!session) {
+ return;
+ }
+
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ if (rs.empty()) {
+ return;
+ }
+
+ session->begin_reversible_command (_("split regions"));
+
+ for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ) {
+
+ RegionSelection::iterator tmp;
+
+ tmp = i;
+ ++tmp;
+
+ boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> ((*i)->region());
+
+ if (ar && (ar->get_transients (positions) == 0)) {
+ split_region_at_points ((*i)->region(), positions, true);
+ positions.clear ();
+ }
+
+ i = tmp;
+ }
+
+ session->commit_reversible_command ();
+
+}
+
+void
+Editor::split_region_at_points (boost::shared_ptr<Region> r, AnalysisFeatureList& positions, bool can_ferret)
+{
+ bool use_rhythmic_rodent = false;
+
+ boost::shared_ptr<Playlist> pl = r->playlist();
+
+ if (!pl) {
+ return;
+ }
+
+ if (positions.empty()) {
+ return;
+ }
+
+
+ if (positions.size() > 20) {
+ Glib::ustring msgstr = string_compose (_("You are about to split\n%1\ninto %2 pieces.\nThis could take a long time."), r->name(), positions.size() + 1);
+ MessageDialog msg (msgstr,
+ false,
+ Gtk::MESSAGE_INFO,
+ Gtk::BUTTONS_OK_CANCEL);
+
+ if (can_ferret) {
+ msg.add_button (_("Call for the Ferret!"), RESPONSE_APPLY);
+ msg.set_secondary_text (_("Press OK to continue with this split operation\nor ask the Ferret dialog to tune the analysis"));
+ } else {
+ msg.set_secondary_text (_("Press OK to continue with this split operation"));
+ }
+
+ msg.set_title (_("Excessive split?"));
+ msg.present ();
+
+ int response = msg.run();
+ msg.hide ();
+ switch (response) {
+ case RESPONSE_OK:
+ break;
+ case RESPONSE_APPLY:
+ use_rhythmic_rodent = true;
+ break;
+ default:
+ return;
+ }
+ }
+
+ if (use_rhythmic_rodent) {
+ show_rhythm_ferret ();
+ return;
+ }
+
+ AnalysisFeatureList::const_iterator x;
+
+ nframes64_t pos = r->position();
+
+ XMLNode& before (pl->get_state());
+
+ x = positions.begin();
+
+ while (x != positions.end()) {
+ if ((*x) > pos) {
+ break;
+ }
+ ++x;
+ }
+
+ if (x == positions.end()) {
+ return;
+ }
+
+ pl->freeze ();
+ pl->remove_region (r);
+
+ while (x != positions.end()) {
+
+ /* file start = original start + how far we from the initial position ?
+ */
+
+ nframes64_t file_start = r->start() + (pos - r->position());
+
+ /* length = next position - current position
+ */
+
+ nframes64_t len = (*x) - pos;
+
+ /* XXX we do we really want to allow even single-sample regions?
+ shouldn't we have some kind of lower limit on region size?
+ */
+
+ if (len <= 0) {
+ break;
+ }
+
+ string new_name;
+
+ if (session->region_name (new_name, r->name())) {
+ break;
+ }
+
+ /* do NOT announce new regions 1 by one, just wait till they are all done */
+
+ boost::shared_ptr<Region> nr = RegionFactory::create (r->sources(), file_start, len, new_name, 0, Region::DefaultFlags, false);
+ pl->add_region (nr, pos);
+
+ pos += len;
+ ++x;
+
+ if (*x > r->last_frame()) {
+
+ /* add final fragment */
+
+ file_start = r->start() + (pos - r->position());
+ len = r->last_frame() - pos;
+
+ nr = RegionFactory::create (r->sources(), file_start, len, new_name, 0, Region::DefaultFlags);
+ pl->add_region (nr, pos);
+
+ break;
+ }
+ }
+
+ pl->thaw ();
+
+ XMLNode& after (pl->get_state());
+
+ session->add_command (new MementoCommand<Playlist>(*pl, &before, &after));
+}
+
+void
+Editor::tab_to_transient (bool forward)
+{
+ AnalysisFeatureList positions;
+
+ if (!session) {
+ return;
+ }
+
+ nframes64_t pos = session->audible_frame ();
+
+ if (!selection->tracks.empty()) {
+
+ for (TrackSelection::iterator t = selection->tracks.begin(); t != selection->tracks.end(); ++t) {
+
+ RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*t);
+
+ if (rtv) {
+ boost::shared_ptr<Diskstream> ds = rtv->get_diskstream();
+ if (ds) {
+ boost::shared_ptr<Playlist> pl = rtv->get_diskstream()->playlist ();
+ if (pl) {
+ nframes64_t result = pl->find_next_transient (pos, forward ? 1 : -1);
+
+ if (result >= 0) {
+ positions.push_back (result);
+ }
+ }
+ }
+ }
+ }
+
+ } else {
+
+ RegionSelection rs;
+
+ get_regions_for_action (rs);
+
+ if (rs.empty()) {
+ return;
+ }
+
+ for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
+ (*r)->region()->get_transients (positions);
+ }
+ }
+
+ TransientDetector::cleanup_transients (positions, session->frame_rate(), 3.0);
+
+ if (forward) {
+ AnalysisFeatureList::iterator x;
+
+ for (x = positions.begin(); x != positions.end(); ++x) {
+ if ((*x) > pos) {
+ break;
+ }
+ }
+
+ if (x != positions.end ()) {
+ session->request_locate (*x);
+ }
+
+ } else {
+ AnalysisFeatureList::reverse_iterator x;
+
+ for (x = positions.rbegin(); x != positions.rend(); ++x) {
+ if ((*x) < pos) {
+ break;
+ }
+ }
+
+ if (x != positions.rend ()) {
+ session->request_locate (*x);
+ }
+ }
+}
+void
+Editor::playhead_forward_to_grid ()
+{
+ if (!session) return;
+ nframes64_t pos = playhead_cursor->current_frame;
+ if (pos < max_frames - 1) {
+ pos += 2;
+ snap_to_internal (pos, 1, false);
+ session->request_locate (pos);
+ }
+}
+
+
+void
+Editor::playhead_backward_to_grid ()
+{
+ if (!session) return;
+ nframes64_t pos = playhead_cursor->current_frame;
+ if (pos > 2) {
+ pos -= 2;
+ snap_to_internal (pos, -1, false);
+ session->request_locate (pos);
+ }
+}
+
+void
+Editor::set_track_height (uint32_t h)
+{
+ TrackSelection& ts (selection->tracks);
+
+ if (ts.empty()) {
+ return;
+ }
+
+ for (TrackSelection::iterator x = ts.begin(); x != ts.end(); ++x) {
+ (*x)->set_height (h);
+ }
+}
+
+void
+Editor::set_track_height_largest ()
+{
+ set_track_height (TimeAxisView::hLargest);
+}
+void
+Editor::set_track_height_large ()
+{
+ set_track_height (TimeAxisView::hLarge);
+}
+void
+Editor::set_track_height_larger ()
+{
+ set_track_height (TimeAxisView::hLarger);
+}
+void
+Editor::set_track_height_normal ()
+{
+ set_track_height (TimeAxisView::hNormal);
+}
+void
+Editor::set_track_height_smaller ()
+{
+ set_track_height (TimeAxisView::hSmaller);
+}
+void
+Editor::set_track_height_small ()
+{
+ set_track_height (TimeAxisView::hSmall);
+}
+
+void
+Editor::toggle_tracks_active ()
+{
+ TrackSelection& ts (selection->tracks);
+ bool first = true;
+ bool target = false;
+
+ if (ts.empty()) {
+ return;
+ }
+
+ for (TrackSelection::iterator x = ts.begin(); x != ts.end(); ++x) {
+ RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(*x);
+
+ if (rtv) {
+ if (first) {
+ target = !rtv->_route->active();
+ first = false;
+ }
+ rtv->_route->set_active (target);
+ }
+ }
+}
+
+void
+Editor::remove_tracks ()
+{
+ TrackSelection& ts (selection->tracks);
+
+ if (ts.empty()) {
+ return;
+ }
+
+ vector<string> choices;
+ string prompt;
+ int ntracks = 0;
+ int nbusses = 0;
+ const char* trackstr;
+ const char* busstr;
+ vector<boost::shared_ptr<Route> > routes;
+
+ for (TrackSelection::iterator x = ts.begin(); x != ts.end(); ++x) {
+ RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*x);
+ if (rtv) {
+ if (rtv->is_track()) {
+ ntracks++;
+ } else {
+ nbusses++;
+ }
+ }
+ routes.push_back (rtv->_route);
+ }
+
+ if (ntracks + nbusses == 0) {
+ return;
+ }
+
+ if (ntracks > 1) {
+ trackstr = _("tracks");
+ } else {
+ trackstr = _("track");
+ }
+
+ if (nbusses > 1) {
+ busstr = _("busses");
+ } else {
+ busstr = _("bus");
+ }
+
+ if (ntracks) {
+ if (nbusses) {
+ prompt = string_compose (_("Do you really want to remove %1 %2 and %3 %4?\n"
+ "(You may also lose the playlists associated with the %2)\n\n"
+ "This action cannot be undone!"),
+ ntracks, trackstr, nbusses, busstr);
+ } else {
+ prompt = string_compose (_("Do you really want to remove %1 %2?\n"
+ "(You may also lose the playlists associated with the %2)\n\n"
+ "This action cannot be undone!"),
+ ntracks, trackstr);
+ }
+ } else if (nbusses) {
+ prompt = string_compose (_("Do you really want to remove %1 %2?"),
+ nbusses, busstr);
+ }
+
+ choices.push_back (_("No, do nothing."));
+ if (ntracks + nbusses > 1) {
+ choices.push_back (_("Yes, remove them."));
+ } else {
+ choices.push_back (_("Yes, remove it."));
+ }
+
+ Choice prompter (prompt, choices);
+
+ if (prompter.run () != 1) {
+ return;
+ }
+
+ for (vector<boost::shared_ptr<Route> >::iterator x = routes.begin(); x != routes.end(); ++x) {
+ session->remove_route (*x);
+ }
+}
+
+void
+Editor::set_waveform_scale (WaveformScale ws)
+{
+ TrackSelection& ts (selection->tracks);
+
+ if (ts.empty()) {
+ return;
+ }
+
+ for (TrackSelection::iterator x = ts.begin(); x != ts.end(); ++x) {
+ AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*> (*x);
+ if (atv) {
+ atv->set_waveform_scale (ws);
+ }
+ }
+}
+
+void
+Editor::do_insert_time ()
+{
+ if (selection->tracks.empty()) {
+ return;
+ }
+
+ nframes64_t pos = get_preferred_edit_position ();
+ ArdourDialog d (*this, _("Insert Time"));
+ VButtonBox button_box;
+ VBox option_box;
+ RadioButtonGroup group;
+ RadioButton leave_button (group, _("Stay in position"));
+ RadioButton move_button (group, _("Move"));
+ RadioButton split_button (group, _("Split & Later Section Moves"));
+ Label intersect_option_label (_("Intersected regions should:"));
+ CheckButton glue_button (_("Move Glued Regions"));
+ CheckButton marker_button (_("Move Markers"));
+ AudioClock clock ("insertTimeClock", true, X_("InsertTimeClock"), true, true, true);
+ HBox clock_box;
+
+ clock.set (0);
+ clock.set_session (session);
+ clock.set_bbt_reference (pos);
+
+ clock_box.pack_start (clock, false, true);
+
+ option_box.set_spacing (6);
+ option_box.pack_start (intersect_option_label, false, false);
+ option_box.pack_start (button_box, false, false);
+ option_box.pack_start (glue_button, false, false);
+ option_box.pack_start (marker_button, false, false);
+
+ button_box.pack_start (leave_button, false, false);
+ button_box.pack_start (move_button, false, false);
+ button_box.pack_start (split_button, false, false);
+
+ d.get_vbox()->set_border_width (12);
+ d.get_vbox()->pack_start (clock_box, false, false);
+ d.get_vbox()->pack_start (option_box, false, false);
+
+ leave_button.show ();
+ move_button.show ();
+ split_button.show ();
+ intersect_option_label.show ();
+ option_box.show ();
+ button_box.show ();
+ glue_button.show ();
+ clock.show_all();
+ clock_box.show ();
+ marker_button.show ();
+
+ d.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
+ d.add_button (Gtk::Stock::OK, Gtk::RESPONSE_OK);
+ d.show ();
+
+ int response = d.run ();
+
+ if (response != RESPONSE_OK) {
+ return;
+ }
+
+ nframes64_t distance = clock.current_duration (pos);
+
+ if (distance == 0) {
+ return;
+ }
+
+ InsertTimeOption opt;
+
+ if (leave_button.get_active()) {
+ opt = LeaveIntersected;
+ } else if (move_button.get_active()) {
+ opt = MoveIntersected;
+ } else {
+ opt = SplitIntersected;
+ }
+
+ insert_time (pos, distance, opt, glue_button.get_active(), marker_button.get_active());
+}
+
+void
+Editor::insert_time (nframes64_t pos, nframes64_t frames, InsertTimeOption opt,
+ bool ignore_music_glue, bool markers_too)
+{
+ bool commit = false;
+
+ if (Config->get_edit_mode() == Lock) {
+ return;
+ }
+
+ begin_reversible_command (_("insert time"));
+
+ for (TrackSelection::iterator x = selection->tracks.begin(); x != selection->tracks.end(); ++x) {
+ boost::shared_ptr<Playlist> pl = (*x)->playlist();
+
+ if (!pl) {
+ continue;
+ }
+
+ XMLNode &before = pl->get_state();
+
+ if (opt == SplitIntersected) {
+ pl->split (pos);
+ }
+
+ pl->shift (pos, frames, (opt == MoveIntersected), ignore_music_glue);
+
+ XMLNode &after = pl->get_state();
+
+ session->add_command (new MementoCommand<Playlist> (*pl, &before, &after));
+ commit = true;
+ }
+
+ if (markers_too) {
+ bool moved = false;
+ XMLNode& before (session->locations()->get_state());
+ Locations::LocationList copy (session->locations()->list());
+
+ for (Locations::LocationList::iterator i = copy.begin(); i != copy.end(); ++i) {
+
+ Locations::LocationList::const_iterator tmp;
+
+ if ((*i)->start() >= pos) {
+ (*i)->set_start ((*i)->start() + frames);
+ if (!(*i)->is_mark()) {
+ (*i)->set_end ((*i)->end() + frames);
+ }
+ moved = true;
+ }
+ }
+
+ if (moved) {
+ XMLNode& after (session->locations()->get_state());
+ session->add_command (new MementoCommand<Locations>(*session->locations(), &before, &after));
+ }
+ }
+
+ if (commit) {
+ commit_reversible_command ();
+ }
+}
+
+void
+Editor::fit_tracks ()
+{
+ if (selection->tracks.empty()) {
+ return;
+ }
+
+ uint32_t child_heights = 0;
+
+ for (TrackSelection::iterator t = selection->tracks.begin(); t != selection->tracks.end(); ++t) {
+
+ if (!(*t)->marked_for_display()) {
+ continue;
+ }
+
+ child_heights += ((*t)->effective_height - (*t)->current_height());
+ }
+
+ uint32_t h = (uint32_t) floor ((canvas_height - child_heights)/selection->tracks.size());
+ double first_y_pos = DBL_MAX;
+
+ undo_visual_stack.push_back (current_visual_state());
+
+ for (TrackSelection::iterator t = selection->tracks.begin(); t != selection->tracks.end(); ++t) {
+ (*t)->set_height (h);
+ first_y_pos = std::min ((*t)->y_position, first_y_pos);
+ }
+
+ vertical_adjustment.set_value (first_y_pos);
+
+ redo_visual_stack.push_back (current_visual_state());
+}
+
+void
+Editor::save_visual_state (uint32_t n)
+{
+ while (visual_states.size() <= n) {
+ visual_states.push_back (0);
+ }
+
+ if (visual_states[n] != 0) {
+ delete visual_states[n];
+ }
+
+ visual_states[n] = current_visual_state (true);
+ gdk_beep ();
+}
+
+void
+Editor::goto_visual_state (uint32_t n)
+{
+ if (visual_states.size() <= n) {
+ return;
+ }
+
+ if (visual_states[n] == 0) {
+ return;
+ }
+
+ use_visual_state (*visual_states[n]);
+}
+
+void
+Editor::start_visual_state_op (uint32_t n)
+{
+ cerr << "Start\n";
+ if (visual_state_op_connection.empty()) {
+ cerr << "\tqueue\n";
+ visual_state_op_connection = Glib::signal_timeout().connect (bind (mem_fun (*this, &Editor::end_visual_state_op), n), 1000);
+ }
+}
+
+void
+Editor::cancel_visual_state_op (uint32_t n)
+{
+ cerr << "Cancel\n";
+ if (!visual_state_op_connection.empty()) {
+ cerr << "\tgoto\n";
+ visual_state_op_connection.disconnect();
+ goto_visual_state (n);
+ }
+}
+
+bool
+Editor::end_visual_state_op (uint32_t n)
+{
+ visual_state_op_connection.disconnect();
+ save_visual_state (n);
+
+ PopUp* pup = new PopUp (WIN_POS_MOUSE, 1000, true);
+ char buf[32];
+ snprintf (buf, sizeof (buf), _("Saved view %u"), n+1);
+ pup->set_text (buf);
+ pup->touch();
+
+ return false; // do not call again
+}
+