Skip to content

Instantly share code, notes, and snippets.

@jaekookang
Created December 5, 2019 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaekookang/c9e903a61092debbf87833e170bd7fb2 to your computer and use it in GitHub Desktop.
Save jaekookang/c9e903a61092debbf87833e170bd7fb2 to your computer and use it in GitHub Desktop.
Compile MEX function (Matlab) with RtAudio on macOS
/*
Compiling MEX function including RtAudio for audio mauipulation (Toy example)
Tested on:
- macOS Catalina 10.15
- Matlab R2019b (XCode clang++)
- RtAudio v5.1.0
To use this .cpp file to compile mex function, you need to download RtAudio first
(See https://www.music.mcgill.ca/~gary/rtaudio/).
`audioprobe_mex.cpp` is only for the demonstration. For other uses, you can further develop from here.
Compile command line on Matlab:
>> mex -D__MACOSX_CORE__ LDFLAGS='$LDFLAGS -framework CoreAudio -framework CoreFoundation' -lpthread -Irtaudio -output audioprobe_mex rtaudio/tests/audioprobe.cpp rtaudio/RtAudio.cpp
This command line will create `audioprobe_mex.mexmaci64`.
Test this on Matlab by simply typing `audioprobe_mex`.
See for more information:
- https://www.music.mcgill.ca/~gary/rtaudio/compiling.html
- https://www.mathworks.com/help/matlab/ref/mex.html
2019-12-05 Jaekoo Kang
*/
#include <mex.h>
#include "RtAudio.h"
#include <iostream>
#include <map>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
mexPrintf("Hello World\n");
/*
The code chunk below was taken from audioprobe.cpp from RtAudio module.
See: https://www.music.mcgill.ca/~gary/rtaudio/probe.html
*/
// Create an api map.
std::map<int, std::string> apiMap;
apiMap[RtAudio::MACOSX_CORE] = "OS-X Core Audio";
apiMap[RtAudio::WINDOWS_ASIO] = "Windows ASIO";
apiMap[RtAudio::WINDOWS_DS] = "Windows DirectSound";
apiMap[RtAudio::WINDOWS_WASAPI] = "Windows WASAPI";
apiMap[RtAudio::UNIX_JACK] = "Jack Client";
apiMap[RtAudio::LINUX_ALSA] = "Linux ALSA";
apiMap[RtAudio::LINUX_PULSE] = "Linux PulseAudio";
apiMap[RtAudio::LINUX_OSS] = "Linux OSS";
apiMap[RtAudio::RTAUDIO_DUMMY] = "RtAudio Dummy";
std::vector< RtAudio::Api > apis;
RtAudio :: getCompiledApi( apis );
std::cout << "\nRtAudio Version " << RtAudio::getVersion() << std::endl;
std::cout << "\nCompiled APIs:\n";
for ( unsigned int i=0; i<apis.size(); i++ )
std::cout << " " << apiMap[ apis[i] ] << std::endl;
RtAudio audio;
RtAudio::DeviceInfo info;
std::cout << "\nCurrent API: " << apiMap[ audio.getCurrentApi() ] << std::endl;
unsigned int devices = audio.getDeviceCount();
std::cout << "\nFound " << devices << " device(s) ...\n";
for (unsigned int i=0; i<devices; i++) {
info = audio.getDeviceInfo(i);
std::cout << "\nDevice Name = " << info.name << '\n';
std::cout << "Device ID = " << i << '\n';
if ( info.probed == false )
std::cout << "Probe Status = UNsuccessful\n";
else {
std::cout << "Probe Status = Successful\n";
std::cout << "Output Channels = " << info.outputChannels << '\n';
std::cout << "Input Channels = " << info.inputChannels << '\n';
std::cout << "Duplex Channels = " << info.duplexChannels << '\n';
if ( info.isDefaultOutput ) std::cout << "This is the default output device.\n";
else std::cout << "This is NOT the default output device.\n";
if ( info.isDefaultInput ) std::cout << "This is the default input device.\n";
else std::cout << "This is NOT the default input device.\n";
if ( info.nativeFormats == 0 )
std::cout << "No natively supported data formats(?)!";
else {
std::cout << "Natively supported data formats:\n";
if ( info.nativeFormats & RTAUDIO_SINT8 )
std::cout << " 8-bit int\n";
if ( info.nativeFormats & RTAUDIO_SINT16 )
std::cout << " 16-bit int\n";
if ( info.nativeFormats & RTAUDIO_SINT24 )
std::cout << " 24-bit int\n";
if ( info.nativeFormats & RTAUDIO_SINT32 )
std::cout << " 32-bit int\n";
if ( info.nativeFormats & RTAUDIO_FLOAT32 )
std::cout << " 32-bit float\n";
if ( info.nativeFormats & RTAUDIO_FLOAT64 )
std::cout << " 64-bit float\n";
}
if ( info.sampleRates.size() < 1 )
std::cout << "No supported sample rates found!";
else {
std::cout << "Supported sample rates = ";
for (unsigned int j=0; j<info.sampleRates.size(); j++)
std::cout << info.sampleRates[j] << " ";
}
std::cout << std::endl;
}
}
std::cout << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment