MATLAB: Index exceeds the number of array elements (480)

audiotoolbox

Error occurs when attemping to validate the plugin
classdef modDelay < audioPlugin
properties
Depth = 0.004; % Range(0.001,0.007)
Rate = 0.6; % Range(0.1,20)
feedback = 0.5;
end
properties (Access = private)
modDelayLineL = zeros(round(0.01*48000),1);
modDelayLineR = zeros(round(0.01*48000),1);
modIdxL = 1;
modIdxR = 1;
phase = 0;
end
properties (Constant)
end
methods
function out = process(plugin,in)
L = length(in);
out = zeros(L,2);
Fs = getSampleRate(plugin);
T = 1/Fs;
for n = 1:L
modReadIdxL = plugin.modIdxL + round(abs(round(plugin.Depth*Fs)*...
sin(plugin.phase)));
modReadIdxR = plugin.modIdxR + round(abs(round(plugin.Depth*Fs)*...
sin(plugin.phase)));
% Left Channel Read Index
if modReadIdxL > round(0.01*Fs)
modReadIdxL = modReadIdxL - round(0.01*Fs);
end
% Right Channel Read Index
if modReadIdxR > round(0.01*Fs)
modReadIdxR = modReadIdxR - round(0.01*Fs);
end
% Increment the phase of control sinusoid
plugin.phase = plugin.phase + 2*pi*plugin.Rate*T;
% Wrap phase around 2*pi
if(plugin.phase > 2*pi)
plugin.phase = plugin.phase - 2*pi;
end
% Write to Output
out(n,1) = plugin.modDelayLineL(modReadIdxL);
out(n,2) = plugin.modDelayLineR(modReadIdxR);
% Write to Delay Lines
% Left Channel Delay
plugin.modDelayLineL(plugin.modIdxL) = in(n,1) +...
plugin.feedback*plugin.modDelayLineL(modReadIdxL);
% Right Channel Delay
plugin.modDelayLineR(plugin.modIdxR) = in(n,2) +...
plugin.modDelayLineR(modReadIdxR);
% Increment Modulation Index Values
plugin.modIdxL = plugin.modIdxL + 1; % Left Channel

plugin.modIdxR = plugin.modIdxL + 1; % Right Channel

% Wrap Index Values Around to Create a Circular Buffer
% Left Channel
if plugin.modIdxL > round(0.01*Fs)
plugin.modIdxL = 1;
end
% Right Channel
if plugin.modIdxR > round(0.01*Fs)
plugin.modIdxR = 1;
end
end
function reset(plugin)
plugin.modDelayLineL = zeros(round(0.01*48000),1);
plugin.modDelayLineR = zeros(round(0.01*48000),1);
plugin.modIdxL = 1;
plugin.modIdxR = 1;
plugin.phase = 0;
end
end
end
end

Best Answer

Hi Nathanael,
I took a look at the code. I saw two issues:
a. There seems to be a misplaced end that was making the reset method unreachable
b. In reset, instead of 48e3, I use getSampleRate to get the sample rate.
Note that reset is called automatically when the sample rate changes
With these changes, it seems validateAudioPlugin works.
classdef modDelay < audioPlugin
properties
Depth = 0.004; % Range(0.001,0.007)
Rate = 0.6; % Range(0.1,20)
feedback = 0.5;
end
properties (Access = private)
modDelayLineL = zeros(round(0.01*48000),1);
modDelayLineR = zeros(round(0.01*48000),1);
modIdxL = 1;
modIdxR = 1;
phase = 0;
end
properties (Constant)
end
methods
function out = process(plugin,in)
L = length(in);
out = zeros(L,2);
Fs = getSampleRate(plugin);
T = 1/Fs;
for n = 1:L
modReadIdxL = plugin.modIdxL + round(abs(round(plugin.Depth*Fs)*...
sin(plugin.phase)));
modReadIdxR = plugin.modIdxR + round(abs(round(plugin.Depth*Fs)*...
sin(plugin.phase)));
% Left Channel Read Index
if modReadIdxL > round(0.01*Fs)
modReadIdxL = modReadIdxL - round(0.01*Fs);
end
% Right Channel Read Index
if modReadIdxR > round(0.01*Fs)
modReadIdxR = modReadIdxR - round(0.01*Fs);
end
% Increment the phase of control sinusoid
plugin.phase = plugin.phase + 2*pi*plugin.Rate*T;
% Wrap phase around 2*pi
if(plugin.phase > 2*pi)
plugin.phase = plugin.phase - 2*pi;
end
% Write to Output
out(n,1) = plugin.modDelayLineL(modReadIdxL);
out(n,2) = plugin.modDelayLineR(modReadIdxR);
% Write to Delay Lines
% Left Channel Delay
plugin.modDelayLineL(plugin.modIdxL) = in(n,1) +...
plugin.feedback*plugin.modDelayLineL(modReadIdxL);
% Right Channel Delay
plugin.modDelayLineR(plugin.modIdxR) = in(n,2) +...
plugin.modDelayLineR(modReadIdxR);
% Increment Modulation Index Values
plugin.modIdxL = plugin.modIdxL + 1; % Left Channel

plugin.modIdxR = plugin.modIdxL + 1; % Right Channel

% Wrap Index Values Around to Create a Circular Buffer
% Left Channel
if plugin.modIdxL > round(0.01*Fs)
plugin.modIdxL = 1;
end
% Right Channel
if plugin.modIdxR > round(0.01*Fs)
plugin.modIdxR = 1;
end
end
end
function reset(plugin)
Fs = getSampleRate(plugin);
plugin.modDelayLineL = zeros(round(0.01*Fs),1);
plugin.modDelayLineR = zeros(round(0.01*Fs),1);
plugin.modIdxL = 1;
plugin.modIdxR = 1;
plugin.phase = 0;
end
end
end
Related Question