fix conflicts and merge with master
[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 "ardour_ui.h"
35 #include "utils.h"
36 #include "gui_thread.h"
37
38 #include "i18n.h"
39
40 using namespace ARDOUR;
41 using namespace PBD;
42 using namespace std;
43 using namespace Gtkmm2ext;
44
45 static void *
46 _peak_amplitude_thread (void* arg)
47 {
48         SessionEvent::create_per_thread_pool ("peak amplitude events", 64);
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 #ifndef WIN32
58         , _peak_channel (false)
59 #endif
60 {
61
62         Gtk::HBox* b = Gtk::manage (new Gtk::HBox);
63         b->set_spacing (6);
64         b->pack_start (gain_entry);
65         b->pack_start (*Gtk::manage (new Gtk::Label (_("dB"))), false, false);
66
67         gain_label.set_name ("AudioRegionEditorLabel");
68         gain_label.set_text (_("Region gain:"));
69         gain_label.set_alignment (1, 0.5);
70         gain_entry.configure (gain_adjustment, 0.0, 1);
71         _table.attach (gain_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
72         _table.attach (*b, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
73         ++_table_row;
74
75         b = Gtk::manage (new Gtk::HBox);
76         b->set_spacing (6);
77         b->pack_start (_peak_amplitude);
78         b->pack_start (*Gtk::manage (new Gtk::Label (_("dBFS"))), false, false);
79
80         _peak_amplitude_label.set_name ("AudioRegionEditorLabel");
81         _peak_amplitude_label.set_text (_("Peak amplitude:"));
82         _peak_amplitude_label.set_alignment (1, 0.5);
83         _table.attach (_peak_amplitude_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
84         _table.attach (*b, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
85         ++_table_row;
86
87         gain_changed ();
88
89         gain_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &AudioRegionEditor::gain_adjustment_changed));
90
91         _peak_amplitude.property_editable() = false;
92         _peak_amplitude.set_text (_("Calculating..."));
93
94         PeakAmplitudeFound.connect (_peak_amplitude_connection, invalidator (*this), boost::bind (&AudioRegionEditor::peak_amplitude_found, this, _1), gui_context ());
95         pthread_create_and_store (X_("peak-amplitude"), &_peak_amplitude_thread_handle, _peak_amplitude_thread, this);
96         signal_peak_thread ();
97 }
98
99 AudioRegionEditor::~AudioRegionEditor ()
100 {
101         void* v;
102         pthread_cancel_one (_peak_amplitude_thread_handle);
103         pthread_join (_peak_amplitude_thread_handle, &v);
104 }
105
106 void
107 AudioRegionEditor::region_changed (const PBD::PropertyChange& what_changed)
108 {
109         RegionEditor::region_changed (what_changed);
110
111         if (what_changed.contains (ARDOUR::Properties::scale_amplitude)) {
112                 gain_changed ();
113         }
114
115         if (what_changed.contains (ARDOUR::Properties::start) || what_changed.contains (ARDOUR::Properties::length)) {
116                 /* ask the peak thread to run again */
117                 signal_peak_thread ();
118         }
119 }
120 void
121 AudioRegionEditor::gain_changed ()
122 {
123         float const region_gain_dB = accurate_coefficient_to_dB (_audio_region->scale_amplitude());
124         if (region_gain_dB != gain_adjustment.get_value()) {
125                 gain_adjustment.set_value(region_gain_dB);
126         }
127 }
128
129 void
130 AudioRegionEditor::gain_adjustment_changed ()
131 {
132         float const gain = dB_to_coefficient (gain_adjustment.get_value());
133         if (_audio_region->scale_amplitude() != gain) {
134                 _audio_region->set_scale_amplitude (gain);
135         }
136 }
137
138 void
139 AudioRegionEditor::signal_peak_thread ()
140 {
141 #ifdef WIN32
142         m_peak_sem.post ();
143 #else
144         _peak_channel.deliver ('c');
145 #endif
146 }
147
148 void
149 AudioRegionEditor::wait_for_signal ()
150 {
151 #ifdef WIN32
152         m_peak_sem.wait ();
153 #else
154         char msg;
155         _peak_channel.receive (msg);
156 #endif
157 }
158
159 void
160 AudioRegionEditor::peak_amplitude_thread ()
161 {
162         while (1) {
163                 /* await instructions to run */
164                 wait_for_signal ();
165
166                 /* compute peak amplitude and signal the fact */
167                 PeakAmplitudeFound (accurate_coefficient_to_dB (_audio_region->maximum_amplitude ())); /* EMIT SIGNAL */
168         }
169 }
170
171 void
172 AudioRegionEditor::peak_amplitude_found (double p)
173 {
174         stringstream s;
175         s.precision (2);
176         s.setf (ios::fixed, ios::floatfield);
177         s << p;
178         _peak_amplitude.set_text (s.str ());
179 }
180