From 0e488bf0608f057b0efbb9a2fb8ffd588ef762cc Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Fri, 4 Jan 2008 03:36:25 +0000 Subject: [PATCH] Back-port tim mayberry's fixes for import; fix JACK slaving to never pay attention to timecode-source-is-synced git-svn-id: svn://localhost/ardour2/branches/2.0-ongoing@2824 d708f5d6-7413-0410-9779-e7cbd77b26cf --- libs/ardour/ardour/importable_source.h | 35 +- libs/ardour/ardour/resampled_source.h | 4 +- libs/ardour/ardour/session.h | 2 +- libs/ardour/ardour/slave.h | 2 + libs/ardour/import.cc | 427 +++++++++++++------------ libs/ardour/resampled_source.cc | 19 +- libs/ardour/session_process.cc | 4 +- libs/ardour/session_transport.cc | 7 - 8 files changed, 273 insertions(+), 227 deletions(-) diff --git a/libs/ardour/ardour/importable_source.h b/libs/ardour/ardour/importable_source.h index 29742d1076..5845d841b6 100644 --- a/libs/ardour/ardour/importable_source.h +++ b/libs/ardour/ardour/importable_source.h @@ -21,26 +21,39 @@ #define __ardour_importable_source_h__ #include +#include #include namespace ARDOUR { class ImportableSource { - public: - ImportableSource (SNDFILE* sf, SF_INFO* info) : in (sf), sf_info (info) {} - virtual ~ImportableSource() {} +public: + ImportableSource (const std::string& path) + : in (sf_open (path.c_str(), SFM_READ, &sf_info), sf_close) + { + if (!in) throw failed_constructor(); + + } + + virtual ~ImportableSource() {} - virtual nframes_t read (Sample* buffer, nframes_t nframes) { - nframes_t per_channel = nframes / sf_info->channels; - per_channel = sf_readf_float (in, buffer, per_channel); - return per_channel * sf_info->channels; - } + virtual nframes_t read (Sample* buffer, nframes_t nframes) { + nframes_t per_channel = nframes / sf_info.channels; + per_channel = sf_readf_float (in.get(), buffer, per_channel); + return per_channel * sf_info.channels; + } - virtual float ratio() const { return 1.0f; } + virtual float ratio() const { return 1.0f; } + + uint channels() const { return sf_info.channels; } + + nframes_t length() const { return sf_info.frames; } + + nframes_t samplerate() const { return sf_info.samplerate; } protected: - SNDFILE* in; - SF_INFO* sf_info; + SF_INFO sf_info; + boost::shared_ptr in; }; } diff --git a/libs/ardour/ardour/resampled_source.h b/libs/ardour/ardour/resampled_source.h index 9a88ca9644..8ca56b52d3 100644 --- a/libs/ardour/ardour/resampled_source.h +++ b/libs/ardour/ardour/resampled_source.h @@ -30,7 +30,9 @@ namespace ARDOUR { class ResampledImportableSource : public ImportableSource { public: - ResampledImportableSource (SNDFILE* sf, SF_INFO* info, nframes_t rate, SrcQuality); + ResampledImportableSource (const std::string& path, + nframes_t rate, SrcQuality); + ~ResampledImportableSource (); nframes_t read (Sample* buffer, nframes_t nframes); diff --git a/libs/ardour/ardour/session.h b/libs/ardour/ardour/session.h index 8aea492910..611ef4813d 100644 --- a/libs/ardour/ardour/session.h +++ b/libs/ardour/ardour/session.h @@ -578,7 +578,7 @@ class Session : public PBD::StatefulDestructible }; - int import_audiofile (import_status&); + void import_audiofiles (import_status&); bool sample_rate_convert (import_status&, string infile, string& outfile); string build_tmp_convert_name (string file); diff --git a/libs/ardour/ardour/slave.h b/libs/ardour/ardour/slave.h index 73b66ca20e..509f8fa9d2 100644 --- a/libs/ardour/ardour/slave.h +++ b/libs/ardour/ardour/slave.h @@ -47,6 +47,7 @@ class Slave { virtual bool starting() const { return false; } virtual nframes_t resolution() const = 0; virtual bool requires_seekahead () const = 0; + virtual bool is_always_synced() const { return false; } }; @@ -139,6 +140,7 @@ class JACK_Slave : public Slave nframes_t resolution() const { return 1; } bool requires_seekahead () const { return false; } void reset_client (jack_client_t* jack); + bool is_always_synced() const { return true; } private: jack_client_t* jack; diff --git a/libs/ardour/import.cc b/libs/ardour/import.cc index 2f4805ac7c..e7cb136a4e 100644 --- a/libs/ardour/import.cc +++ b/libs/ardour/import.cc @@ -31,6 +31,9 @@ #include +#include +#include + #include #include @@ -49,226 +52,258 @@ using namespace ARDOUR; using namespace PBD; -int -Session::import_audiofile (import_status& status) +static std::auto_ptr +open_importable_source (const string& path, nframes_t samplerate, ARDOUR::SrcQuality quality) +{ + std::auto_ptr source(new ImportableSource(path)); + + if (source->samplerate() == samplerate) { + return source; + } + + return std::auto_ptr(new ResampledImportableSource(path, samplerate, quality)); +} + +static std::string +get_non_existent_filename (const std::string& basename, uint channel, uint channels) { - SNDFILE *in; - vector > newfiles; - SF_INFO info; - float *data = 0; - Sample **channel_data = 0; - int nfiles = 0; - string basepath; - string sounds_dir; - nframes_t so_far; char buf[PATH_MAX+1]; - int ret = -1; + bool goodfile = false; + string base(basename); + + do { + if (channels == 2) { + if (channel == 0) { + snprintf (buf, sizeof(buf), "%s-L.wav", base.c_str()); + } else { + snprintf (buf, sizeof(buf), "%s-R.wav", base.c_str()); + } + } else if (channels > 1) { + snprintf (buf, sizeof(buf), "%s-c%d.wav", base.c_str(), channel+1); + } else { + snprintf (buf, sizeof(buf), "%s.wav", base.c_str()); + } + + if (Glib::file_test (buf, Glib::FILE_TEST_EXISTS)) { + + /* if the file already exists, we must come up with + * a new name for it. for now we just keep appending + * _ to basename + */ + + base += "_"; + + } else { + + goodfile = true; + } + + } while ( !goodfile); + + return buf; +} + +static vector +get_paths_for_new_sources (const string& import_file_path, const string& session_dir, uint channels) +{ vector new_paths; - struct tm* now; - ImportableSource* importable = 0; + const string basename = basename_nosuffix (import_file_path); + + for (uint n = 0; n < channels; ++n) { + + std::string filepath; + + filepath = session_dir; + filepath += '/'; + filepath += get_non_existent_filename (basename, n, channels); + + new_paths.push_back (filepath); + } + + return new_paths; +} + +static bool +create_mono_sources_for_writing (const vector& new_paths, Session& sess, + uint samplerate, vector >& newfiles) +{ + for (vector::const_iterator i = new_paths.begin(); + i != new_paths.end(); ++i) + { + boost::shared_ptr source; + + try + { + source = SourceFactory::createWritable ( + sess, + i->c_str(), + false, // destructive + samplerate + ); + } + catch (const failed_constructor& err) + { + error << string_compose (_("Unable to create file %1 during import"), *i) << endmsg; + return false; + } + + newfiles.push_back(boost::dynamic_pointer_cast(source)); + } + return true; +} + +static Glib::ustring +compose_status_message (const string& path, + uint file_samplerate, + uint session_samplerate, + uint current_file, + uint total_files) +{ + if (file_samplerate != session_samplerate) { + return string_compose (_("converting %1\n(resample from %2KHz to %3KHz)\n(%4 of %5)"), + Glib::path_get_basename (path), + file_samplerate/1000.0f, + session_samplerate/1000.0f, + current_file, total_files); + } + + return string_compose (_("converting %1\n(%2 of %3)"), + Glib::path_get_basename (path), + current_file, total_files); +} + +static void +write_audio_data_to_new_files (ImportableSource* source, Session::import_status& status, + vector >& newfiles) +{ const nframes_t nframes = ResampledImportableSource::blocksize; + uint channels = source->channels(); + + boost::scoped_array data(new float[nframes * channels]); + vector > channel_data; + + for (uint n = 0; n < channels; ++n) { + channel_data.push_back(boost::shared_array(new Sample[nframes])); + } + + uint read_count = 0; + status.progress = 0.0f; + + while (!status.cancel) { + + nframes_t nread, nfread; + uint x; + uint chn; + + if ((nread = source->read (data.get(), nframes)) == 0) { + break; + } + nfread = nread / channels; + + /* de-interleave */ + + for (chn = 0; chn < channels; ++chn) { + + nframes_t n; + for (x = chn, n = 0; n < nfread; x += channels, ++n) { + channel_data[chn][n] = (Sample) data[x]; + } + } + + /* flush to disk */ + + for (chn = 0; chn < channels; ++chn) { + newfiles[chn]->write (channel_data[chn].get(), nfread); + } + + read_count += nread; + status.progress = read_count / (source->ratio () * source->length() * channels); + } +} + +static void +remove_file_source (boost::shared_ptr file_source) +{ + ::unlink (file_source->path().c_str()); +} + +void +Session::import_audiofiles (import_status& status) +{ uint32_t cnt = 1; + typedef vector > AudioSources; + AudioSources all_new_sources; status.sources.clear (); - for (vector::iterator p = status.paths.begin(); p != status.paths.end(); ++p, ++cnt) { + for (vector::iterator p = status.paths.begin(); + p != status.paths.end() && !status.cancel; + ++p, ++cnt) + { + std::auto_ptr source; - if ((in = sf_open ((*p).c_str(), SFM_READ, &info)) == 0) { + try + { + source = open_importable_source (*p, frame_rate(), status.quality); + } + catch (const failed_constructor& err) + { error << string_compose(_("Import: cannot open input sound file \"%1\""), (*p)) << endmsg; - status.done = 1; - status.cancel = 1; - return -1; + status.done = status.cancel = true; + return; } + + vector new_paths = get_paths_for_new_sources (*p, + discover_best_sound_dir (), + source->channels()); - if ((nframes_t) info.samplerate != frame_rate()) { - importable = new ResampledImportableSource (in, &info, frame_rate(), status.quality); - } else { - importable = new ImportableSource (in, &info); - } - - newfiles.clear (); + AudioSources newfiles; - for (int n = 0; n < info.channels; ++n) { - newfiles.push_back (boost::shared_ptr()); - } - - sounds_dir = discover_best_sound_dir (); - basepath = PBD::basename_nosuffix ((*p)); - - for (int n = 0; n < info.channels; ++n) { - - bool goodfile = false; - - do { - if (info.channels == 2) { - if (n == 0) { - snprintf (buf, sizeof(buf), "%s/%s-L.wav", sounds_dir.c_str(), basepath.c_str()); - } else { - snprintf (buf, sizeof(buf), "%s/%s-R.wav", sounds_dir.c_str(), basepath.c_str()); - } - } else if (info.channels > 1) { - snprintf (buf, sizeof(buf), "%s/%s-c%d.wav", sounds_dir.c_str(), basepath.c_str(), n+1); - } else { - snprintf (buf, sizeof(buf), "%s/%s.wav", sounds_dir.c_str(), basepath.c_str()); - } + status.cancel = !create_mono_sources_for_writing (new_paths, *this, frame_rate(), newfiles); - if (Glib::file_test (buf, Glib::FILE_TEST_EXISTS)) { + // copy on cancel/failure so that any files that were created will be removed below + std::copy (newfiles.begin(), newfiles.end(), std::back_inserter(all_new_sources)); - /* if the file already exists, we must come up with - * a new name for it. for now we just keep appending - * _ to basepath - */ - - basepath += "_"; + if (status.cancel) break; - } else { - - goodfile = true; - } - - } while ( !goodfile); - - try { - newfiles[n] = boost::dynamic_pointer_cast (SourceFactory::createWritable (*this, buf, false, frame_rate())); - } - - catch (failed_constructor& err) { - error << string_compose(_("Session::import_audiofile: cannot open new file source for channel %1"), n+1) << endmsg; - goto out; - } - - new_paths.push_back (buf); - newfiles[n]->prepare_for_peakfile_writes (); - nfiles++; - } - - if (data) { - delete [] data; + for (AudioSources::iterator i = newfiles.begin(); i != newfiles.end(); ++i) { + (*i)->prepare_for_peakfile_writes (); } - data = new float[nframes * info.channels]; + status.doing_what = compose_status_message (*p, source->samplerate(), + frame_rate(), cnt, status.paths.size()); - if (channel_data) { - delete [] channel_data; - } - - channel_data = new Sample * [ info.channels ]; - - for (int n = 0; n < info.channels; ++n) { - channel_data[n] = new Sample[nframes]; - } - - so_far = 0; - - if ((nframes_t) info.samplerate != frame_rate()) { - status.doing_what = string_compose (_("converting %1\n(resample from %2KHz to %3KHz)\n(%4 of %5)"), - basepath, - info.samplerate/1000.0f, - frame_rate()/1000.0f, - cnt, status.paths.size()); - - } else { - status.doing_what = string_compose (_("converting %1\n(%2 of %3)"), - basepath, - cnt, status.paths.size()); - - } - - status.progress = 0.0; - - while (!status.cancel) { - - nframes_t nread, nfread; - long x; - long chn; - - if ((nread = importable->read (data, nframes)) == 0) { - break; - } - nfread = nread / info.channels; - - /* de-interleave */ - - for (chn = 0; chn < info.channels; ++chn) { - - nframes_t n; - for (x = chn, n = 0; n < nfread; x += info.channels, ++n) { - channel_data[chn][n] = (Sample) data[x]; - } - } - - /* flush to disk */ - - for (chn = 0; chn < info.channels; ++chn) { - newfiles[chn]->write (channel_data[chn], nfread); - } - - so_far += nread; - status.progress = so_far / (importable->ratio () * info.frames * info.channels); - } - - if (status.cancel) { - goto out; - } - - for (int n = 0; n < info.channels; ++n) { - status.sources.push_back (newfiles[n]); - } - - if (status.cancel) { - goto out; - } + write_audio_data_to_new_files (source.get(), status, newfiles); } - - status.freeze = true; - - time_t xnow; - time (&xnow); - now = localtime (&xnow); - - /* flush the final length(s) to the header(s) */ - - for (SourceList::iterator x = status.sources.begin(); x != status.sources.end() && !status.cancel; ++x) { - boost::dynamic_pointer_cast(*x)->update_header(0, *now, xnow); - boost::dynamic_pointer_cast(*x)->done_with_peakfile_writes (); - } - - /* save state so that we don't lose these new Sources */ if (!status.cancel) { + struct tm* now; + time_t xnow; + time (&xnow); + now = localtime (&xnow); + status.freeze = true; + + /* flush the final length(s) to the header(s) */ + + for (AudioSources::iterator x = all_new_sources.begin(); + x != all_new_sources.end(); ++x) + { + (*x)->update_header(0, *now, xnow); + (*x)->done_with_peakfile_writes (); + } + + /* save state so that we don't lose these new Sources */ + save_state (_name); + + std::copy (all_new_sources.begin(), all_new_sources.end(), + std::back_inserter(status.sources)); + } else { + // this can throw...but it seems very unlikely + std::for_each (all_new_sources.begin(), all_new_sources.end(), remove_file_source); } - ret = 0; - - out: - - if (data) { - delete [] data; - } - - if (channel_data) { - for (int n = 0; n < info.channels; ++n) { - delete [] channel_data[n]; - } - delete [] channel_data; - } - - if (status.cancel) { - - status.sources.clear (); - - for (vector::iterator i = new_paths.begin(); i != new_paths.end(); ++i) { - unlink ((*i).c_str()); - } - } - - if (importable) { - delete importable; - } - - sf_close (in); status.done = true; - - return ret; } + + diff --git a/libs/ardour/resampled_source.cc b/libs/ardour/resampled_source.cc index 38aa3832b9..8330196d8a 100644 --- a/libs/ardour/resampled_source.cc +++ b/libs/ardour/resampled_source.cc @@ -28,12 +28,13 @@ using namespace PBD; const uint32_t ResampledImportableSource::blocksize = 4096U; -ResampledImportableSource::ResampledImportableSource (SNDFILE* sf, SF_INFO* info, nframes_t rate, SrcQuality srcq) - : ImportableSource (sf, info) +ResampledImportableSource::ResampledImportableSource (const std::string& path, + nframes_t rate, SrcQuality srcq) + : ImportableSource (path) { int err; - sf_seek (in, 0, SEEK_SET) ; + sf_seek (in.get(), 0, SEEK_SET) ; /* Initialize the sample rate converter. */ @@ -57,7 +58,7 @@ ResampledImportableSource::ResampledImportableSource (SNDFILE* sf, SF_INFO* info break; } - if ((src_state = src_new (src_type, sf_info->channels, &err)) == 0) { + if ((src_state = src_new (src_type, sf_info.channels, &err)) == 0) { error << string_compose(_("Import: src_new() failed : %1"), src_strerror (err)) << endmsg ; throw failed_constructor (); } @@ -69,7 +70,7 @@ ResampledImportableSource::ResampledImportableSource (SNDFILE* sf, SF_INFO* info src_data.input_frames = 0 ; src_data.data_in = input ; - src_data.src_ratio = ((float) rate) / sf_info->samplerate ; + src_data.src_ratio = ((float) rate) / sf_info.samplerate ; input = new float[blocksize]; } @@ -97,14 +98,14 @@ ResampledImportableSource::read (Sample* output, nframes_t nframes) src_data.end_of_input = SF_TRUE ; } - src_data.input_frames /= sf_info->channels; + src_data.input_frames /= sf_info.channels; src_data.data_in = input ; } src_data.data_out = output; if (!src_data.end_of_input) { - src_data.output_frames = nframes / sf_info->channels ; + src_data.output_frames = nframes / sf_info.channels ; } else { src_data.output_frames = src_data.input_frames; } @@ -120,9 +121,9 @@ ResampledImportableSource::read (Sample* output, nframes_t nframes) return 0; } - src_data.data_in += src_data.input_frames_used * sf_info->channels ; + src_data.data_in += src_data.input_frames_used * sf_info.channels ; src_data.input_frames -= src_data.input_frames_used ; - return src_data.output_frames_gen * sf_info->channels; + return src_data.output_frames_gen * sf_info.channels; } diff --git a/libs/ardour/session_process.cc b/libs/ardour/session_process.cc index e77ed26098..99d1446bf4 100644 --- a/libs/ardour/session_process.cc +++ b/libs/ardour/session_process.cc @@ -477,7 +477,7 @@ Session::follow_slave (nframes_t nframes, nframes_t offset) << endl; #endif - if (Config->get_timecode_source_is_synced()) { + if (_slave->is_always_synced() || Config->get_timecode_source_is_synced()) { /* if the TC source is synced, then we assume that its speed is binary: 0.0 or 1.0 @@ -626,7 +626,7 @@ Session::follow_slave (nframes_t nframes, nframes_t offset) slave_state = Stopped; } - if (slave_state == Running && !Config->get_timecode_source_is_synced()) { + if (slave_state == Running && !_slave->is_always_synced() && !Config->get_timecode_source_is_synced()) { if (_transport_speed != 0.0f) { diff --git a/libs/ardour/session_transport.cc b/libs/ardour/session_transport.cc index a23261834d..d55c217bf5 100644 --- a/libs/ardour/session_transport.cc +++ b/libs/ardour/session_transport.cc @@ -76,10 +76,6 @@ Session::request_slave_source (SlaveSource src) void Session::request_transport_speed (float speed) { - if (speed != 0.0 && speed != 1.0) { - cerr << "odd speed requested\n"; - stacktrace (cerr, 20); - } Event* ev = new Event (Event::SetTransportSpeed, Event::Add, Event::Immediate, 0, speed); queue_event (ev); } @@ -756,8 +752,6 @@ Session::locate (nframes_t target_frame, bool with_roll, bool with_flush, bool w void Session::set_transport_speed (float speed, bool abort) { - cerr << "Session::set_transport_speed " << speed << " abort capture ? " << abort << endl; - if (_transport_speed == speed) { return; } @@ -819,7 +813,6 @@ Session::set_transport_speed (float speed, bool abort) } if ((synced_to_jack()) && speed != 0.0 && speed != 1.0) { - cerr << "synced to jack and speed == " << speed << endl; warning << _("Global varispeed cannot be supported while Ardour is connected to JACK transport control") << endmsg; return;