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

조회 수: 6 (최근 30일)
% 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
Jan
Jan 2017년 10월 4일
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?

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2017년 10월 4일
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
Bastien Lechat
Bastien Lechat 2017년 10월 4일
편집: Bastien Lechat 2017년 10월 4일
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 2017년 10월 4일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Audio Processing Algorithm Design에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by