matlab sounds in game design

3 visualizaciones (últimos 30 días)
a a
a a el 3 de Dic. de 2018
Editada: Walter Roberson el 26 de Jul. de 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 comentarios
Adam Danz
Adam Danz el 3 de Dic. de 2018
I replied in the answer section.
John D'Errico
John D'Errico el 3 de Dic. de 2018
Editada: John D'Errico el 3 de Dic. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Adam Danz
Adam Danz el 3 de Dic. de 2018
Editada: Adam Danz el 3 de Dic. de 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 comentarios
Walter Roberson
Walter Roberson el 10 de Dic. de 2018
Editada: Walter Roberson el 26 de Jul. de 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 el 10 de Dic. de 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'))

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Audio I/O and Waveform Generation en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by