Skip to content
Merged
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
24 changes: 19 additions & 5 deletions chowdsp_convolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,15 @@ void load_ir (const Config* config, IR_Uniform* ir, const float* ir_data, int ir
for (int seg_idx = 0; seg_idx < ir->num_segments; ++seg_idx)
{
float* segment = get_segment (config, ir->segments, seg_idx);
memcpy (segment,
ir_data + current_ptr,
std::min (config->fft_size - config->block_size, ir_num_samples - current_ptr) * sizeof (float));
const auto segment_n = std::min (config->fft_size - config->block_size, ir_num_samples - current_ptr);
memcpy (segment, ir_data + current_ptr, segment_n * sizeof (float));
memset (segment + segment_n, 0, (config->fft_size - segment_n) * sizeof (float));
fft::fft_transform_unordered (config->fft,
segment,
segment,
fft_scratch,
fft::FFT_FORWARD);

current_ptr += config->fft_size - config->block_size;
current_ptr += segment_n;
}
}

Expand Down Expand Up @@ -129,6 +128,7 @@ void load_multichannel_ir (const Config* config, IR_Uniform* ir, const float* co
{
assert (num_channels == ir->num_channels);

int new_num_segments = 0;
for (int ch = 0; ch < num_channels; ++ch)
{
IR_Uniform this_channel_ir {
Expand All @@ -138,7 +138,9 @@ void load_multichannel_ir (const Config* config, IR_Uniform* ir, const float* co
.num_channels = 1,
};
load_ir (config, &this_channel_ir, ir_data[ch], ir_num_samples, fft_scratch);
Copy link

Copilot AI Jun 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assignment overwrites the segment count on each channel without checking consistency. If different channels have different segment counts, the final value may be incorrect. Add an assert or verify that this_channel_ir.num_segments matches previous channels before updating.

Suggested change
load_ir (config, &this_channel_ir, ir_data[ch], ir_num_samples, fft_scratch);
load_ir (config, &this_channel_ir, ir_data[ch], ir_num_samples, fft_scratch);
if (ch == 0)
{
expected_num_segments = this_channel_ir.num_segments; // Set expected value
}
else
{
assert(this_channel_ir.num_segments == expected_num_segments && "Inconsistent num_segments across channels");
}

Copilot uses AI. Check for mistakes.
new_num_segments = this_channel_ir.num_segments;
}
ir->num_segments = new_num_segments;
}

//================================================================================================================
Expand Down Expand Up @@ -214,6 +216,18 @@ void reset_process_state (const Config* config, Process_Uniform_State* state)
}
}

void reset_process_state_segments (const Convolution_Config* config, Process_Uniform_State* state, const IR_Uniform* ir)
{
const auto segment_num_samples = config->fft_size;
for (int ch = 0; ch < state->num_channels; ++ch)
{
auto& state_data = state->state_data[ch];
memset (state_data.segments + segment_num_samples * ir->num_segments,
0,
segment_num_samples * (state->max_num_segments - ir->num_segments) * sizeof (float));
}
}

void destroy_process_state (Process_Uniform_State* state)
{
if (state->state_data != nullptr)
Expand Down
3 changes: 3 additions & 0 deletions chowdsp_convolution.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ void create_multichannel_process_state (const struct Convolution_Config*, const
/** Zeros the process state. */
void reset_process_state (const struct Convolution_Config*, struct Process_Uniform_State*);

/** Zeros the process state. */
void reset_process_state_segments (const struct Convolution_Config*, struct Process_Uniform_State*, const struct IR_Uniform*);

/** De-allocates the state's internal data. */
void destroy_process_state (struct Process_Uniform_State*);

Expand Down
Loading