How do you keep the sound going? help me please
2 views (last 30 days)
Show older comments
EUNHEI KIM
on 8 Feb 2020
Commented: Walter Roberson
on 21 Feb 2020
I saved this code in a script
f = 770;
fs = 24000;
t = 0:1/fs:3;
sori = sin(2*pi*f*t);
sound(sori,fs);
i = input('Please enter a number from 1 to 5.');
com = randi(5)
while i <= com
sound(sori,fs);
i = input('Please enter a number from 1 to 5.');
com = randi(5)
end
if i > com % If you look at the code above, the sound stops when the 'i' number is greater than the 'com' number.
break
end
I hope the sound will continue if the 'i' number is less than or equal to 'com' number.
However, after the duration of the sound is 3 seconds, it will no longer be heard (until you enter a new 'i' number).
So I set the duration of the sound to 10 seconds.
But I set it to 10 seconds,
when the 'i' number entered is greater than 'com' number, the sound does not stop immediately.
Help me. I'm self-taught. Thank you.
0 Comments
Accepted Answer
Walter Roberson
on 8 Feb 2020
You need Audio System Toolbox and audioDeviceWriter(), and callback code to keep filling up the queue with sounds when the queue runs short. (The size of the queue will depend how quickly you can stop the sound when you need to.)
A lot of people think you can use audioplayer() instead, but audioplayer() and sound() always have a pause between repetitions of the sound, even if you use the Stop callback to start playing again. For continuous sound of indefinite duration you need Audio System Toolbox.
2 Comments
Temu
on 20 Feb 2020
Hi Walter,
To my knowledge, audioDeviceWriter() does not have a callback. I know you can easily just make a loop, but if there really is a callback, I would be very interested. Is there one?
Thanks,
Temu
Walter Roberson
on 21 Feb 2020
Looks like possibly I was getting some functionality confused.
https://www.mathworks.com/help/daq/examples/getting-started-acquiring-data-using-audio-in-session.html shows how to use callbacks with the Data Acquisition Toolbox to be notified when data is available, using a listener.
But I was also thinking of the Silicon Graphics audio implementation, which had callbacks for "low-water marks" -- that is, you could request to be notified when a certain number of samples were remaining in the output queue, to give you time to queue more output before the end of the queue was reached.
More Answers (0)
See Also
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!