Deploy Audio Applications with MATLAB Compiler
This example shows how to use MATLAB Compiler™ to create a standalone application from a MATLAB function. The function implements an audio processing algorithm and plays the result through your audio output device.
Introduction
In this example, you generate and run an executable application that applies artificial reverberation to an audio signal and plays it through your selected audio device. The benefit of such applications is that they can be run on a machine that need not have MATLAB installed. You would only need an installation of MATLAB Runtime to deploy the application created in this example.
Reverberation Algorithm
The reverberation algorithm is implemented using the System object reverberator
. It allows you to add a reverberation effect to mono or stereo channel audio input. The object provides six properties that control the nature of reverberation. Each of them can be tuned while the simulation is running.
MATLAB Simulation
The function audioReverberationCompilerExampleApp
is a wrapper around reverberator
. To verify the behavior of audioReverberationCompilerExampleApp
, run the function in MATLAB. It takes an optional input which is time, in seconds, for which you want to play the audio. The default value is 60.
audioReverberationCompilerExampleApp
The function audioReverberationCompilerExampleApp
uses the getAudioDevices
method of audioDeviceWriter
to list the audio output devices available on the current machine so that you can play reverberated audio through the sound card of your choice. This is particularly helpful in deployed applications because function authors rarely know what device will be connected on the target machine.
audioReverberationCompilerExampleApp
also maps the tunable properties of reverberator
to a UI so that you can easily tune them while the simulation is running and observe its effect instantly. For example, move the slider 'Diffusion' to the right while the simulation is running. You will hear an effect of increase in the density of reflections. You can use the buttons on the UI to pause or stop the simulation.
Create a Temporary Directory for Compilation
Once you have verified the MATLAB simulation, you can compile the function. Before compiling, create a temporary directory in which you have write permissions. Copy the main MATLAB function and the associated helper files into this temporary directory.
compilerDir = fullfile(tempdir,'compilerDir'); %Name of temporary directory if ~exist(compilerDir,'dir') mkdir(compilerDir); % Create temporary directory end curDir = cd(compilerDir); copyfile(which('audioReverberationCompilerExampleApp')); copyfile(which('HelperAudioReverberation')); copyfile(which('FunkyDrums-44p1-stereo-25secs.mp3')) copyfile(which('HelperCreateParamTuningUI')); copyfile(which('HelperUnpackUIData'));
Compile the MATLAB Function into a Standalone Application
Use the mcc
(MATLAB Compiler) function from MATLAB Compiler to compile audioReverberationCompilerExampleApp
into a standalone application. This will be saved in the current directory. Specify the '-m' option to generate a standalone application, '-N' option to include only the directories in the path specified using the '-p' option.
mcc('-mN','audioReverberationCompilerExampleApp', ... '-p',fullfile(matlabroot,'toolbox','dsp'), ... '-p',fullfile(matlabroot,'toolbox','audio'));
This step takes a few minutes to complete.
Run the Generated Application
Use the system
command to run the generated standalone application. Note that running the standalone application using the system command uses the current MATLAB environment and any library files needed from this installation of MATLAB. To deploy this application on a machine which does not have MATLAB installed, refer to About MATLAB Runtime (MATLAB Compiler).
if ismac status = system(fullfile('audioReverberationCompilerExampleApp.app', ... 'Contents','MacOS','audioReverberationCompilerExampleApp')); else status = system(fullfile(pwd,'audioReverberationCompilerExampleApp')); end
Similar to the MATLAB simulation, running this deployed application will first ask you to choose the audio device that you want to use to play audio. Then, it launches the user interface (UI) to interact with the reverberation algorithm while the simulation is running.
Clean up Generated Files
After generating and deploying the executable, you can clean up the temporary directory by running the following in the MATLAB command prompt:
cd(curDir);
rmdir(compilerDir,'s');