enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[ardour.git] / gtk2_ardour / audio_region_editor.cc
1 /*
2     Copyright (C) 2001 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <cmath>
21
22 #include <gtkmm2ext/utils.h>
23
24 #include "pbd/memento_command.h"
25 #include "pbd/stateful_diff_command.h"
26 #include "pbd/pthread_utils.h"
27
28 #include "ardour/audioregion.h"
29 #include "ardour/session_event.h"
30 #include "ardour/dB.h"
31
32 #include "audio_region_editor.h"
33 #include "audio_region_view.h"
34 #include "gui_thread.h"
35
36 #include "pbd/i18n.h"
37
38 using namespace ARDOUR;
39 using namespace PBD;
40 using namespace std;
41 using namespace Gtkmm2ext;
42
43 static void *
44 _peak_amplitude_thread (void* arg)
45 {
46         static_cast<AudioRegionEditor*>(arg)->peak_amplitude_thread ();
47         return 0;
48 }
49
50 AudioRegionEditor::AudioRegionEditor (Session* s, boost::shared_ptr<AudioRegion> r)
51         : RegionEditor (s, r)
52         , _audio_region (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)
55 {
56
57         Gtk::HBox* b = Gtk::manage (new Gtk::HBox);
58         b->set_spacing (6);
59         b->pack_start (gain_entry);
60         b->pack_start (*Gtk::manage (new Gtk::Label (_("dB"))), false, false);
61
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);
68         ++_table_row;
69
70         b = Gtk::manage (new Gtk::HBox);
71         b->set_spacing (6);
72         b->pack_start (_peak_amplitude);
73         b->pack_start (*Gtk::manage (new Gtk::Label (_("dBFS"))), false, false);
74
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);
80         ++_table_row;
81
82         gain_changed ();
83
84         gain_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &AudioRegionEditor::gain_adjustment_changed));
85
86         _peak_amplitude.property_editable() = false;
87         _peak_amplitude.set_text (_("Calculating..."));
88
89         PeakAmplitudeFound.connect (_peak_amplitude_connection, invalidator (*this), boost::bind (&AudioRegionEditor::peak_amplitude_found, this, _1), gui_context ());
90
91         char name[64];
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 ();
95 }
96
97 AudioRegionEditor::~AudioRegionEditor ()
98 {
99         void* v;
100         _peak_channel.deliver ('t');
101         pthread_join (_peak_amplitude_thread_handle, &v);
102 }
103
104 void
105 AudioRegionEditor::region_changed (const PBD::PropertyChange& what_changed)
106 {
107         RegionEditor::region_changed (what_changed);
108
109         if (what_changed.contains (ARDOUR::Properties::scale_amplitude)) {
110                 gain_changed ();
111         }
112
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 ();
116         }
117 }
118 void
119 AudioRegionEditor::gain_changed ()
120 {
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);
124         }
125 }
126
127 void
128 AudioRegionEditor::gain_adjustment_changed ()
129 {
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);
133         }
134 }
135
136 void
137 AudioRegionEditor::signal_peak_thread ()
138 {
139         _peak_channel.deliver ('c');
140 }
141
142 void
143 AudioRegionEditor::peak_amplitude_thread ()
144 {
145         while (1) {
146                 char msg;
147                 /* await instructions to run */
148                 _peak_channel.receive (msg);
149
150                 if (msg == 't') {
151                         break;
152                 }
153
154                 /* compute peak amplitude and signal the fact */
155                 PeakAmplitudeFound (accurate_coefficient_to_dB (_audio_region->maximum_amplitude ())); /* EMIT SIGNAL */
156         }
157 }
158
159 void
160 AudioRegionEditor::peak_amplitude_found (double p)
161 {
162         stringstream s;
163         s.precision (2);
164         s.setf (ios::fixed, ios::floatfield);
165         s << p;
166         _peak_amplitude.set_text (s.str ());
167 }
168