Skip to content

Add the ability to remove ports #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions include/jackaudioio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,23 @@ In that method you can get audio in from jack and write it out to jack.
virtual unsigned int addOutPort(std::string name)
throw(std::runtime_error);

/**
@brief Remove a jack input port from our client.
This method cannot be called while the client is running.
\param name string the name of the port to remove
\return the number of total input ports
*/
virtual unsigned int removeInPort(std::string name)
throw(std::runtime_error);
/**
@brief Remove a jack output port from our client.
This method cannot be called while the client is running.
\param name string the name of the port to remove
\return the number of total output ports
*/
virtual unsigned int removeOutPort(std::string name)
throw(std::runtime_error);

/**
@brief Connect our output to a jack client's source port.
\param index the index of our output port to connect from.
Expand Down
60 changes: 60 additions & 0 deletions src/jackaudioio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,66 @@ unsigned int JackCpp::AudioIO::addOutPort(std::string name)
return mOutputPorts.size() - 1;
}

unsigned int JackCpp::AudioIO::removeInPort(std::string name)
throw(std::runtime_error)
{
// if the client is active this presents all sorts of issues
if (mJackState == active)
throw std::runtime_error("removing ports while the client is active is not supported");

std::vector<std::string>::iterator name_iter;
name_iter = std::find(mPortNames.begin(), mPortNames.end(), name);
if (name_iter == mPortNames.end())
throw std::runtime_error("cannot remove non-existent port: " + name);

std::vector<jack_port_t *>::iterator port_iter = mInputPorts.begin();
for (; port_iter != mInputPorts.end(); ++port_iter) {
if (std::string(jack_port_short_name(*port_iter)) == name)
break;
}
if (port_iter == mInputPorts.end())
throw std::runtime_error("could not find port in port list!");

if (jack_port_unregister(mJackClient, *port_iter))
throw std::runtime_error("could not unregister port!");

mInputPorts.erase(port_iter);
mPortNames.erase(name_iter);
mJackInBuf.pop_back();

return mInputPorts.size();
}

unsigned int JackCpp::AudioIO::removeOutPort(std::string name)
throw(std::runtime_error)
{
// if the client is active this presents all sorts of issues
if (mJackState == active)
throw std::runtime_error("removing ports while the client is active is not supported");

std::vector<std::string>::iterator name_iter;
name_iter = std::find(mPortNames.begin(), mPortNames.end(), name);
if (name_iter == mPortNames.end())
throw std::runtime_error("cannot remove non-existent port: " + name);

std::vector<jack_port_t *>::iterator port_iter = mOutputPorts.begin();
for (; port_iter != mOutputPorts.end(); ++port_iter) {
if (std::string(jack_port_short_name(*port_iter)) == name)
break;
}
if (port_iter == mOutputPorts.end())
throw std::runtime_error("could not find port in port list!");

if (jack_port_unregister(mJackClient, *port_iter))
throw std::runtime_error("could not unregister port!");

mOutputPorts.erase(port_iter);
mPortNames.erase(name_iter);
mJackOutBuf.pop_back();

return mOutputPorts.size();
}

void JackCpp::AudioIO::connectTo(unsigned int index, std::string destPortName)
throw(std::range_error, std::runtime_error)
{
Expand Down