MATLAB Answers

0

How do I use ASIO, ALSA, and other APIs to communicate with PC audio hardware from Simulink?

Asked by MathWorks Support Team on 26 Oct 2010
Latest activity Answered by Darel
on 15 Nov 2013

I want to build my own version of the PortAudio library and use it in place of the PortAudio library that ships with the Signal Processing Blockset, so I can use the API of my choice (ASIO, ALSA, OSS, DirectSound, CoreAudio) to communicate with the audio hardware on my PC from Simulink.

Tags

No tags entered yet.

2 Answers

Answer by MathWorks Support Team on 26 Oct 2010
 Accepted Answer

In Release 2007b of MATLAB, the Signal Processing Blockset released two blocks, the To Audio Device and From Audio Device blocks, which replaced the To/From Wave Device blocks. These blocks operate on multiple platforms, and use the PortAudio Application Programming Interface to interact with to the audio hardware on a given computer.

The PortAudio library is an open source library, which is capable of interacting with the audio hardware on a given operating system in a variety of methods. The method by which PortAudio communicates with the audio hardware on a given computer is known as the Host API in PortAudio terminology. The following choices of default host API were made for the version of PortAudio that ships with the Signal Processing Blockset:

Windows: DirectSound

Linux: OSS

Mac: CoreAudio

In certain circumstances, a user may wish to choose a different host API. For

example, the user may have hardware that operates better with JACK (the JACK Audio

Connection Kit), the ASIO (Audio Stream Input Output) API, or some other host API.

If you wish to use these APIs instead of (or in addition to) the host API that ships

with the Signal Processing Blockset, it is necessary to compile and use a new version

of the PortAudio library. To do so, please follow the following instructions on

compiling and replacing the PortAudio library.

  • Compiling the PortAudio Library

----------------------------------------

1) In order to compile the PortAudio library, you should be familiar with compiling software for your desired target platform, and have a working compiler. You will need to build a shared (or dynamic) library, which is called libportaudio.EXT, where EXT is determined by the extension of shared libraries on the target platform (i.e., ?.so? for Unix/Linux, ?.dylib? for Apple, and ?.dll? for Windows).

2) The version of the PortAudio library that ships with MATLAB R2008a is a slightly modified version of pa_stable_v19_20071207, which is available on the PortAudio web site. You are encouraged to use the latest stable version of PortAudio in your applications. If you would like to patch these sources so that they are identical to the sources as used by The MathWorks, please make the changes to the source code before building the library. The patches can be downloaded by searching for "How can I customize the version of PortAudio that MATLAB uses?" at the MathWorks support site (<http://www.mathworks.com/support>).

3) The manner in which PortAudio was compiled at the MathWorks is briefly described below. For more details about the compilation process, please visit the PortAudio web site (<http://www.portaudio.com>).

4) On Linux and Macintosh platforms, the configure program was used. On Windows, a hand-grown (gmake) makefile was used, although this is not recommended for most users. The following list displays the compilation options used on each platform:

    a. Linux: --with-alsa=no --with-oss=yes
    b. Mac: # The default options were used
    c. Windows: -DPA_NO_ASIO -DPA_NO_WMME -DPA_NO_WASAPI -DPA_NO_WDMKS
  • Configuring the To/From Audio Device for Minimal Latency (R2009a and later)

----------------------------------------

In order to achieve minimal latency from the To/From Audio Device blocks of the Signal Processing Blockset, it is necessary to eliminate the use of the audio queues. In general, these queue are beneficial, since they can eliminate glitches in the audio stream. They must be eliminated, however, if you wish to drive the latency to its lowest possible value.

In order to remove the audio queues (and thereby reduce the buffering to a single (ping-pong) buffer):

1) Set the "Queue Duration" parameter to 0.

2) Ensure that the device "Buffer size" parameter is equal to the Simulink frame size.

Note that you may have to increase the "Buffer size" parameter if you cannot hear audio or the audio stream becomes choppy. Please note that this optimization only works with R2009a or later versions.

  • Replacing the PortAudio Library

----------------------------------------

The PortAudio shared library exists in the following (platform-dependent) location:

    32-bit Linux: matlab/bin/glnx86/libportaudio.so.2.0.0
    64-bit Linux: matlab/bin/glnxa64/libportaudio.so.2.0.0
    32-bit Mac: matlab/bin/maci/libportaudio.2.0.0.dylib
    32-bit Windows: matlab/bin/win32/libportaudio.dll
    64-bit Windows: matlab/bin/win64/libportaudio.dll

The replacement process is simple: rename the existing library to something like libportaudio.orig, and place the new version of the PortAudio library (with the same name as the library that you moved out of the way) in that same directory. The next time that you use the To/From Audio Device Blocks, they should use the new library in order to interact with the sound hardware.

  • Other Useful Information -----------------------------------

The PortAudio library webpage: http://www.portaudio.com

There is an unsupported MEX file that ships with the Signal Processing Blockset which lists the devices found by the PortAudio library from within MATLAB called dspAudioDeviceInfo. If you type dspAudioDeviceInfo at the MATLAB command prompt, you will receive a structure array with the names of the devices, and the number of supported inputs and outputs for each device.

  0 Comments

Sign in to comment.


Answer by Darel
on 15 Nov 2013

There is no longer any need to rebuild PortAudio for MATLAB to get ALSA or ASIO support. ALSA support was added to Signal Processing Blockset (now DSP System Toolbox) in R2009b and was made the default API on Linux in R2013a. ASIO was added in R2012a and can be selected as the Audio API on Windows by going to the DSP System Toolbox tab of the MATLAB preferences. These Audio API choices are relevant to To Audio Device and From Audio Device blocks, and to dsp.AudioRecorder and dsp.AudioPlayer system objects, all in DSP System Toolbox.

  0 Comments

Sign in to comment.