Create and dynamically change a continuous tone with user input in MATLAB

5 views (last 30 days)
Hi all,
Does anyone know a way that you would dynamically change a tone based on user input? That is, I want to generate a continuous tone in Matlab (for 5 seconds), of a particular frequency, and allow changing of that frequency while it plays (i.e. within that five seconds), by a particular percentage at a time, using a keyboard (e.g. up for increase pitch, down for decrease). Is this even possible with Matlab? At present, I currently just produce a several rapid tones, checking for user input after each one and adjust the next tone based on this, but a dynamically-changing continuous tone would be more ideal. Thank you!

Accepted Answer

Walter Roberson
Walter Roberson on 7 Dec 2021
See
In particular, you cannot change the tone instantly . There is always going to be some processing delay, and MATLAB itself is being interrupted by the operating system for random things, and processing at the MATLAB level is slower than you would need for real-time audio.
So what you need to do is have a buffer of samples that you write into, and the audio drivers take samples from the buffer and output them when the time comes. If you use a large buffer then you have plenty of time to do other things, but it could be a while before any new sounds you queue make it to the speakers.
There is always a trade-off: if your buffer is too small, your code has too much overhead and cannot keep up. If your buffer is too big, delays before changes become audible become too large.
In some cases it is not possible to resolve the problem to your satisfaction -- not without doing things like killing off nearly all the other programs on the host, or compiling the MATLAB code to reduce interpretation delays.
  1 Comment
neuroDuck
neuroDuck on 8 Dec 2021
Thanks for the very quick response and clear advice. So I guess in my instance, I could load all sounds into different buffers and then select sounds from other buffers depending on the user input in the while loop (i.e. for the duration of the total sound presentation), essentially looping the same duration sound each time for the total sounds presentation duration, but changing which buffer it selects from (wherein each sound is separated according to its frequency), thus minimizing the delay as much as possible?

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!