This is an old revision of the document!
Matlab gives users the ability to create powerful audio algorithms. This tutorial gives an overview of how to create a simple volume control function and then add it to the baremetal framework.
All Matlab work was using version R2018b
From the Home tab choose New::Function and then overwrite all template contents with the following:
function audio_out = volume_control( audio_in, vol_ctrl_factor ) %increase/decrease the gain by using vol_ctrl_factor % takes input channel and multiplies by a factor vol_ctrl_factor and stores in % the output channel audio_out = audio_in * vol_ctrl_factor; end
This function takes an input audio channel, multiplies it by vol_ctrl_factor and stores the result in the output audio channel. It simply changes the volume. Notice that we don't use a for loop because MATLAB has built in implicit expansion for scalar * vector multiplication. The file name should match the function name so name the file volume_control.
In order for Matlab Coder to know what the input variable types are and to verify the function is working properly, a test script needs to be created. From the Home tab choose New::Script and copy the following contents into the window:
AUDIO_BLOCK_SIZE = int32(32); audio_in = ones(AUDIO_BLOCK_SIZE,1); vol_ctrl = single(0.5); volume_control(single(audio_in), vol_ctrl);
This page assumes that users have already gone through the Getting Started and Support and Bare Metal Framework content.
Now we have our C++ function ready to add to the framework. There are multiple ways to do this but the easiest in this case is to just add the function to callback_audio_processing.cpp. Using the SHARC Audio Module Bare Metal Project wizard, users should choose all default options with the addition of selecting the Audio Project Fin.
/* Function Definitions */ void volume_control(const float audio_in[32], float vol_ctrl_factor, float audio_out[32]) { int i; /* increase/decrease the gain by using vol_ctrl_factor */ /* takes input channel and multiplies by a factor vol_ctrl_factor and stores in */ /* the output channel */ for (i = 0; i < 32; i++) { audio_out[i] = audio_in[i] * vol_ctrl_factor; } }
float pot0_val = multicore_data->audioproj_fin_pot_hadc0; volume_control(audiochannel_0_left_in, pot0_val, audiochannel_0_left_out); volume_control(audiochannel_0_right_in, pot0_val, audiochannel_0_right_out);
The code will read the value of HADC0 and pass that into the volume_control() function created from MATLAB Coder to allow the volume to be adjusted.
At this point it is assumed that users are familiar with using the framework in CCES. Build and run the code in CCES and notice that turning HADC0 will increase or decrease the volume.