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
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
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 个评论
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 Center 和 File Exchange 中查找有关 Audio Processing Algorithm Design 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!