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/midi_model.h"
40 #include "ardour/operations.h"
41 #include "ardour/region_factory.h"
42 #include "ardour/smf_source.h"
43 #include "ardour/source_factory.h"
44 #include "ardour/utils.h"
45 #include "ardour/playlist.h"
46 #include "ardour/session.h"
47 #include "pbd/memento_command.h"
49 #include "ptformat/ptfformat.h"
51 #include "ardour_ui.h"
52 #include "cursor_context.h"
56 #include "audio_time_axis.h"
57 #include "midi_time_axis.h"
58 #include "session_import_dialog.h"
59 #include "gui_thread.h"
60 #include "interthread_progress_window.h"
61 #include "mouse_cursors.h"
62 #include "editor_cursors.h"
67 using namespace ARDOUR;
70 using namespace Gtkmm2ext;
71 using namespace Editing;
74 /* Functions supporting the incorporation of PT sessions into ardour */
77 Editor::external_pt_dialog ()
82 MessageDialog msg (_("You can't import a PT session until you have a session loaded."));
87 Gtk::FileChooserDialog dialog(_("Import PT Session"), FILE_CHOOSER_ACTION_OPEN);
88 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
89 dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
92 int result = dialog.run();
94 if (result == Gtk::RESPONSE_OK) {
95 ptpath = dialog.get_filename ();
97 if (!Glib::file_test (ptpath, Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
98 Gtk::MessageDialog msg (string_compose (_("%1: this is only the directory/folder name, not the filename.\n"), ptpath));
104 if (ptpath.length()) {
105 do_ptimport(ptpath, SrcBest);
109 if (result == Gtk::RESPONSE_CANCEL) {
116 Editor::do_ptimport (std::string ptpath,
119 vector<boost::shared_ptr<Region> > regions;
120 boost::shared_ptr<ARDOUR::Track> track;
121 ARDOUR::PluginInfoPtr instrument;
122 vector<string> to_import;
125 bool onefailed = false;
129 vector<ptflookup_t> ptfwavpair;
130 vector<ptflookup_t> ptfregpair;
132 if (ptf.load(ptpath, _session->frame_rate()) == -1) {
133 MessageDialog msg (_("Doesn't seem to be a valid PT session file"));
137 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()));
138 msg.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
140 int result = msg.run ();
141 if (result != Gtk::RESPONSE_OK) {
145 current_interthread_info = &import_status;
146 import_status.current = 1;
147 import_status.total = ptf.audiofiles.size ();
148 import_status.all_done = false;
150 ImportProgressWindow ipw (&import_status, _("Import"), _("Cancel Import"));
155 for (vector<PTFFormat::wav_t>::iterator a = ptf.audiofiles.begin(); a != ptf.audiofiles.end(); ++a) {
158 fullpath = Glib::build_filename (Glib::path_get_dirname(ptpath), "Audio Files");
159 fullpath = Glib::build_filename (fullpath, a->filename);
161 to_import.push_back (fullpath);
163 ok = import_sndfiles (to_import, Editing::ImportDistinctFiles, Editing::ImportAsRegion, quality, pos, 1, -1, track, false, instrument);
164 if (!import_status.sources.empty()) {
166 p.id = import_status.sources.back()->id();
168 ptfwavpair.push_back(p);
169 imported.push_back(import_status.sources.back());
176 MessageDialog msg (_("Failed to load one or more of the audio files, but continuing to attempt import."));
179 MessageDialog msg (_("Success! Import should complete soon."));
183 // Create a dummy midi track first to get a midi Source
184 list<boost::shared_ptr<MidiTrack> > mt (
185 _session->new_midi_track (ChanCount (DataType::MIDI, 1),
186 ChanCount (DataType::MIDI, 1),
188 instrument, (Plugin::PresetRecord*) 0,
192 PresentationInfo::max_order));
197 for (vector<PTFFormat::region_t>::iterator a = ptf.regions.begin();
198 a != ptf.regions.end(); ++a) {
199 for (vector<ptflookup_t>::iterator p = ptfwavpair.begin();
200 p != ptfwavpair.end(); ++p) {
201 if ((p->index1 == a->wave.index) && (strcmp(a->wave.filename.c_str(), "") != 0)) {
202 for (SourceList::iterator x = imported.begin();
203 x != imported.end(); ++x) {
204 if ((*x)->id() == p->id) {
205 // Matched an uncreated ptf region to ardour region
209 plist.add (ARDOUR::Properties::start, a->sampleoffset);
210 plist.add (ARDOUR::Properties::position, 0);
211 plist.add (ARDOUR::Properties::length, a->length);
212 plist.add (ARDOUR::Properties::name, a->name);
213 plist.add (ARDOUR::Properties::layer, 0);
214 plist.add (ARDOUR::Properties::whole_file, false);
215 plist.add (ARDOUR::Properties::external, true);
218 just_one.push_back(*x);
220 boost::shared_ptr<Region> r = RegionFactory::create (just_one, plist);
221 regions.push_back(r);
223 rp.id = regions.back()->id();
224 rp.index1 = a->index;
225 ptfregpair.push_back(rp);
230 if (strcmp(a->wave.filename.c_str(), "") == 0) {
231 /* Empty wave - assume MIDI region */
232 boost::shared_ptr<MidiTrack> midi_track = mt.back();
233 boost::shared_ptr<Playlist> playlist = midi_track->playlist();
234 framepos_t f = (framepos_t)a->startpos;
235 framecnt_t length = (framecnt_t)a->length;
236 MusicFrame pos (f, 0);
237 boost::shared_ptr<Source> src = _session->create_midi_source_by_stealing_name (midi_track);
239 plist.add (ARDOUR::Properties::start, 0);
240 plist.add (ARDOUR::Properties::length, length);
241 plist.add (ARDOUR::Properties::name, PBD::basename_nosuffix(src->name()));
242 boost::shared_ptr<Region> region = (RegionFactory::create (src, plist));
243 /* sets beat position */
244 region->set_position (pos.frame, pos.division);
245 midi_track->playlist()->add_region (region, pos.frame, 1.0, false, pos.division);
247 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(region);
248 boost::shared_ptr<MidiModel> mm = mr->midi_source(0)->model();
249 MidiModel::NoteDiffCommand *midicmd;
250 midicmd = mm->new_note_diff_command ("Import ProTools MIDI");
252 for (vector<PTFFormat::midi_ev_t>::iterator
254 j != a->midi.end(); ++j) {
255 Evoral::Beats start = (Evoral::Beats)(j->pos/960000.);
256 Evoral::Beats len = (Evoral::Beats)(j->length/960000.);
257 // PT C-2 = 0, Ardour C-1 = 0, subtract twelve to convert...
258 midicmd->add(boost::shared_ptr<Evoral::Note<Evoral::Beats> >
259 (new Evoral::Note<Evoral::Beats>( (uint8_t)1, start, len, j->note - 12, j->velocity )));
261 mm->apply_command (_session, midicmd);
262 boost::shared_ptr<Region> copy (RegionFactory::create (mr, true));
263 playlist->clear_changes ();
264 playlist->add_region (copy, a->startpos);
268 boost::shared_ptr<AudioTrack> existing_track;
270 vector<ptflookup_t> usedtracks;
273 for (vector<PTFFormat::track_t>::iterator a = ptf.tracks.begin();
274 a != ptf.tracks.end(); ++a) {
275 for (vector<ptflookup_t>::iterator p = ptfregpair.begin();
276 p != ptfregpair.end(); ++p) {
278 if (p->index1 == a->reg.index) {
279 // Matched a ptf active region to an ardour region
280 utr.index1 = a->index;
283 boost::shared_ptr<Region> r = RegionFactory::region_by_id (p->id);
284 vector<ptflookup_t>::iterator lookuptr = usedtracks.begin();
285 vector<ptflookup_t>::iterator found;
286 if ((found = std::find(lookuptr, usedtracks.end(), utr)) != usedtracks.end()) {
287 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));
288 existing_track = get_nth_selected_audio_track(found->index2);
289 // Put on existing track
290 boost::shared_ptr<Playlist> playlist = existing_track->playlist();
291 boost::shared_ptr<Region> copy (RegionFactory::create (r, true));
292 playlist->clear_changes ();
293 playlist->add_region (copy, a->reg.startpos);
294 //_session->add_command (new StatefulDiffCommand (playlist));
296 // Put on a new track
297 DEBUG_TRACE (DEBUG::FileUtils, string_compose ("\twav(%1) reg(%2) new_tr(%3)\n", a->reg.wave.filename.c_str(), a->reg.index, nth));
298 list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (1, 2, 0, 1, string(), PresentationInfo::max_order, Normal));
302 existing_track = at.back();
303 std::string trackname;
305 trackname = Glib::convert_with_fallback (a->name, "UTF-8", "UTF-8", "_");
306 } catch (Glib::ConvertError& err) {
307 trackname = string_compose ("Invalid %1", a->index);
309 // TODO legalize track name (no slashes, no colons)
310 #if 0 // TODO -- "find_route_name" is currently private
311 /* generate a unique name by adding a number if needed */
313 if (!_session->find_route_name (trackname.c_str (), id, trackname, false)) {
314 fatal << _("PTImport: UINT_MAX routes? impossible!") << endmsg;
315 abort(); /*NOTREACHED*/
318 existing_track->set_name (trackname);
319 boost::shared_ptr<Playlist> playlist = existing_track->playlist();
320 boost::shared_ptr<Region> copy (RegionFactory::create (r, true));
321 playlist->clear_changes ();
322 playlist->add_region (copy, a->reg.startpos);
323 //_session->add_command (new StatefulDiffCommand (playlist));
326 usedtracks.push_back(utr);
331 import_status.sources.clear();
334 _session->save_state ("");
336 import_status.all_done = true;