Drop references held by any GUI Lua script after execution
[ardour.git] / gtk2_ardour / audio_region_editor.cc
1 /*
2  * Copyright (C) 2006-2012 David Robillard <d@drobilla.net>
3  * Copyright (C) 2006-2015 Tim Mayberry <mojofunk@gmail.com>
4  * Copyright (C) 2006-2016 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
6  * Copyright (C) 2014-2016 Robin Gareus <robin@gareus.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include <cmath>
24
25 #include <gtkmm2ext/utils.h>
26
27 #include "pbd/memento_command.h"
28 #include "pbd/stateful_diff_command.h"
29 #include "pbd/pthread_utils.h"
30
31 #include "ardour/audioregion.h"
32 #include "ardour/session_event.h"
33 #include "ardour/dB.h"
34
35 #include "audio_region_editor.h"
36 #include "audio_region_view.h"
37 #include "gui_thread.h"
38
39 #include "pbd/i18n.h"
40
41 using namespace ARDOUR;
42 using namespace PBD;
43 using namespace std;
44 using namespace Gtkmm2ext;
45
46 static void *
47 _peak_amplitude_thread (void* arg)
48 {
49         static_cast<AudioRegionEditor*>(arg)->peak_amplitude_thread ();
50         return 0;
51 }
52
53 AudioRegionEditor::AudioRegionEditor (Session* s, boost::shared_ptr<AudioRegion> r)
54         : RegionEditor (s, r)
55         , _audio_region (r)
56         , gain_adjustment(accurate_coefficient_to_dB(_audio_region->scale_amplitude()), -40.0, +40.0, 0.1, 1.0, 0)
57         , _peak_channel (false)
58 {
59
60         Gtk::HBox* b = Gtk::manage (new Gtk::HBox);
61         b->set_spacing (6);
62         b->pack_start (gain_entry);
63         b->pack_start (*Gtk::manage (new Gtk::Label (_("dB"))), false, false);
64
65         gain_label.set_name ("AudioRegionEditorLabel");
66         gain_label.set_text (_("Region gain:"));
67         gain_label.set_alignment (1, 0.5);
68         gain_entry.configure (gain_adjustment, 0.0, 1);
69         _table.attach (gain_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
70         _table.attach (*b, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
71         ++_table_row;
72
73         b = Gtk::manage (new Gtk::HBox);
74         b->set_spacing (6);
75         b->pack_start (_peak_amplitude);
76         b->pack_start (*Gtk::manage (new Gtk::Label (_("dBFS"))), false, false);
77
78         _peak_amplitude_label.set_name ("AudioRegionEditorLabel");
79         _peak_amplitude_label.set_text (_("Peak amplitude:"));
80         _peak_amplitude_label.set_alignment (1, 0.5);
81         _table.attach (_peak_amplitude_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
82         _table.attach (*b, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
83         ++_table_row;
84
85         gain_changed ();
86
87         gain_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &AudioRegionEditor::gain_adjustment_changed));
88
89         _peak_amplitude.property_editable() = false;
90         _peak_amplitude.set_text (_("Calculating..."));
91
92         PeakAmplitudeFound.connect (_peak_amplitude_connection, invalidator (*this), boost::bind (&AudioRegionEditor::peak_amplitude_found, this, _1), gui_context ());
93
94         char name[64];
95         snprintf (name, 64, "peak amplitude-%p", this);
96         pthread_create_and_store (name, &_peak_amplitude_thread_handle, _peak_amplitude_thread, this);
97         signal_peak_thread ();
98 }
99
100 AudioRegionEditor::~AudioRegionEditor ()
101 {
102         void* v;
103         _peak_channel.deliver ('t');
104         pthread_join (_peak_amplitude_thread_handle, &v);
105 }
106
107 void
108 AudioRegionEditor::region_changed (const PBD::PropertyChange& what_changed)
109 {
110         RegionEditor::region_changed (what_changed);
111
112         if (what_changed.contains (ARDOUR::Properties::scale_amplitude)) {
113                 gain_changed ();
114         }
115
116         if (what_changed.contains (ARDOUR::Properties::start) || what_changed.contains (ARDOUR::Properties::length)) {
117                 /* ask the peak thread to run again */
118                 signal_peak_thread ();
119         }
120 }
121 void
122 AudioRegionEditor::gain_changed ()
123 {
124         float const region_gain_dB = accurate_coefficient_to_dB (_audio_region->scale_amplitude());
125         if (region_gain_dB != gain_adjustment.get_value()) {
126                 gain_adjustment.set_value(region_gain_dB);
127         }
128 }
129
130 void
131 AudioRegionEditor::gain_adjustment_changed ()
132 {
133         float const gain = dB_to_coefficient (gain_adjustment.get_value());
134         if (_audio_region->scale_amplitude() != gain) {
135                 _audio_region->set_scale_amplitude (gain);
136         }
137 }
138
139 void
140 AudioRegionEditor::signal_peak_thread ()
141 {
142         _peak_channel.deliver ('c');
143 }
144
145 void
146 AudioRegionEditor::peak_amplitude_thread ()
147 {
148         while (1) {
149                 char msg;
150                 /* await instructions to run */
151                 _peak_channel.receive (msg);
152
153                 if (msg == 't') {
154                         break;
155                 }
156
157                 /* compute peak amplitude and signal the fact */
158                 PeakAmplitudeFound (accurate_coefficient_to_dB (_audio_region->maximum_amplitude ())); /* EMIT SIGNAL */
159         }
160 }
161
162 void
163 AudioRegionEditor::peak_amplitude_found (double p)
164 {
165         stringstream s;
166         s.precision (2);
167         s.setf (ios::fixed, ios::floatfield);
168         s << p;
169         _peak_amplitude.set_text (s.str ());
170 }
171