File Exchange

image thumbnail

Real-Time Audio Processor

version (48.5 KB) by Tucker McClure
Simplifies streaming or generating audio in real time.


Updated 04 Sep 2012

View License

The RealTimeAudioProcessor makes it easy to stream or generate audio in real time in MATLAB.

It includes examples for generating sound from a qwerty keyboard, generating sound from a MIDI keyboard (with included MIDI interface for acquiring MIDI inputs), and using any old algorithm you have lying around as real-time processor.

For a smooth experience, please start with the included documentation. There are a few things to do for the best performance.

* Windows
* MATLAB r2012a or greater
* Signal Processing Toolbox
* DSP System Toolbox
* Soundcard with ASIO driver (recommended)
* MIDI input device (recommended only for MidiDemo)

Early beta.

Cite As

Tucker McClure (2021). Real-Time Audio Processor (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (17)

Tucker McClure

Hi Albert. Other than changing matlab.system.System to matlab.System in these files, I'm not sure what else is necessary with modern versions, since I no longer have access to the toolboxes. Good luck, and I hope you can get something good working!

Albert Doolittle

I'm getting the error "Error using QwertyDemo The specified superclass "matlab.system.System' contains a parse error, cannot be found on MATLAB's search path or is shadowed by another file with the same name." A search of my hard drive, the r2019b package, and the 2020a package turned up nothing. I do have the DSP and Signal processing. The code alone has been a fantastic education but it would be nice to run it.

Albert Doolittle

I do have the DSP and Signal Processing Toolboxes.

jing zhang



For my function handle, I simply copied your sine_of_the_times.m function, but fed in my own serial values to augment the value of frequency. I'm no signals expert, so I'm a little fuzzy on what happens in that function (state transition matrix?). To get rid of the clicking, I just changed the output line to be: < out = 0.5*[outR outR] + 0.1 * in; >, since I noticed the clicking was only happening in the Left earphone.


Hi Tucker,

Great code, thanks for posting. I was able to use the HandlePlayer class to make a real-time tone whose frequency is proportional to a signal in an instrument's serial feed. However, I'm finding that there is quite a bit of "clicking" going on. Do you have any suggestions for how I can make the sound more smooth?

Thanks again,


Ok thank you Tucker, that works !

So do you think I could use your tool for note detection ? I am not familiar with the functions you use , basically I would like to set a threshold and indicate the detection of an audio event when its amplitude is higher than this threshold value , would it be possible to do this as an Handle function ? but I do not really see which input parameters to put in ...

Thank you very much for your work and help !


Tucker McClure

Hi louise and Dorian,

Dorian, you must be using a newer version of MATLAB? I believe you need to open RealTimeAudioProcessor.m and change 'matlab.system.System' to 'matlab.System'. I'm not in a place where I can test this for you or I would. Please email if this doesn't work.

louise, the RTAP runs in a loop that tries to maintain real-time performance. As such, you can't run anything directly. However, there is a provision to allow other things to happen. It requires a bit of object-oriented programming. You'll create a new class that derives from RealTimeAudioProcessor (or one of its child classes) and overload the UpdateGraphics method. This is updated at 50Hz and so is suitable for visualization, such as showing a series of images. Please email if this is something you'd like to talk about more.


Dear Tucker,

I am interested in developing a real-time sound event detector , and I was wondering whether your tool was appropriate for this application . And if it is, could you give me some preliminary steps so I can get started with it?

Also, I am getting this error

Error using HandlePlayer
The specified superclass 'matlab.system.System' contains a parse error, cannot be found on MATLAB's search path, or is shadowed
by another file with the same name.

Error in handle_driver (line 22)
rtap = HandlePlayer(@sine_of_the_times, ...

that I do not understand ..

Thank you for any help

Dorian CAZAU


Hi Tucker,
Thanks for this awesome tool.
With this I successfully played real-time music with my midi keyboard, but I realized that I can't do something else(ex.opening a screen using Screen function from Psychtoolbox3) with the rtap open.
What I want to do is playing real-time audio while I'm executing other things. Do you think it's possible to show moving images on the screen at the same time playing RealtimeAudioProcessor?



Tucker McClure

Hi Fer,

This should be pretty easy. Here's where to get started.

1. Usually, one calls MATLAB's 'filter' function when all of the data is present, but if we want to use it on a real-time stream, then we'll need to call it many times on little chunks of audio as they come in. Of course, filters have state, so we can't just call the filter "fresh" on each little chunk; we'll have pass in the last filter state to get it started where it left off last time. MATLAB has this built in:

[out, state_out] = filter(b, a, in, state_in);

2. Now we need to wrap this up in a usable function with our coefficients. Something like this:

[out, state] = my_filter(t, in, state)

b = [...];
a = [...];
[out, state] = filter(a, b, in, state);


(See sine_of_the_times.m for an example.)

3. Now we tell the Real-Time Audio Process to use this function on all incoming audio. We can easily do this with a "HandlePlayer", something like this:

rtap = HandlePlayer(@my_filter, ...);

(See handle_driver.m for an example; note that the example is stereo in/out.)

Please also see the "Real Time Audio Processor.docx" file for much more discussion.


Hi Tucker,
i want to process a signal in real time. But i dont understand how its works...

Usually i use the function filter() in matlab with some coefficient thay i desing.

Which of these code i have to use for real time aplication?
Muchas Gracias!


Hi Tucker,
Thanks for uploading this! I've been looking for some real time audio processing power for matlab. I have a question. I've been trying to get my fm synthesis function to work within your code but I find my modifications aren't working. I have used the QwertyDemo.m file as a jumping point and was wondering: How can I modify it to create a variable (based on accelerometer data) drone. I have no problem getting accelerometer data in Matlab

Tucker McClure

Hi Bert,

Your question prompts a few things.

1. The MEX files for midi_interface.cpp are included, so you shouldn't need to build them. That script is only included in case someone *wants* to rebuild it for whatever reason. You should be able to just call midi_interface('open') from the command line to get going.

2. If for whatever reason you *do* want to rebuild it, it requires that you have the Windows SDK and edit midi_interface_builder.m to reflect the right path for its include and binary directories. This isn't documented because it's not intended for the user to do this.

3. The error is actually about a loss of precision in a cast. This is usually a warning instead of an error. Somewhere in your compiler, there's a setting for whether loss of precision should be considered an error or a warning. If you set it to warning, it will probably move on.

There's some included documentation about setting everything up. Have you read through this? Have you tried running any of the examples yet?

Hope that helps,

- Tucker


I tried to run the midi_interface_builder but got his error:

>> midi_interface_builder
Building MEX file... midi_interface.cpp: In function 'bool OpenMidiInputDevice(unsigned int)':
midi_interface.cpp:111:64: error: cast from 'void (*)(HMIDIIN, UINT, DWORD, DWORD, DWORD) {aka void (*)(HMIDIIN__*, unsigned int, long unsigned int, long unsigned int, long unsigned int)}' to 'DWORD {aka long unsigned int}' loses precision [-fpermissive]

C:\PROGRA~1\MATLAB\R2012B\BIN\MEX.PL: Error: Compile of 'midi_interface.cpp' failed.

Error using mex (line 206)
Unable to complete successfully.
Error in midi_interface_builder (line 18)
mex(['-I' sdk_path include_sub], ...
206 throw(exception);

MATLAB Release Compatibility
Created with R2012a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!