13
0
livetrax/gtk2_ardour/audio_clip_editor.cc

620 lines
16 KiB
C++
Raw Normal View History

/*
* Copyright (C) 2011-2017 Paul Davis <paul@linuxaudiosystems.com>
* Copyright (C) 2021 Ben Loftis <ben@harrisonconsoles.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "pbd/compose.h"
2021-12-12 07:43:40 -05:00
#include <algorithm>
2021-12-12 07:43:40 -05:00
#include "gtkmm2ext/actions.h"
#include "gtkmm2ext/gui_thread.h"
#include "gtkmm2ext/utils.h"
#include "canvas/canvas.h"
#include "canvas/debug.h"
#include "canvas/utils.h"
#include "waveview/wave_view.h"
#include "ardour/audioengine.h"
#include "ardour/audioregion.h"
#include "ardour/location.h"
#include "ardour/profile.h"
#include "ardour/region_factory.h"
#include "ardour/session.h"
#include "ardour/source.h"
#include "widgets/ardour_button.h"
#include "widgets/ardour_icon.h"
#include "audio_clip_editor.h"
#include "audio_clock.h"
#include "automation_line.h"
#include "control_point.h"
#include "editor.h"
#include "region_view.h"
#include "ui_config.h"
#include "pbd/i18n.h"
using namespace Gtk;
using namespace Gtkmm2ext;
using namespace ARDOUR;
using namespace ArdourCanvas;
using namespace ArdourWaveView;
using namespace ArdourWidgets;
using std::max;
2021-12-12 07:43:40 -05:00
using std::min;
Glib::RefPtr<Gtk::ActionGroup> ClipEditorBox::clip_editor_actions;
void
ClipEditorBox::init ()
{
Bindings* bindings = Bindings::get_bindings (X_("Clip Editing"));
register_clip_editor_actions (bindings);
2021-12-12 07:43:40 -05:00
//_track_canvas_viewport->canvas()->set_data ("ardour-bindings", midi_bindings);
}
void
ClipEditorBox::register_clip_editor_actions (Bindings* clip_editor_bindings)
{
clip_editor_actions = ActionManager::create_action_group (clip_editor_bindings, X_("ClipEditing"));
/* two versions to allow same action for Delete and Backspace */
// ActionManager::register_action (clip_editor_actions, X_("zoom-in"), _("Zoom In"), sigc::mem_fun (*this, &ClipEditorBox::zoom_in));
// ActionManager::register_action (clip_editor_actions, X_("zoom-in"), _("Zoom In"), sigc::mem_fun (*this, &ClipEditorBox::zoom_out));
}
void
AudioClipEditor::ClipBBTMetric::get_marks (std::vector<ArdourCanvas::Ruler::Mark>& marks, int64_t lower, int64_t upper, int maxchars) const
{
if (!trigger) {
std::cerr << "No trigger\n";
return;
}
ArdourCanvas::Ruler::Mark mark;
assert (trigger->apparent_tempo() > 0.);
Temporal::Tempo tempo (trigger->apparent_tempo(), 4); /* XXX don't assume 4 */
std::cerr << "get marks between " << lower << " .. " << upper << " with tempo " << tempo << " upp = " << units_per_pixel << std::endl;
samplecnt_t samples_per_beat = tempo.samples_per_note_type (AudioEngine::instance()->sample_rate());
int64_t beat_number = (lower + (samples_per_beat/2)) / samples_per_beat;
int64_t last = INT64_MIN;
const double scale = UIConfiguration::instance ().get_ui_scale ();
for (int64_t n = beat_number * samples_per_beat; n < upper; n += samples_per_beat) {
/* ensure at least a 15 pixel (scaled) gap between marks */
if (marks.empty() || (((n - last) / units_per_pixel) > (15. * scale))) {
mark.style = ArdourCanvas::Ruler::Mark::Major;
mark.label = string_compose ("%1", beat_number);
mark.position = n;
marks.push_back (mark);
beat_number++;
last = n;
}
}
}
AudioClipEditor::AudioClipEditor ()
: _spp (0)
, scroll_fraction (0)
, current_line_drag (0)
, current_scroll_drag (0)
{
2021-12-12 07:43:40 -05:00
const double scale = UIConfiguration::instance ().get_ui_scale ();
2021-12-15 11:38:10 -05:00
frame = new ArdourCanvas::Rectangle (root ());
frame->name = "audio clip editor frame";
frame->set_fill (false);
frame->Event.connect (sigc::mem_fun (*this, &AudioClipEditor::event_handler));
2021-12-10 11:47:56 -05:00
/* Scroll bar does not scroll and it outside the frame */
2021-12-15 11:38:10 -05:00
scroll_bar_trough = new ArdourCanvas::Rectangle (root ());
scroll_bar_handle = new ArdourCanvas::Rectangle (scroll_bar_trough);
scroll_bar_handle->set_outline (false);
scroll_bar_handle->set_corner_radius (5.);
scroll_bar_handle->Event.connect (sigc::mem_fun (*this, &AudioClipEditor::scroll_event_handler));
/* A scrolling container for our waves and lines etc. */
waves_container = new ArdourCanvas::ScrollGroup (frame, ScrollGroup::ScrollsHorizontally);
add_scroller (*waves_container);
/* A ruler, that scrolls with the waves and is overlapped by our
* various vertical lines
*/
clip_metric = new ClipBBTMetric ();
2021-12-12 07:43:40 -05:00
ruler_container = new ArdourCanvas::Container (waves_container);
ruler = new ArdourCanvas::Ruler (ruler_container, *clip_metric);
ruler->name = "Clip Editor";
ruler->set_font_description (UIConfiguration::instance ().get_SmallerFont ());
ruler->set_fill_color (UIConfiguration::instance().color (X_("theme:bg1")));
ruler->set_outline_color (UIConfiguration::instance().color (X_("theme:contrasting less")));
/* the lines */
line_container = new ArdourCanvas::Container (waves_container);
2021-12-10 11:47:56 -05:00
const double line_width = 3.;
2021-12-10 11:47:56 -05:00
start_line = new Line (line_container);
start_line->set_outline_width (line_width * scale);
2021-12-10 11:47:56 -05:00
end_line = new Line (line_container);
end_line->set_outline_width (line_width * scale);
2021-12-10 11:47:56 -05:00
loop_line = new Line (line_container);
loop_line->set_outline_width (line_width * scale);
start_line->Event.connect (sigc::bind (sigc::mem_fun (*this, &AudioClipEditor::line_event_handler), start_line));
end_line->Event.connect (sigc::bind (sigc::mem_fun (*this, &AudioClipEditor::line_event_handler), end_line));
loop_line->Event.connect (sigc::bind (sigc::mem_fun (*this, &AudioClipEditor::line_event_handler), loop_line));
/* hide lines until there is a region */
2021-12-10 11:47:56 -05:00
line_container->hide ();
set_colors ();
}
AudioClipEditor::~AudioClipEditor ()
{
drop_waves ();
}
bool
AudioClipEditor::line_event_handler (GdkEvent* ev, ArdourCanvas::Line* l)
{
2021-12-12 07:43:40 -05:00
std::cerr << "event type " << Gtkmm2ext::event_type_string (ev->type) << " on line " << std::endl;
switch (ev->type) {
2021-12-12 07:43:40 -05:00
case GDK_BUTTON_PRESS:
current_line_drag = new LineDrag (*this, *l);
return true;
2021-12-12 07:43:40 -05:00
case GDK_BUTTON_RELEASE:
if (current_line_drag) {
current_line_drag->end (&ev->button);
delete current_line_drag;
current_line_drag = 0;
return true;
}
break;
case GDK_MOTION_NOTIFY:
if (current_line_drag) {
current_line_drag->motion (&ev->motion);
return true;
}
break;
case GDK_KEY_PRESS:
return key_press (&ev->key);
default:
break;
}
return false;
}
bool
AudioClipEditor::scroll_event_handler (GdkEvent* ev)
{
switch (ev->type) {
2021-12-12 07:43:40 -05:00
case GDK_BUTTON_PRESS:
current_scroll_drag = new ScrollDrag (*this);
current_scroll_drag->begin (&ev->button);
return true;
2021-12-12 07:43:40 -05:00
case GDK_BUTTON_RELEASE:
if (current_scroll_drag) {
current_scroll_drag->end (&ev->button);
delete current_scroll_drag;
current_scroll_drag = 0;
return true;
}
break;
case GDK_MOTION_NOTIFY:
if (current_scroll_drag) {
current_scroll_drag->motion (&ev->motion);
return true;
}
break;
case GDK_KEY_PRESS:
return key_press (&ev->key);
default:
break;
}
return false;
}
bool
AudioClipEditor::key_press (GdkEventKey* ev)
{
return false;
}
void
AudioClipEditor::position_lines ()
{
if (!audio_region) {
return;
}
2021-12-12 07:43:40 -05:00
start_line->set_x0 (sample_to_pixel (audio_region->start ().samples ()));
start_line->set_x1 (sample_to_pixel (audio_region->start ().samples ()));
2021-12-12 07:43:40 -05:00
end_line->set_x0 (sample_to_pixel (audio_region->end ().samples ()));
end_line->set_x1 (sample_to_pixel (audio_region->end ().samples ()));
}
double
AudioClipEditor::sample_to_pixel (samplepos_t s)
{
return round (s / _spp);
}
samplepos_t
AudioClipEditor::pixel_to_sample (double p)
{
return round (p * _spp);
}
AudioClipEditor::LineDrag::LineDrag (AudioClipEditor& ed, ArdourCanvas::Line& l)
2021-12-12 07:43:40 -05:00
: editor (ed)
, line (l)
{
2021-12-12 07:43:40 -05:00
line.grab ();
}
void
AudioClipEditor::LineDrag::begin (GdkEventButton* ev)
{
}
void
AudioClipEditor::LineDrag::end (GdkEventButton* ev)
{
2021-12-12 07:43:40 -05:00
line.ungrab ();
}
void
AudioClipEditor::LineDrag::motion (GdkEventMotion* ev)
{
line.set_x0 (ev->x);
line.set_x1 (ev->x);
}
2021-12-10 11:47:56 -05:00
void
AudioClipEditor::set_colors ()
{
set_background_color (UIConfiguration::instance ().color (X_("theme:bg")));
2021-12-10 11:47:56 -05:00
frame->set_outline_color (UIConfiguration::instance ().color (X_("neutral:midground")));
start_line->set_outline_color (UIConfiguration::instance ().color (X_("theme:contrasting clock")));
end_line->set_outline_color (UIConfiguration::instance ().color (X_("theme:contrasting alt")));
loop_line->set_outline_color (UIConfiguration::instance ().color (X_("theme:contrasting selection")));
2021-12-10 11:47:56 -05:00
scroll_bar_trough->set_fill_color (UIConfiguration::instance ().color (X_("theme:bg")));
scroll_bar_trough->set_outline_color (UIConfiguration::instance ().color (X_("theme:contrasting less")));
scroll_bar_handle->set_fill_color (UIConfiguration::instance ().color (X_("theme:contrasting clock")));
2021-12-10 11:47:56 -05:00
set_waveform_colors ();
}
void
AudioClipEditor::scroll_changed ()
{
if (!audio_region) {
return;
}
2021-12-12 07:43:40 -05:00
const double right_edge = scroll_bar_handle->get ().x0;
const double avail_width = scroll_bar_trough->get ().width () - scroll_bar_handle->get ().width ();
scroll_fraction = right_edge / avail_width;
scroll_fraction = std::min (1., std::max (0., scroll_fraction));
const samplepos_t s = llrintf (audio_region->source (0)->length ().samples () * scroll_fraction);
ruler->set_range (s, s + pixel_to_sample (frame->get().width() - 2.));
scroll_to (sample_to_pixel (s), 0);
queue_draw ();
}
AudioClipEditor::ScrollDrag::ScrollDrag (AudioClipEditor& e)
2021-12-12 07:43:40 -05:00
: editor (e)
{
2021-12-12 07:43:40 -05:00
e.scroll_bar_handle->grab ();
}
void
AudioClipEditor::ScrollDrag::begin (GdkEventButton* ev)
{
last_x = ev->x;
}
void
AudioClipEditor::ScrollDrag::end (GdkEventButton* ev)
{
editor.scroll_bar_handle->ungrab ();
editor.scroll_changed ();
}
void
AudioClipEditor::ScrollDrag::motion (GdkEventMotion* ev)
{
ArdourCanvas::Rectangle& r (*editor.scroll_bar_handle);
2021-12-12 07:43:40 -05:00
const double xdelta = ev->x - last_x;
ArdourCanvas::Rect n (r.get ());
const double handle_width = n.width ();
const double avail_width = editor.scroll_bar_trough->get ().width () - handle_width;
n.x0 = std::max (0., std::min (avail_width, n.x0 + xdelta));
n.x1 = n.x0 + handle_width;
r.set (n);
last_x = ev->x;
editor.scroll_changed ();
}
void
AudioClipEditor::drop_waves ()
{
2021-12-12 07:43:40 -05:00
for (auto& wave : waves) {
delete wave;
}
waves.clear ();
}
void
AudioClipEditor::set_region (boost::shared_ptr<AudioRegion> r, TriggerPtr t)
{
drop_waves ();
audio_region = r;
clip_metric->set_trigger (t);
2021-12-12 07:43:40 -05:00
uint32_t n_chans = r->n_channels ();
samplecnt_t len;
2021-12-12 07:43:40 -05:00
len = r->source (0)->length ().samples ();
for (uint32_t n = 0; n < n_chans; ++n) {
boost::shared_ptr<Region> wr = RegionFactory::get_whole_region_for_source (r->source (n));
if (!wr) {
continue;
}
boost::shared_ptr<AudioRegion> war = boost::dynamic_pointer_cast<AudioRegion> (wr);
if (!war) {
continue;
}
2021-12-10 11:47:56 -05:00
WaveView* wv = new WaveView (waves_container, war);
wv->set_channel (n);
wv->set_show_zero_line (false);
wv->set_clip_level (1.0);
wv->lower_to_bottom ();
waves.push_back (wv);
}
if (t) {
if (t->apparent_tempo() == 0.) {
/* tempo unknown, hide ruler */
ruler->hide ();
} else {
ruler->show ();
ruler->set_range (0, pixel_to_sample (frame->get().width() - 2.));
}
} else {
ruler->hide ();
}
set_spp_from_length (len);
set_wave_heights ();
set_waveform_colors ();
2021-12-10 11:47:56 -05:00
line_container->show ();
line_container->raise_to_top ();
}
void
AudioClipEditor::on_size_allocate (Gtk::Allocation& alloc)
{
GtkCanvas::on_size_allocate (alloc);
2021-12-12 07:43:40 -05:00
ArdourCanvas::Rect r (1, 1, alloc.get_width () - 2, alloc.get_height () - 2);
frame->set (r);
const double ruler_height = 25.;
ruler->set (ArdourCanvas::Rect (2, 2, alloc.get_width() - 4, ruler_height));
2021-12-12 07:43:40 -05:00
const double scroll_bar_height = 10.;
const double scroll_bar_width = alloc.get_width () - 2;
const double scroll_bar_handle_left = scroll_bar_width * scroll_fraction;
scroll_bar_trough->set (ArdourCanvas::Rect (1, alloc.get_height () - scroll_bar_height, scroll_bar_width, alloc.get_height ()));
scroll_bar_handle->set (ArdourCanvas::Rect (scroll_bar_handle_left, scroll_bar_trough->get ().y0 + 1, scroll_bar_handle_left + 30., scroll_bar_trough->get ().y1 - 1));
position_lines ();
2021-12-12 07:43:40 -05:00
start_line->set_y1 (frame->get ().height () - 2. - scroll_bar_height);
end_line->set_y1 (frame->get ().height () - 2. - scroll_bar_height);
loop_line->set_y1 (frame->get ().height () - 2. - scroll_bar_height);
2021-12-10 11:47:56 -05:00
set_wave_heights ();
}
void
AudioClipEditor::set_spp (double samples_per_pixel)
{
_spp = samples_per_pixel;
clip_metric->units_per_pixel = _spp;
position_lines ();
2021-12-12 07:43:40 -05:00
for (auto& wave : waves) {
wave->set_samples_per_pixel (_spp);
}
}
void
AudioClipEditor::set_spp_from_length (samplecnt_t len)
{
2021-12-12 07:43:40 -05:00
double available_width = frame->get ().width ();
double s = floor (len / available_width);
set_spp (s);
}
void
AudioClipEditor::set_wave_heights ()
{
2021-12-12 07:43:40 -05:00
if (waves.empty ()) {
return;
}
2021-12-12 07:43:40 -05:00
uint32_t n = 0;
const Distance w = frame->get ().height () - scroll_bar_trough->get ().height () - 2. - ruler->get().height();
2021-12-12 07:43:40 -05:00
Distance ht = w / waves.size ();
2021-12-12 07:43:40 -05:00
for (auto& wave : waves) {
wave->set_height (ht);
2021-12-12 07:43:40 -05:00
wave->set_y_position (ruler->get ().height () + (n * ht));
++n;
}
}
void
AudioClipEditor::set_waveform_colors ()
{
2021-12-12 07:43:40 -05:00
Gtkmm2ext::Color clip = UIConfiguration::instance ().color ("clipped waveform");
Gtkmm2ext::Color zero = UIConfiguration::instance ().color ("zero line");
Gtkmm2ext::Color fill = UIConfiguration::instance ().color ("waveform fill");
Gtkmm2ext::Color outline = UIConfiguration::instance ().color ("waveform outline");
2021-12-12 07:43:40 -05:00
for (auto& wave : waves) {
wave->set_fill_color (fill);
wave->set_outline_color (outline);
wave->set_clip_color (clip);
wave->set_zero_color (zero);
}
}
2021-12-12 07:43:40 -05:00
bool
AudioClipEditor::event_handler (GdkEvent* ev)
{
switch (ev->type) {
2021-12-12 07:43:40 -05:00
case GDK_BUTTON_PRESS:
break;
case GDK_ENTER_NOTIFY:
break;
case GDK_LEAVE_NOTIFY:
break;
default:
break;
}
return false;
}
AudioClipEditorBox::AudioClipEditorBox ()
{
_header_label.set_text (_("AUDIO Region Trimmer:"));
2021-12-12 07:43:40 -05:00
_header_label.set_alignment (0.0, 0.5);
zoom_in_button.set_icon (ArdourIcon::ZoomIn);
zoom_out_button.set_icon (ArdourIcon::ZoomOut);
zoom_in_button.signal_clicked.connect (sigc::mem_fun (*this, &AudioClipEditorBox::zoom_in_click));
zoom_out_button.signal_clicked.connect (sigc::mem_fun (*this, &AudioClipEditorBox::zoom_out_click));
header_box.pack_start (_header_label, false, false);
header_box.pack_start (zoom_in_button, false, false);
header_box.pack_start (zoom_out_button, false, false);
2021-12-12 07:43:40 -05:00
pack_start (header_box, false, false, 6);
editor = manage (new AudioClipEditor);
2021-12-12 07:43:40 -05:00
editor->set_size_request (600, 120);
2021-12-12 07:43:40 -05:00
pack_start (*editor, true, true);
editor->show ();
}
AudioClipEditorBox::~AudioClipEditorBox ()
{
delete editor;
}
void
AudioClipEditorBox::zoom_in_click ()
{
2021-12-12 07:43:40 -05:00
editor->set_spp (editor->spp () / 2.);
}
void
AudioClipEditorBox::zoom_out_click ()
{
2021-12-12 07:43:40 -05:00
editor->set_spp (editor->spp () * 2.);
}
void
AudioClipEditorBox::set_region (boost::shared_ptr<Region> r, TriggerPtr t)
{
boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (r);
if (!ar) {
return;
}
2021-12-12 07:43:40 -05:00
set_session (&r->session ());
2021-12-12 07:43:40 -05:00
state_connection.disconnect ();
_region = r;
editor->set_region (ar, t);
PBD::PropertyChange interesting_stuff;
2021-12-12 07:43:40 -05:00
region_changed (interesting_stuff);
2021-12-12 07:43:40 -05:00
_region->PropertyChanged.connect (state_connection, invalidator (*this), boost::bind (&AudioClipEditorBox::region_changed, this, _1), gui_context ());
}
void
AudioClipEditorBox::region_changed (const PBD::PropertyChange& what_changed)
{
}