2 Copyright (C) 2000-2006 Paul Davis
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.
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.
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.
20 #include <sys/types.h>
27 #include "pbd/pthread_utils.h"
28 #include "pbd/basename.h"
29 #include "pbd/shortpath.h"
30 #include "pbd/stateful_diff_command.h"
32 #include <gtkmm2ext/choice.h>
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"
48 #include "ptformat/ptfformat.h"
50 #include "ardour_ui.h"
51 #include "cursor_context.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"
66 using namespace ARDOUR;
69 using namespace Gtkmm2ext;
70 using namespace Editing;
73 /* Functions supporting the incorporation of PT sessions into ardour */
76 Editor::external_pt_dialog ()
81 MessageDialog msg (_("You can't import a PT session until you have a session loaded."));
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);
91 int result = dialog.run();
93 if (result == Gtk::RESPONSE_OK) {
94 ptpath = dialog.get_filename ();
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));
103 if (ptpath.length()) {
104 do_ptimport(ptpath, SrcBest);
108 if (result == Gtk::RESPONSE_CANCEL) {
115 Editor::do_ptimport (std::string ptpath,
118 vector<boost::shared_ptr<Region> > regions;
119 boost::shared_ptr<ARDOUR::Track> track;
120 ARDOUR::PluginInfoPtr instrument;
121 vector<string> to_import;
127 vector<ptflookup_t> ptfwavpair;
128 vector<ptflookup_t> ptfregpair;
130 if (ptf.load(ptpath) == -1) {
131 MessageDialog msg (_("Doesn't seem to be a valid PT session file (.ptf only currently supported)"));
136 current_interthread_info = &import_status;
137 import_status.current = 1;
138 import_status.total = ptf.audiofiles.size ();
139 import_status.all_done = false;
141 ImportProgressWindow ipw (&import_status, _("Import"), _("Cancel Import"));
146 for (vector<PTFFormat::wav_t>::iterator a = ptf.audiofiles.begin(); a != ptf.audiofiles.end(); ++a) {
149 fullpath = Glib::build_filename (Glib::path_get_dirname(ptpath), "Audio Files");
150 fullpath = Glib::build_filename (fullpath, a->filename);
152 to_import.push_back (fullpath);
154 ok = import_sndfiles (to_import, Editing::ImportDistinctFiles, Editing::ImportAsRegion, quality, pos, 1, -1, track, false, instrument);
155 if (!import_status.sources.empty()) {
157 p.id = import_status.sources.back()->id();
159 ptfwavpair.push_back(p);
160 imported.push_back(import_status.sources.back());
164 for (vector<PTFFormat::region_t>::iterator a = ptf.regions.begin();
165 a != ptf.regions.end(); ++a) {
166 for (vector<ptflookup_t>::iterator p = ptfwavpair.begin();
167 p != ptfwavpair.end(); ++p) {
168 if (p->index1 == a->wave.index) {
169 for (SourceList::iterator x = imported.begin();
170 x != imported.end(); ++x) {
171 if ((*x)->id() == p->id) {
172 // Matched an uncreated ptf region to ardour region
176 plist.add (ARDOUR::Properties::start, a->sampleoffset);
177 plist.add (ARDOUR::Properties::position, 0);
178 plist.add (ARDOUR::Properties::length, a->length);
179 plist.add (ARDOUR::Properties::name, a->name);
180 plist.add (ARDOUR::Properties::layer, 0);
181 plist.add (ARDOUR::Properties::whole_file, false);
182 plist.add (ARDOUR::Properties::external, true);
185 just_one.push_back(*x);
187 boost::shared_ptr<Region> r = RegionFactory::create (just_one, plist);
188 regions.push_back(r);
190 rp.id = regions.back()->id();
191 rp.index1 = a->index;
192 ptfregpair.push_back(rp);
199 boost::shared_ptr<AudioTrack> existing_track;
201 vector<ptflookup_t> usedtracks;
204 for (vector<PTFFormat::track_t>::iterator a = ptf.tracks.begin();
205 a != ptf.tracks.end(); ++a) {
206 for (vector<ptflookup_t>::iterator p = ptfregpair.begin();
207 p != ptfregpair.end(); ++p) {
209 if ((p->index1 == a->reg.index)) {
210 // Matched a ptf active region to an ardour region
211 utr.index1 = a->index;
214 boost::shared_ptr<Region> r = RegionFactory::region_by_id (p->id);
215 vector<ptflookup_t>::iterator lookuptr = usedtracks.begin();
216 vector<ptflookup_t>::iterator found;
217 if ((found = std::find(lookuptr, usedtracks.end(), utr)) != usedtracks.end()) {
218 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));
219 existing_track = get_nth_selected_audio_track(found->index2);
220 // Put on existing track
221 boost::shared_ptr<Playlist> playlist = existing_track->playlist();
222 boost::shared_ptr<Region> copy (RegionFactory::create (r, true));
223 playlist->clear_changes ();
224 playlist->add_region (copy, a->reg.startpos);
225 //_session->add_command (new StatefulDiffCommand (playlist));
227 // Put on a new track
228 DEBUG_TRACE (DEBUG::FileUtils, string_compose ("\twav(%1) reg(%2) new_tr(%3)\n", a->reg.wave.filename.c_str(), a->reg.index, nth));
229 list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (1, 2, Normal, 0, 1));
233 existing_track = at.back();
234 existing_track->set_name (a->name);
235 boost::shared_ptr<Playlist> playlist = existing_track->playlist();
236 boost::shared_ptr<Region> copy (RegionFactory::create (r, true));
237 playlist->clear_changes ();
238 playlist->add_region (copy, a->reg.startpos);
239 //_session->add_command (new StatefulDiffCommand (playlist));
242 usedtracks.push_back(utr);
247 import_status.sources.clear();
250 _session->save_state ("");
252 import_status.all_done = true;