MATLAB Answers

How to read audio on app designer from simulink

18 views (last 30 days)
Roberto Lemaitre
Roberto Lemaitre on 24 May 2021
Answered: Pavan Guntha on 1 Sep 2021
Hello!
I want to be able to read the audio output from my simulink simulation on app designer, here's my simulation:
The output is the result of the filtering of an audio file and then everything is summed at the end. I've tried different things but can't manage to get the audio output on my app designer app to later play it, pause it, etc...
If possible, i would like to read the audio with audioplayer to be able to more easily pause and play it. This is the code i have at the moment for writing and reading for example an audio file from my pc.
Any tip or recommendation will be greatly appreciated!

Answers (1)

Pavan Guntha
Pavan Guntha on 1 Sep 2021
Hello,
One of the possible solutions is to use 'sim' command to simulate the Simulink model from the MATLAB App (as you have used in 'subirButtonPushed' callback function). In order to play/ pause/ stop the audio from the MATLAB App, you could use respective playback functions on the 'audioplayer' object as illustrated in the example below.
1) The Simulink model stores the output to a '.wav' file as shown in the figure.
2) The 'startupFcn' function of the MATLAB App simulates the Simulink model and the creates an audioplayer object.
3) The four buttons in the MATLAB App (Play, Pause, Resume & Stop) are attached to their respective callback functions to perform appropriate playback function.
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
sim("musicSimulink.slx",'StopTime','20');
[app.audio, app.Fs] = audioread('simuLinkMusic.wav');
app.player = audioplayer(app.audio, app.Fs);
end
% Button pushed function: PlayButton
function PlayButtonPushed(app, event)
play(app.player);
end
% Button pushed function: PauseButton
function PauseButtonPushed(app, event)
pause(app.player)
end
% Button pushed function: StopButton
function StopButtonPushed(app, event)
stop(app.player)
end
% Button pushed function: ResumeButton
function ResumeButtonPushed(app, event)
resume(app.player)
end
end
For more information on playback functions, you could have a look at the documentation page: Play or Record Audio
Hope this helps!

Community Treasure Hunt

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

Start Hunting!