How do I decrease sound exponentially when a user press button stop on my player app ?

6 visualizzazioni (ultimi 30 giorni)
% 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 Commento
Jan
Jan il 4 Ott 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?

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 4 Ott 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 Commenti
Bastien Lechat
Bastien Lechat il 4 Ott 2017
Modificato: Bastien Lechat il 4 Ott 2017
When I'm doing an error occur telling me :
Error using audioDeviceWriter/setup
"The number of input channels must be less than or equal to 255."
Do you know where it comes from ? Edit: I just had problem with matrix dimension
But I still have a problem where it's telling me:
Error using audioDeviceWriter/setup: A given audio device may only be opened once.
Walter Roberson
Walter Roberson il 4 Ott 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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Audio Processing Algorithm Design in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by