I am using code that uses the following subfunction:
function playTone (duration, toneFreq)
% Generate a tone
samplesPerSecond = 44100; % the bit rate of the tone
y = sin(linspace(0, duration * toneFreq * 2 * pi, round(duration * samplesPerSecond))); % the equation of the sound wave
player = audioplayer(y, samplesPerSecond); % create an audio object from the sound wave at the specified bit rate
playblocking(player) % play the audio, blocking control until the sound completes
This function is called, for example, with the following:
playTone(4, 400);
This causes a sound to play at 400Hz, lasting for 4 seconds.
The problem is, the function playblocking() restricts control until the sound has completed. The alternative is to use play(), which means that no sound plays at all (because sound stops as soon as the function completes).
I can’t use the sound() function due to a known bug in my version of MATLAB… How can I make the audioplayer() function play a sound without taking control of the system, if the sound is created within a subfunction?
You can define
playervariable as global. Just put this line in the beginning of the function:Although it’s considered not a good programming practice, it may work for you.