Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

[b]Implementation of an ADSR envelope, help needed! [/b]

Status
Not open for further replies.

antonywilliams

Civil/Environmental
Mar 21, 2006
7
Guys,


I'm trying to implement an ADSR envelope to a piano note that I have synthesised. I basically just want to apply the envelope to the final signal 'y'.

Anyone done this before or have and code/ideas on how to get it to work?

Thanks in advance!
 
Replies continue below

Recommended for you

You've got 6 parameters. Build an envelope with the same number of samples as the note. multiply the two together.

OK, that could sound odd as there are some weird phase effects.

Still, i haven't found any suggestion that it is more complex than I have suggested.

Here's a hint or two:


Cheers

Greg Locock

Please see FAQ731-376 for tips on how to make the best use of Eng-Tips.
 
Thanks for the post but I should add that I'm really new to Matlab and don't really know what I'm doing. Here's my note that I've synthesised:

close all; clear all;
rate=20000;
duration=4;
length=duration*rate;
t=linspace(0,duration,length);
A1=exp(-0.1*t)*1; A2=exp(-0.7*t)*0.907; A3=exp(-0.5*t)*0.366; A4=exp(-0.9*t)*0.159; A5=exp(-1.2*t)*0.194; A6=exp(-1.4*t)*0.169; A7=exp(-1.7*t)*0.176; A8=exp(-2*t)*0.101;
f1=261.2; f2=523.4; f3=785.0; f4=1047.7; f5=1312.45; f6=1577.1; f7=1843.85; f8=2111;
y=A1.*sin(2*pi*f1*t)+A2.*sin(2*pi*f2*t)+A3.*sin(2*pi*f3*t)+A4.*sin(2*pi*f4*t)+A5.*sin(2*pi*f5*t)+A6.*sin(2*pi*f6*t)+A7.*sin(2*pi*f7*t)+A8.*sin(2*pi*f8*t);
y=y/8;
sound(y,rate);


Now, I've found this ADSR envelope, but I don't understand how to apply it to my note.

function y = adsr(a, d, s, r, fs)
% ADSR An ADSR envelope generator.
% Y = ADSR(A,D,S,R,FS) generates a vector filled with envelope
% values as computed for the specified attack (A), decay (D),
% sustain (S), and release (R) times in seconds at the sample rate
% FS. The envelope attack reaches a value of 1 and then decays to a
% sustained value of 0.5, before decaying to 0.0

% Do attack portion
N = a * fs;
y = [0:N-1] / (N-1);

% Do decay portion
N = d * fs;
y = [y ([N-1:-1:0] / (2*N)) + 0.5];

% Do sustain portion
N = s * fs;
y = [y 0.5 * ones(1, N)];

% Do release portion
N = r * fs;
y = [y, 0.5 * [N-1:-1:0] / N];

Can anyone help me out?

 
Firstly, do you get a note out of your code?

if so then you need to create an m file called adsr with that function in it.

Then tack on to your code
a=whatever;
d=whatever;
s=whatever;
r=whatever;

x = adsr(a, d, s, r, rate); 'creates a vector x with the envelope

'Then do an element by element multiplication of x and y

'I can't remember the posh way

for i:0;length-1
z(i)=x(i)*y(i);
end;

sound(z,rate);


Cheers

Greg Locock

Please see FAQ731-376 for tips on how to make the best use of Eng-Tips.
 
The "posh way" is to forget the for loop and just do

z=x.*y

assuming x and y are the same length

M

--
Dr Michael F Platten
 
Superb guys!

Everything's working as intended. Thanks for the help, it's actually quite easy when someone explains it to me...

Cheers!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor