Output signal silence (underrun) occurs when the device buffer is empty and it is time for digital-to-analog conversion. This results when the processing loop in MATLAB does not supply samples at the rate the sound card demands. The number of samples underrun is returned when you call your audioPlayerRecorder or audioDeviceWriter object.
Input signal drops (overrun) occur when the processing stage does not keep pace with the acquisition of samples. The number of samples overrun is returned when you call your audioPlayerRecorder or audioDeviceReader object.
Generally, since you encounter overrun or underrun, I recommend you to try improving your I/O system:
1. Identify when the overrun or underrun occurs. If it occurs in the first few iterations, consider calling setup on your System objects before the loop where real-time processing is required. You can also run the I/O system with dummy data for a few frames before starting the real processing. For more information, see Measure Performance of Streaming Real-Time Audio Algorithms.
2. If you are using a DirectSound driver on a Windows platform, consider switching to a WASAPI or ASIO driver. ASIO drivers have the least overhead. If you are using an ASIO driver, make sure to match the frame size in MATLAB to the ASIO buffer size.
3. If you can afford to add more latency to your application, consider increasing the buffer size of your object. By default, the buffer size is the frame size of the data processed by the audio object.
4. If you can afford to decrease signal resolution, consider decreasing the sample rate.
5. Close all nonessential processes on your machine, such as mail checkers and file sync utilities. These processes can asynchronously ask for CPU time through interrupts and disturb the audio processing loop.
6. To maximize performance, remove all plotting and visualization from your real-time loop. If you require a visualization to update in your processing loop, use a DSP System ToolboxT scope such as dsp.TimeScope, , dsp.SpectrumAnalyzer, , or dsp.ArrayPlot, . Follow the recommendations listed in point 1 to setup and pre-run your scopes. If you require custom graphics or are processing callbacks in the loop, use the drawnow command and specify a limited update rate to optimize your event queue.
7. If the processing loop is algorithm-heavy, try profiling your loop to locate the bottlenecks, and then apply appropriate measures: o Replace handwritten code with MATLAB features that have been optimized for speed. o Follow best practices for performance: Techniques to Improve Performance. o Generating MATLAB executables (MEX files) using MATLAB CoderT may result in faster execution. See Desktop Real-Time Audio Acceleration with MATLAB Coder for an example. You can also generate standalone executables (EXE files). See Generate Standalone Executable for Parametric Audio Equalizer for an example. o If you are considering turning your algorithm into a VST plugin, then try running it as a VST plugin within MATLAB. VST plugin generation uses C code generation technology under the hood, and running the generated VST plugin within MATLAB may result in faster execution than with your original MATLAB code. See Design an Audio Plugin and Host External Audio Plugins to learn how to design, generate, and then host a VST plugin.
About Latency, there are output latency and input latency. If properties and frame size remain consistent, the ratio of input latency (between when audio enters the sound card to when the frame is output by the processing stage) to output latency (between the generation of an audio frame in MATLAB to the time that audio is heard through the speaker) is consistent between calls to an audioPlayerRecorder object. To minimize latency, you can:
1. Optimize the processing stage. If your processing stage has reached a peak algorithmically, compiling your MATLAB code into C code using MATLAB Coder may result in faster execution.
2. Decrease the sample rate.
3. Decrease the frame size.