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

조회 수: 10 (최근 30일)
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!

채택된 답변

Walter Roberson
Walter Roberson 2021년 12월 7일
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
neuroDuck
neuroDuck 2021년 12월 8일
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?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Audio I/O and Waveform Generation에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by