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 "ardour/audio_track.h"
33 #include "ardour/audiofilesource.h"
34 #include "ardour/audioregion.h"
35 #include "ardour/midi_region.h"
36 #include "ardour/midi_track.h"
37 #include "ardour/midi_model.h"
38 #include "ardour/operations.h"
39 #include "ardour/region_factory.h"
40 #include "ardour/smf_source.h"
41 #include "ardour/source_factory.h"
42 #include "ardour/utils.h"
43 #include "ardour/playlist.h"
44 #include "ardour/session.h"
45 #include "pbd/memento_command.h"
47 #include "ptformat/ptfformat.h"
49 #include "ardour_ui.h"
50 #include "cursor_context.h"
54 #include "audio_time_axis.h"
55 #include "midi_time_axis.h"
56 #include "session_import_dialog.h"
57 #include "gui_thread.h"
58 #include "interthread_progress_window.h"
59 #include "mouse_cursors.h"
60 #include "editor_cursors.h"
65 using namespace ARDOUR;
68 using namespace Gtkmm2ext;
69 using namespace Editing;
72 /* Functions supporting the incorporation of PT sessions into ardour */
75 Editor::external_pt_dialog ()
80 MessageDialog msg (_("You can't import a PT session until you have a session loaded."));
85 Gtk::FileChooserDialog dialog (_("Import PT Session"), FILE_CHOOSER_ACTION_OPEN);
86 dialog.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
87 dialog.add_button (Gtk::Stock::OK, Gtk::RESPONSE_OK);
90 int result = dialog.run ();
92 if (result == Gtk::RESPONSE_OK) {
93 ptpath = dialog.get_filename ();
95 if (!Glib::file_test (ptpath, Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
96 Gtk::MessageDialog msg (string_compose (_("%1: this is only the directory/folder name, not the filename.\n"), ptpath));
102 if (ptpath.length ()) {
103 do_ptimport (ptpath, SrcBest);
107 if (result == Gtk::RESPONSE_CANCEL) {
114 Editor::do_ptimport (std::string ptpath,
117 vector<boost::shared_ptr<Region> > regions;
118 boost::shared_ptr<ARDOUR::Track> track;
119 ARDOUR::PluginInfoPtr instrument;
120 vector<string> to_import;
123 bool onefailed = false;
125 samplepos_t pos = -1;
126 uint32_t srate = _session->sample_rate ();
128 vector<ptflookup_t> ptfwavpair;
129 vector<ptflookup_t> ptfregpair;
131 if (ptf.load (ptpath, srate) == -1) {
132 MessageDialog msg (_("Doesn't seem to be a valid PT session file"));
136 MessageDialog msg (string_compose (_("PT v%1 Session @ %2Hz\n\n%3 audio files\n%4 audio regions\n%5 active audio regions\n%6 midi regions\n%7 active midi regions\n\nContinue..."), (int)ptf.version, ptf.sessionrate, ptf.audiofiles.size (), ptf.regions.size () - ptf.midiregions.size (), ptf.tracks.size (), ptf.midiregions.size (), ptf.miditracks.size ()));
137 msg.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
139 int result = msg.run ();
140 if (result != Gtk::RESPONSE_OK) {
144 current_interthread_info = &import_status;
145 import_status.current = 1;
146 import_status.total = ptf.audiofiles.size ();
147 import_status.all_done = false;
149 ImportProgressWindow ipw (&import_status, _("Import"), _("Cancel Import"));
154 for (vector<PTFFormat::wav_t>::iterator a = ptf.audiofiles.begin (); a != ptf.audiofiles.end (); ++a) {
157 fullpath = Glib::build_filename (Glib::path_get_dirname (ptpath), "Audio Files");
158 fullpath = Glib::build_filename (fullpath, a->filename);
160 to_import.push_back (fullpath);
162 ok = import_sndfiles (to_import, Editing::ImportDistinctFiles, Editing::ImportAsRegion, quality, pos, 1, -1, track, false, instrument);
163 if (!import_status.sources.empty ()) {
165 p.id = import_status.sources.back ()->id ();
167 ptfwavpair.push_back (p);
168 imported.push_back (import_status.sources.back ());
175 MessageDialog msg (_("Failed to load one or more of the audio files, but continuing to attempt import."));
178 MessageDialog msg (_("Success! Import should complete soon."));
182 for (vector<PTFFormat::region_t>::iterator a = ptf.regions.begin ();
183 a != ptf.regions.end (); ++a) {
184 for (vector<ptflookup_t>::iterator p = ptfwavpair.begin ();
185 p != ptfwavpair.end (); ++p) {
186 if ((p->index1 == a->wave.index) && (strcmp (a->wave.filename.c_str (), "") != 0)) {
187 for (SourceList::iterator x = imported.begin (); x != imported.end (); ++x) {
188 if ((*x)->id () == p->id) {
189 /* Matched an uncreated ptf region to ardour region */
193 plist.add (ARDOUR::Properties::start, a->sampleoffset);
194 plist.add (ARDOUR::Properties::position, 0);
195 plist.add (ARDOUR::Properties::length, a->length);
196 plist.add (ARDOUR::Properties::name, a->name);
197 plist.add (ARDOUR::Properties::layer, 0);
198 plist.add (ARDOUR::Properties::whole_file, false);
199 plist.add (ARDOUR::Properties::external, true);
202 just_one.push_back (*x);
204 boost::shared_ptr<Region> r = RegionFactory::create (just_one, plist);
205 regions.push_back (r);
207 rp.id = regions.back ()->id ();
208 rp.index1 = a->index;
209 ptfregpair.push_back (rp);
216 boost::shared_ptr<AudioTrack> existing_track;
218 vector<ptflookup_t> usedtracks;
221 for (vector<PTFFormat::track_t>::iterator a = ptf.tracks.begin (); a != ptf.tracks.end (); ++a) {
222 for (vector<ptflookup_t>::iterator p = ptfregpair.begin ();
223 p != ptfregpair.end (); ++p) {
225 if (p->index1 == a->reg.index) {
226 /* Matched a ptf active region to an ardour region */
227 utr.index1 = a->index;
230 boost::shared_ptr<Region> r = RegionFactory::region_by_id (p->id);
231 vector<ptflookup_t>::iterator lookuptr = usedtracks.begin ();
232 vector<ptflookup_t>::iterator found;
233 if ((found = std::find (lookuptr, usedtracks.end (), utr)) != usedtracks.end ()) {
234 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));
235 existing_track = get_nth_selected_audio_track (found->index2);
236 /* Put on existing track */
237 boost::shared_ptr<Playlist> playlist = existing_track->playlist ();
238 boost::shared_ptr<Region> copy (RegionFactory::create (r, true));
239 playlist->clear_changes ();
240 playlist->add_region (copy, a->reg.startpos);
241 //_session->add_command (new StatefulDiffCommand (playlist));
243 /* Put on a new track */
244 DEBUG_TRACE (DEBUG::FileUtils, string_compose ("\twav(%1) reg(%2) new_tr(%3)\n", a->reg.wave.filename.c_str (), a->reg.index, nth));
245 list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (1, 2, 0, 1, "", PresentationInfo::max_order, Normal));
249 existing_track = at.back ();
250 std::string trackname;
252 trackname = Glib::convert_with_fallback (a->name, "UTF-8", "UTF-8", "_");
253 } catch (Glib::ConvertError& err) {
254 trackname = string_compose ("Invalid %1", a->index);
256 /* TODO legalize track name (no slashes, no colons) */
257 #if 0 /* TODO -- "find_route_name" is currently private */
258 /* generate a unique name by adding a number if needed */
260 if (!_session->find_route_name (trackname.c_str (), id, trackname, false)) {
261 fatal << _("PTImport: UINT_MAX routes? impossible!") << endmsg;
262 abort(); /*NOTREACHED*/
265 existing_track->set_name (trackname);
266 boost::shared_ptr<Playlist> playlist = existing_track->playlist();
267 boost::shared_ptr<Region> copy (RegionFactory::create (r, true));
268 playlist->clear_changes ();
269 playlist->add_region (copy, a->reg.startpos);
270 //_session->add_command (new StatefulDiffCommand (playlist));
273 usedtracks.push_back (utr);
278 /* MIDI - Find list of unique midi tracks first */
279 typedef struct midipair {
284 vector<midipair_t> uniquetr;
288 for (vector<PTFFormat::track_t>::iterator a = ptf.miditracks.begin (); a != ptf.miditracks.end (); ++a) {
290 for (vector<midipair_t>::iterator b = uniquetr.begin (); b != uniquetr.end (); ++b) {
291 if (b->trname == a->name) {
297 uniquetr.push_back ({ith, a->name});
298 //printf(" : %d : %s\n", ith, a->name.c_str());
303 /* MIDI - Create unique midi tracks and a lookup table for used tracks */
304 for (vector<midipair_t>::iterator a = uniquetr.begin ();
305 a != uniquetr.end (); ++a) {
307 list<boost::shared_ptr<MidiTrack> > mt (_session->new_midi_track (
308 ChanCount (DataType::MIDI, 1),
309 ChanCount (DataType::MIDI, 1),
311 instrument, (Plugin::PresetRecord*) 0,
315 PresentationInfo::max_order,
319 /* Select all MIDI tracks by selecting all tracks then subsetting to midi */
320 select_all_tracks ();
322 TrackViewList miditracks;
323 MidiTimeAxisView* mtv;
324 TrackSelection::iterator x;
326 for (x = selection->tracks.begin (); x != selection->tracks.end (); ++x) {
328 mtv = dynamic_cast<MidiTimeAxisView*>(*x);
332 } else if (mtv->is_midi_track ()) {
333 miditracks.push_back (dynamic_cast<TimeAxisView*> (*x));
337 selection->set (miditracks);
339 /* MIDI - Add midi regions one-by-one to corresponding midi tracks */
340 for (vector<PTFFormat::track_t>::iterator a = ptf.miditracks.begin (); a != ptf.miditracks.end (); ++a) {
342 boost::shared_ptr<MidiTrack> midi_track = get_nth_selected_midi_track (a->index);
343 boost::shared_ptr<Playlist> playlist = midi_track->playlist ();
344 samplepos_t f = (samplepos_t)a->reg.startpos * srate / 1920000.;
345 samplecnt_t length = (samplecnt_t)a->reg.length * srate / 1920000.;
346 MusicSample pos (f, 0);
347 boost::shared_ptr<Source> src = _session->create_midi_source_by_stealing_name (midi_track);
349 plist.add (ARDOUR::Properties::start, 0);
350 plist.add (ARDOUR::Properties::length, length);
351 plist.add (ARDOUR::Properties::name, PBD::basename_nosuffix (src->name ()));
352 //printf(" : %d - trackname: (%s)\n", a->index, src->name ().c_str ());
353 boost::shared_ptr<Region> region = (RegionFactory::create (src, plist));
354 /* sets beat position */
355 region->set_position (pos.sample, pos.division);
356 midi_track->playlist ()->add_region (region, pos.sample, 1.0, false, pos.division);
358 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(region);
359 boost::shared_ptr<MidiModel> mm = mr->midi_source (0)->model ();
360 MidiModel::NoteDiffCommand *midicmd;
361 midicmd = mm->new_note_diff_command ("Import ProTools MIDI");
363 for (vector<PTFFormat::midi_ev_t>::iterator j = a->reg.midi.begin (); j != a->reg.midi.end (); ++j) {
364 //printf(" : MIDI : pos=%f len=%f\n", (float)j->pos / 960000., (float)j->length / 960000.);
365 Temporal::Beats start = (Temporal::Beats)(j->pos / 960000.);
366 Temporal::Beats len = (Temporal::Beats)(j->length / 960000.);
367 /* PT C-2 = 0, Ardour C-1 = 0, subtract twelve to convert ? */
368 midicmd->add (boost::shared_ptr<Evoral::Note<Temporal::Beats> > (new Evoral::Note<Temporal::Beats> ((uint8_t)1, start, len, j->note, j->velocity)));
370 mm->apply_command (_session, midicmd);
371 boost::shared_ptr<Region> copy (RegionFactory::create (mr, true));
372 playlist->clear_changes ();
373 playlist->add_region (copy, f);
376 import_status.sources.clear ();
379 _session->save_state ("");
381 import_status.all_done = true;