Matlab how do I appy a ASDR Curve to get smoother sounds (Problem with the implementation)

303 Views Asked by At

So I want to apply a ADSR Curve for my tone function that uses different Amplitude values

In the function envel I have the function for my ADSR Curve Array

First off

I have problems do apply the values to my E Array

I want it to take 1/8 to reach 1 for my Attack phase

Any suggestions on how to accomplish the implementation are welcome thanks.

But my implementations don't work I have tried create a vector that has 1/8 Values of my sin wave

Tried to use this array for 1/8 Values of my E but I only get 0 as entries for the region of E

I have to use the parameters of the envel

sound(note(23, 1 / 2, 1, 8000));

function [sinusoid] = createWaveform(frequency, fs, duration, A)
    Fs = fs;     % samples per second
    dt = 1 / Fs; % seconds per sample
    t = (0:dt:duration)'; % seconds
    % %Sine wave:
    Fc = frequency; % hertz
    sinusoid = A .* cos(2 * pi * Fc * t + (2 * pi) .* rand(1, 1));
    % Plot the signal versus time:
end

function [tone] = note(keynum, relDuration, fullDuration, fs)
    basetone = 440;
    frequency = basetone * nthroot(2, 12) ^ (keynum - 49);
    tone = createWaveform(frequency, fs, relDuration * fullDuration, 1) .* envel(relDuration, fullDuration, fs);
end

% [E] = envel(relDuration,fullDuration,fs)

function [E] = envel(relDuration, fullDuration, fs)

    dt = 1 / fs;
    dr = fullDuration * relDuration;
    t = (0:dt:dr)';
    E = (0:dt:dr)';
    S = 0;
    a = 1 / 8 * length(t);
    f = linspace(0, 1, a);
    E(1:round(1 / 8 * length(t))) = f(1, :);

    X = 1; % Delay
    for i = round(length(t) * 1 / 8) : round(length(t) * 3 / 8)
        E(i) = X - 1 / 10 * (relDuration * fullDuration * fs * 1 / 8);
        X = E(i);
    end
    F = X; % Sustain
    for i = round(length(t) * 3 / 8) : round(length(t) * 6 / 8)
        E(i) = F;
    end
    Y = F; % Restain
    for i = round(length(t) * 6 / 8) : round(length(t) * 8 / 8)
        E(i) = Y - F / (relDuration * fullDuration * 1 / 8);
    end
    disp(E);
end
0

There are 0 best solutions below