From 23faf45fcf184813712d2dac3aa955b4da20d4af Mon Sep 17 00:00:00 2001 From: Damien Zammit Date: Fri, 28 Sep 2018 23:34:29 +1000 Subject: PT import: Refactor away from Editor into Session --- gtk2_ardour/editor.h | 21 +- gtk2_ardour/editor_pt_import.cc | 308 +++++----------------------- libs/ardour/ardour/session.h | 6 + libs/ardour/import_pt.cc | 433 ++++++++++++++++++++++++++++++++++++++++ libs/ardour/session.cc | 20 ++ libs/ardour/wscript | 2 + libs/ptformat/wscript | 2 +- 7 files changed, 519 insertions(+), 273 deletions(-) create mode 100644 libs/ardour/import_pt.cc diff --git a/gtk2_ardour/editor.h b/gtk2_ardour/editor.h index 1f66a69c78..5a91582d10 100644 --- a/gtk2_ardour/editor.h +++ b/gtk2_ardour/editor.h @@ -61,6 +61,8 @@ #include "selection_memento.h" #include "tempo_curve.h" +#include + namespace Gtkmm2ext { class Bindings; } @@ -1346,18 +1348,6 @@ private: void insert_region_list_selection (float times); - /* PT import */ - void external_pt_dialog (); - typedef struct ptflookup { - uint16_t index1; - uint16_t index2; - PBD::ID id; - - bool operator ==(const struct ptflookup& other) { - return (this->index1 == other.index1); - } - } ptflookup_t; - /* import & embed */ void add_external_audio_action (Editing::ImportMode); @@ -1427,6 +1417,13 @@ private: void external_audio_dialog (); void session_import_dialog (); + /* PT import specific */ + void external_pt_dialog (); + ARDOUR::ImportStatus import_pt_status; + static void *_import_pt_thread (void *); + void* import_pt_thread (); + PTFFormat import_ptf; + /* import specific info */ struct EditorImportStatus : public ARDOUR::ImportStatus { diff --git a/gtk2_ardour/editor_pt_import.cc b/gtk2_ardour/editor_pt_import.cc index db9fc03b2d..f83a76e295 100644 --- a/gtk2_ardour/editor_pt_import.cc +++ b/gtk2_ardour/editor_pt_import.cc @@ -69,7 +69,7 @@ using namespace Gtkmm2ext; using namespace Editing; using std::string; -/* Functions supporting the incorporation of PT sessions into ardour */ +/* Editor dialogs supporting the incorporation of PT sessions into ardour */ void Editor::external_pt_dialog () @@ -100,279 +100,67 @@ Editor::external_pt_dialog () } if (ptpath.length ()) { - do_ptimport (ptpath, SrcBest); - break; - } - - if (result == Gtk::RESPONSE_CANCEL) { - break; - } - } -} - -struct midipair { - midipair (uint16_t idx, string n) - : ptfindex (idx) - , trname (n) - {} - uint16_t ptfindex; - string trname; -}; - -void -Editor::do_ptimport (std::string ptpath, - SrcQuality quality) -{ - vector > regions; - boost::shared_ptr track; - ARDOUR::PluginInfoPtr instrument; - vector to_import; - string fullpath; - bool ok = false; - bool onefailed = false; - PTFFormat ptf; - samplepos_t pos = -1; - uint32_t srate = _session->sample_rate (); - - vector ptfwavpair; - vector ptfregpair; - - if (ptf.load (ptpath, srate) == -1) { - MessageDialog msg (_("Doesn't seem to be a valid PT session file")); - msg.run (); - return; - } else { - 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.tracks.size (), ptf.midiregions.size (), ptf.miditracks.size ())); - msg.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); - - int result = msg.run (); - if (result != Gtk::RESPONSE_OK) { - return; - } - } - current_interthread_info = &import_status; - import_status.current = 1; - import_status.total = ptf.audiofiles.size (); - import_status.all_done = false; - - ImportProgressWindow ipw (&import_status, _("Import"), _("Cancel Import")); - - SourceList just_one; - SourceList imported; - - for (vector::iterator a = ptf.audiofiles.begin (); a != ptf.audiofiles.end (); ++a) { - ptflookup_t p; - - fullpath = Glib::build_filename (Glib::path_get_dirname (ptpath), "Audio Files"); - fullpath = Glib::build_filename (fullpath, a->filename); - to_import.clear (); - to_import.push_back (fullpath); - ipw.show (); - ok = import_sndfiles (to_import, Editing::ImportDistinctFiles, Editing::ImportAsRegion, quality, pos, 1, -1, track, false, instrument); - if (!import_status.sources.empty ()) { - p.index1 = a->index; - p.id = import_status.sources.back ()->id (); - - ptfwavpair.push_back (p); - imported.push_back (import_status.sources.back ()); - } else { - /* ptformat does not know the length of sources so we cannot do this: - XMLNode srcxml (X_("Source")); - srcxml.set_property ("name", a->filename); - srcxml.set_property ("type", "audio"); - srcxml.set_property ("id", PBD::ID ().to_s ()); - boost::shared_ptr source = SourceFactory::createSilent (*_session, srcxml, a->length, _session->sample_rate ()); - p.index1 = a->index; - p.id = source->id (); - ptfwavpair.push_back (p); - imported.push_back (source); - */ - onefailed = true; - } - } - - if (onefailed) { - MessageDialog msg (_("Failed to load one or more of the audio files. Click to attempt partial import.")); - msg.run (); - } else { - MessageDialog msg (_("Success! All audio files found. Click to complete import.")); - msg.run (); - } - - for (vector::iterator a = ptf.regions.begin (); - a != ptf.regions.end (); ++a) { - for (vector::iterator p = ptfwavpair.begin (); - p != ptfwavpair.end (); ++p) { - if ((p->index1 == a->wave.index) && (strcmp (a->wave.filename.c_str (), "") != 0)) { - for (SourceList::iterator x = imported.begin (); x != imported.end (); ++x) { - if ((*x)->id () == p->id) { - /* Matched an uncreated ptf region to ardour region */ - ptflookup_t rp; - PropertyList plist; + uint32_t srate = _session->sample_rate (); - plist.add (ARDOUR::Properties::start, a->sampleoffset); - plist.add (ARDOUR::Properties::position, 0); - plist.add (ARDOUR::Properties::length, a->length); - plist.add (ARDOUR::Properties::name, a->name); - plist.add (ARDOUR::Properties::layer, 0); - plist.add (ARDOUR::Properties::whole_file, false); - plist.add (ARDOUR::Properties::external, true); - - just_one.clear (); - just_one.push_back (*x); - - boost::shared_ptr r = RegionFactory::create (just_one, plist); - regions.push_back (r); - - rp.id = regions.back ()->id (); - rp.index1 = a->index; - ptfregpair.push_back (rp); - } + if (import_ptf.load (ptpath, srate) == -1) { + MessageDialog msg (_("Doesn't seem to be a valid PT session file")); + msg.run (); + return; + } else { + 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)import_ptf.version, import_ptf.sessionrate, import_ptf.audiofiles.size (), import_ptf.regions.size (), import_ptf.tracks.size (), import_ptf.midiregions.size (), import_ptf.miditracks.size ())); + msg.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); + + int result = msg.run (); + if (result != Gtk::RESPONSE_OK) { + return; } } - } - } - boost::shared_ptr existing_track; - uint16_t nth = 0; - vector usedtracks; - ptflookup_t utr; + ImportProgressWindow ipw (&import_pt_status, _("PT Import"), _("Cancel Import")); + pthread_create_and_store ("import_pt", &import_pt_status.thread, _import_pt_thread, this); + pthread_detach (import_pt_status.thread); - for (vector::iterator a = ptf.tracks.begin (); a != ptf.tracks.end (); ++a) { - for (vector::iterator p = ptfregpair.begin (); - p != ptfregpair.end (); ++p) { + ipw.show(); - if (p->index1 == a->reg.index) { - /* Matched a ptf active region to an ardour region */ - utr.index1 = a->index; - utr.index2 = nth; - utr.id = p->id; - boost::shared_ptr r = RegionFactory::region_by_id (p->id); - vector::iterator lookuptr = usedtracks.begin (); - vector::iterator found; - if ((found = std::find (lookuptr, usedtracks.end (), utr)) != usedtracks.end ()) { - 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)); - existing_track = get_nth_selected_audio_track (found->index2); // FIXME, don't rely on selection - /* Put on existing track */ - boost::shared_ptr playlist = existing_track->playlist (); - boost::shared_ptr copy (RegionFactory::create (r, true)); - playlist->clear_changes (); - playlist->add_region (copy, a->reg.startpos); - //_session->add_command (new StatefulDiffCommand (playlist)); - } else { - /* Put on a new track */ - DEBUG_TRACE (DEBUG::FileUtils, string_compose ("\twav(%1) reg(%2) new_tr(%3)\n", a->reg.wave.filename.c_str (), a->reg.index, nth)); - list > at (_session->new_audio_track (1, 2, 0, 1, "", PresentationInfo::max_order, Normal)); - if (at.empty ()) { - return; - } - existing_track = at.back (); - std::string trackname; - try { - trackname = Glib::convert_with_fallback (a->name, "UTF-8", "UTF-8", "_"); - } catch (Glib::ConvertError& err) { - trackname = string_compose ("Invalid %1", a->index); - } - /* TODO legalize track name (no slashes, no colons) */ -#if 0 /* TODO -- "find_route_name" is currently private */ - /* generate a unique name by adding a number if needed */ - uint32_t id = 0; - if (!_session->find_route_name (trackname.c_str (), id, trackname, false)) { - fatal << _("PTImport: UINT_MAX routes? impossible!") << endmsg; - abort(); /*NOTREACHED*/ - } -#endif - existing_track->set_name (trackname); - boost::shared_ptr playlist = existing_track->playlist(); - boost::shared_ptr copy (RegionFactory::create (r, true)); - playlist->clear_changes (); - playlist->add_region (copy, a->reg.startpos); - //_session->add_command (new StatefulDiffCommand (playlist)); - nth++; - } - usedtracks.push_back (utr); - } - } - } + import_pt_status.all_done = false; - /* MIDI - Find list of unique midi tracks first */ + while (!import_pt_status.done && !import_pt_status.cancel) { + gtk_main_iteration (); + } - vector uniquetr; + // wait for thread to terminate + while (!import_pt_status.all_done) { + gtk_main_iteration (); + } - for (vector::iterator a = ptf.miditracks.begin (); a != ptf.miditracks.end (); ++a) { - bool found = false; - for (vector::iterator b = uniquetr.begin (); b != uniquetr.end (); ++b) { - if (b->trname == a->name) { - found = true; - break; + if (import_pt_status.cancel) { + MessageDialog msg (_("PT import was cancelled,\ncheck session for incomplete import")); + msg.run (); + } else { + MessageDialog msg (_("PT import complete!")); + msg.run (); } + break; } - if (!found) { - uniquetr.push_back (midipair (a->index, a->name)); - //printf(" : %d : %s\n", a->index, a->name.c_str()); - } - } - - std::map > midi_tracks; - /* MIDI - Create unique midi tracks and a lookup table for used tracks */ - for (vector::iterator a = uniquetr.begin (); a != uniquetr.end (); ++a) { - ptflookup_t miditr; - list > mt (_session->new_midi_track ( - ChanCount (DataType::MIDI, 1), - ChanCount (DataType::MIDI, 1), - true, - instrument, (Plugin::PresetRecord*) 0, - (RouteGroup*) 0, - 1, - a->trname, - PresentationInfo::max_order, - Normal)); - assert (mt.size () == 1); - midi_tracks[a->ptfindex] = mt.front (); - } - /* MIDI - Add midi regions one-by-one to corresponding midi tracks */ - for (vector::iterator a = ptf.miditracks.begin (); a != ptf.miditracks.end (); ++a) { - - boost::shared_ptr midi_track = midi_tracks[a->index]; - assert (midi_track); - boost::shared_ptr playlist = midi_track->playlist (); - samplepos_t f = (samplepos_t)a->reg.startpos * srate / 1920000.; - samplecnt_t length = (samplecnt_t)a->reg.length * srate / 1920000.; - MusicSample pos (f, 0); - boost::shared_ptr src = _session->create_midi_source_by_stealing_name (midi_track); - PropertyList plist; - plist.add (ARDOUR::Properties::start, 0); - plist.add (ARDOUR::Properties::length, length); - plist.add (ARDOUR::Properties::name, PBD::basename_nosuffix (src->name ())); - //printf(" : %d - trackname: (%s)\n", a->index, src->name ().c_str ()); - boost::shared_ptr region = (RegionFactory::create (src, plist)); - /* sets beat position */ - region->set_position (pos.sample, pos.division); - midi_track->playlist ()->add_region (region, pos.sample, 1.0, false, pos.division); - - boost::shared_ptr mr = boost::dynamic_pointer_cast(region); - boost::shared_ptr mm = mr->midi_source (0)->model (); - MidiModel::NoteDiffCommand *midicmd; - midicmd = mm->new_note_diff_command ("Import ProTools MIDI"); - - for (vector::iterator j = a->reg.midi.begin (); j != a->reg.midi.end (); ++j) { - //printf(" : MIDI : pos=%f len=%f\n", (float)j->pos / 960000., (float)j->length / 960000.); - Temporal::Beats start = (Temporal::Beats)(j->pos / 960000.); - Temporal::Beats len = (Temporal::Beats)(j->length / 960000.); - /* PT C-2 = 0, Ardour C-1 = 0, subtract twelve to convert ? */ - midicmd->add (boost::shared_ptr > (new Evoral::Note ((uint8_t)1, start, len, j->note, j->velocity))); + if (result == Gtk::RESPONSE_CANCEL) { + break; } - mm->apply_command (_session, midicmd); - boost::shared_ptr copy (RegionFactory::create (mr, true)); - playlist->clear_changes (); - playlist->add_region (copy, f); } +} - import_status.sources.clear (); +void * +Editor::_import_pt_thread (void *arg) +{ + SessionEvent::create_per_thread_pool ("import pt events", 2048); - if (ok) { - _session->save_state (""); - } - import_status.all_done = true; + Editor *ed = (Editor *) arg; + return ed->import_pt_thread (); +} + +void * +Editor::import_pt_thread () +{ + _session->import_pt (import_ptf, import_pt_status); + return 0; } diff --git a/libs/ardour/ardour/session.h b/libs/ardour/ardour/session.h index 6e7ec93dec..70e3c7df38 100644 --- a/libs/ardour/ardour/session.h +++ b/libs/ardour/ardour/session.h @@ -41,6 +41,7 @@ #include #include +#include #include "pbd/error.h" #include "pbd/event_loop.h" @@ -302,6 +303,8 @@ public: boost::shared_ptr get_routes_with_internal_returns() const; boost::shared_ptr get_routes_with_regions_at (samplepos_t const) const; + boost::shared_ptr get_nth_audio_track (int nth) const; + uint32_t nstripables (bool with_monitor = false) const; uint32_t nroutes() const { return routes.reader()->size(); } uint32_t ntracks () const; @@ -1197,6 +1200,9 @@ public: bool transport_master_is_external() const; boost::shared_ptr transport_master() const; + void import_pt (PTFFormat& ptf, ImportStatus& status); + bool import_sndfile_as_region (std::string path, SrcQuality quality, samplepos_t& pos, SourceList& sources, ImportStatus& status); + protected: friend class AudioEngine; void set_block_size (pframes_t nframes); diff --git a/libs/ardour/import_pt.cc b/libs/ardour/import_pt.cc new file mode 100644 index 0000000000..c30030d10c --- /dev/null +++ b/libs/ardour/import_pt.cc @@ -0,0 +1,433 @@ +/* + Copyright (C) 2000-2006 Paul Davis + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#include +#include +#include +#include +#include +#include +#include + +#include "pbd/pthread_utils.h" +#include "pbd/basename.h" +#include "pbd/shortpath.h" +#include "pbd/stateful_diff_command.h" + +#include "ardour/audio_track.h" +#include "ardour/audiofilesource.h" +#include "ardour/audioregion.h" +#include "ardour/import_status.h" +#include "ardour/midi_region.h" +#include "ardour/midi_track.h" +#include "ardour/midi_model.h" +#include "ardour/operations.h" +#include "ardour/region_factory.h" +#include "ardour/smf_source.h" +#include "ardour/source_factory.h" +#include "ardour/utils.h" +#include "ardour/playlist.h" +#include "ardour/session.h" +#include "pbd/memento_command.h" + +#include "ptformat/ptfformat.h" + +#include "pbd/i18n.h" + +using namespace std; +using namespace ARDOUR; +using namespace PBD; +using namespace Glib; +using std::string; + +/* Functions supporting the incorporation of PT sessions into ardour */ + +struct midipair { + midipair (uint16_t idx, string n) + : ptfindex (idx) + , trname (n) + {} + uint16_t ptfindex; + string trname; +}; + +typedef struct ptflookup { + uint16_t index1; + uint16_t index2; + PBD::ID id; + + bool operator ==(const struct ptflookup& other) { + return (this->index1 == other.index1); + } +} ptflookup_t; + + +bool +Session::import_sndfile_as_region (string path, SrcQuality quality, samplepos_t& pos, SourceList& sources, ImportStatus& status) +{ + /* Import the source */ + vector paths; + paths.push_back(path); + + status.current = 1; + status.total = 1; + status.paths = paths; + status.freeze = false; + status.quality = quality; + status.replace_existing_source = false; + status.split_midi_channels = false; + status.done = false; + status.cancel = false; + + import_files(status); + sources = status.sources; + + /* Return early if no source was imported */ + if (sources.size() == 0) { + return false; + } + + /* Put the source on a region */ + vector > regions; + string region_name; + bool use_timestamp; + vector track_names; + + use_timestamp = (pos == -1); + + /* take all the sources we have and package them up as a region */ + + region_name = region_name_from_path (paths.front(), (sources.size() > 1), false); + + /* we checked in import_sndfiles() that there were not too many */ + + while (RegionFactory::region_by_name (region_name)) { + region_name = bump_name_once (region_name, '.'); + } + + PropertyList plist; + + plist.add (ARDOUR::Properties::start, 0); + plist.add (ARDOUR::Properties::length, sources[0]->length (pos)); + plist.add (ARDOUR::Properties::name, region_name); + plist.add (ARDOUR::Properties::layer, 0); + plist.add (ARDOUR::Properties::whole_file, true); + plist.add (ARDOUR::Properties::external, true); + + boost::shared_ptr r = RegionFactory::create (sources, plist); + + if (use_timestamp && boost::dynamic_pointer_cast(r)) { + boost::dynamic_pointer_cast(r)->special_set_position(sources[0]->natural_position()); + } + + regions.push_back (r); + + /* if we're creating a new track, name it after the cleaned-up + * and "merged" region name. + */ + + track_names.push_back (region_name); + + int n = 0; + + begin_reversible_command (Operations::insert_file); + + /* we only use tracks names when importing to new tracks, but we + * require that one is defined for every region, just to keep + * the API simpler. + */ + assert (regions.size() == track_names.size()); + + for (vector >::iterator r = regions.begin(); r != regions.end(); ++r, ++n) { + boost::shared_ptr ar = boost::dynamic_pointer_cast (*r); + + if (use_timestamp) { + if (ar) { + + /* get timestamp for this region */ + + const boost::shared_ptr s (ar->sources().front()); + const boost::shared_ptr as = boost::dynamic_pointer_cast (s); + + assert (as); + + if (as->natural_position() != 0) { + pos = as->natural_position(); + } else { + pos = 0; + } + } else { + /* should really get first position in MIDI file, but for now, use 0 */ + pos = 0; + } + } + } + + commit_reversible_command (); + + /* setup peak file building in another thread */ + + for (SourceList::iterator x = sources.begin(); x != sources.end(); ++x) { + SourceFactory::setup_peakfile (*x, true); + } + + return true; +} + + +void +Session::import_pt (PTFFormat& ptf, ImportStatus& status) +{ + vector > regions; + boost::shared_ptr track; + ARDOUR::PluginInfoPtr instrument; + vector to_import; + string fullpath; + bool ok = false; + bool onefailed = false; + samplepos_t pos = -1; + uint32_t srate = sample_rate (); + + vector ptfwavpair; + vector ptfregpair; + + SourceList just_one_src; + SourceList imported; + + for (vector::iterator a = ptf.audiofiles.begin (); a != ptf.audiofiles.end (); ++a) { + ptflookup_t p; + + fullpath = Glib::build_filename (Glib::path_get_dirname (ptf.path), "Audio Files"); + fullpath = Glib::build_filename (fullpath, a->filename); + ok = import_sndfile_as_region (fullpath, SrcBest, pos, just_one_src, status); + if (ok) { + p.index1 = a->index; + p.id = just_one_src.back ()->id (); + + ptfwavpair.push_back (p); + imported.push_back (just_one_src.back ()); + } else { + /* ptformat does not know the length of sources so we cannot do this: + XMLNode srcxml (X_("Source")); + srcxml.set_property ("name", a->filename); + srcxml.set_property ("type", "audio"); + srcxml.set_property ("id", PBD::ID ().to_s ()); + boost::shared_ptr source = SourceFactory::createSilent (this, srcxml, a->length, sample_rate ()); + p.index1 = a->index; + p.id = source->id (); + ptfwavpair.push_back (p); + imported.push_back (source); + */ + onefailed = true; + } + } + + if (onefailed) { + warning << _("Failed to load one or more of the audio files for PT import") << endmsg; + } else { + info << _("All audio files found for PT import") << endmsg; + } + + for (vector::iterator a = ptf.regions.begin (); + a != ptf.regions.end (); ++a) { + for (vector::iterator p = ptfwavpair.begin (); + p != ptfwavpair.end (); ++p) { + if ((p->index1 == a->wave.index) && (strcmp (a->wave.filename.c_str (), "") != 0)) { + for (SourceList::iterator x = imported.begin (); x != imported.end (); ++x) { + if ((*x)->id () == p->id) { + /* Matched an uncreated ptf region to ardour region */ + ptflookup_t rp; + PropertyList plist; + + plist.add (ARDOUR::Properties::start, a->sampleoffset); + plist.add (ARDOUR::Properties::position, 0); + plist.add (ARDOUR::Properties::length, a->length); + plist.add (ARDOUR::Properties::name, a->name); + plist.add (ARDOUR::Properties::layer, 0); + plist.add (ARDOUR::Properties::whole_file, false); + plist.add (ARDOUR::Properties::external, true); + + just_one_src.clear (); + just_one_src.push_back (*x); + + boost::shared_ptr r = RegionFactory::create (just_one_src, plist); + regions.push_back (r); + + rp.id = regions.back ()->id (); + rp.index1 = a->index; + ptfregpair.push_back (rp); + } + } + } + } + } + + boost::shared_ptr existing_track; + uint16_t nth = 0; + vector usedtracks; + ptflookup_t utr; + + for (vector::iterator a = ptf.tracks.begin (); a != ptf.tracks.end (); ++a) { + for (vector::iterator p = ptfregpair.begin (); + p != ptfregpair.end (); ++p) { + + if (p->index1 == a->reg.index) { + /* Matched a ptf active region to an ardour region */ + utr.index1 = a->index; + utr.index2 = nth; + utr.id = p->id; + boost::shared_ptr r = RegionFactory::region_by_id (p->id); + vector::iterator lookuptr = usedtracks.begin (); + vector::iterator found; + if ((found = std::find (lookuptr, usedtracks.end (), utr)) != usedtracks.end ()) { + 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)); + + /* Use existing track if possible */ + existing_track = get_nth_audio_track (found->index2); + if (!existing_track) { + list > at (new_audio_track (1, 2, 0, 1, "", PresentationInfo::max_order, Normal)); + if (at.empty ()) { + return; + } + existing_track = at.back (); + } + /* Put on existing track */ + boost::shared_ptr playlist = existing_track->playlist (); + boost::shared_ptr copy (RegionFactory::create (r, true)); + playlist->clear_changes (); + playlist->add_region (copy, a->reg.startpos); + //add_command (new StatefulDiffCommand (playlist)); + } else { + /* Put on a new track */ + DEBUG_TRACE (DEBUG::FileUtils, string_compose ("\twav(%1) reg(%2) new_tr(%3)\n", a->reg.wave.filename.c_str (), a->reg.index, nth)); + list > at (new_audio_track (1, 2, 0, 1, "", PresentationInfo::max_order, Normal)); + if (at.empty ()) { + return; + } + existing_track = at.back (); + std::string trackname; + try { + trackname = Glib::convert_with_fallback (a->name, "UTF-8", "UTF-8", "_"); + } catch (Glib::ConvertError& err) { + trackname = string_compose ("Invalid %1", a->index); + } + /* TODO legalize track name (no slashes, no colons) */ +#if 0 /* TODO -- "find_route_name" is currently private */ + /* generate a unique name by adding a number if needed */ + uint32_t id = 0; + if (!find_route_name (trackname.c_str (), id, trackname, false)) { + fatal << _("PTImport: UINT_MAX routes? impossible!") << endmsg; + abort(); /*NOTREACHED*/ + } +#endif + existing_track->set_name (trackname); + boost::shared_ptr playlist = existing_track->playlist(); + boost::shared_ptr copy (RegionFactory::create (r, true)); + playlist->clear_changes (); + playlist->add_region (copy, a->reg.startpos); + //add_command (new StatefulDiffCommand (playlist)); + nth++; + } + usedtracks.push_back (utr); + } + } + } + + /* MIDI - Find list of unique midi tracks first */ + + vector uniquetr; + + for (vector::iterator a = ptf.miditracks.begin (); a != ptf.miditracks.end (); ++a) { + bool found = false; + for (vector::iterator b = uniquetr.begin (); b != uniquetr.end (); ++b) { + if (b->trname == a->name) { + found = true; + break; + } + } + if (!found) { + uniquetr.push_back (midipair (a->index, a->name)); + //printf(" : %d : %s\n", a->index, a->name.c_str()); + } + } + + std::map > midi_tracks; + /* MIDI - Create unique midi tracks and a lookup table for used tracks */ + for (vector::iterator a = uniquetr.begin (); a != uniquetr.end (); ++a) { + ptflookup_t miditr; + list > mt (new_midi_track ( + ChanCount (DataType::MIDI, 1), + ChanCount (DataType::MIDI, 1), + true, + instrument, (Plugin::PresetRecord*) 0, + (RouteGroup*) 0, + 1, + a->trname, + PresentationInfo::max_order, + Normal)); + assert (mt.size () == 1); + midi_tracks[a->ptfindex] = mt.front (); + } + + /* MIDI - Add midi regions one-by-one to corresponding midi tracks */ + for (vector::iterator a = ptf.miditracks.begin (); a != ptf.miditracks.end (); ++a) { + + boost::shared_ptr midi_track = midi_tracks[a->index]; + assert (midi_track); + boost::shared_ptr playlist = midi_track->playlist (); + samplepos_t f = (samplepos_t)a->reg.startpos * srate / 1920000.; + samplecnt_t length = (samplecnt_t)a->reg.length * srate / 1920000.; + MusicSample pos (f, 0); + boost::shared_ptr src = create_midi_source_by_stealing_name (midi_track); + PropertyList plist; + plist.add (ARDOUR::Properties::start, 0); + plist.add (ARDOUR::Properties::length, length); + plist.add (ARDOUR::Properties::name, PBD::basename_nosuffix (src->name ())); + //printf(" : %d - trackname: (%s)\n", a->index, src->name ().c_str ()); + boost::shared_ptr region = (RegionFactory::create (src, plist)); + /* sets beat position */ + region->set_position (pos.sample, pos.division); + midi_track->playlist ()->add_region (region, pos.sample, 1.0, false, pos.division); + + boost::shared_ptr mr = boost::dynamic_pointer_cast(region); + boost::shared_ptr mm = mr->midi_source (0)->model (); + MidiModel::NoteDiffCommand *midicmd; + midicmd = mm->new_note_diff_command ("Import ProTools MIDI"); + + for (vector::iterator j = a->reg.midi.begin (); j != a->reg.midi.end (); ++j) { + //printf(" : MIDI : pos=%f len=%f\n", (float)j->pos / 960000., (float)j->length / 960000.); + Temporal::Beats start = (Temporal::Beats)(j->pos / 960000.); + Temporal::Beats len = (Temporal::Beats)(j->length / 960000.); + /* PT C-2 = 0, Ardour C-1 = 0, subtract twelve to convert ? */ + midicmd->add (boost::shared_ptr > (new Evoral::Note ((uint8_t)1, start, len, j->note, j->velocity))); + } + mm->apply_command (this, midicmd); + boost::shared_ptr copy (RegionFactory::create (mr, true)); + playlist->clear_changes (); + playlist->add_region (copy, f); + } + + status.sources.clear (); + + if (ok) { + save_state (""); + } + + status.all_done = true; +} diff --git a/libs/ardour/session.cc b/libs/ardour/session.cc index 9bdf758a17..f0f8681570 100644 --- a/libs/ardour/session.cc +++ b/libs/ardour/session.cc @@ -6457,6 +6457,26 @@ Session::route_removed_from_route_group (RouteGroup* rg, boost::weak_ptr } } +boost::shared_ptr +Session::get_nth_audio_track (int nth) const +{ + boost::shared_ptr rl = routes.reader (); + boost::shared_ptr ret = NULL; + --nth; + + for (RouteList::const_iterator r = rl->begin(); r != rl->end(); ++r) { + boost::shared_ptr at = boost::dynamic_pointer_cast (*r); + if (at) { + if (nth > 0) { + --nth; + } else { + ret = at; + } + } + } + return ret; +} + boost::shared_ptr Session::get_tracks () const { diff --git a/libs/ardour/wscript b/libs/ardour/wscript index efc305325a..ffc6827334 100644 --- a/libs/ardour/wscript +++ b/libs/ardour/wscript @@ -103,6 +103,7 @@ libardour_sources = [ 'iec1ppmdsp.cc', 'iec2ppmdsp.cc', 'import.cc', + 'import_pt.cc', 'instrument_info.cc', 'internal_return.cc', 'internal_send.cc', @@ -399,6 +400,7 @@ def build(bld): 'libaudiographer', 'libtemporal', 'liblua', + 'libptformat', 'zita-resampler', ] if bld.env['build_target'] != 'mingw': diff --git a/libs/ptformat/wscript b/libs/ptformat/wscript index 4d381234fe..77f70b90e8 100644 --- a/libs/ptformat/wscript +++ b/libs/ptformat/wscript @@ -34,7 +34,7 @@ def build(bld): obj.includes = ['.'] obj.name = 'libptformat' obj.target = 'ptformat' - obj.use = 'libardour' + obj.use = 'libpbd' autowaf.ensure_visible_symbols (obj, True) obj.vnum = LIBPTFORMAT_LIB_VERSION obj.install_path = bld.env['LIBDIR'] -- cgit v1.2.3