% fmbrass.m
%
% Author Dave Marshall Oct 2020

% Signal parameters
fs = 22050;
T = 1/fs;
dur = 0.6;
t = 0:T:dur;
env_scaling = floor(length(t)/1024); % 1024 was the initial size of envelope

%Create and Amplitude Envelope as per Chowing via CSOUND GEN7 params
env1 = gen7(5,0,length(t),7,0,102*env_scaling,1,51*env_scaling,.75,768*env_scaling,.75,103*env_scaling,0);

% env1 is the overall amplitude envelope.
%This is essentially an ADSR curve --- it approximates the blowing of brass instrument where there is an initial burst of air (AD part) then a steady state blowing (S) and quite an abrupt release (r) when the blowing stops.

plot(env1);

% Now let's creat the FM brass sound a la Chowning, fmbrass.m:

% FM parameters
fc = 440;
fm = 440;
Dmax = 5;
D = Dmax.*env1;

ybrass = env1.*sin(2*pi*fc*t + D.*sin(2*pi*fm*t));

figure
plot(t, ybrass);

dospectrum = input('\nPlot spectrum? Y/[N]:\n\n', 's');

if dospectrum == 'Y',
figure
specgram(ybrass, 512, fs, [], 256)
end;

% It sounds like this

PlayFM = input('\nPlay Brass Sound? Y/[N]:\n\n', 's');

if PlayFM == 'Y',
  sound(ybrass, fs);
end
