Skip to content

Commit

Permalink
Improved thread safety for ofxSoundMixer. Added getObjectConnectionIn…
Browse files Browse the repository at this point in the history
…dex function
  • Loading branch information
roymacdonald committed Jul 17, 2023
1 parent 1887d6e commit 289a069
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
53 changes: 38 additions & 15 deletions src/ofxSoundMixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,30 @@ void ofxSoundMixer::masterVolChanged(float& f) {
}
//----------------------------------------------------
ofxSoundMixer::~ofxSoundMixer(){
std::lock_guard<std::mutex> lck(connectionMutex);
connections.clear();
connectionVolume.clear();
}
//----------------------------------------------------
ofxSoundObject* ofxSoundMixer::getConnectionSource(int connectionNumber){
if (connectionNumber < connections.size()) {
return connections[connectionNumber];
}else{
return nullptr;
}
ofxSoundObject* ofxSoundMixer::getConnectionSource(size_t connectionNumber){
ofxSoundObject* src = nullptr;
{
std::lock_guard<std::mutex> lck(connectionMutex);
if (connectionNumber < connections.size()) {
src = connections[connectionNumber];
}
}
return src;
}
//----------------------------------------------------
ofxSoundObject* ofxSoundMixer::getChannelSource(int channelNumber){
return getConnectionSource(channelNumber);
}
//----------------------------------------------------
void ofxSoundMixer::disconnectInput(ofxSoundObject * input){
for (int i =0; i<connections.size(); i++) {
std::lock_guard<std::mutex> lck(connectionMutex);

for (size_t i =0; i<connections.size(); i++) {
if (input == connections[i]) {
connections.erase(connections.begin() + i);
connectionVolume.erase(connectionVolume.begin() + i);
Expand All @@ -51,7 +57,8 @@ void ofxSoundMixer::disconnectInput(ofxSoundObject * input){
}
//----------------------------------------------------
void ofxSoundMixer::setInput(ofxSoundObject *obj){
for (int i =0; i<connections.size(); i++) {
std::lock_guard<std::mutex> lck(connectionMutex);
for (size_t i =0; i<connections.size(); i++) {
if (obj == connections[i]) {
ofLogNotice("ofxSoundMixer::setInput") << " already connected" << endl;
return;
Expand All @@ -62,9 +69,11 @@ void ofxSoundMixer::setInput(ofxSoundObject *obj){
}
//----------------------------------------------------
size_t ofxSoundMixer::getNumChannels(){
std::lock_guard<std::mutex> lck(connectionMutex);
return connections.size();
}
size_t ofxSoundMixer::getNumConnections(){
std::lock_guard<std::mutex> lck(connectionMutex);
return connections.size();
}
//----------------------------------------------------
Expand All @@ -88,8 +97,20 @@ void ofxSoundMixer::setMasterPan(float pan){
mutex.unlock();
}
//----------------------------------------------------
bool ofxSoundMixer::getObjectConnectionIndex(ofxSoundObject& obj, size_t& index){
std::lock_guard<std::mutex> lck(connectionMutex);
for (size_t i =0; i<connections.size(); i++) {
if (&obj == connections[i]) {
index = i;
return true;
}
}
return false;
}
//----------------------------------------------------
bool ofxSoundMixer::isConnectedTo(ofxSoundObject& obj){
for (int i =0; i<connections.size(); i++) {
std::lock_guard<std::mutex> lck(connectionMutex);
for (size_t i =0; i<connections.size(); i++) {
if (&obj == connections[i]) {
return true;
}
Expand All @@ -101,19 +122,21 @@ void ofxSoundMixer::setChannelVolume(int channelNumber, float vol){
setConnectionVolume(channelNumber, vol);
}
//----------------------------------------------------
void ofxSoundMixer::setConnectionVolume(int channelNumber, float vol){
if (channelNumber < connectionVolume.size()) {
connectionVolume[channelNumber] = vol;
void ofxSoundMixer::setConnectionVolume(size_t connectionIndex, float vol){
std::lock_guard<std::mutex> lck(connectionMutex);
if (connectionIndex < connectionVolume.size()) {
connectionVolume[connectionIndex] = vol;
}
}
//----------------------------------------------------
float ofxSoundMixer::getChannelVolume(int channelNumber){
return getConnectionVolume(channelNumber);
}
//----------------------------------------------------
float ofxSoundMixer::getConnectionVolume(int channelNumber){
if (channelNumber < connectionVolume.size()) {
return connectionVolume[channelNumber];
float ofxSoundMixer::getConnectionVolume(size_t connectionIndex){
std::lock_guard<std::mutex> lck(connectionMutex);
if (connectionIndex < connectionVolume.size()) {
return connectionVolume[connectionIndex];
}
return 0;
}
Expand Down
15 changes: 11 additions & 4 deletions src/ofxSoundMixer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ofxSoundMixer: public ofxSoundObject {
OF_DEPRECATED_MSG("Use getConnectionSource instead",ofxSoundObject* getChannelSource(int channelIndex));

/// returns the connected object at the specified index
ofxSoundObject* getConnectionSource(int connectionIndex);
ofxSoundObject* getConnectionSource(size_t connectionIndex);
/// get the number of connected objects.
OF_DEPRECATED_MSG("Use getNumConnections() instead", size_t getNumChannels() override;)
size_t getNumConnections();
Expand All @@ -34,12 +34,18 @@ class ofxSoundMixer: public ofxSoundObject {

void audioOut(ofSoundBuffer &output) override;
bool isConnectedTo(ofxSoundObject& obj);

///\ brief Gets the connection index for the passed object, if it is connected.
///\param obj the object for which you want to find the connection channel
///\param index the index, if found will be passed back in this parameter, as it is a reference.
///\returns true if there is a connection for the passed object, false otherwise.
bool getObjectConnectionIndex(ofxSoundObject& obj, size_t& index);

OF_DEPRECATED_MSG("Use setConnectionVolume", void setChannelVolume(int channelNumber, float vol));
OF_DEPRECATED_MSG("Use getConnectionVolume", float getChannelVolume(int channelNumber));

void setConnectionVolume(int channelNumber, float vol);
float getConnectionVolume(int channelNumber);
void setConnectionVolume(size_t connectionIndex, float vol);
float getConnectionVolume(size_t connectionIndex);

ofParameter<float> masterVol;
protected:
Expand All @@ -50,6 +56,7 @@ class ofxSoundMixer: public ofxSoundObject {
float masterPan;
float masterVolume;
void setInput(ofxSoundObject *obj) override;
ofMutex mutex;
ofMutex mutex, connectionMutex;

};

0 comments on commit 289a069

Please sign in to comment.