prevent new session construction if not connected to JACK
[ardour.git] / gtk2_ardour / export_range_markers_dialog.cc
1 /*
2     Copyright (C) 2006 Paul Davis
3     Author: Andre Raue
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 #include <sys/stat.h>
22
23 #include <sstream>
24
25 #include <ardour/audioengine.h>
26 #include <ardour/sndfile_helpers.h>
27
28 #include "ardour_ui.h"
29 #include "export_range_markers_dialog.h"
30
31 #include "i18n.h"
32
33 using namespace Gtk;
34 using namespace ARDOUR;
35 using namespace PBD;
36 using namespace std;
37
38 ExportRangeMarkersDialog::ExportRangeMarkersDialog (PublicEditor& editor) 
39         : ExportDialog(editor)
40
41         do_not_allow_export_cd_markers();
42         
43         total_duration = 0;
44         current_range_marker_index = 0;
45 }
46         
47         
48 void 
49 ExportRangeMarkersDialog::export_audio_data ()
50 {
51         getSession().locations()->apply(*this, &ExportRangeMarkersDialog::process_range_markers_export);
52 }
53
54 void
55 ExportRangeMarkersDialog::process_range_markers_export(Locations::LocationList& locations)
56 {
57         Locations::LocationList::iterator locationIter;
58         current_range_marker_index = 0;
59         init_progress_computing(locations);
60         
61         for (locationIter = locations.begin(); locationIter != locations.end(); ++locationIter) {
62                 Location *currentLocation = (*locationIter);
63
64                 if(currentLocation->is_range_marker()){
65                         // init filename
66                         string filepath = get_target_filepath(
67                                 get_selected_file_name(),
68                                 currentLocation->name(),
69                                 sndfile_file_ending_from_string(get_selected_header_format()));
70                         
71                         initSpec(filepath);
72                         
73                         spec.start_frame = currentLocation->start();
74                         spec.end_frame = currentLocation->end();
75
76                         getSession().request_locate(spec.start_frame, false);
77
78                         if (getSession().start_audio_export(spec)){
79                                 // if export fails                      
80                                 return;
81                         }
82
83                         // wait until export of this range finished
84                         gtk_main_iteration();
85                         while(spec.running){
86                                 if(gtk_events_pending()){
87                                         gtk_main_iteration();
88                                 }else {
89                                         usleep(10000);
90                                 }
91                         }
92                         
93                         current_range_marker_index++;
94                 }
95         }
96         
97         spec.running = false;
98 }
99
100
101 string
102 ExportRangeMarkersDialog::get_target_filepath(string path, string filename, string postfix)
103 {
104         string target_path = path;
105         if ((target_path.find_last_of ('/')) != string::npos) {
106                 target_path += '/';
107         }
108
109         string target_filepath = target_path + filename + postfix;
110         struct stat statbuf;
111         
112         for(int counter=1; (stat (target_filepath.c_str(), &statbuf) == 0); counter++){
113                 // while file exists    
114                 ostringstream scounter;
115                 scounter.flush();
116                 scounter << counter;
117                 
118                 target_filepath = 
119                         target_path + filename + "_" + scounter.str() + postfix;
120         }
121         
122         return target_filepath;
123 }
124
125
126 bool
127 ExportRangeMarkersDialog::is_filepath_valid(string &filepath)
128 {
129         // sanity check file name first
130         struct stat statbuf;
131   
132         if (filepath.empty()) {
133                 // warning dialog
134                 string txt = _("Please enter a valid target directory.");
135                 MessageDialog msg (*this, txt, false, MESSAGE_ERROR, BUTTONS_OK, true);
136                 msg.run();
137                 return false;
138         }
139         
140         if ( (stat (filepath.c_str(), &statbuf) != 0) || 
141                 (!S_ISDIR (statbuf.st_mode)) ) {
142                         string txt = _("Please select an existing target directory. Files are not allowed!");
143                         MessageDialog msg (*this, txt, false, MESSAGE_ERROR, BUTTONS_OK, true);
144                         msg.run();
145                         return false;
146         }
147         
148         // directory needs to exist and be writable
149         string dirpath = Glib::path_get_dirname (filepath);
150         if (::access (dirpath.c_str(), W_OK) != 0) {
151                 string txt = _("Cannot write file in: ") + dirpath;
152                 MessageDialog msg (*this, txt, false, MESSAGE_ERROR, BUTTONS_OK, true);
153                 msg.run();
154                 return false;
155         }
156         
157         return true;
158 }
159
160
161 void
162 ExportRangeMarkersDialog::init_progress_computing(Locations::LocationList& locations)
163 {
164         // flush vector
165         range_markers_durations_aggregated.resize(0);
166         
167         nframes_t duration_before_current_location = 0;
168         Locations::LocationList::iterator locationIter;
169                 
170         for (locationIter = locations.begin(); locationIter != locations.end(); ++locationIter) {
171                 Location *currentLocation = (*locationIter);
172                 
173                 if(currentLocation->is_range_marker()){
174                         range_markers_durations_aggregated.push_back (duration_before_current_location);
175                         
176                         nframes_t duration = currentLocation->end() - currentLocation->start();
177                         
178                         range_markers_durations.push_back (duration);
179                         duration_before_current_location += duration;   
180                 }
181         }
182
183         total_duration = duration_before_current_location;      
184 }
185
186
187 gint
188 ExportRangeMarkersDialog::progress_timeout ()
189 {
190         double progress = 0.0;
191
192         cerr << "Progress timeout, total = " << total_duration << " index = " << current_range_marker_index
193              << " current = " << range_markers_durations[current_range_marker_index]
194              << " agg = " << range_markers_durations_aggregated[current_range_marker_index]
195              << " prog = " << spec.progress
196              << endl;
197         
198         if (current_range_marker_index >= range_markers_durations.size()){
199                 progress = 1.0;
200         } else{
201                 progress = ((double) range_markers_durations_aggregated[current_range_marker_index] +
202                             (spec.progress * (double) range_markers_durations[current_range_marker_index])) /
203                         (double) total_duration;
204         }
205         
206         set_progress_fraction( progress );
207         return TRUE;
208 }