Skip to content

Commit

Permalink
Call resize_output_buffers() from set_channels(), fixing crash in Cha…
Browse files Browse the repository at this point in the history
…nnelPanner; update SIGNALFLOW_NODE_INITIAL_OUTPUT_CHANNELS
  • Loading branch information
ideoforms committed Aug 5, 2024
1 parent f79180c commit 72ee87a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
7 changes: 3 additions & 4 deletions source/include/signalflow/core/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ typedef RingBuffer<sample> SampleRingBuffer;
#define SIGNALFLOW_MAX_CHANNELS 64

/*------------------------------------------------------------------------
* Initial number of output buffers to allocate per node
* TODO: Turn this into a run-time config parameter, and set default to 2
* Otherwise memory usage is very high by default
* Initial number of output channels to allocate per node.
* More channels are allocated as needed at runtime.
*-----------------------------------------------------------------------*/
#define SIGNALFLOW_NODE_INITIAL_OUTPUT_BUFFERS 64
#define SIGNALFLOW_NODE_INITIAL_OUTPUT_CHANNELS 2

/*------------------------------------------------------------------------
* Max supported number of FFT bins.
Expand Down
2 changes: 1 addition & 1 deletion source/include/signalflow/node/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class Node

/**------------------------------------------------------------------------
* Get the number of output channels allocated in memory.
* Initially, a node allocates SIGNALFLOW_NODE_INITIAL_OUTPUT_BUFFERS
* Initially, a node allocates SIGNALFLOW_NODE_INITIAL_OUTPUT_CHANNELS
* channels. During the course of graph construction, if more channels
* than this are required, it re-allocates the memory.
*
Expand Down
16 changes: 14 additions & 2 deletions source/src/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Node::Node()
this->output_buffer_length = SIGNALFLOW_NODE_BUFFER_SIZE;
}

this->resize_output_buffers(SIGNALFLOW_NODE_INITIAL_OUTPUT_BUFFERS);
this->resize_output_buffers(SIGNALFLOW_NODE_INITIAL_OUTPUT_CHANNELS);

/*------------------------------------------------------------------------
* last_num_frames caches the number of frames generated in the last
Expand Down Expand Up @@ -126,6 +126,18 @@ void Node::set_channels(int num_input_channels, int num_output_channels)
this->num_input_channels = num_input_channels;
this->num_output_channels = num_output_channels;
this->matches_input_channels = false;

/*--------------------------------------------------------------------------------
* Added 2024-08-05 to address crash in which a multichannel ChannelPanner
* connected directly to AudioOut does not correctly allocate its buffers, as
* resize_output_buffers() currently only gets called in the
* `this->matches_input_channels` block in `update_channels()` (below).
*
* This means that resize_output_buffers() is called on every call to
* set_channels(). However, resize_output_buffers() is a no-op if no resize
* operation is needed, so this should have minimal impact on CPU.
*--------------------------------------------------------------------------------*/
this->resize_output_buffers(num_output_channels);
}

void Node::update_channels()
Expand Down Expand Up @@ -245,7 +257,7 @@ void Node::resize_output_buffers(int output_buffer_count)
else
{
/*------------------------------------------------------------------------
* If not enough channels/frames are allocated, dealloc the current
* If not enough channels/frames are allocated, dealloc the current
* allocations and resize
*-----------------------------------------------------------------------*/
this->free();
Expand Down
2 changes: 1 addition & 1 deletion source/src/python/constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void init_python_constants(py::module &m)
.export_values();

m.attr("SIGNALFLOW_MAX_CHANNELS") = SIGNALFLOW_MAX_CHANNELS;
m.attr("SIGNALFLOW_NODE_INITIAL_OUTPUT_BUFFERS") = SIGNALFLOW_NODE_INITIAL_OUTPUT_BUFFERS;
m.attr("SIGNALFLOW_NODE_INITIAL_OUTPUT_CHANNELS") = SIGNALFLOW_NODE_INITIAL_OUTPUT_CHANNELS;
m.attr("SIGNALFLOW_DEFAULT_FFT_SIZE") = SIGNALFLOW_DEFAULT_FFT_SIZE;
m.attr("SIGNALFLOW_MAX_FFT_SIZE") = SIGNALFLOW_MAX_FFT_SIZE;
m.attr("SIGNALFLOW_DEFAULT_FFT_HOP_SIZE") = SIGNALFLOW_DEFAULT_FFT_HOP_SIZE;
Expand Down

0 comments on commit 72ee87a

Please sign in to comment.