function [env p1 p2] = gen7(varargin)
%Score F-Statements

% The Csound GEN routines are specified by a function, or "f-statements" in the score file. A Csound score may contain hundreds of f-statements, each with many parameters. A prototypical f-statement appears as follows:
% 
% f	p1	p2	p3	p4	p5 . . .
% 
% While parameter numbers 5 and higher specify different f-table attributes for different GEN routines, the first four parameters determine the following qualities for all GEN routines:
% 
% • p1 = a unique f-table identification number
% 
% • p2 = f-statement initialization time expressed in score beats
% 
% • p3 = f-table size
% 
% • p4 = GEN routine called to create the f-table
% GEN7
% 
% Csound also includes four GEN routines that draw lines and curves 
% between specified points. For each of these, 
% the odd-numbered parameters (p5, p7 and p9 ) 
% and even-numbered parameters (p6, p8, p10), 
% contain ordinate and segment length values respectively.
% 
% e.g. 
% f 15	0	512	7	1 512 -1	; sawtooth
%   a = gen7(15,0,512,7,1,512,-1)
%
% f 16	0	1024	7	1 512 1 0 -1 512 -1	; square
%   a = gen7(16,0,1024,7,1,512,1,0,-1,512,-1);
%
% f 17	0	512	7	-1 100 -1 312 1 100 1	; for waveshaping
%   a = gen7(17,0,512,7,-1,100,-1,312,1,100,1);
%
% f 18	0	128	7	1 1 1 0 0	; a single impulse
%   a = gen7(18,0,128,7,1,1,1,0,0);
%
% FM:
% wood-drum index envelope, linear
% f 4 0 1024 7 1 156 0 871 0
%
% brass envelopes, a simple linear 4-segment envelope
% f 5 0 1024 7 0 102 1 51 .75 768 .75103 0
%    a = gen7(5,0,1024,7,0,102,1,51,.75,768,.75103,0);
% 
% See https://www.eumus.edu.uy/eme/ensenanza/electivas/csound/materiales/book_chapters/02nelson/02nelson.html
%
%  V1.0
%  Author: David Marshall Oct 2020


if (varargin{4} ~= 7)
    disp('Oops Wrong Gen this is GEN7');
    retrun;
end;


p1 = varargin{1};

p2 = varargin{2};

env = zeros(1,varargin{3});
env_i= 0;

for index = 5:2:nargin-2
    grad = (varargin{index+2}-varargin{index})/(varargin{index+1}-1);
    if ( isnan(grad) ) grad = 0; end;
    for loop_i = 1:varargin{index+1}
        env(env_i + loop_i) = (loop_i-1)*grad + varargin{index};
    end;
    env_i = env_i + varargin{index+1};
end;

end

