Hello everyone, I've been trying to code an audio player for wav files throught the Matlab GUI (App Designer).
Currently stuck on the volume slider function.
When I choose another volume level, the sound file restarts instead of continuing from the point I used the slider to adjust the volume level.
Any ideas?
My code so far:
Sample code I found online:
Thanks in advance!

1 commentaire

Nikos Korobos
Nikos Korobos le 9 Avr 2022
I have also created an audio file player on μatlab, with Upload file and Play/Stop buttons.
Interested to code a display timer to show how long is the audio file while being played.
Can I use disp.AudioFileReader to count its length?
Need to count seconds in every loop.
Any suggestions appreciated.

Connectez-vous pour commenter.

 Réponse acceptée

jibrahim
jibrahim le 4 Avr 2022
Hi Nikos,
Consider using audioDeviceWriter to play the audio, and dsp.AudioFileReader to read from the audio file. You do not need to restart these objects when you tune the volume gain.
% EXAMPLE: Read in an audio file and play it back using the standard
% audio device writer.
AFR = dsp.AudioFileReader('speech_dft.mp3');
ADW = audioDeviceWriter(AFR.SampleRate);
gain = 1;
while ~isDone(AFR)
audio = AFR();
ADW(gain*audio);
end
release(AFR); % close the input file
release(ADW); % close the audio output device
At a high level, you create AFR and ADW once in your app (at setup time). You feed the audio signal to ADW. Use the latest and greatest gain to scale the audio.

8 commentaires

Nikos Korobos
Nikos Korobos le 5 Avr 2022
Hello jibrahim, appreciate taking the time to respond.
Tried some stuff out, Play seems to work but Volume and Stop wont function.
Any ideas?
Volume slider code:
Hi Nikos,
For stop button to work, its callback should set a flag on the app object, so that the while loop which is running in PlayPauseButtonValueChanged can see it and react to it.
For volume button or any other button's callbacks to execute when the while loop is actively running, add a "drawnow limitrate" inside the while loop.
E.g.
In StopButtonPushed
app.KeepPlaying = false;
In PlayPauseButtonValueChanged
while ~isDone(app.AFR) && app.KeepPlaying
...
...
drawnow limitrate
end
release(app.AFR);
release(app.ADW)
Nikos Korobos
Nikos Korobos le 6 Avr 2022
Modifié(e) : Nikos Korobos le 6 Avr 2022
Hello Raja, thanks for your input on this.
Could you explain the drawnow limitrate line a bit?
Will we need to use drawnow as a flag?
Play/Pause is also a state button.
Increasing the volume slider just increases the noise instead of the volume levels.
Also audioplayer worked just fine before.
Raja Palanimurugan
Raja Palanimurugan le 7 Avr 2022
drawnow is to make sure graphics callbacks are processed. This is typically needed when you are running a long loop and want to interrupt the loop and process a UI button click callback (or any other graphics callback). See this doc page for more details: https://www.mathworks.com/help/matlab/ref/drawnow.html
Make sure you have a reasonable limit for your volume slider and that you are not clipping the audio signal by multiplying it with a large gain.
Nikos Korobos
Nikos Korobos le 8 Avr 2022
Hello again,
Volume Sliders values are set between 0 and 100 in general
Nikos Korobos
Nikos Korobos le 9 Avr 2022
@Raja Palanimurugan We used gain as a value of the volume slider on app.VolumeSlider.Value
Is that correct?
Raja Palanimurugan
Raja Palanimurugan le 12 Avr 2022
Yes, that is correct.
Mohamed Turkmani
Mohamed Turkmani le 7 Sep 2022
Modifié(e) : Mohamed Turkmani le 7 Sep 2022
@Raja Palanimurugan the volume still doesnt work even when i add the drawnow limitrate. can you please explain how to use it more? when i change the value for the first time it works , for the second time it gives this error
Error using audioDeviceWriter/setupImpl
A given audio device may only be opened once.
drawnow limitrate

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by