Change from wavplay to audioplayer

function play_Callback(hObject, eventdata, handles)
% hObject handle to play (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global stop file_name C;
stop=1;
equalizer_play();
function equalizer_play()
global stop file_name C;
[x,Fs]= audioread(file_name);
[a,b]=coef();
l_bucata=2*Fs;
Nb=round(length(x)/l_bucata);
y=0;
for i=1:floor(Nb)
bucata=x((i-1)*l_bucata+1:i*l_bucata);
for k=1:5
y=y+filter(10^(C(k)/20)*b{k},a{k},bucata);
if(stop==0)
break;
end
end
wavplay(y,Fs,'async');
y=0;
if(stop==0)
break;
end
end
i want to change this code to fit audioplayer() ?

 Accepted Answer

Walter Roberson
Walter Roberson on 24 Feb 2019
Edited: Walter Roberson on 25 Feb 2019
p = audioplayer(y, Fs);
playblocking(p);
Note that using playblocking() is not exactly the same as what you have now. Your current code loops back around and starts playing the new y for the next i while the first wavplay is still playing, and leaves up to floor(Nb) sounds playing simultaneously when the function returns. It is possible to emulate that behaviour, but is it really what you want?

7 Comments

how will that affect playing of the sound file ?
Walter Roberson
Walter Roberson on 25 Feb 2019
Edited: Walter Roberson on 30 Oct 2020
Ah, I did not read through the definition of 'async' for the old wavplay(): it did not permit two players to play at the same time.
The difference between what I wrote with playblocking() compared to wavplay() with the 'async' call, is that with the code you have now, if you ran it on a system old enough to have wavplay, then when the last iteration of for i was being executed, the old code would return out of equalizer_play(), and therefore out of the play_Callback function, giving the user back control of the user interface while that very last iteration was being played, but not before that last for i iteration. With playblocking() that I suggest, then instead the control being returned to the user while that last sound is playing, control would not return to the user until after that last sound was played. It looks to me as if that would be a difference of 2 seconds.
Hassan Bosha
Hassan Bosha on 25 Feb 2019
Edited: Hassan Bosha on 25 Feb 2019
actually it works , but the problem is that it has pauses of silence , the song is not streaming as normal instead it works for 3 seconds take a pause of silence for 1/2 seconds and then continue and so on
What is it ?
Would it improve anything if i put audioplayer() out of the loop ?
and where should i put it if so ?1234.png
The analyzer is not noticing that you are playing different sounds each time.
Any one audioplayer object is for playing one sound, possibly positioning to different parts of it (though that is not simple.) To play a different sound you need a different audioplayer object. Creation and deletion of those objects is a bit slow. The only way to get streaming would be to have the objects prepared in advance and have the stopfcn of one start the playing of another.
If you want streaming audio then you should give up compatibility with wavplay(), which never supported streaming audio. You should instead switch to the tools available through the Audio System Toolbox: those tools permit you to queue samples to be played while playing is going on.
will that possible to be done through my code ?
if it's can u show some code examples ?

Sign in to comment.

More Answers (1)

Jeje Ahmad
Jeje Ahmad on 29 Oct 2020
Hi i am trying a code but give me this error
Undefined function or variable 'audioplayr'.
Error in CELP_RUN (line 31)
wavplay(x,8000); and my virsion matlab is 2016a
can you help me?

4 Comments

You must have added your own wavplay() as wavplay() did not exist in R2016a.
When you created that function, you misspelt audioplayr which should be audioplayer
i add the function wavpaly() but still give me the error
i am test this code but give me this errot
Undefined function or variable 'audioplayr'.
Error in CELP_RUN (line 34)
audioplayr(xhat1,8000);
You need to create a file named audioplayr.m with content
function varargout = audioplayr(varargin)
[varargout{:}] = audioplayer(varargin{:});

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!