Skip to content

Commit

Permalink
a bunch of improvements and new classes
Browse files Browse the repository at this point in the history
  • Loading branch information
roymacdonald committed Oct 10, 2023
1 parent 16da79c commit e9e21b2
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 24 deletions.
54 changes: 54 additions & 0 deletions src/SoundObjects/Panner.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// Panner.cpp
// ofxHapPlayerMultiAudio
//
// Created by Shadow Tuner on 10-10-23.
//

#include "Panner.h"
#include "ofSoundUtils.h"

Panner::Panner():
ofxSoundObject(OFX_SOUND_OBJECT_PROCESSOR),
_pan(0),
leftPan(1), rightPan(1)
{
setPan(0);
}

Panner::Panner(const Panner& a){
this->_pan = a.getPan();
}

Panner& Panner::operator=(const Panner& mom){
this->_pan = mom.getPan();
return *this;
}

void Panner::setPan(float pan){
float left, right;
ofStereoVolumes(1, pan, left, right);
_pan = pan;
leftPan = left;
rightPan = right;
}

float Panner::getPan() const{
return _pan;
}


void Panner::process(ofSoundBuffer &input, ofSoundBuffer &output){

output = input;
if(isBypassed())return;

if(output.getNumChannels() == 2){
output.stereoPan(leftPan, rightPan);
}else{
if(bShowNotice){
ofLogNotice("Panner::process") << "Not applying pan to buffer as it only works on stereo signals";
bShowNotice = false;
}
}
}
39 changes: 39 additions & 0 deletions src/SoundObjects/Panner.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// Panner.hpp
// ofxHapPlayerMultiAudio
//
// Created by Shadow Tuner on 10-10-23.
//
#pragma once
#include "ofMain.h"
#include "ofxSoundObject.h"

#include <atomic>


