2019-08-03 09:48:50 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-12-15 18:25:07 -05:00
|
|
|
#include <iostream>
|
|
|
|
#include <cstdlib>
|
2015-12-16 12:46:44 -05:00
|
|
|
#include <glibmm.h>
|
2015-12-15 18:25:07 -05:00
|
|
|
|
2020-10-13 15:46:52 -04:00
|
|
|
|
2015-12-15 18:25:07 -05:00
|
|
|
#include "pbd/debug.h"
|
|
|
|
#include "pbd/event_loop.h"
|
|
|
|
#include "pbd/error.h"
|
|
|
|
#include "pbd/failed_constructor.h"
|
|
|
|
#include "pbd/pthread_utils.h"
|
2020-10-13 15:46:52 -04:00
|
|
|
#include "pbd/receiver.h"
|
|
|
|
#include "pbd/transmitter.h"
|
2015-12-15 18:25:07 -05:00
|
|
|
|
|
|
|
#include "ardour/audioengine.h"
|
2015-12-16 12:46:44 -05:00
|
|
|
#include "ardour/filename_extensions.h"
|
|
|
|
#include "ardour/types.h"
|
2015-12-15 18:25:07 -05:00
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace ARDOUR;
|
|
|
|
using namespace PBD;
|
|
|
|
|
|
|
|
static const char* localedir = LOCALEDIR;
|
|
|
|
|
2020-10-13 15:46:52 -04:00
|
|
|
class LogReceiver : public Receiver
|
2015-12-15 18:25:07 -05:00
|
|
|
{
|
2020-10-13 15:46:52 -04:00
|
|
|
protected:
|
|
|
|
void receive (Transmitter::Channel chn, const char * str) {
|
|
|
|
const char *prefix = "";
|
|
|
|
switch (chn) {
|
|
|
|
case Transmitter::Debug:
|
|
|
|
/* ignore */
|
|
|
|
return;
|
|
|
|
case Transmitter::Info:
|
|
|
|
/* ignore */
|
|
|
|
return;
|
|
|
|
case Transmitter::Warning:
|
|
|
|
prefix = ": [WARNING]: ";
|
|
|
|
break;
|
|
|
|
case Transmitter::Error:
|
|
|
|
prefix = ": [ERROR]: ";
|
|
|
|
break;
|
|
|
|
case Transmitter::Fatal:
|
|
|
|
prefix = ": [FATAL]: ";
|
|
|
|
break;
|
|
|
|
case Transmitter::Throw:
|
|
|
|
/* this isn't supposed to happen */
|
|
|
|
abort ();
|
|
|
|
}
|
2015-12-15 18:25:07 -05:00
|
|
|
|
2020-10-13 15:46:52 -04:00
|
|
|
/* note: iostreams are already thread-safe: no external
|
|
|
|
lock required.
|
|
|
|
*/
|
2015-12-15 18:25:07 -05:00
|
|
|
|
2020-10-13 15:46:52 -04:00
|
|
|
std::cout << prefix << str << std::endl;
|
2015-12-15 18:25:07 -05:00
|
|
|
|
2020-10-13 15:46:52 -04:00
|
|
|
if (chn == Transmitter::Fatal) {
|
|
|
|
::exit (9);
|
|
|
|
}
|
2015-12-15 18:25:07 -05:00
|
|
|
}
|
2020-10-13 15:46:52 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
LogReceiver log_receiver;
|
2015-12-15 18:25:07 -05:00
|
|
|
|
|
|
|
/* temporarily required due to some code design confusion (Feb 2014) */
|
|
|
|
|
|
|
|
#include "ardour/vst_types.h"
|
|
|
|
|
|
|
|
int vstfx_init (void*) { return 0; }
|
|
|
|
void vstfx_exit () {}
|
|
|
|
void vstfx_destroy_editor (VSTState*) {}
|
|
|
|
|
|
|
|
class MyEventLoop : public sigc::trackable, public EventLoop
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MyEventLoop (std::string const& name) : EventLoop (name) {
|
|
|
|
run_loop_thread = Glib::Threads::Thread::self();
|
|
|
|
}
|
|
|
|
|
2024-10-18 19:51:44 -04:00
|
|
|
bool call_slot (InvalidationRecord*, const std::function<void()>& f) {
|
2015-12-15 18:25:07 -05:00
|
|
|
if (Glib::Threads::Thread::self() == run_loop_thread) {
|
|
|
|
f ();
|
|
|
|
}
|
2022-06-08 19:46:27 -04:00
|
|
|
return true;
|
2015-12-15 18:25:07 -05:00
|
|
|
}
|
|
|
|
|
2023-04-21 14:02:22 -04:00
|
|
|
Glib::Threads::RWLock& slot_invalidation_rwlock() { return request_buffer_map_lock; }
|
2015-12-15 18:25:07 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
Glib::Threads::Thread* run_loop_thread;
|
2023-04-21 14:02:22 -04:00
|
|
|
Glib::Threads::RWLock request_buffer_map_lock;
|
2015-12-15 18:25:07 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
static MyEventLoop *event_loop;
|
|
|
|
|
|
|
|
void
|
2016-06-02 16:02:37 -04:00
|
|
|
SessionUtils::init (bool print_log)
|
2015-12-15 18:25:07 -05:00
|
|
|
{
|
2021-08-10 09:54:37 -04:00
|
|
|
if (!ARDOUR::init (true, localedir)) {
|
2015-12-15 18:25:07 -05:00
|
|
|
cerr << "Ardour failed to initialize\n" << endl;
|
2015-12-16 12:46:44 -05:00
|
|
|
::exit (EXIT_FAILURE);
|
2015-12-15 18:25:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
event_loop = new MyEventLoop ("util");
|
|
|
|
EventLoop::set_event_loop_for_thread (event_loop);
|
|
|
|
SessionEvent::create_per_thread_pool ("util", 512);
|
|
|
|
|
2016-06-02 16:02:37 -04:00
|
|
|
if (print_log) {
|
2020-10-13 15:46:52 -04:00
|
|
|
log_receiver.listen_to (warning);
|
|
|
|
log_receiver.listen_to (error);
|
|
|
|
log_receiver.listen_to (fatal);
|
2016-06-02 16:02:37 -04:00
|
|
|
}
|
2015-12-15 18:25:07 -05:00
|
|
|
}
|
|
|
|
|
2015-12-16 12:46:44 -05:00
|
|
|
// TODO return NULL, rather than exit() ?!
|
2015-12-15 18:25:07 -05:00
|
|
|
static Session * _load_session (string dir, string state)
|
|
|
|
{
|
|
|
|
AudioEngine* engine = AudioEngine::create ();
|
|
|
|
|
|
|
|
if (!engine->set_backend ("None (Dummy)", "Unit-Test", "")) {
|
|
|
|
std::cerr << "Cannot create Audio/MIDI engine\n";
|
|
|
|
::exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-12-16 12:46:44 -05:00
|
|
|
float sr;
|
|
|
|
SampleFormat sf;
|
2017-01-20 12:16:32 -05:00
|
|
|
std::string v;
|
2015-12-16 12:46:44 -05:00
|
|
|
|
|
|
|
std::string s = Glib::build_filename (dir, state + statefile_suffix);
|
2017-08-06 08:47:50 -04:00
|
|
|
|
|
|
|
if (!Glib::file_test (s, Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR)) {
|
|
|
|
std::cerr << "Cannot read session '"<< s << "'\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-20 12:16:32 -05:00
|
|
|
if (Session::get_info_from_path (s, sr, sf, v) == 0) {
|
2015-12-16 12:46:44 -05:00
|
|
|
if (engine->set_sample_rate (sr)) {
|
|
|
|
std::cerr << "Cannot set session's samplerate.\n";
|
2016-06-03 11:37:05 -04:00
|
|
|
return 0;
|
2015-12-16 12:46:44 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
std::cerr << "Cannot get samplerate from session.\n";
|
2016-06-03 11:37:05 -04:00
|
|
|
return 0;
|
2015-12-16 12:46:44 -05:00
|
|
|
}
|
|
|
|
|
2015-12-15 18:25:07 -05:00
|
|
|
if (engine->start () != 0) {
|
|
|
|
std::cerr << "Cannot start Audio/MIDI engine\n";
|
2016-06-03 11:37:05 -04:00
|
|
|
return 0;
|
2015-12-15 18:25:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Session* session = new Session (*engine, dir, state);
|
|
|
|
engine->set_session (session);
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
|
|
|
Session *
|
2016-06-03 11:37:05 -04:00
|
|
|
SessionUtils::load_session (string dir, string state, bool exit_at_failure)
|
2015-12-15 18:25:07 -05:00
|
|
|
{
|
|
|
|
Session* s = 0;
|
|
|
|
try {
|
|
|
|
s = _load_session (dir, state);
|
|
|
|
} catch (failed_constructor& e) {
|
|
|
|
cerr << "failed_constructor: " << e.what() << "\n";
|
2015-12-16 12:46:44 -05:00
|
|
|
::exit (EXIT_FAILURE);
|
2015-12-15 18:25:07 -05:00
|
|
|
} catch (AudioEngine::PortRegistrationFailure& e) {
|
|
|
|
cerr << "PortRegistrationFailure: " << e.what() << "\n";
|
2015-12-16 12:46:44 -05:00
|
|
|
::exit (EXIT_FAILURE);
|
2015-12-15 18:25:07 -05:00
|
|
|
} catch (exception& e) {
|
|
|
|
cerr << "exception: " << e.what() << "\n";
|
2015-12-16 12:46:44 -05:00
|
|
|
::exit (EXIT_FAILURE);
|
2015-12-15 18:25:07 -05:00
|
|
|
} catch (...) {
|
|
|
|
cerr << "unknown exception.\n";
|
2015-12-16 12:46:44 -05:00
|
|
|
::exit (EXIT_FAILURE);
|
2015-12-15 18:25:07 -05:00
|
|
|
}
|
2016-06-03 11:37:05 -04:00
|
|
|
if (!s && exit_at_failure) {
|
|
|
|
::exit (EXIT_FAILURE);
|
|
|
|
}
|
2015-12-15 18:25:07 -05:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-09-27 12:22:49 -04:00
|
|
|
Session *
|
|
|
|
SessionUtils::create_session (string dir, string state, float sample_rate)
|
|
|
|
{
|
|
|
|
AudioEngine* engine = AudioEngine::create ();
|
|
|
|
|
|
|
|
if (!engine->set_backend ("None (Dummy)", "Unit-Test", "")) {
|
|
|
|
std::cerr << "Cannot create Audio/MIDI engine\n";
|
|
|
|
::exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (engine->set_sample_rate (sample_rate)) {
|
|
|
|
std::cerr << "Cannot set session's samplerate.\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (engine->start () != 0) {
|
|
|
|
std::cerr << "Cannot start Audio/MIDI engine\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string s = Glib::build_filename (dir, state + statefile_suffix);
|
|
|
|
|
|
|
|
if (Glib::file_test (dir, Glib::FILE_TEST_EXISTS)) {
|
|
|
|
std::cerr << "Session folder already exists '"<< dir << "'\n";
|
|
|
|
}
|
|
|
|
if (Glib::file_test (s, Glib::FILE_TEST_EXISTS)) {
|
|
|
|
std::cerr << "Session file exists '"<< s << "'\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Session* session = new Session (*engine, dir, state);
|
|
|
|
engine->set_session (session);
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
2015-12-15 18:25:07 -05:00
|
|
|
void
|
|
|
|
SessionUtils::unload_session (Session *s)
|
|
|
|
{
|
|
|
|
delete s;
|
|
|
|
AudioEngine::instance()->stop ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SessionUtils::cleanup ()
|
|
|
|
{
|
|
|
|
ARDOUR::cleanup ();
|
|
|
|
delete event_loop;
|
|
|
|
pthread_cancel_all ();
|
|
|
|
}
|