#include <sstream>
#include <glibmm/timer.h>
-#include <pbd/pthread_utils.h>
-#include <pbd/stacktrace.h>
-#include <pbd/unknown_type.h>
-
-#include <midi++/jack.h>
-
-#include <ardour/audioengine.h>
-#include <ardour/buffer.h>
-#include <ardour/port.h>
-#include <ardour/jack_audio_port.h>
-#include <ardour/jack_midi_port.h>
-#include <ardour/audio_port.h>
-#include <ardour/session.h>
-#include <ardour/cycle_timer.h>
-#include <ardour/utils.h>
-#ifdef VST_SUPPORT
-#include <fst.h>
-#endif
-
-#include <ardour/timestamps.h>
+#include "pbd/pthread_utils.h"
+#include "pbd/stacktrace.h"
+#include "pbd/unknown_type.h"
+
+#include "midi++/jack.h"
+
+#include "ardour/amp.h"
+#include "ardour/audio_port.h"
+#include "ardour/audioengine.h"
+#include "ardour/buffer.h"
+#include "ardour/buffer_set.h"
+#include "ardour/cycle_timer.h"
+#include "ardour/delivery.h"
+#include "ardour/event_type_map.h"
+#include "ardour/internal_return.h"
+#include "ardour/io.h"
+#include "ardour/meter.h"
+#include "ardour/midi_port.h"
+#include "ardour/port.h"
+#include "ardour/port_set.h"
+#include "ardour/session.h"
+#include "ardour/timestamps.h"
+#include "ardour/utils.h"
#include "i18n.h"
gint AudioEngine::m_meter_exit;
-static void
-ardour_jack_error (const char* msg)
-{
- error << "JACK: " << msg << endmsg;
-}
-
AudioEngine::AudioEngine (string client_name)
: ports (new Ports)
{
knows about it.
*/
- PBD::ThreadCreatedWithRequestSize (pthread_self(), X_("Audioengine"), 4096);
+ PBD::notify_gui_about_thread_creation (pthread_self(), X_("Audioengine"), 4096);
+#ifdef WITH_JACK_MIDI
MIDI::JACK_MidiPort::set_process_thread (pthread_self());
+#endif // WITH_JACK_MIDI
}
int
if (!_running) {
+ nframes_t blocksize = jack_get_buffer_size (_jack);
+
if (session) {
- nframes_t blocksize = jack_get_buffer_size (_jack);
-
BootMessage (_("Connect session to engine"));
session->set_block_size (blocksize);
session->set_frame_rate (jack_get_sample_rate (_jack));
-
+
/* page in as much of the session process code as we
can before we really start running.
*/
jack_set_sync_callback (_jack, _jack_sync_callback, this);
jack_set_freewheel_callback (_jack, _freewheel_callback, this);
- if (Config->get_jack_time_master()) {
+ if (session && session->config.get_jack_time_master()) {
jack_set_timebase_callback (_jack, 0, _jack_timebase_callback, this);
}
}
start_metering_thread();
+
+ _raw_buffer_sizes[DataType::AUDIO] = blocksize * sizeof(float);
}
return _running ? 0 : -1;
static_cast<AudioEngine*>(arg)->_freewheeling = onoff;
}
+void
+AudioEngine::split_cycle (nframes_t offset)
+{
+ /* caller must hold process lock */
+
+ Port::increment_port_offset (offset);
+
+ /* tell all Ports that we're going to start a new (split) cycle */
+
+ boost::shared_ptr<Ports> p = ports.reader();
+
+ for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
+ (*i)->cycle_split ();
+ }
+}
+
/** Method called by JACK (via _process_callback) which says that there
* is work to be done.
* @param nframes Number of frames to process.
AudioEngine::process_callback (nframes_t nframes)
{
// CycleTimer ct ("AudioEngine::process");
+
Glib::Mutex::Lock tm (_process_lock, Glib::TRY_LOCK);
/// The number of frames that will have been processed when we've finished
return 0;
}
- boost::shared_ptr<Ports> p = ports.reader();
+ /* tell all relevant objects that we're starting a new cycle */
- for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
+ Delivery::CycleStart (nframes);
+ Port::set_port_offset (0);
+ InternalReturn::CycleStart (nframes);
- /* Only run cycle_start() on output ports, because
- inputs must be done in the correct processing order,
- which requires interleaving with route processing.
- */
+ /* tell all Ports that we're starting a new cycle */
- (*i)->cycle_start (nframes, 0);
+ boost::shared_ptr<Ports> p = ports.reader();
+
+ for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
+ (*i)->cycle_start (nframes);
}
if (_freewheeling) {
Port *port = (*i);
if (port->sends_output()) {
- port->get_buffer(nframes, 0 ).silence(nframes);
+ port->get_buffer(nframes).silence(nframes);
}
}
- }
+ }
- // Finalize ports (ie write data if necessary)
+ // Finalize ports
for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
-
- (*i)->cycle_end (nframes, 0);
+ (*i)->cycle_end (nframes);
}
_processed_frames = next_processed_frames;
AudioEngine::jack_bufsize_callback (nframes_t nframes)
{
_buffer_size = nframes;
+ _raw_buffer_sizes[DataType::AUDIO] = nframes * sizeof(float);
+ cout << "FIXME: Assuming maximum MIDI buffer size " << nframes * 4 << "bytes" << endl;
+ _raw_buffer_sizes[DataType::MIDI] = nframes * 4;
_usecs_per_cycle = (int) floor ((((double) nframes / frame_rate())) * 1000000.0);
last_monitor_check = 0;
if (g_atomic_int_get(&m_meter_exit)) {
break;
}
- IO::update_meters ();
+ Metering::update_meters ();
}
}
boost::shared_ptr<Ports> p = ports.reader();
for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
- (*i)->cycle_start (blocksize, 0);
+ (*i)->cycle_start (blocksize);
}
s->process (blocksize);
s->process (blocksize);
for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
- (*i)->cycle_end (blocksize, 0);
+ (*i)->cycle_end (blocksize);
}
}
}
string reason;
if (p) {
- reason = _("a port with this name already exists: check for duplicated track/bus names");
+ reason = string_compose (_("a port with the name \"%1\" already exists: check for duplicated track/bus names"), portname);
} else {
- reason = _("unknown error");
+ reason = _("No more JACK ports are available. You will need to stop Ardour and restart JACK with ports if you need this many tracks.");
}
throw PortRegistrationFailure (string_compose (_("AudioEngine: cannot register port \"%1\": %2"), portname, reason).c_str());
}
Port *
-AudioEngine::register_port (DataType dtype, const string& portname, bool input, bool publish)
+AudioEngine::register_port (DataType dtype, const string& portname, bool input)
{
Port* newport = 0;
- /*cerr << "trying to register port with name " << portname << endl;*/
try {
if (dtype == DataType::AUDIO) {
- newport = new AudioPort (portname, (input ? Port::IsInput : Port::IsOutput), publish, frames_per_cycle());
+ newport = new AudioPort (portname, (input ? Port::IsInput : Port::IsOutput));
} else if (dtype == DataType::MIDI) {
- newport = new MidiPort (portname, (input ? Port::IsInput : Port::IsOutput), publish, frames_per_cycle());
+ newport = new MidiPort (portname, (input ? Port::IsInput : Port::IsOutput));
} else {
- throw unknown_type();
+ throw PortRegistrationFailure("unable to create port (unknown type)");
+ }
+
+ size_t& old_buffer_size = _raw_buffer_sizes[newport->type()];
+ size_t port_buffer_size = newport->raw_buffer_size(0);
+ if (port_buffer_size > old_buffer_size) {
+ old_buffer_size = port_buffer_size;
}
-
- /*cerr << "successfully got port " << portname << " with address " << newport << endl;*/
RCUWriter<Ports> writer (ports);
boost::shared_ptr<Ports> ps = writer.get_copy ();
- /*cerr << "Address of ports list: " << ps << endl
- << "Ports set size before insert: " << ps->size() << endl;*/
ps->insert (ps->begin(), newport);
- /*cerr << "Ports set size after insert: " << ps->size() << endl;*/
/* writer goes out of scope, forces update */
return newport;
}
- catch (...) {
- throw PortRegistrationFailure("unable to create port (unknown type?)");
+ catch (PortRegistrationFailure& err) {
+ throw err;
+ } catch (std::exception& e) {
+ throw PortRegistrationFailure(string_compose(
+ _("unable to create port: %1"), e.what()).c_str());
+ } catch (...) {
+ throw PortRegistrationFailure("unable to create port (unknown error)");
}
}
-Port*
-AudioEngine::get_port (const std::string& full_name)
-{
- boost::shared_ptr<Ports> p = ports.reader();
-
- for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
- //cerr << "comparing port name '" << (*i)->name() << "' with '" << full_name << "'" << endl;
- if ((*i)->name() == full_name) {
- return *i;
- }
- }
- return 0;
-}
-
-
Port *
-AudioEngine::register_input_port (DataType type, const string& portname, bool publish)
+AudioEngine::register_input_port (DataType type, const string& portname)
{
- return register_port (type, portname, true, publish);
+ return register_port (type, portname, true);
}
Port *
-AudioEngine::register_output_port (DataType type, const string& portname, bool publish)
+AudioEngine::register_output_port (DataType type, const string& portname)
{
- return register_port (type, portname, false, publish);
+ return register_port (type, portname, false);
}
int
{
/* caller must hold process lock */
- cerr << "about to unregister Port xx x" << &port << "\n";
-
if (!_running) {
/* probably happening when the engine has been halted by JACK,
in which case, there is nothing we can do here.
*/
- cerr << "not running\n";
return 0;
}
{
- cerr << "before getcopy\n";
-
RCUWriter<Ports> writer (ports);
boost::shared_ptr<Ports> ps = writer.get_copy ();
- cerr << "Ports set size: " << ps.get()->size() << endl;
-
for (Ports::iterator i = ps->begin(); i != ps->end(); ++i) {
- cerr << "before delete" << endl;
if ((*i) == &port) {
- cerr << "About to delete " << &port << endl;
delete *i;
ps->erase (i);
- cerr << "After erasing ports size: " << ps->size();
break;
}
}
/* writer goes out of scope, forces update */
}
- cerr << "before remove_connections\n";
remove_connections_for (port);
return 0;
int
AudioEngine::connect (const string& source, const string& destination)
{
+ /* caller must hold process lock */
+
int ret;
if (!_running) {
string s = make_port_name_non_relative (source);
string d = make_port_name_non_relative (destination);
-
- //cerr << "Trying to connect source: " << s << " with destination " << d << endl;
-
- Port* src = get_port (s);
- Port* dst = get_port (d);
-
- if (src && dst) {
-
- /* both ports are known to us, so do the internal connect stuff */
-
- if ((ret = src->connect (*dst)) == 0) {
- ret = dst->connect (*src);
- }
- } else if (src || dst) {
-
- /* one port is known to us, try to connect it to something external */
-
- PortConnectableByName* pcn;
- string other;
+ Port* src = get_port_by_name_locked (s);
+ Port* dst = get_port_by_name_locked (d);
if (src) {
- pcn = dynamic_cast<PortConnectableByName*>(src);
- other = d;
- } else {
- pcn = dynamic_cast<PortConnectableByName*>(dst);
- other = s;
- }
-
- if (pcn) {
- ret = pcn->connect (other);
- } else {
- ret = -1;
- }
-
+ ret = src->connect (d);
+ } else if (dst) {
+ ret = dst->connect (s);
} else {
-
/* neither port is known to us, and this API isn't intended for use as a general patch bay */
-
ret = -1;
-
}
if (ret > 0) {
- error << string_compose(_("AudioEngine: connection already exists: %1 (%2) to %3 (%4)"),
- source, s, destination, d)
- << endmsg;
+ /* already exists - no error, no warning */
} else if (ret < 0) {
error << string_compose(_("AudioEngine: cannot connect %1 (%2) to %3 (%4)"),
source, s, destination, d)
int
AudioEngine::disconnect (const string& source, const string& destination)
{
+ /* caller must hold process lock */
+
int ret;
if (!_running) {
string s = make_port_name_non_relative (source);
string d = make_port_name_non_relative (destination);
- //cerr << "trying to disconnect port '" << s << "' from port '" << d << endl;
-
- Port* src = get_port (s);
- Port* dst = get_port (d);
-
- if (src && dst) {
-
- /* both ports are known to us, so do the internal connect stuff */
-
- if ((ret = src->disconnect (*dst)) == 0) {
- ret = dst->disconnect (*src);
- }
-
- } else if (src || dst) {
-
- /* one port is known to us, try to connect it to something external */
-
-
- PortConnectableByName* pcn;
- string other;
-
- if (src) {
- pcn = dynamic_cast<PortConnectableByName*>(src);
- other = d;
- } else {
- pcn = dynamic_cast<PortConnectableByName*>(dst);
- other = s;
- }
-
- if (pcn) {
- ret = pcn->disconnect (other);
- } else {
- ret = -1;
- }
+ Port* src = get_port_by_name_locked (s);
+ Port* dst = get_port_by_name_locked (d);
+ if (src) {
+ ret = src->disconnect (d);
+ } else if (dst) {
+ ret = dst->disconnect (s);
} else {
-
/* neither port is known to us, and this API isn't intended for use as a general patch bay */
-
ret = -1;
-
}
-
return ret;
}
}
}
+size_t
+AudioEngine::raw_buffer_size (DataType t)
+{
+ std::map<DataType,size_t>::const_iterator s = _raw_buffer_sizes.find(t);
+ return (s != _raw_buffer_sizes.end()) ? s->second : 0;
+}
+
ARDOUR::nframes_t
AudioEngine::frames_per_cycle ()
{
}
}
-/** Get a port by name.
- * Note this can return NULL, it will NOT create a port if it is not found (any more).
+/** @param name Full name of port (including prefix:)
+ * @return Corresponding Port*, or 0. This object remains the property of the AudioEngine
+ * so must not be deleted.
*/
Port *
-AudioEngine::get_port_by_name (const string& portname, bool keep)
+AudioEngine::get_port_by_name (const string& portname)
{
+ string s;
+ if (portname.find_first_of (':') == string::npos) {
+ s = make_port_name_non_relative (portname);
+ } else {
+ s = portname;
+ }
+
Glib::Mutex::Lock lm (_process_lock);
+ return get_port_by_name_locked (s);
+}
+
+Port *
+AudioEngine::get_port_by_name_locked (const string& portname)
+{
+ /* caller must hold process lock */
if (!_running) {
if (!_has_run) {
- fatal << _("get_port_by_name() called before engine was started") << endmsg;
+ fatal << _("get_port_by_name_locked() called before engine was started") << endmsg;
/*NOTREACHED*/
} else {
return 0;
}
}
-
+
+ if (portname.substr (0, jack_client_name.length ()) != jack_client_name) {
+ /* not an ardour: port */
+ return 0;
+ }
+
+ std::string const rel = make_port_name_relative (portname);
+
boost::shared_ptr<Ports> pr = ports.reader();
-
+
for (Ports::iterator i = pr->begin(); i != pr->end(); ++i) {
- if (portname == (*i)->name()) {
- return (*i);
+ if (rel == (*i)->name()) {
+ return *i;
}
}
return 0;
}
+
+
+
+
const char **
AudioEngine::get_ports (const string& port_name_pattern, const string& type_name_pattern, uint32_t flags)
{
ae->_buffer_size = 0;
ae->_frame_rate = 0;
- cerr << "!!! HALTED !!!\n";
-
if (was_running) {
ae->Halted(); /* EMIT SIGNAL */
}
return 0;
}
- for (i = 0; ports[i]; ++i);
+ for (i = 0; ports[i]; ++i) {}
free (ports);
return i;
}
if (ports) {
- for (i = 0; ports[i]; ++i);
+ for (i = 0; ports[i]; ++i) {}
free (ports);
}
return i;
return "";
}
- for (i = 0; i < n && ports[i]; ++i);
+ for (i = 0; i < n && ports[i]; ++i) {}
if (ports[i]) {
ret = ports[i];
return ret;
}
-ARDOUR::nframes_t
-AudioEngine::get_port_total_latency (const Port& port)
-{
- return port.total_latency ();
-}
-
void
AudioEngine::update_total_latency (const Port& port)
{
void
AudioEngine::transport_start ()
{
- // cerr << "tell JACK to start\n";
if (_jack) {
jack_transport_start (_jack);
}
int
AudioEngine::reset_timebase ()
{
- if (_jack) {
- if (Config->get_jack_time_master()) {
+ if (_jack && session) {
+ if (session->config.get_jack_time_master()) {
return jack_set_timebase_callback (_jack, 0, _jack_timebase_callback, this);
} else {
return jack_release_timebase (_jack);
}
-#ifdef HAVE_JACK_CLIENT_OPEN
+static void
+ardour_jack_error (const char* msg)
+{
+ error << "JACK: " << msg << endmsg;
+}
int
AudioEngine::connect_to_jack (string client_name)
return 0;
}
-#else
-
-int
-AudioEngine::connect_to_jack (string client_name)
-{
- jack_client_name = client_name;
-
- if ((_jack = jack_client_new (client_name.c_str())) == 0) {
- return -1;
- }
-
- return 0;
-}
-
-#endif /* HAVE_JACK_CLIENT_OPEN */
-
int
AudioEngine::disconnect_from_jack ()
{
return 0;
}
-
if (_running) {
stop_metering_thread ();
}
_buffer_size = 0;
_frame_rate = 0;
+ _raw_buffer_sizes.clear();
if (_running) {
_running = false;
nframes_t blocksize = jack_get_buffer_size (_jack);
session->set_block_size (blocksize);
session->set_frame_rate (jack_get_sample_rate (_jack));
+
+ _raw_buffer_sizes[DataType::AUDIO] = blocksize * sizeof(float);
+ cout << "FIXME: Assuming maximum MIDI buffer size " << blocksize * 4 << "bytes" << endl;
+ _raw_buffer_sizes[DataType::MIDI] = blocksize * 4;
}
last_monitor_check = 0;
jack_set_sync_callback (_jack, _jack_sync_callback, this);
jack_set_freewheel_callback (_jack, _freewheel_callback, this);
- if (Config->get_jack_time_master()) {
+ if (session && session->config.get_jack_time_master()) {
jack_set_timebase_callback (_jack, 0, _jack_timebase_callback, this);
- }
+ }
if (jack_activate (_jack) == 0) {
_running = true;