From 56c1fa0c901fedcb04d8ed2cacf6f304190a50e3 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Fri, 31 Jan 2020 16:26:41 +0100 Subject: [PATCH] Break out API to create readables from files --- libs/ardour/ardour/readable.h | 8 +++-- libs/ardour/convolver.cc | 39 ++++----------------- libs/ardour/readable.cc | 66 +++++++++++++++++++++++++++++++++++ libs/ardour/wscript | 1 + 4 files changed, 80 insertions(+), 34 deletions(-) create mode 100644 libs/ardour/readable.cc diff --git a/libs/ardour/ardour/readable.h b/libs/ardour/ardour/readable.h index 0463b2fce4..ada47776cf 100644 --- a/libs/ardour/ardour/readable.h +++ b/libs/ardour/ardour/readable.h @@ -25,11 +25,15 @@ namespace ARDOUR { +class Session; + class LIBARDOUR_API Readable { - public: - Readable () {} +public: virtual ~Readable() {} + static std::vector > + load (Session&, std::string const&); + virtual samplecnt_t read (Sample*, samplepos_t pos, samplecnt_t cnt, int channel) const = 0; virtual samplecnt_t readable_length() const = 0; virtual uint32_t n_channels () const = 0; diff --git a/libs/ardour/convolver.cc b/libs/ardour/convolver.cc index b556c212b3..2b00f0894e 100644 --- a/libs/ardour/convolver.cc +++ b/libs/ardour/convolver.cc @@ -24,6 +24,7 @@ #include "ardour/audioengine.h" #include "ardour/audiofilesource.h" #include "ardour/convolver.h" +#include "ardour/readable.h" #include "ardour/session.h" #include "ardour/srcfilesource.h" #include "ardour/source_factory.h" @@ -48,44 +49,18 @@ Convolver::Convolver ( , _offset (0) , _configured (false) { - ARDOUR::SoundFileInfo sf_info; - std::string error_msg; - - if (!AudioFileSource::get_soundfile_info (path, sf_info, error_msg)) { - PBD::error << string_compose(_("Convolver: cannot open IR \"%1\": %2"), path, error_msg) << endmsg; - throw failed_constructor (); - } - - if (sf_info.length > 0x1000000 /*2^24*/) { - PBD::error << string_compose(_("Convolver: IR \"%1\" file too long."), path) << endmsg; - throw failed_constructor (); - } - - for (unsigned int n = 0; n < sf_info.channels; ++n) { - try { - boost::shared_ptr afs; - afs = boost::dynamic_pointer_cast ( - SourceFactory::createExternal (DataType::AUDIO, _session, - path, n, - Source::Flag (ARDOUR::AudioFileSource::NoPeakFile), false)); - - if (afs->sample_rate() != _session.nominal_sample_rate()) { - boost::shared_ptr sfs (new SrcFileSource(_session, afs, ARDOUR::SrcBest)); - _readables.push_back(sfs); - } else { - _readables.push_back (afs); - } - } catch (failed_constructor& err) { - PBD::error << string_compose(_("Convolver: Could not open IR \"%1\"."), path) << endmsg; - throw failed_constructor (); - } - } + _readables = Readable::load (_session, path); if (_readables.empty ()) { PBD::error << string_compose (_("Convolver: IR \"%1\" no usable audio-channels sound."), path) << endmsg; throw failed_constructor (); } + if (_readables[0]->readable_length () > 0x1000000 /*2^24*/) { + PBD::error << string_compose(_("Convolver: IR \"%1\" file too long."), path) << endmsg; + throw failed_constructor (); + } + AudioEngine::instance ()->BufferSizeChanged.connect_same_thread (*this, boost::bind (&Convolver::reconfigure, this)); reconfigure (); diff --git a/libs/ardour/readable.cc b/libs/ardour/readable.cc new file mode 100644 index 0000000000..0390a0169d --- /dev/null +++ b/libs/ardour/readable.cc @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2020 Robin Gareus + * + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include + +#include "pbd/error.h" + +#include "ardour/audiofilesource.h" +#include "ardour/readable.h" +#include "ardour/session.h" +#include "ardour/srcfilesource.h" +#include "ardour/source_factory.h" + +#include "pbd/i18n.h" + +using namespace ARDOUR; + +std::vector > +Readable::load (Session& session, std::string const& path) +{ + std::vector > readables; + + ARDOUR::SoundFileInfo sf_info; + std::string error_msg; + + if (!AudioFileSource::get_soundfile_info (path, sf_info, error_msg)) { + PBD::error << string_compose(_("Cannot open File \"%1\": %2"), path, error_msg) << endmsg; + throw failed_constructor (); + } + + for (unsigned int n = 0; n < sf_info.channels; ++n) { + try { + boost::shared_ptr afs; + afs = boost::dynamic_pointer_cast ( + SourceFactory::createExternal (DataType::AUDIO, session, + path, n, + Source::Flag (ARDOUR::AudioFileSource::NoPeakFile), false)); + + if (afs->sample_rate() != session.nominal_sample_rate()) { + boost::shared_ptr sfs (new SrcFileSource(session, afs, ARDOUR::SrcBest)); + readables.push_back(sfs); + } else { + readables.push_back (afs); + } + } catch (failed_constructor& err) { + PBD::error << string_compose(_("Could not read file \"%1\"."), path) << endmsg; + throw failed_constructor (); + } + } + return readables; +} diff --git a/libs/ardour/wscript b/libs/ardour/wscript index 9de7953d09..8d6090d956 100644 --- a/libs/ardour/wscript +++ b/libs/ardour/wscript @@ -181,6 +181,7 @@ libardour_sources = [ 'progress.cc', 'quantize.cc', 'rc_configuration.cc', + 'readable.cc', 'readonly_control.cc', 'raw_midi_parser.cc', 'recent_sessions.cc',