better comments
[ardour.git] / libs / ardour / import_pt.cc
1 /*
2  * Copyright (C) 2018-2019 Damien Zammit <damien@zamaudio.com>
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 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.
17  */
18
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/time.h>
22 #include <errno.h>
23 #include <unistd.h>
24 #include <algorithm>
25 #include <glibmm.h>
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 "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"
48
49 #include "ptformat/ptformat.h"
50
51 #include "pbd/i18n.h"
52
53 using namespace std;
54 using namespace ARDOUR;
55 using namespace PBD;
56 using namespace Glib;
57 using std::string;
58
59 /* Functions supporting the incorporation of PT sessions into ardour */
60
61 struct midipair {
62         midipair (uint16_t idx, string n)
63                 : ptfindex (idx)
64                   , trname (n)
65         {}
66         uint16_t ptfindex;
67         string trname;
68 };
69
70 typedef struct ptflookup {
71         uint16_t index1;
72         uint16_t index2;
73         PBD::ID  id;
74
75         bool operator ==(const struct ptflookup& other) {
76                 return (this->index1 == other.index1);
77         }
78 } ptflookup_t;
79
80
81 bool
82 Session::import_sndfile_as_region (string path, SrcQuality quality, samplepos_t& pos, SourceList& sources, ImportStatus& status)
83 {
84         /* Import the source */
85         status.paths.clear();
86         status.paths.push_back(path);
87         status.current = 1;
88         status.total = 1;
89         status.freeze = false;
90         status.quality = quality;
91         status.replace_existing_source = false;
92         status.split_midi_channels = false;
93         status.done = false;
94         status.cancel = false;
95
96         import_files(status);
97         sources.clear();
98
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
102          */
103         if (status.cancel && status.current > 1) {
104                 /* Succeeded to import file, assume user hit cancel */
105                 return false;
106         } else if (status.cancel && status.current == 1) {
107                 /* Failed to import file, assume user did not hit cancel */
108                 status.cancel = false;
109                 return false;
110         }
111
112         sources.push_back(status.sources.front());
113
114         /* Put the source on a region */
115         vector<boost::shared_ptr<Region> > regions;
116         string region_name;
117         bool use_timestamp;
118
119         use_timestamp = (pos == -1);
120
121         /* take all the sources we have and package them up as a region */
122
123         region_name = region_name_from_path (status.paths.front(), (sources.size() > 1), false);
124
125         /* we checked in import_sndfiles() that there were not too many */
126
127         while (RegionFactory::region_by_name (region_name)) {
128                 region_name = bump_name_once (region_name, '.');
129         }
130
131         PropertyList plist;
132
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);
139
140         boost::shared_ptr<Region> r = RegionFactory::create (sources, plist);
141
142         if (use_timestamp && boost::dynamic_pointer_cast<AudioRegion>(r)) {
143                 boost::dynamic_pointer_cast<AudioRegion>(r)->special_set_position(sources[0]->natural_position());
144         }
145
146         regions.push_back (r);
147
148         /* if we're creating a new track, name it after the cleaned-up
149          * and "merged" region name.
150          */
151
152         int n = 0;
153
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);
156
157                 if (use_timestamp) {
158                         if (ar) {
159
160                                 /* get timestamp for this region */
161
162                                 const boost::shared_ptr<Source> s (ar->sources().front());
163                                 const boost::shared_ptr<AudioSource> as = boost::dynamic_pointer_cast<AudioSource> (s);
164
165                                 assert (as);
166
167                                 if (as->natural_position() != 0) {
168                                         pos = as->natural_position();
169                                 } else {
170                                         pos = 0;
171                                 }
172                         } else {
173                                 /* should really get first position in MIDI file, but for now, use 0 */
174                                 pos = 0;
175                         }
176                 }
177         }
178
179         for (SourceList::iterator x = sources.begin(); x != sources.end(); ++x) {
180                 SourceFactory::setup_peakfile (*x, true);
181         }
182
183         return true;
184 }
185
186
187 void
188 Session::import_pt (PTFFormat& ptf, ImportStatus& status)
189 {
190         vector<boost::shared_ptr<Region> > regions;
191         boost::shared_ptr<ARDOUR::Track> track;
192         ARDOUR::PluginInfoPtr instrument;
193         vector<string> to_import;
194         string fullpath;
195         bool ok = false;
196         bool onefailed = false;
197         samplepos_t pos = -1;
198         uint32_t srate = sample_rate ();
199
200         vector<ptflookup_t> ptfwavpair;
201         vector<ptflookup_t> ptfregpair;
202         vector<PTFFormat::wav_t>::const_iterator w;
203
204         SourceList just_one_src;
205         SourceList imported;
206
207         boost::shared_ptr<AudioTrack> existing_track;
208         uint16_t nth = 0;
209         vector<ptflookup_t> usedtracks;
210         ptflookup_t utr;
211
212         Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock (), Glib::Threads::NOT_LOCK);
213
214         for (w = ptf.audiofiles ().begin (); w != ptf.audiofiles ().end () && !status.cancel; ++w) {
215                 ptflookup_t p;
216                 ok = false;
217                 /* Try audio file */
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);
223                 } else {
224                         /* Try fade file */
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);
230                         } else {
231                                 onefailed = true;
232
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
237                                  */
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 ());
246                                         p.index1 = w->index;
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;
251                                 } else {
252                                         warning << string_compose (_("PT Import : MISSING `%1`, please check Audio Files"), fullpath) << endmsg;
253                                 }
254                         }
255                 }
256                 if (ok) {
257                         p.index1 = w->index;
258                         p.id = just_one_src.back ()->id ();
259
260                         ptfwavpair.push_back (p);
261                         imported.push_back (just_one_src.back ());
262                 }
263         }
264
265         if (imported.empty ()) {
266                 error << _("Failed to find any audio for PT import") << endmsg;
267                 goto trymidi;
268         } else if (onefailed) {
269                 warning << _("Failed to load one or more of the audio files for PT import, see above list") << endmsg;
270         } else {
271                 info << _("All audio files found for PT import!") << endmsg;
272         }
273
274         lx.acquire();
275         save_state("");
276
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 */
285                                                 ptflookup_t rp;
286                                                 PropertyList plist;
287
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);
295
296                                                 just_one_src.clear ();
297                                                 just_one_src.push_back (*x);
298
299                                                 boost::shared_ptr<Region> r = RegionFactory::create (just_one_src, plist);
300                                                 regions.push_back (r);
301
302                                                 rp.id = regions.back ()->id ();
303                                                 rp.index1 = a->index;
304                                                 ptfregpair.push_back (rp);
305                                         }
306                                 }
307                         }
308                 }
309         }
310
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) {
314
315                         if (p->index1 == a->reg.index)  {
316
317                                 /* Matched a ptf active region to an ardour region */
318                                 utr.index1 = a->index;
319                                 utr.index2 = nth;
320                                 utr.id = p->id;
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));
326
327                                         /* Use existing track if possible */
328                                         existing_track = get_nth_audio_track (found->index2 + 1);
329                                         if (!existing_track) {
330                                                 lx.release();
331                                                 list<boost::shared_ptr<AudioTrack> > at (new_audio_track (1, 2, 0, 1, "", PresentationInfo::max_order, Normal));
332                                                 if (at.empty ()) {
333                                                         return;
334                                                 }
335                                                 lx.acquire();
336                                                 existing_track = at.back ();
337                                         }
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));
344                                 } else {
345                                         lx.release();
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));
349                                         if (at.empty ()) {
350                                                 return;
351                                         }
352                                         lx.acquire();
353                                         existing_track = at.back ();
354                                         std::string trackname;
355                                         try {
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);
359                                         }
360                                         /* generate a unique name by adding a number if needed */
361                                         uint32_t id = 0;
362                                         if (!find_route_name (trackname.c_str (), id, trackname, false)) {
363                                                 fatal << _("PTImport: UINT_MAX routes? impossible!") << endmsg;
364                                                 abort(); /*NOTREACHED*/
365                                         }
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));
372                                         nth++;
373                                 }
374                                 usedtracks.push_back (utr);
375
376                                 save_state("");
377                         }
378                 }
379         }
380
381         lx.release();
382
383 trymidi:
384         status.paths.clear();
385         status.paths.push_back(ptf.path ());
386         status.current = 1;
387         status.total = 1;
388         status.freeze = false;
389         status.done = false;
390         status.cancel = false;
391         status.progress = 0;
392
393         /* MIDI - Find list of unique midi tracks first */
394
395         vector<midipair> uniquetr;
396
397         for (vector<PTFFormat::track_t>::const_iterator a = ptf.miditracks ().begin (); a != ptf.miditracks ().end (); ++a) {
398                 bool found = false;
399                 for (vector<midipair>::iterator b = uniquetr.begin (); b != uniquetr.end (); ++b) {
400                         if (b->trname == a->name) {
401                                 found = true;
402                                 break;
403                         }
404                 }
405                 if (!found) {
406                         uniquetr.push_back (midipair (a->index, a->name));
407                         //printf(" : %d : %s\n", a->index, a->name.c_str());
408                 }
409         }
410
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) {
414                 ptflookup_t miditr;
415                 list<boost::shared_ptr<MidiTrack> > mt (new_midi_track (
416                                 ChanCount (DataType::MIDI, 1),
417                                 ChanCount (DataType::MIDI, 1),
418                                 true,
419                                 instrument, (Plugin::PresetRecord*) 0,
420                                 (RouteGroup*) 0,
421                                 1,
422                                 a->trname,
423                                 PresentationInfo::max_order,
424                                 Normal));
425                 assert (mt.size () == 1);
426                 midi_tracks[a->ptfindex] = mt.front ();
427         }
428
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) {
431
432                 boost::shared_ptr<MidiTrack> midi_track = midi_tracks[a->index];
433                 assert (midi_track);
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);
439                 PropertyList plist;
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);
448
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");
453
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)));
460                 }
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);
465         }
466
467         status.progress = 1.0;
468         status.done = true;
469         save_state("");
470         status.sources.clear ();
471         status.all_done = true;
472 }