13
0

pt import: Fix race condition/threading, don't call new_audio_track() in bg thread

This commit is contained in:
Damien Zammit 2021-01-06 17:06:54 +11:00
parent a307cc602c
commit 12d62cd16e
3 changed files with 66 additions and 52 deletions

View File

@ -97,19 +97,21 @@ Editor::external_pt_dialog ()
ipw.show(); ipw.show();
while (!import_pt_status.all_done) { while (!import_pt_status.all_done && !import_pt_status.cancel) {
gtk_main_iteration (); gtk_main_iteration ();
} }
// wait for thread to terminate // wait for thread to terminate
while (!import_pt_status.done) { while (!import_pt_status.done && !import_pt_status.cancel) {
gtk_main_iteration (); gtk_main_iteration ();
} }
if (import_pt_status.cancel) { if (import_pt_status.cancel) {
MessageDialog msg (_("PT import may have missing files, check session log for details")); MessageDialog msg (_("PT import cancelled"));
msg.run (); msg.run ();
} else { } else {
_session->import_pt_rest (import_ptf);
import_pt_status.progress = 1.0;
MessageDialog msg (_("PT import complete!")); MessageDialog msg (_("PT import complete!"));
msg.run (); msg.run ();
} }
@ -118,7 +120,7 @@ Editor::external_pt_dialog ()
void * void *
Editor::_import_pt_thread (void *arg) Editor::_import_pt_thread (void *arg)
{ {
SessionEvent::create_per_thread_pool ("import pt events", 2048); SessionEvent::create_per_thread_pool ("import pt events", 64);
Editor *ed = (Editor *) arg; Editor *ed = (Editor *) arg;
return ed->import_pt_thread (); return ed->import_pt_thread ();
@ -127,6 +129,6 @@ Editor::_import_pt_thread (void *arg)
void * void *
Editor::import_pt_thread () Editor::import_pt_thread ()
{ {
_session->import_pt (import_ptf, import_pt_status); _session->import_pt_sources (import_ptf, import_pt_status);
return 0; return 0;
} }

View File

@ -1276,9 +1276,22 @@ public:
bool transport_master_no_external_or_using_engine() const; bool transport_master_no_external_or_using_engine() const;
boost::shared_ptr<TransportMaster> transport_master() const; boost::shared_ptr<TransportMaster> transport_master() const;
void import_pt (PTFFormat& ptf, ImportStatus& status); void import_pt_sources (PTFFormat& ptf, ImportStatus& status);
void import_pt_rest (PTFFormat& ptf);
bool import_sndfile_as_region (std::string path, SrcQuality quality, samplepos_t& pos, SourceList& sources, ImportStatus& status); bool import_sndfile_as_region (std::string path, SrcQuality quality, samplepos_t& pos, SourceList& sources, ImportStatus& status);
struct ptflookup {
uint16_t index1;
uint16_t index2;
PBD::ID id;
bool operator ==(const struct ptflookup& other) {
return (this->index1 == other.index1);
}
};
std::vector<struct ptflookup> ptfwavpair;
SourceList pt_imported_sources;
protected: protected:
friend class AudioEngine; friend class AudioEngine;
void set_block_size (pframes_t nframes); void set_block_size (pframes_t nframes);

View File

@ -67,17 +67,6 @@ struct midipair {
string trname; 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 bool
Session::import_sndfile_as_region (string path, SrcQuality quality, samplepos_t& pos, SourceList& sources, ImportStatus& status) Session::import_sndfile_as_region (string path, SrcQuality quality, samplepos_t& pos, SourceList& sources, ImportStatus& status)
{ {
@ -185,7 +174,7 @@ Session::import_sndfile_as_region (string path, SrcQuality quality, samplepos_t&
void void
Session::import_pt (PTFFormat& ptf, ImportStatus& status) Session::import_pt_sources (PTFFormat& ptf, ImportStatus& status)
{ {
vector<boost::shared_ptr<Region> > regions; vector<boost::shared_ptr<Region> > regions;
boost::shared_ptr<ARDOUR::Track> track; boost::shared_ptr<ARDOUR::Track> track;
@ -195,22 +184,21 @@ Session::import_pt (PTFFormat& ptf, ImportStatus& status)
bool ok = false; bool ok = false;
bool onefailed = false; bool onefailed = false;
samplepos_t pos = -1; samplepos_t pos = -1;
uint32_t srate = sample_rate ();
vector<ptflookup_t> ptfwavpair; vector<struct ptflookup> ptfregpair;
vector<ptflookup_t> ptfregpair;
vector<PTFFormat::wav_t>::const_iterator w; vector<PTFFormat::wav_t>::const_iterator w;
SourceList just_one_src; SourceList just_one_src;
SourceList imported;
boost::shared_ptr<AudioTrack> existing_track; boost::shared_ptr<AudioTrack> existing_track;
uint16_t nth = 0; vector<struct ptflookup> usedtracks;
vector<ptflookup_t> usedtracks; struct ptflookup utr;
ptflookup_t utr;
ptfwavpair.clear();
pt_imported_sources.clear();
for (w = ptf.audiofiles ().begin (); w != ptf.audiofiles ().end () && !status.cancel; ++w) { for (w = ptf.audiofiles ().begin (); w != ptf.audiofiles ().end () && !status.cancel; ++w) {
ptflookup_t p; struct ptflookup p;
ok = false; ok = false;
/* Try audio file */ /* Try audio file */
fullpath = Glib::build_filename (Glib::path_get_dirname (ptf.path ()), "Audio Files"); fullpath = Glib::build_filename (Glib::path_get_dirname (ptf.path ()), "Audio Files");
@ -244,7 +232,7 @@ Session::import_pt (PTFFormat& ptf, ImportStatus& status)
p.index1 = w->index; p.index1 = w->index;
p.id = source->id (); p.id = source->id ();
ptfwavpair.push_back (p); ptfwavpair.push_back (p);
imported.push_back (source); pt_imported_sources.push_back (source);
warning << string_compose (_("PT Import : MISSING `%1`, inserting ref to missing source"), fullpath) << endmsg; warning << string_compose (_("PT Import : MISSING `%1`, inserting ref to missing source"), fullpath) << endmsg;
} else { } else {
warning << string_compose (_("PT Import : MISSING `%1`, please check Audio Files"), fullpath) << endmsg; warning << string_compose (_("PT Import : MISSING `%1`, please check Audio Files"), fullpath) << endmsg;
@ -256,28 +244,54 @@ Session::import_pt (PTFFormat& ptf, ImportStatus& status)
p.id = just_one_src.back ()->id (); p.id = just_one_src.back ()->id ();
ptfwavpair.push_back (p); ptfwavpair.push_back (p);
imported.push_back (just_one_src.back ()); pt_imported_sources.push_back (just_one_src.back ());
} }
} }
if (imported.empty ()) { if (pt_imported_sources.empty ()) {
error << _("Failed to find any audio for PT import") << endmsg; error << _("Failed to find any audio for PT import") << endmsg;
goto trymidi;
} else if (onefailed) { } else if (onefailed) {
warning << _("Failed to load one or more of the audio files for PT import, see above list") << endmsg; warning << _("Failed to load one or more of the audio files for PT import, see above list") << endmsg;
} else { } else {
info << _("All audio files found for PT import!") << endmsg; info << _("All audio files found for PT import!") << endmsg;
} }
status.progress = 1.0;
status.sources.clear ();
status.done = true;
status.all_done = true;
}
void
Session::import_pt_rest (PTFFormat& ptf)
{
vector<boost::shared_ptr<Region> > regions;
boost::shared_ptr<ARDOUR::Track> track;
ARDOUR::PluginInfoPtr instrument;
vector<string> to_import;
string fullpath;
uint32_t srate = sample_rate ();
vector<struct ptflookup> ptfregpair;
vector<PTFFormat::wav_t>::const_iterator w;
SourceList just_one_src;
boost::shared_ptr<AudioTrack> existing_track;
uint16_t nth = 0;
vector<struct ptflookup> usedtracks;
struct ptflookup utr;
for (vector<PTFFormat::region_t>::const_iterator a = ptf.regions ().begin (); for (vector<PTFFormat::region_t>::const_iterator a = ptf.regions ().begin ();
a != ptf.regions ().end (); ++a) { a != ptf.regions ().end (); ++a) {
for (vector<ptflookup_t>::iterator p = ptfwavpair.begin (); for (vector<struct ptflookup>::iterator p = ptfwavpair.begin ();
p != ptfwavpair.end (); ++p) { p != ptfwavpair.end (); ++p) {
if ((p->index1 == a->wave.index) && (strcmp (a->wave.filename.c_str (), "") != 0)) { if ((p->index1 == a->wave.index) && (strcmp (a->wave.filename.c_str (), "") != 0)) {
for (SourceList::iterator x = imported.begin (); x != imported.end (); ++x) { for (SourceList::iterator x = pt_imported_sources.begin (); x != pt_imported_sources.end (); ++x) {
if ((*x)->id () == p->id) { if ((*x)->id () == p->id) {
/* Matched an uncreated ptf region to ardour region */ /* Matched an uncreated ptf region to ardour region */
ptflookup_t rp; struct ptflookup rp;
PropertyList plist; PropertyList plist;
plist.add (ARDOUR::Properties::start, a->sampleoffset); plist.add (ARDOUR::Properties::start, a->sampleoffset);
@ -304,7 +318,7 @@ Session::import_pt (PTFFormat& ptf, ImportStatus& status)
} }
for (vector<PTFFormat::track_t>::const_iterator a = ptf.tracks ().begin (); a != ptf.tracks ().end (); ++a) { for (vector<PTFFormat::track_t>::const_iterator a = ptf.tracks ().begin (); a != ptf.tracks ().end (); ++a) {
for (vector<ptflookup_t>::iterator p = ptfregpair.begin (); for (vector<struct ptflookup>::iterator p = ptfregpair.begin ();
p != ptfregpair.end (); ++p) { p != ptfregpair.end (); ++p) {
if (p->index1 == a->reg.index) { if (p->index1 == a->reg.index) {
@ -314,8 +328,8 @@ Session::import_pt (PTFFormat& ptf, ImportStatus& status)
utr.index2 = nth; utr.index2 = nth;
utr.id = p->id; utr.id = p->id;
boost::shared_ptr<Region> r = RegionFactory::region_by_id (p->id); boost::shared_ptr<Region> r = RegionFactory::region_by_id (p->id);
vector<ptflookup_t>::iterator lookuptr = usedtracks.begin (); vector<struct ptflookup>::iterator lookuptr = usedtracks.begin ();
vector<ptflookup_t>::iterator found; vector<struct ptflookup>::iterator found;
if ((found = std::find (lookuptr, usedtracks.end (), utr)) != usedtracks.end ()) { 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)); 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));
@ -367,16 +381,6 @@ Session::import_pt (PTFFormat& ptf, ImportStatus& status)
} }
} }
trymidi:
status.paths.clear();
status.paths.push_back(ptf.path ());
status.current = 1;
status.total = 1;
status.freeze = false;
status.done = false;
status.cancel = false;
status.progress = 0;
/* MIDI - Find list of unique midi tracks first */ /* MIDI - Find list of unique midi tracks first */
vector<midipair> uniquetr; vector<midipair> uniquetr;
@ -398,7 +402,7 @@ trymidi:
std::map <int, boost::shared_ptr<MidiTrack> > midi_tracks; std::map <int, boost::shared_ptr<MidiTrack> > midi_tracks;
/* MIDI - Create unique midi tracks and a lookup table for used tracks */ /* MIDI - Create unique midi tracks and a lookup table for used tracks */
for (vector<midipair>::iterator a = uniquetr.begin (); a != uniquetr.end (); ++a) { for (vector<midipair>::iterator a = uniquetr.begin (); a != uniquetr.end (); ++a) {
ptflookup_t miditr; struct ptflookup miditr;
list<boost::shared_ptr<MidiTrack> > mt (new_midi_track ( list<boost::shared_ptr<MidiTrack> > mt (new_midi_track (
ChanCount (DataType::MIDI, 1), ChanCount (DataType::MIDI, 1),
ChanCount (DataType::MIDI, 1), ChanCount (DataType::MIDI, 1),
@ -450,9 +454,4 @@ trymidi:
playlist->clear_changes (); playlist->clear_changes ();
playlist->add_region (copy, f); playlist->add_region (copy, f);
} }
status.progress = 1.0;
status.done = true;
status.sources.clear ();
status.all_done = true;
} }