How do I decrease sound exponentially when a user press button stop on my player app ?
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Bastien Lechat
le 4 Oct 2017
Commenté : Walter Roberson
le 4 Oct 2017
% code
afr = dsp.AudioFileReader('track1.wav');
adw = audioDeviceWriter('SampleRate', afr.SampleRate)
app.dec =1;
while ~isDone(afr)
audioIn = afr();
audioout = audioIn * app.dec;
adw(audioout);
end
with for the stopbutton
% code
t = 0:1/441000:4;
alpha = 0.1;
app.dec = exp(- alpha *t);
Basically I just want to avoid the brutal stop (which give me bad harmonics) by decreasing the sound smoothly with an exponential when we press the button stop.
Thank you !
1 commentaire
Jan
le 4 Oct 2017
Do you want to load a WAV file, fade out the sound in the last 4 seconds and save the file? Did you draw the curve?
t = 0:1/441000:4;
alpha = 0.1;
dec = exp(- alpha *t);
plot(t, dec)
Are you sure that this is the wanted amplification?
Réponse acceptée
Walter Roberson
le 4 Oct 2017
Make counter and alpha shared variables. Initialize counter to 0 and alpha to 0. Enter your loop. In it,
t = counter + 1 : counter + length(audioin)
audioout = audioin .* exp(- alpha * t/44100);
counter = t(end);
And in the stop button callback, set the shared variables
counter = 0;
alpha = 0.1;
That is, the exp is done every time, but with alpha zero it has the effect of multiplying by 1. When the stop button is pressed then a nonzero alpha is put into effect leading to exponential drop off in volume.
Just make sure that you add something to tell it to stop looping when counter reaches or exceeds 4*44100 after the stop is pushed
2 commentaires
Walter Roberson
le 4 Oct 2017
function go_button_Callback(hObject, event, handles)
afr = dsp.AudioFileReader('track1.wav');
adw = audioDeviceWriter('SampleRate', afr.SampleRate)
app.dec =1;
stopping = false; %shared variable
counter = 0; %shared variable
alpha = 0.; %shared variable
set(handles.stop_button, 'Callback', @(hObject, event) stop_play_nested(hObject, event, handles), 'enable', 'on' );
while ~isDone(afr)
drawnow(); %give a chance for graphics interrupt
audioIn = afr();
t = (counter + 1 : counter + size(audioIn, 1)).';
audioout = audioin .* repmat( exp(- alpha * t/44100), 1, size(audioIn,2) );
adw(audioout);
counter = t(end);
if stopping && counter >= 44100 * 4; break; end
end
set(handles.stop_button, 'enable', 'disable');
delete(afr);
delete(adw);
function stop_play_nested(hObject, event, handles)
stopping = true; %shared variable
counter = 0; %shared variable
alpha = 0.1; %shared variable
end %end of stop_play_nested
end %end of go_button_Callback
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Audio Processing Algorithm Design dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!