Use compiler provided __BIG_ENDIAN__ instead of WORD_BIGENDIAN
[ardour.git] / gtk2_ardour / sfdb_ui.cc
1 /*
2     Copyright (C) 2005 Paul Davis 
3     Written by Taybin Rutkin
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19
20 */
21
22 #include <map>
23
24 #include <sndfile.h>
25
26 #include <pbd/basename.h>
27
28 #include <gtkmm/box.h>
29 #include <gtkmm/stock.h>
30
31 #include <ardour/audio_library.h>
32 #include <ardour/audioregion.h>
33 #include <ardour/sndfile_helpers.h>
34 #include <ardour/sndfilesource.h>
35
36 #include "sfdb_ui.h"
37 #include "gui_thread.h"
38
39 #include "i18n.h"
40
41 using namespace ARDOUR;
42
43 std::string length2string (const int32_t frames, const int32_t sample_rate);
44
45 SoundFileBox::SoundFileBox ()
46         :
47         _session(0),
48         current_pid(0),
49         fields(Gtk::ListStore::create(label_columns)),
50         main_box (false, 3),
51         top_box (true, 4),
52         bottom_box (true, 4),
53         play_btn(_("Play")),
54         stop_btn(_("Stop")),
55         add_field_btn(_("Add Field...")),
56         remove_field_btn(_("Remove Field"))
57 {
58         set_name (X_("SoundFileBox"));
59         border_frame.set_label (_("Soundfile Info"));
60         border_frame.add (main_box);
61
62         pack_start (border_frame);
63         set_border_width (4);
64
65         main_box.set_border_width (4);
66
67         main_box.pack_start(length, false, false);
68         main_box.pack_start(format, false, false);
69         main_box.pack_start(channels, false, false);
70         main_box.pack_start(samplerate, false, false);
71         main_box.pack_start(field_view, true, true);
72         main_box.pack_start(top_box, false, false);
73         main_box.pack_start(bottom_box, false, false);
74
75         field_view.set_size_request(200, 150);
76         field_view.append_column (_("Field"), label_columns.field);
77         field_view.append_column_editable (_("Value"), label_columns.data);
78
79         top_box.set_homogeneous(true);
80         top_box.pack_start(add_field_btn);
81         top_box.pack_start(remove_field_btn);
82
83         remove_field_btn.set_sensitive(false);
84
85         bottom_box.set_homogeneous(true);
86         bottom_box.pack_start(play_btn);
87         bottom_box.pack_start(stop_btn);
88
89         play_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::play_btn_clicked));
90         stop_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::stop_btn_clicked));
91
92         add_field_btn.signal_clicked().connect
93                         (mem_fun (*this, &SoundFileBox::add_field_clicked));
94         remove_field_btn.signal_clicked().connect
95                         (mem_fun (*this, &SoundFileBox::remove_field_clicked));
96
97         field_view.get_selection()->signal_changed().connect (mem_fun (*this, &SoundFileBox::field_selected));
98         Library->fields_changed.connect (mem_fun (*this, &SoundFileBox::setup_fields));
99
100         show_all();
101         stop_btn.hide();
102 }
103
104 void
105 SoundFileBox::set_session(Session* s)
106 {
107         _session = s;
108
109     if (!_session) {
110                 play_btn.set_sensitive(false);
111         } else {
112                 _session->AuditionActive.connect(mem_fun (*this, &SoundFileBox::audition_status_changed));
113         }
114 }
115
116 bool
117 SoundFileBox::setup_labels (string filename) 
118 {
119         path = filename;
120
121     SNDFILE *sf;
122
123         sf_info.format = 0; // libsndfile says to clear this before sf_open().
124
125     if ((sf = sf_open ((char *) filename.c_str(), SFM_READ, &sf_info)) < 0) {
126         return false;
127     }
128
129         sf_close (sf);
130
131     if (sf_info.frames == 0 && sf_info.channels == 0 &&
132                 sf_info.samplerate == 0 && sf_info.format == 0 &&
133                 sf_info.sections == 0) {
134                 /* .. ok, it's not a sound file */
135             return false;
136         }
137
138         length.set_alignment (0.0f, 0.0f);
139         length.set_text (string_compose("Length: %1", length2string(sf_info.frames, sf_info.samplerate)));
140
141         format.set_alignment (0.0f, 0.0f);
142         format.set_text (string_compose("Format: %1, %2", 
143                                 sndfile_major_format(sf_info.format),
144                                 sndfile_minor_format(sf_info.format)));
145
146         channels.set_alignment (0.0f, 0.0f);
147         channels.set_text (string_compose("Channels: %1", sf_info.channels));
148
149         samplerate.set_alignment (0.0f, 0.0f);
150         samplerate.set_text (string_compose("Samplerate: %1", sf_info.samplerate));
151
152         setup_fields ();
153
154         return true;
155 }
156
157 void
158 SoundFileBox::setup_fields ()
159 {
160         ENSURE_GUI_THREAD(mem_fun (*this, &SoundFileBox::setup_fields));
161
162         vector<string> field_list;
163         Library->get_fields(field_list);
164
165         vector<string>::iterator i;
166         Gtk::TreeModel::iterator iter;
167         Gtk::TreeModel::Row row;
168         for (i = field_list.begin(); i != field_list.end(); ++i) {
169                 string value = Library->get_field(path, *i);
170                 iter = fields->append();
171                 row = *iter;
172
173                 row[label_columns.field] = *i;
174                 row[label_columns.data]  = value;
175         }
176 }
177
178 void
179 SoundFileBox::play_btn_clicked ()
180 {
181         if (!_session) {
182                 return;
183         }
184
185         _session->cancel_audition();
186
187         if (access(path.c_str(), R_OK)) {
188                 warning << string_compose(_("Could not read file: %1 (%2)."), path, strerror(errno)) << endmsg;
189                 return;
190         }
191
192         static std::map<string, AudioRegion*> region_cache;
193
194         if (region_cache.find (path) == region_cache.end()) {
195                 AudioRegion::SourceList srclist;
196                 SndFileSource* sfs;
197
198                 for (int n = 0; n < sf_info.channels; ++n) {
199                         try {
200                                 sfs = new SndFileSource(path+":"+string_compose("%1", n), false);
201                                 srclist.push_back(sfs);
202
203                         } catch (failed_constructor& err) {
204                                 error << _("Could not access soundfile: ") << path << endmsg;
205                                 return;
206                         }
207                 }
208
209                 if (srclist.empty()) {
210                         return;
211                 }
212
213                 string result;
214                 _session->region_name (result, PBD::basename(srclist[0]->name()), false);
215                 AudioRegion* a_region = new AudioRegion(srclist, 0, srclist[0]->length(), result, 0, Region::DefaultFlags, false);
216                 region_cache[path] = a_region;
217         }
218
219         play_btn.hide();
220         stop_btn.show();
221
222         _session->audition_region(*region_cache[path]);
223 }
224
225 void
226 SoundFileBox::stop_btn_clicked ()
227 {
228         if (_session) {
229                 _session->cancel_audition();
230                 play_btn.show();
231                 stop_btn.hide();
232         }
233 }
234
235 void
236 SoundFileBox::add_field_clicked ()
237 {}
238
239 void
240 SoundFileBox::remove_field_clicked ()
241 {}
242
243 void
244 SoundFileBox::audition_status_changed (bool active)
245 {
246     ENSURE_GUI_THREAD(bind (mem_fun (*this, &SoundFileBox::audition_status_changed), active));
247
248         if (!active) {
249                 stop_btn_clicked ();
250         }
251 }
252
253 void
254 SoundFileBox::field_selected ()
255 {}
256
257 SoundFileBrowser::SoundFileBrowser (std::string title)
258         :
259         ArdourDialog(title),
260         chooser(Gtk::FILE_CHOOSER_ACTION_OPEN)
261 {
262         get_vbox()->pack_start(chooser);
263         chooser.set_preview_widget(preview);
264
265         chooser.signal_update_preview().connect(mem_fun(*this, &SoundFileBrowser::update_preview));
266 }
267
268 void
269 SoundFileBrowser::set_session (Session* s)
270 {
271         preview.set_session(s);
272 }
273
274 void
275 SoundFileBrowser::update_preview ()
276 {
277         chooser.set_preview_widget_active(preview.setup_labels(chooser.get_filename()));
278 }
279
280 SoundFileChooser::SoundFileChooser (std::string title)
281         :
282         SoundFileBrowser(title)
283 {
284         add_button (Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
285         add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
286
287         show_all ();
288 }
289
290 SoundFileOmega::SoundFileOmega (std::string title)
291         :
292         SoundFileBrowser(title),
293         embed_btn (_("Embed")),
294         import_btn (_("Import")),
295         split_check (_("Split Channels"))
296 {
297         get_action_area()->pack_start(embed_btn);
298         get_action_area()->pack_start(import_btn);
299         add_button (Gtk::Stock::CLOSE, Gtk::RESPONSE_CLOSE);
300
301         chooser.set_extra_widget(split_check);
302
303         embed_btn.signal_clicked().connect (mem_fun (*this, &SoundFileOmega::embed_clicked));
304         import_btn.signal_clicked().connect (mem_fun (*this, &SoundFileOmega::import_clicked));
305
306         show_all ();
307 }
308
309 void
310 SoundFileOmega::embed_clicked ()
311 {
312         Embedded (chooser.get_filenames(), split_check.get_active());
313 }
314
315 void
316 SoundFileOmega::import_clicked ()
317 {
318         Imported (chooser.get_filenames(), split_check.get_active());
319 }
320
321 std::string
322 length2string (const int32_t frames, const int32_t sample_rate)
323 {
324     int secs = (int) (frames / (float) sample_rate);
325     int hrs =  secs / 3600;
326     secs -= (hrs * 3600);
327     int mins = secs / 60;
328     secs -= (mins * 60);
329
330     int total_secs = (hrs * 3600) + (mins * 60) + secs;
331     int frames_remaining = frames - (total_secs * sample_rate);
332     float fractional_secs = (float) frames_remaining / sample_rate;
333
334     char duration_str[32];
335     sprintf (duration_str, "%02d:%02d:%05.2f", hrs, mins, (float) secs + fractional_secs);
336
337     return duration_str;
338 }
339