2 Copyright (C) 2001 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.
22 #include <gtkmm2ext/utils.h>
24 #include "pbd/memento_command.h"
25 #include "pbd/stateful_diff_command.h"
26 #include "pbd/pthread_utils.h"
28 #include "ardour/audioregion.h"
29 #include "ardour/session_event.h"
30 #include "ardour/dB.h"
32 #include "audio_region_editor.h"
33 #include "audio_region_view.h"
34 #include "gui_thread.h"
38 using namespace ARDOUR;
41 using namespace Gtkmm2ext;
44 _peak_amplitude_thread (void* arg)
46 static_cast<AudioRegionEditor*>(arg)->peak_amplitude_thread ();
50 AudioRegionEditor::AudioRegionEditor (Session* s, boost::shared_ptr<AudioRegion> r)
53 , gain_adjustment(accurate_coefficient_to_dB(_audio_region->scale_amplitude()), -40.0, +40.0, 0.1, 1.0, 0)
54 , _peak_channel (false)
57 Gtk::HBox* b = Gtk::manage (new Gtk::HBox);
59 b->pack_start (gain_entry);
60 b->pack_start (*Gtk::manage (new Gtk::Label (_("dB"))), false, false);
62 gain_label.set_name ("AudioRegionEditorLabel");
63 gain_label.set_text (_("Region gain:"));
64 gain_label.set_alignment (1, 0.5);
65 gain_entry.configure (gain_adjustment, 0.0, 1);
66 _table.attach (gain_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
67 _table.attach (*b, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
70 b = Gtk::manage (new Gtk::HBox);
72 b->pack_start (_peak_amplitude);
73 b->pack_start (*Gtk::manage (new Gtk::Label (_("dBFS"))), false, false);
75 _peak_amplitude_label.set_name ("AudioRegionEditorLabel");
76 _peak_amplitude_label.set_text (_("Peak amplitude:"));
77 _peak_amplitude_label.set_alignment (1, 0.5);
78 _table.attach (_peak_amplitude_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
79 _table.attach (*b, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
84 gain_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &AudioRegionEditor::gain_adjustment_changed));
86 _peak_amplitude.property_editable() = false;
87 _peak_amplitude.set_text (_("Calculating..."));
89 PeakAmplitudeFound.connect (_peak_amplitude_connection, invalidator (*this), boost::bind (&AudioRegionEditor::peak_amplitude_found, this, _1), gui_context ());
92 snprintf (name, 64, "peak amplitude-%p", this);
93 pthread_create_and_store (name, &_peak_amplitude_thread_handle, _peak_amplitude_thread, this);
94 signal_peak_thread ();
97 AudioRegionEditor::~AudioRegionEditor ()
100 _peak_channel.deliver ('t');
101 pthread_join (_peak_amplitude_thread_handle, &v);
105 AudioRegionEditor::region_changed (const PBD::PropertyChange& what_changed)
107 RegionEditor::region_changed (what_changed);
109 if (what_changed.contains (ARDOUR::Properties::scale_amplitude)) {
113 if (what_changed.contains (ARDOUR::Properties::start) || what_changed.contains (ARDOUR::Properties::length)) {
114 /* ask the peak thread to run again */
115 signal_peak_thread ();
119 AudioRegionEditor::gain_changed ()
121 float const region_gain_dB = accurate_coefficient_to_dB (_audio_region->scale_amplitude());
122 if (region_gain_dB != gain_adjustment.get_value()) {
123 gain_adjustment.set_value(region_gain_dB);
128 AudioRegionEditor::gain_adjustment_changed ()
130 float const gain = dB_to_coefficient (gain_adjustment.get_value());
131 if (_audio_region->scale_amplitude() != gain) {
132 _audio_region->set_scale_amplitude (gain);
137 AudioRegionEditor::signal_peak_thread ()
139 _peak_channel.deliver ('c');
143 AudioRegionEditor::peak_amplitude_thread ()
147 /* await instructions to run */
148 _peak_channel.receive (msg);
154 /* compute peak amplitude and signal the fact */
155 PeakAmplitudeFound (accurate_coefficient_to_dB (_audio_region->maximum_amplitude ())); /* EMIT SIGNAL */
160 AudioRegionEditor::peak_amplitude_found (double p)
164 s.setf (ios::fixed, ios::floatfield);
166 _peak_amplitude.set_text (s.str ());