×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

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

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

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

(OP)
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!

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

(OP)
Opps, forgot to add that it's in Matlab!!

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

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:

http://kevindumpscore.com/docs/csound-manual/adsr.html

Cheers

Greg Locock

Please see FAQ731-376 for tips on how to make the best use of Eng-Tips.

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

(OP)
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?

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

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.

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

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

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

(OP)
Superb guys!

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

Cheers!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources