15723c3b27972e5bca9e8c67e4084a19d4597d48
[ardour.git] / gtk2_ardour / editor_pt_import.cc
1 /*
2     Copyright (C) 2000-2006 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 <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/time.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <algorithm>
26
27 #include "pbd/pthread_utils.h"
28 #include "pbd/basename.h"
29 #include "pbd/shortpath.h"
30 #include "pbd/stateful_diff_command.h"
31
32 #include <gtkmm2ext/choice.h>
33
34 #include "ardour/audio_track.h"
35 #include "ardour/audiofilesource.h"
36 #include "ardour/audioregion.h"
37 #include "ardour/midi_region.h"
38 #include "ardour/midi_track.h"
39 #include "ardour/operations.h"
40 #include "ardour/region_factory.h"
41 #include "ardour/smf_source.h"
42 #include "ardour/source_factory.h"
43 #include "ardour/utils.h"
44 #include "ardour/playlist.h"
45 #include "ardour/session.h"
46 #include "pbd/memento_command.h"
47
48 #include "ptformat/ptfformat.h"
49
50 #include "ardour_ui.h"
51 #include "cursor_context.h"
52 #include "editor.h"
53 #include "sfdb_ui.h"
54 #include "editing.h"
55 #include "audio_time_axis.h"
56 #include "midi_time_axis.h"
57 #include "session_import_dialog.h"
58 #include "gui_thread.h"
59 #include "interthread_progress_window.h"
60 #include "mouse_cursors.h"
61 #include "editor_cursors.h"
62
63 #include "i18n.h"
64
65 using namespace std;
66 using namespace ARDOUR;
67 using namespace PBD;
68 using namespace Gtk;
69 using namespace Gtkmm2ext;
70 using namespace Editing;
71 using std::string;
72
73 /* Functions supporting the incorporation of PT sessions into ardour */
74
75 void
76 Editor::external_pt_dialog ()
77 {
78         std::string ptpath;
79
80         if (_session == 0) {
81                 MessageDialog msg (_("You can't import a PT session until you have a session loaded."));
82                 msg.run ();
83                 return;
84         }
85
86         Gtk::FileChooserDialog dialog(_("Import PT Session"), FILE_CHOOSER_ACTION_OPEN);
87         dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
88         dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
89
90         while (true) {
91                 int result = dialog.run();
92
93                 if (result == Gtk::RESPONSE_OK) {
94                         ptpath = dialog.get_filename ();
95
96                         if (!Glib::file_test (ptpath, Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
97                                 Gtk::MessageDialog msg (string_compose (_("%1: this is only the directory/folder name, not the filename.\n"), ptpath));
98                                 msg.run ();
99                                 continue;
100                         }
101                 }
102
103                 if (ptpath.length()) {
104                         do_ptimport(ptpath, SrcBest);
105                         break;
106                 }
107
108                 if (result == Gtk::RESPONSE_CANCEL) {
109                         break;
110                 }
111         }
112 }
113
114 void
115 Editor::do_ptimport (std::string ptpath,
116                       SrcQuality  quality)
117 {
118         vector<boost::shared_ptr<Region> > regions;
119         boost::shared_ptr<ARDOUR::Track> track;
120         ARDOUR::PluginInfoPtr instrument;
121         vector<string> to_import;
122         string fullpath;
123         bool ok = false;
124         PTFFormat ptf;
125         framepos_t pos = -1;
126
127         vector<ptflookup_t> ptfwavpair;
128         vector<ptflookup_t> ptfregpair;
129
130         if (ptf.load(ptpath, _session->frame_rate()) == -1) {
131                 MessageDialog msg (_("Doesn't seem to be a valid PT session file"));
132                 msg.run ();
133                 return;
134         } else {
135                 MessageDialog msg (string_compose (_("PT v%1 Session @ %2Hz\n\n%3 audio files\n%4 regions\n%5 active regions\n\nContinue..."), (int)ptf.version, ptf.sessionrate, ptf.audiofiles.size(), ptf.regions.size(), ptf.tracks.size()));
136                 msg.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
137
138                 int result = msg.run ();
139                 if (result != Gtk::RESPONSE_OK) {
140                         return;
141                 }
142         }
143         current_interthread_info = &import_status;
144         import_status.current = 1;
145         import_status.total = ptf.audiofiles.size ();
146         import_status.all_done = false;
147
148         ImportProgressWindow ipw (&import_status, _("Import"), _("Cancel Import"));
149
150         SourceList just_one;
151         SourceList imported;
152
153         for (vector<PTFFormat::wav_t>::iterator a = ptf.audiofiles.begin(); a != ptf.audiofiles.end(); ++a) {
154                 ptflookup_t p;
155
156                 fullpath = Glib::build_filename (Glib::path_get_dirname(ptpath), "Audio Files");
157                 fullpath = Glib::build_filename (fullpath, a->filename);
158                 to_import.clear ();
159                 to_import.push_back (fullpath);
160                 ipw.show ();
161                 ok = import_sndfiles (to_import, Editing::ImportDistinctFiles, Editing::ImportAsRegion, quality, pos, 1, -1, track, false, instrument);
162                 if (!import_status.sources.empty()) {
163                         p.index1 = a->index;
164                         p.id = import_status.sources.back()->id();
165
166                         ptfwavpair.push_back(p);
167                         imported.push_back(import_status.sources.back());
168                 }
169         }
170
171         for (vector<PTFFormat::region_t>::iterator a = ptf.regions.begin();
172                         a != ptf.regions.end(); ++a) {
173                 for (vector<ptflookup_t>::iterator p = ptfwavpair.begin();
174                                 p != ptfwavpair.end(); ++p) {
175                         if (p->index1 == a->wave.index) {
176                                 for (SourceList::iterator x = imported.begin();
177                                                 x != imported.end(); ++x) {
178                                         if ((*x)->id() == p->id) {
179                                                 // Matched an uncreated ptf region to ardour region
180                                                 ptflookup_t rp;
181                                                 PropertyList plist;
182
183                                                 plist.add (ARDOUR::Properties::start, a->sampleoffset);
184                                                 plist.add (ARDOUR::Properties::position, 0);
185                                                 plist.add (ARDOUR::Properties::length, a->length);
186                                                 plist.add (ARDOUR::Properties::name, a->name);
187                                                 plist.add (ARDOUR::Properties::layer, 0);
188                                                 plist.add (ARDOUR::Properties::whole_file, false);
189                                                 plist.add (ARDOUR::Properties::external, true);
190
191                                                 just_one.clear();
192                                                 just_one.push_back(*x);
193
194                                                 boost::shared_ptr<Region> r = RegionFactory::create (just_one, plist);
195                                                 regions.push_back(r);
196
197                                                 rp.id = regions.back()->id();
198                                                 rp.index1 = a->index;
199                                                 ptfregpair.push_back(rp);
200                                         }
201                                 }
202                         }
203                 }
204         }
205
206         boost::shared_ptr<AudioTrack> existing_track;
207         uint16_t nth = 0;
208         vector<ptflookup_t> usedtracks;
209         ptflookup_t utr;
210
211         for (vector<PTFFormat::track_t>::iterator a = ptf.tracks.begin();
212                         a != ptf.tracks.end(); ++a) {
213                 for (vector<ptflookup_t>::iterator p = ptfregpair.begin();
214                                 p != ptfregpair.end(); ++p) {
215
216                         if (p->index1 == a->reg.index)  {
217                                 // Matched a ptf active region to an ardour region
218                                 utr.index1 = a->index;
219                                 utr.index2 = nth;
220                                 utr.id = p->id;
221                                 boost::shared_ptr<Region> r = RegionFactory::region_by_id (p->id);
222                                 vector<ptflookup_t>::iterator lookuptr = usedtracks.begin();
223                                 vector<ptflookup_t>::iterator found;
224                                 if ((found = std::find(lookuptr, usedtracks.end(), utr)) != usedtracks.end()) {
225                                         DEBUG_TRACE (DEBUG::FileUtils, string_compose ("\twav(%1) reg(%2) ptf_tr(%3) ard_tr(%4)\n", a->reg.wave.filename.c_str(), a->reg.index, found->index1, found->index2));
226                                         existing_track =  get_nth_selected_audio_track(found->index2);
227                                         // Put on existing track
228                                         boost::shared_ptr<Playlist> playlist = existing_track->playlist();
229                                         boost::shared_ptr<Region> copy (RegionFactory::create (r, true));
230                                         playlist->clear_changes ();
231                                         playlist->add_region (copy, a->reg.startpos);
232                                         //_session->add_command (new StatefulDiffCommand (playlist));
233                                 } else {
234                                         // Put on a new track
235                                         DEBUG_TRACE (DEBUG::FileUtils, string_compose ("\twav(%1) reg(%2) new_tr(%3)\n", a->reg.wave.filename.c_str(), a->reg.index, nth));
236                                         list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (1, 2, Normal, 0, 1));
237                                         if (at.empty()) {
238                                                 return;
239                                         }
240                                         existing_track = at.back();
241                                         existing_track->set_name (a->name);
242                                         boost::shared_ptr<Playlist> playlist = existing_track->playlist();
243                                         boost::shared_ptr<Region> copy (RegionFactory::create (r, true));
244                                         playlist->clear_changes ();
245                                         playlist->add_region (copy, a->reg.startpos);
246                                         //_session->add_command (new StatefulDiffCommand (playlist));
247                                         nth++;
248                                 }
249                                 usedtracks.push_back(utr);
250                         }
251                 }
252         }
253
254         import_status.sources.clear();
255
256         if (ok) {
257                 _session->save_state ("");
258         }
259         import_status.all_done = true;
260 }