Skip to content

Commit

Permalink
added helper function to set circular buffer size in millisecods
Browse files Browse the repository at this point in the history
  • Loading branch information
roymacdonald committed May 15, 2024
1 parent e4aaee1 commit 38ec68e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
23 changes: 16 additions & 7 deletions src/SoundObjects/waveformDraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,15 @@ void waveformDraw_<BufferType>::draw(const ofRectangle& viewport){
if(!viewport.isZero() && viewport != (ofRectangle)*this){
this->set(viewport);
bRenderWaveforms = true;
}
}


if(bRenderWaveforms){
makeWaveformMesh();
updateWaveformMesh();
bRenderWaveforms = false;
if(bUseFbo) bUpdateFbo = true;
}
if(bRenderWaveforms){
makeWaveformMesh();
updateWaveformMesh();
bRenderWaveforms = false;
if(bUseFbo) bUpdateFbo = true;
}
makeGrid();

if(!bCanvasIsSetup){
Expand Down Expand Up @@ -428,6 +428,15 @@ size_t circularBufferWaveformDraw::getNumBuffers(){
return buffer.getNumBuffersToStore();
}

//--------------------------------------------------------------
size_t circularBufferWaveformDraw::getBufferLengthInMs(){
return buffer.getBufferLengthInMs();
}
//--------------------------------------------------------------
void circularBufferWaveformDraw::setBufferLengthInMs(size_t lengthMs){
buffer.setBufferLengthInMs(lengthMs);
}

//--------------------------------------------------------------
void circularBufferWaveformDraw::updateWaveformMesh() {

Expand Down
4 changes: 3 additions & 1 deletion src/SoundObjects/waveformDraw.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ class circularBufferWaveformDraw : public waveformDraw_<ofxCircularSoundBuffer>{
void setNumBuffers(size_t numBuffers);
size_t getNumBuffers();


size_t getBufferLengthInMs();
void setBufferLengthInMs(size_t lengthMs);

///\brief Push a buffer into the cicularbuffer and render it.
void pushBuffer(const ofSoundBuffer& buffer);
void pushBuffer(const float* src, const size_t& srcSizePerChannel, int numChannels, int sampleRate);
Expand Down
9 changes: 8 additions & 1 deletion src/ofxSoundSpliter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,14 @@ bool ofxSoundSpliter::getObjectConnectionIndex(ofxSoundObject& obj, size_t& inde
bool ofxSoundSpliter::disconnectOutput(ofxSoundObject &soundObject){
if(isConnectedTo(soundObject)){
std::lock_guard<std::mutex> lck(connectionMutex);
ofRemove(connections, [&](ofxSoundObject * o){ return o == &soundObject; });
// ofRemove(connections, [&](ofxSoundObject * o){ return o == &soundObject; });

for (size_t i =0; i<connections.size(); i++) {
if (&soundObject == connections[i]) {
connections.erase(connections.begin() + i);
return true;
}
}
}
return false;
}
Expand Down
39 changes: 36 additions & 3 deletions src/ofxSoundUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,25 @@ class ofxCircularSoundBuffer: public ofSoundBuffer{

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(srcSizePerChannel* numBuffers, numChannels);
// if(bufferLengthInMs != 0){
if(!bSetNumBuffers && bufferLengthInMs == 0){
numBuffers = 100;
bSetNumBuffers = true;
}
if(bSetNumBuffers){
bufferLengthInMs = (srcSizePerChannel *numBuffers)/(sampleRate/1000);
bSetNumBuffers = false;
}

size_t allocSize = (sampleRate/1000)* bufferLengthInMs;
allocate(allocSize, numChannels);
numBuffers = allocSize/srcSizePerChannel;

// }else{
// if(numBuffers == 0) numBuffers = 100;
// size_t allocSize = srcSizePerChannel* numBuffers;
// allocate(allocSize, numChannels);
// }
setSampleRate(sampleRate);
if( getBuffer().size()){
bNeedsAllocation = false;
Expand Down Expand Up @@ -85,10 +103,23 @@ class ofxCircularSoundBuffer: public ofSoundBuffer{
void setNumBuffersToStore(size_t n){
if(numBuffers != n){
bNeedsAllocation = true;
bSetNumBuffers = true;
numBuffers = n;
}
}
size_t getNumBuffersToStore(){return numBuffers;}

size_t getNumBuffersToStore(){
return numBuffers;
}

size_t getBufferLengthInMs(){return bufferLengthInMs; }
void setBufferLengthInMs(size_t lengthMs){
if(bufferLengthInMs != lengthMs){
bNeedsAllocation = true;
bSetNumBuffers = false;
bufferLengthInMs = lengthMs;
}
}


// extract part of a channel from the circular buffer into an ofSoundBuffer.
Expand Down Expand Up @@ -167,8 +198,10 @@ class ofxCircularSoundBuffer: public ofSoundBuffer{

private:
std::atomic<bool> bNeedsAllocation;
bool bSetNumBuffers = false;
// std::atomic<size_t> newAllocSize;
size_t numBuffers = 100;
size_t numBuffers = 0;
size_t bufferLengthInMs = 0;

size_t lastPushSize = 0;

Expand Down

0 comments on commit 38ec68e

Please sign in to comment.