2 * Copyright (C) 2018-2019 Damien Zammit <damien@zamaudio.com>
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 along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #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/audioengine.h"
33 #include "ardour/audio_track.h"
34 #include "ardour/audiofilesource.h"
35 #include "ardour/audioregion.h"
36 #include "ardour/import_status.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/ptformat.h"
54 using namespace ARDOUR;
59 /* Functions supporting the incorporation of PT sessions into ardour */
62 midipair (uint16_t idx, string n)
70 typedef struct ptflookup {
75 bool operator ==(const struct ptflookup& other) {
76 return (this->index1 == other.index1);
82 Session::import_sndfile_as_region (string path, SrcQuality quality, samplepos_t& pos, SourceList& sources, ImportStatus& status)
84 /* Import the source */
86 status.paths.push_back(path);
89 status.freeze = false;
90 status.quality = quality;
91 status.replace_existing_source = false;
92 status.split_midi_channels = false;
94 status.cancel = false;
99 /* FIXME: There is no way to tell if cancel button was pressed
100 * or if the file failed to import, just that one of these occurred.
101 * We want status.cancel to reflect the user's choice only
103 if (status.cancel && status.current > 1) {
104 /* Succeeded to import file, assume user hit cancel */
106 } else if (status.cancel && status.current == 1) {
107 /* Failed to import file, assume user did not hit cancel */
108 status.cancel = false;
112 sources.push_back(status.sources.front());
114 /* Put the source on a region */
115 vector<boost::shared_ptr<Region> > regions;
119 use_timestamp = (pos == -1);
121 /* take all the sources we have and package them up as a region */
123 region_name = region_name_from_path (status.paths.front(), (sources.size() > 1), false);
125 /* we checked in import_sndfiles() that there were not too many */
127 while (RegionFactory::region_by_name (region_name)) {
128 region_name = bump_name_once (region_name, '.');
133 plist.add (ARDOUR::Properties::start, 0);
134 plist.add (ARDOUR::Properties::length, sources[0]->length (pos));
135 plist.add (ARDOUR::Properties::name, region_name);
136 plist.add (ARDOUR::Properties::layer, 0);
137 plist.add (ARDOUR::Properties::whole_file, true);
138 plist.add (ARDOUR::Properties::external, true);
140 boost::shared_ptr<Region> r = RegionFactory::create (sources, plist);
142 if (use_timestamp && boost::dynamic_pointer_cast<AudioRegion>(r)) {
143 boost::dynamic_pointer_cast<AudioRegion>(r)->special_set_position(sources[0]->natural_position());
146 regions.push_back (r);
148 /* if we're creating a new track, name it after the cleaned-up
149 * and "merged" region name.
154 for (vector<boost::shared_ptr<Region> >::iterator r = regions.begin(); r != regions.end(); ++r, ++n) {
155 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (*r);
160 /* get timestamp for this region */
162 const boost::shared_ptr<Source> s (ar->sources().front());
163 const boost::shared_ptr<AudioSource> as = boost::dynamic_pointer_cast<AudioSource> (s);
167 if (as->natural_position() != 0) {
168 pos = as->natural_position();
173 /* should really get first position in MIDI file, but for now, use 0 */
179 for (SourceList::iterator x = sources.begin(); x != sources.end(); ++x) {
180 SourceFactory::setup_peakfile (*x, true);
188 Session::import_pt (PTFFormat& ptf, ImportStatus& status)
190 vector<boost::shared_ptr<Region> > regions;
191 boost::shared_ptr<ARDOUR::Track> track;
192 ARDOUR::PluginInfoPtr instrument;
193 vector<string> to_import;
196 bool onefailed = false;
197 samplepos_t pos = -1;
198 uint32_t srate = sample_rate ();
200 vector<ptflookup_t> ptfwavpair;
201 vector<ptflookup_t> ptfregpair;
202 vector<PTFFormat::wav_t>::const_iterator w;
204 SourceList just_one_src;
207 boost::shared_ptr<AudioTrack> existing_track;
209 vector<ptflookup_t> usedtracks;
212 Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock (), Glib::Threads::NOT_LOCK);
214 for (w = ptf.audiofiles ().begin (); w != ptf.audiofiles ().end () && !status.cancel; ++w) {
218 fullpath = Glib::build_filename (Glib::path_get_dirname (ptf.path ()), "Audio Files");
219 fullpath = Glib::build_filename (fullpath, w->filename);
220 if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS)) {
221 just_one_src.clear();
222 ok = import_sndfile_as_region (fullpath, SrcBest, pos, just_one_src, status);
225 fullpath = Glib::build_filename (Glib::path_get_dirname (ptf.path ()), "Fade Files");
226 fullpath = Glib::build_filename (fullpath, w->filename);
227 if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS)) {
228 just_one_src.clear();
229 ok = import_sndfile_as_region (fullpath, SrcBest, pos, just_one_src, status);
233 /* ptformat knows length of sources *in PT sample rate*
234 * BUT if ardour user later resolves missing file,
235 * it won't be resampled, so we can only do this
236 * when sample rates are matching
238 if (sample_rate () == ptf.sessionrate ()) {
239 /* Insert reference to missing source */
240 samplecnt_t sourcelen = w->length;
241 XMLNode srcxml (X_("Source"));
242 srcxml.set_property ("name", w->filename);
243 srcxml.set_property ("type", "audio");
244 srcxml.set_property ("id", PBD::ID ().to_s ());
245 boost::shared_ptr<Source> source = SourceFactory::createSilent (*this, srcxml, sourcelen, sample_rate ());
247 p.id = source->id ();
248 ptfwavpair.push_back (p);
249 imported.push_back (source);
250 warning << string_compose (_("PT Import : MISSING `%1`, inserting ref to missing source"), fullpath) << endmsg;
252 warning << string_compose (_("PT Import : MISSING `%1`, please check Audio Files"), fullpath) << endmsg;
258 p.id = just_one_src.back ()->id ();
260 ptfwavpair.push_back (p);
261 imported.push_back (just_one_src.back ());
265 if (imported.empty ()) {
266 error << _("Failed to find any audio for PT import") << endmsg;
268 } else if (onefailed) {
269 warning << _("Failed to load one or more of the audio files for PT import, see above list") << endmsg;
271 info << _("All audio files found for PT import!") << endmsg;
277 for (vector<PTFFormat::region_t>::const_iterator a = ptf.regions ().begin ();
278 a != ptf.regions ().end (); ++a) {
279 for (vector<ptflookup_t>::iterator p = ptfwavpair.begin ();
280 p != ptfwavpair.end (); ++p) {
281 if ((p->index1 == a->wave.index) && (strcmp (a->wave.filename.c_str (), "") != 0)) {
282 for (SourceList::iterator x = imported.begin (); x != imported.end (); ++x) {
283 if ((*x)->id () == p->id) {
284 /* Matched an uncreated ptf region to ardour region */
288 plist.add (ARDOUR::Properties::start, a->sampleoffset);
289 plist.add (ARDOUR::Properties::position, 0);
290 plist.add (ARDOUR::Properties::length, a->length);
291 plist.add (ARDOUR::Properties::name, a->name);
292 plist.add (ARDOUR::Properties::layer, 0);
293 plist.add (ARDOUR::Properties::whole_file, false);
294 plist.add (ARDOUR::Properties::external, true);
296 just_one_src.clear ();
297 just_one_src.push_back (*x);
299 boost::shared_ptr<Region> r = RegionFactory::create (just_one_src, plist);
300 regions.push_back (r);
302 rp.id = regions.back ()->id ();
303 rp.index1 = a->index;
304 ptfregpair.push_back (rp);
311 for (vector<PTFFormat::track_t>::const_iterator a = ptf.tracks ().begin (); a != ptf.tracks ().end (); ++a) {
312 for (vector<ptflookup_t>::iterator p = ptfregpair.begin ();
313 p != ptfregpair.end (); ++p) {
315 if (p->index1 == a->reg.index) {
317 /* Matched a ptf active region to an ardour region */
318 utr.index1 = a->index;
321 boost::shared_ptr<Region> r = RegionFactory::region_by_id (p->id);
322 vector<ptflookup_t>::iterator lookuptr = usedtracks.begin ();
323 vector<ptflookup_t>::iterator found;
324 if ((found = std::find (lookuptr, usedtracks.end (), utr)) != usedtracks.end ()) {
325 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));
327 /* Use existing track if possible */
328 existing_track = get_nth_audio_track (found->index2 + 1);
329 if (!existing_track) {
331 list<boost::shared_ptr<AudioTrack> > at (new_audio_track (1, 2, 0, 1, "", PresentationInfo::max_order, Normal));
336 existing_track = at.back ();
338 /* Put on existing track */
339 boost::shared_ptr<Playlist> playlist = existing_track->playlist ();
340 boost::shared_ptr<Region> copy (RegionFactory::create (r, true));
341 playlist->clear_changes ();
342 playlist->add_region (copy, a->reg.startpos);
343 //add_command (new StatefulDiffCommand (playlist));
346 /* Put on a new track */
347 DEBUG_TRACE (DEBUG::FileUtils, string_compose ("\twav(%1) reg(%2) new_tr(%3)\n", a->reg.wave.filename.c_str (), a->reg.index, nth));
348 list<boost::shared_ptr<AudioTrack> > at (new_audio_track (1, 2, 0, 1, "", PresentationInfo::max_order, Normal));
353 existing_track = at.back ();
354 std::string trackname;
356 trackname = Glib::convert_with_fallback (a->name, "UTF-8", "UTF-8", "_");
357 } catch (Glib::ConvertError& err) {
358 trackname = string_compose ("Invalid %1", a->index);
360 /* generate a unique name by adding a number if needed */
362 if (!find_route_name (trackname.c_str (), id, trackname, false)) {
363 fatal << _("PTImport: UINT_MAX routes? impossible!") << endmsg;
364 abort(); /*NOTREACHED*/
366 existing_track->set_name (trackname);
367 boost::shared_ptr<Playlist> playlist = existing_track->playlist();
368 boost::shared_ptr<Region> copy (RegionFactory::create (r, true));
369 playlist->clear_changes ();
370 playlist->add_region (copy, a->reg.startpos);
371 //add_command (new StatefulDiffCommand (playlist));
374 usedtracks.push_back (utr);
384 status.paths.clear();
385 status.paths.push_back(ptf.path ());
388 status.freeze = false;
390 status.cancel = false;
393 /* MIDI - Find list of unique midi tracks first */
395 vector<midipair> uniquetr;
397 for (vector<PTFFormat::track_t>::const_iterator a = ptf.miditracks ().begin (); a != ptf.miditracks ().end (); ++a) {
399 for (vector<midipair>::iterator b = uniquetr.begin (); b != uniquetr.end (); ++b) {
400 if (b->trname == a->name) {
406 uniquetr.push_back (midipair (a->index, a->name));
407 //printf(" : %d : %s\n", a->index, a->name.c_str());
411 std::map <int, boost::shared_ptr<MidiTrack> > midi_tracks;
412 /* MIDI - Create unique midi tracks and a lookup table for used tracks */
413 for (vector<midipair>::iterator a = uniquetr.begin (); a != uniquetr.end (); ++a) {
415 list<boost::shared_ptr<MidiTrack> > mt (new_midi_track (
416 ChanCount (DataType::MIDI, 1),
417 ChanCount (DataType::MIDI, 1),
419 instrument, (Plugin::PresetRecord*) 0,
423 PresentationInfo::max_order,
425 assert (mt.size () == 1);
426 midi_tracks[a->ptfindex] = mt.front ();
429 /* MIDI - Add midi regions one-by-one to corresponding midi tracks */
430 for (vector<PTFFormat::track_t>::const_iterator a = ptf.miditracks ().begin (); a != ptf.miditracks ().end (); ++a) {
432 boost::shared_ptr<MidiTrack> midi_track = midi_tracks[a->index];
434 boost::shared_ptr<Playlist> playlist = midi_track->playlist ();
435 samplepos_t f = (samplepos_t)a->reg.startpos * srate / 1920000.;
436 samplecnt_t length = (samplecnt_t)a->reg.length * srate / 1920000.;
437 MusicSample pos (f, 0);
438 boost::shared_ptr<Source> src = create_midi_source_by_stealing_name (midi_track);
440 plist.add (ARDOUR::Properties::start, 0);
441 plist.add (ARDOUR::Properties::length, length);
442 plist.add (ARDOUR::Properties::name, PBD::basename_nosuffix (src->name ()));
443 //printf(" : %d - trackname: (%s)\n", a->index, src->name ().c_str ());
444 boost::shared_ptr<Region> region = (RegionFactory::create (src, plist));
445 /* sets beat position */
446 region->set_position (pos.sample, pos.division);
447 midi_track->playlist ()->add_region (region, pos.sample, 1.0, false, pos.division);
449 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(region);
450 boost::shared_ptr<MidiModel> mm = mr->midi_source (0)->model ();
451 MidiModel::NoteDiffCommand *midicmd;
452 midicmd = mm->new_note_diff_command ("Import ProTools MIDI");
454 for (vector<PTFFormat::midi_ev_t>::const_iterator j = a->reg.midi.begin (); j != a->reg.midi.end (); ++j) {
455 //printf(" : MIDI : pos=%f len=%f\n", (float)j->pos / 960000., (float)j->length / 960000.);
456 Temporal::Beats start = (Temporal::Beats)(j->pos / 960000.);
457 Temporal::Beats len = (Temporal::Beats)(j->length / 960000.);
458 /* PT C-2 = 0, Ardour C-1 = 0, subtract twelve to convert ? */
459 midicmd->add (boost::shared_ptr<Evoral::Note<Temporal::Beats> > (new Evoral::Note<Temporal::Beats> ((uint8_t)1, start, len, j->note, j->velocity)));
461 mm->apply_command (this, midicmd);
462 boost::shared_ptr<Region> copy (RegionFactory::create (mr, true));
463 playlist->clear_changes ();
464 playlist->add_region (copy, f);
467 status.progress = 1.0;
470 status.sources.clear ();
471 status.all_done = true;