Many customers utilize MATLAB to create custom audio effects and algorithms. This example will help customers go from concept to an actual product using the SHARC Audio Module. The PitchShifter Audio Plugin from MATLAB will be used for this example.
First, start audioTestBench in the MATLAB command window
>> audioTestBench(audiopluginexample.PitchShifter)
Although PitchShifter is one of the MATLAB Audio Plugin Examples, a function needs to be created that exercises the audiopluginexample.PitchShifter.
function y = applyPitchShift(PitchShift,Overlap,x) persistent h if isempty(h) h = audiopluginexample.PitchShifter; end % Exercise parameter tuning h.PitchShift = PitchShift; h.Overlap = Overlap; % Exercise process function y = h(x); end
MATLAB will need a test for the function that was created to verify parameters.
AUDIO_BLOCK_SIZE = int32(32); audio_in = double(ones(AUDIO_BLOCK_SIZE,1)); pitchShift = double(0); overLap = double(0); applyPitchShift(pitchShift, overLap, audio_in); % PitchShift Audio
MATLAB Coder will create code that can then be used in the SHARC Audio Module Bare Metal Framework
Browse to where the Pitch Shift function is stored.
Manually defining the input values can be done but pointing to the test function that was created already has them defined properly.
This section will describe all steps to run the pitch shift algorithm in CCES.
Use the Bare Metal Project Wizard to create a project that the MATLAB generated code can be integrated into. Using all the default settings in the wizard will create a working 3 core project.
#include "matlab/applyPitchShift.h" #include "matlab/applyPitchShift_initialize.h"
/* Variable Definitions */ static applyPitchShiftStackData applyPitchShiftStackDataGlobal1; static applyPitchShiftPersistentData c_applyPitchShiftPersistentData1; static applyPitchShiftStackData applyPitchShiftStackDataGlobal2; static applyPitchShiftPersistentData c_applyPitchShiftPersistentData2;
void processaudio_setup(void) { // Initialize the audio effects in the audio_processing/ folder audio_effects_setup_core1(); // ****************************************************************** // Add any custom setup code here // ****************************************************************** /* Initialize reentrant memory structures */ applyPitchShiftStackDataGlobal1.pd = &c_applyPitchShiftPersistentData1; /* Initialize the application. You do not need to do this more than one time. */ applyPitchShift_initialize(&applyPitchShiftStackDataGlobal1); /* Initialize reentrant memory structures */ applyPitchShiftStackDataGlobal2.pd = &c_applyPitchShiftPersistentData2; /* Initialize the application. You do not need to do this more than one time. */ applyPitchShift_initialize(&applyPitchShiftStackDataGlobal2); }
Update processaudio_callback() for core 1 to apply the pitch shift
void processaudio_callback(void) { double pitch_shift = 3; double overlap = 0.2; double temp_right_in[AUDIO_BLOCK_SIZE]; double temp_left_in[AUDIO_BLOCK_SIZE]; double temp_right_out[AUDIO_BLOCK_SIZE]; double temp_left_out[AUDIO_BLOCK_SIZE]; int i = 0; // applyPitchShift expects a double[] so create temporary buffers to hold doubles for (i = 0; i < AUDIO_BLOCK_SIZE; i+=1) { temp_right_in[i] = (double)audiochannel_0_right_in[i]; temp_left_in[i] = (double)audiochannel_0_left_in[i]; } applyPitchShift(&applyPitchShiftStackDataGlobal1, pitch_shift, overlap, temp_right_in, temp_right_out); applyPitchShift(&applyPitchShiftStackDataGlobal2, pitch_shift, overlap, temp_left_in, temp_left_out); // applyPitchShift expects a double[] so convert back to float for (i = 0; i < AUDIO_BLOCK_SIZE; i+=1) { audiochannel_0_right_out[i] = (float)temp_right_out[i]; audiochannel_0_left_out[i] = (float)temp_left_out[i]; } }
Before executing the application in CCES, the hardware should be setup as shown below in order for correct execution of the application.
By default the optimizer in CCES is not enabled for the new matlab code.
Using the FTDI cable(Rev 1.4 boards) or a USB cable(Rev 1.5 boards), logging output can be seen when running the bare metal framework. Opening a console application, such as PuTTY, with the following serial settings should show the logging information.
Non-Optimized | Optimized |
---|---|
![]() | ![]() |