Matlab real-time audio plotting in a GUI
Show older comments
Hi Community,
I encountered the following two problems using the audioPlayerRecorder object. I hope to display some processed variable based on input audio and provide audio alarming if such variable above a certain threshold. I followed instructions here: https://www.mathworks.com/help/audio/gs/real-time-audio-in-matlab.html Right now the audio processing is conducted in an infinite while loop. The code is roughly working however still have some issues
Problem 1): since the current procesing time is long. When running the code, the audio player queue always has some underruns. I do not know how to solve this. I tried Malab multi-threading while the performance is even worse
Problem 2) I tried to design a GUI for the existing code, when pressing the 'start' button, the code should run and real-time display the variable in the area speicifed by the GUI settings. However, when pressing the start button, the loop not conduct properly, should I use timer for such process?
Thank you in advance for any help.
Best,
Larry
3 Comments
Mario Malic
on 23 Feb 2024
Hi, I haven't worked much with such object, but maybe I can give some hints.
I will try to explain your problem 1: if you want to capture a signal for 20 seconds with sample rate of 100, meaning, in total you will get 2000 samples. There is a thing called buffer size which says, what number of samples MATLAB is going to take before processing it (whatever you want to do to signal). For the sake of the example, let's say, buffer size is 500 samples.
You start acquiring signal with sound device, MATLAB waits until 500 samples are in the buffer (~5 seconds, with sample rate from above) and then it gives you signal (data) to process with your algorithm. As you are processing it, MATLAB is still acquiring signal in the background. Once analysis/processing is finished, your algorithm wants to process more signals. If there isn't a signal to process, then you have an underrun which means that MATLAB has to wait for another 500 samples to feed them into your analysis. This is not really a bad thing, but it's not as close to the real-time as it can(should) get which depends on your problem.
I wouldn't go into details, how would you organise your acquired data as I am not super familiar with the audiodevice object.
I don't think you should use timer as it may slow down the acquisition process.
If I manage to, I could create an example app soon.
Liwei Wang
on 8 Mar 2024
Matthew
on 2 Apr 2024
Hi,
I am facing a similar issue. I am creating an application that plays real time audio and plots the spectrum with some other functions. Also using the AudioPlayerRecorder.
Here is a quick list of things I have tried that have not seemed to work:
- Using the background thread or parallel processing toolbox. Unfortunately neither the AudioPlayerRecorder or the GUI can be pushed to a background thread, meaning that an update to the GUI results in an underflow in the audio.
- Timer. I have a successful demo of using a timer to periodically play/record samples. However I've run into the same issue regarding underflows with GUI underflows. This solution works to a point, but does not scale as the GUI demands increase.
The big issue that I am facing is the fact that the AudioPlayerRecorder and anything to do with the GUI seems to fight over the same resource without an easy way to multithread the two operations.
I am open to all ideas to get this to work.
Thanks,
Matt
Answers (0)
Categories
Find more on Audio I/O and Waveform Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!