matlab sounds in game design

3 views (last 30 days)
a a
a a on 3 Dec 2018
Edited: Walter Roberson on 26 Jul 2019
Say I build a snooker game, where balls need to jitter around. I added a sound when balls collide, importing a sound file. But playing a sound turns out to slow down the program quite a lot, as I observe substantial pauses, which look bad. I use matlab. Is it just because matlab is slow?
  5 Comments
Adam Danz
Adam Danz on 3 Dec 2018
I replied in the answer section.
John D'Errico
John D'Errico on 3 Dec 2018
Edited: John D'Errico on 3 Dec 2018
Upvote Adam's answer. But yes, wavread does load the file in every time you call it. So you are waiting on disk access every time you make that sound.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 3 Dec 2018
Edited: Adam Danz on 3 Dec 2018
(continued here from comments above)
If you're using matlab verstion 2012b or later, you should be using audioread() (link) rather than wavread. In either case, those functions "read" (or load) the data from a file "blablabla.wav". If you're embedding that function within sound(), then you're loading it every time you're playing it.
Instead, load it once when you first open the game and make it a persistent variable.
persistent y Fs
if isempty(y)
[y,Fs] = audioread(blablabla.wav)
end
Then every time you want to play it, the data are already available.
sound(y,Fs)
  23 Comments
Walter Roberson
Walter Roberson on 10 Dec 2018
Edited: Walter Roberson on 26 Jul 2019
beep invokes an operating system routine to make a noise when it is invoked with no outputs or inputs. The code is internal to MATLAB .
when it is called with an output it returns a character vector that is the beep state.
Walter Roberson
Walter Roberson on 10 Dec 2018
It is possible that on sufficiently old implementations, sound() passed a character vector might treat the character vector as values to be played, like sound(double('on')) and sound(double('off'))

Sign in to comment.

More Answers (0)

Categories

Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!