///\brief This object sets the pan for a mono or stereo buffer passing through it
class Panner: public ofxSoundObject{
public:

Panner();
Panner(const Panner& a);
Panner& operator=(const Panner& mom);

///\brief set the pan applied to the signal
///\param pan the pan value. range is -1 to 1. (left - right)
void setPan(float pan);

///\brief get the pan applied to the signal
///\returns pan in the range -1 to 1. (left - right)
float getPan() const;


virtual void process(ofSoundBuffer &input, ofSoundBuffer &output) override;
private:

std::atomic<float> _pan;
std::atomic<float> leftPan, rightPan;

bool bShowNotice = true;

};
3 changes: 2 additions & 1 deletion src/SoundObjects/VUMeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ void VUMeter::process(ofSoundBuffer &input, ofSoundBuffer &output){
}
//--------------------------------------------------------------
void VUMeter::calculate(ofSoundBuffer &input){

if(isBypassed())return;

size_t nc = input.getNumChannels();

processData.resize(nc);
Expand Down
14 changes: 12 additions & 2 deletions src/SoundObjects/waveformDraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,22 @@ void circularBufferWaveformDraw::allocate(size_t numFrames, size_t numChannels){
}

//--------------------------------------------------------------
void circularBufferWaveformDraw::pushBuffer(ofSoundBuffer& _buffer){
void circularBufferWaveformDraw::pushBuffer(const ofSoundBuffer& _buffer){
if(isBypassed())return;
std::lock_guard<std::mutex> lck(mutex1);
buffer.push(_buffer);
bRenderWaveforms = true;

}

//--------------------------------------------------------------
void circularBufferWaveformDraw::pushBuffer(const float* src, const size_t& srcSizePerChannel, int numChannels, int sampleRate){
if(isBypassed())return;
std::lock_guard<std::mutex> lck(mutex1);
buffer.push(src, srcSizePerChannel, numChannels, sampleRate);
bRenderWaveforms = true;
}


//--------------------------------------------------------------
void circularBufferWaveformDraw::process(ofSoundBuffer &input, ofSoundBuffer &output){

Expand Down
3 changes: 2 additions & 1 deletion src/SoundObjects/waveformDraw.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ class circularBufferWaveformDraw : public waveformDraw_<ofxCircularSoundBuffer>{


///\brief Push a buffer into the cicularbuffer and render it.
void pushBuffer(ofSoundBuffer& buffer);
void pushBuffer(const ofSoundBuffer& buffer);
void pushBuffer(const float* src, const size_t& srcSizePerChannel, int numChannels, int sampleRate);

protected:
virtual void updateWaveformMesh() override;
Expand Down
23 changes: 18 additions & 5 deletions src/ofxSoundMixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,25 @@ void ofxSoundMixer::audioOut(ofSoundBuffer &output) {
tempBuffer.setNumChannels(output.getNumChannels());
tempBuffer.setSampleRate(output.getSampleRate());

for(int i = 0; i < connections.size(); i++){
if (connections[i] != nullptr && connectionVolume[i] > 0) {

float v;
//create a temporary vector to avoid threading issues.
vector<ofxSoundObject*> tempConnections ;
{
std::lock_guard<std::mutex> lck(connectionMutex);
tempConnections = connections;
}

for(int i = 0; i < tempConnections.size(); i++){
{
std::lock_guard<std::mutex> lck(connectionMutex);
v = connectionVolume[i];
}

if (tempConnections[i] != nullptr && v > 0) {
tempBuffer.set(0);
connections[i]->audioOut(tempBuffer);

float v = connectionVolume[i];
tempConnections[i]->audioOut(tempBuffer);

for (int j = 0; j < tempBuffer.size(); j++) {
output.getBuffer()[j] += tempBuffer.getBuffer()[j] * v;
}
Expand Down
58 changes: 43 additions & 15 deletions src/ofxSoundUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,38 +40,66 @@ namespace ofxSoundUtils{
class ofxCircularSoundBuffer: public ofSoundBuffer{
public:
ofxCircularSoundBuffer():ofSoundBuffer(),
bNeedsAllocation(false),
newAllocSize(0)
bNeedsAllocation(false)
// newAllocSize(0)
{

}

void push(ofSoundBuffer& buffer){
void push(const float* src, const size_t& srcSizePerChannel, int numChannels, int sampleRate){
if(size() == 0 || getNumChannels() == 0 || bNeedsAllocation.load()){//} || getNumFrames() != buffer.getNumFrames() * numBuffers){
allocate(buffer.getNumFrames() * numBuffers, buffer.getNumChannels());
setSampleRate(buffer.getSampleRate());
allocate(srcSizePerChannel* numBuffers, numChannels);
setSampleRate(sampleRate);

bNeedsAllocation = false;
pushIndex %= getBuffer().size();
}
if(getBuffer().size() > 0){
if(getBuffer().size() > 0){

lastPushSize = buffer.getBuffer().size();
lastPushSize = srcSizePerChannel * numChannels;
if(pushIndex + lastPushSize < getBuffer().size()){
memcpy(&getBuffer()[pushIndex], &buffer.getBuffer()[0], sizeof(float) * lastPushSize);
memcpy(&getBuffer()[pushIndex], src, sizeof(float) * lastPushSize);
}else{

size_t n = getBuffer().size() - pushIndex;
memcpy(&getBuffer()[pushIndex], &buffer.getBuffer()[0], sizeof(float) * n);
memcpy(&getBuffer()[pushIndex], src, sizeof(float) * n);
if(lastPushSize - n > 0){
memcpy(&getBuffer()[0], &buffer.getBuffer()[n], sizeof(float) * (lastPushSize - n));
memcpy(&getBuffer()[0], src, sizeof(float) * (lastPushSize - n));
}
}
pushIndex += lastPushSize;

pushIndex %= getBuffer().size();


}
}

}

void push(const ofSoundBuffer& buffer){
push(buffer.getBuffer().data(), buffer.getNumFrames(), buffer.getNumChannels(), buffer.getSampleRate());
// if(size() == 0 || getNumChannels() == 0 || bNeedsAllocation.load()){//} || getNumFrames() != buffer.getNumFrames() * numBuffers){
// allocate(buffer.getNumFrames() * numBuffers, buffer.getNumChannels());
// setSampleRate(buffer.getSampleRate());
// bNeedsAllocation = false;
// pushIndex %= getBuffer().size();
// }
// if(getBuffer().size() > 0){
//
// lastPushSize = buffer.getBuffer().size();
// if(pushIndex + lastPushSize < getBuffer().size()){
// memcpy(&getBuffer()[pushIndex], &buffer.getBuffer()[0], sizeof(float) * lastPushSize);
// }else{
//
// size_t n = getBuffer().size() - pushIndex;
// memcpy(&getBuffer()[pushIndex], &buffer.getBuffer()[0], sizeof(float) * n);
// if(lastPushSize - n > 0){
// memcpy(&getBuffer()[0], &buffer.getBuffer()[n], sizeof(float) * (lastPushSize - n));
// }
// }
// pushIndex += lastPushSize;
//
// pushIndex %= getBuffer().size();
//
//
// }
}
// returns the index at which the last push was done. This means, the index of the newest data.
size_t getPushIndex() const {return pushIndex; }
Expand Down Expand Up @@ -127,7 +155,7 @@ class ofxCircularSoundBuffer: public ofSoundBuffer{

private:
std::atomic<bool> bNeedsAllocation;
std::atomic<size_t> newAllocSize;
// std::atomic<size_t> newAllocSize;
size_t numBuffers = 100;

size_t lastPushSize = 0;
Expand Down

0 comments on commit e9e21b2

Please sign in to comment.