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 "ardour_ui.h"
35 #include "gui_thread.h"
39 using namespace ARDOUR;
42 using namespace Gtkmm2ext;
45 _peak_amplitude_thread (void* arg)
47 SessionEvent::create_per_thread_pool ("peak amplitude events", 64);
48 static_cast<AudioRegionEditor*>(arg)->peak_amplitude_thread ();
52 AudioRegionEditor::AudioRegionEditor (Session* s, boost::shared_ptr<AudioRegion> r)
55 , gain_adjustment(accurate_coefficient_to_dB(_audio_region->scale_amplitude()), -40.0, +40.0, 0.1, 1.0, 0)
56 #ifndef PLATFORM_WINDOWS
57 , _peak_channel (false)
61 Gtk::HBox* b = Gtk::manage (new Gtk::HBox);
63 b->pack_start (gain_entry);
64 b->pack_start (*Gtk::manage (new Gtk::Label (_("dB"))), false, false);
66 gain_label.set_name ("AudioRegionEditorLabel");
67 gain_label.set_text (_("Region gain:"));
68 gain_label.set_alignment (1, 0.5);
69 gain_entry.configure (gain_adjustment, 0.0, 1);
70 _table.attach (gain_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
71 _table.attach (*b, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
74 b = Gtk::manage (new Gtk::HBox);
76 b->pack_start (_peak_amplitude);
77 b->pack_start (*Gtk::manage (new Gtk::Label (_("dBFS"))), false, false);
79 _peak_amplitude_label.set_name ("AudioRegionEditorLabel");
80 _peak_amplitude_label.set_text (_("Peak amplitude:"));
81 _peak_amplitude_label.set_alignment (1, 0.5);
82 _table.attach (_peak_amplitude_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
83 _table.attach (*b, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
88 gain_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &AudioRegionEditor::gain_adjustment_changed));
90 _peak_amplitude.property_editable() = false;
91 _peak_amplitude.set_text (_("Calculating..."));
93 PeakAmplitudeFound.connect (_peak_amplitude_connection, invalidator (*this), boost::bind (&AudioRegionEditor::peak_amplitude_found, this, _1), gui_context ());
94 pthread_create_and_store (X_("peak-amplitude"), &_peak_amplitude_thread_handle, _peak_amplitude_thread, this);
95 signal_peak_thread ();
98 AudioRegionEditor::~AudioRegionEditor ()
101 pthread_cancel_one (_peak_amplitude_thread_handle);
102 pthread_join (_peak_amplitude_thread_handle, &v);
106 AudioRegionEditor::region_changed (const PBD::PropertyChange& what_changed)
108 RegionEditor::region_changed (what_changed);
110 if (what_changed.contains (ARDOUR::Properties::scale_amplitude)) {
114 if (what_changed.contains (ARDOUR::Properties::start) || what_changed.contains (ARDOUR::Properties::length)) {
115 /* ask the peak thread to run again */
116 signal_peak_thread ();
120 AudioRegionEditor::gain_changed ()
122 float const region_gain_dB = accurate_coefficient_to_dB (_audio_region->scale_amplitude());
123 if (region_gain_dB != gain_adjustment.get_value()) {
124 gain_adjustment.set_value(region_gain_dB);
129 AudioRegionEditor::gain_adjustment_changed ()
131 float const gain = dB_to_coefficient (gain_adjustment.get_value());
132 if (_audio_region->scale_amplitude() != gain) {
133 _audio_region->set_scale_amplitude (gain);
138 AudioRegionEditor::signal_peak_thread ()
140 #ifdef PLATFORM_WINDOWS
143 _peak_channel.deliver ('c');
148 AudioRegionEditor::wait_for_signal ()
150 #ifdef PLATFORM_WINDOWS
154 _peak_channel.receive (msg);
159 AudioRegionEditor::peak_amplitude_thread ()
162 /* await instructions to run */
165 /* compute peak amplitude and signal the fact */
166 PeakAmplitudeFound (accurate_coefficient_to_dB (_audio_region->maximum_amplitude ())); /* EMIT SIGNAL */
171 AudioRegionEditor::peak_amplitude_found (double p)
175 s.setf (ios::fixed, ios::floatfield);
177 _peak_amplitude.set_text (s.str ());