matlab sounds in game design

3 visualizzazioni (ultimi 30 giorni)
a a
a a il 3 Dic 2018
Modificato: Walter Roberson il 26 Lug 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 Commenti
Adam Danz
Adam Danz il 3 Dic 2018
I replied in the answer section.
John D'Errico
John D'Errico il 3 Dic 2018
Modificato: John D'Errico il 3 Dic 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.

Accedi per commentare.

Risposta accettata

Adam Danz
Adam Danz il 3 Dic 2018
Modificato: Adam Danz il 3 Dic 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 Commenti
Walter Roberson
Walter Roberson il 10 Dic 2018
Modificato: Walter Roberson il 26 Lug 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 il 10 Dic 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'))

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Audio I/O and Waveform Generation in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by