matlab sounds in game design

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

Are you sure that playing the sound is what's slowing down matlab or are you loading the sounds file every time you want to play the sound? You should load the sound data once and make it a persistent variable. Then when you need to play the sound, you won't have to load it again.
If the sound itself takes too long to play, find a simpler, shorter sound byte.
I would also note that MATLAB is not designed to be a game programming environment, so things are clearly not optimized for that purpose. Regardless, Adam is correct, in that you may well be loading the sound each time. That would be a bad idea.
@ Adam
I'm not sure if I'm 'loading' the sound every time I run it. The command I use to make a sound:
sound(wavread('blablabla.wav'));
Is it 'loading' or 'playing' the sound?
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

Thank you very much. That speeds up. But I tried not declaring y and Fs as persistent:
[y,Fs] = audioread(blablabla.wav)
and running sound(y,Fs) multiple times without clearing y and Fs. That also works equally well. So do I need to declare them as persistent?
I don't know how your code is set up but once the function that contains (y, Fs) is done running, you'll lose those variables. If that function needs accessed multiple times during the game, it should be a persistent variable so you don't have to load the data again. In fact, those data will persist until your matlab session is closed or until all persistent variables are cleared intentionally. Making is persistent within a conditional to see if it already exists only adds 3 lines of code.
I have a main.m and a function.m. The main.m is used to run the game. y and Fs are defined in main.m:
global y Fs
[y,Fs] = audioread(blablabla.wav);
...
while
function() is run somewhere in main.m
end
...
The function.m has a command: sound(y,Fs), where y and Fs are global, as defined in main.m. Without declaring y and Fs as persistent, sounds are available for many times (whenever balls collide with one another).
I see. I hope these are just example names and that you don't have a file named 'function.m'
181204 083307-MATLAB Editor.jpg
If the function.m is the only file that needs Y and Fs, it would be better to use the persistent variable approach so you don't have to use global variables which should be avoided when possible. However, if there are many files that need those variables and you don't want to add them as function inputs, then maybe global variables is the better approach - but usually not. When using global variables you risk Y and Fs being overwritten somewhere since those are really common variable names, especially Y. Typically global variables should be longer and more unique and they are typically in all CAPITAL_LETTERS. Another bad part about using global variables is that it's unclear in each constituent file where those variables came from or what they are.
Stephen23
Stephen23 on 4 Dec 2018
Edited: Stephen23 on 4 Dec 2018
@a a: avoid global variables, they cause more problems than they are worth.
@ Adam Danz
Thanks, I know for the 1st time function.m isn't allowed. Anyway nobody uses it.
I use global variables when sth is used across different .m files and main file. What's the problem of global variables?
Any time I see someone asking what is wrong with global variables, I can make several predictions with high certainty:
  1. the person is not in the habit of creating data dictionaries that list every variable name and its data type and dimensions and describing its permitted interpretations before the code is written
  2. in any sufficiently long program there will arise different sections in which different understandings of a global variable will arise leading to conflicts that are hard to grasp because in each section the expected meaning will predispose the person to think that the variable must have a certain value
  3. that the user will have a heck of a time figuring out where the value is being changed, let alone why the changes do not work together. Probably the user will even come to suspect that MATLAB is corrupting the variable someplace that it is being passed around as that can start to feel like the only possible way.
It would also be common to see the user poofing variables into existence using load without an output , or using eval or using evalin or using assignin, making it even more difficult to figure out where the global variables are being changed .
Thank you. I understand your point. My code is relatively shorter compared to the situations you addressed. I build computer games that contain less than 10 .m file functions and I can make sure there's no conflict of variables in my mind.
what about sound(beep)? is it 'playing' or 'loading' very time i run it? anyway of using the y, Fs approach to make it faster?
use audioplayer to set up an object ready to play .Then use the play() or playblocking() method .
looks great. what are the specific commands to implement? thanks
documentation for audioplayer has an example .
the docu example for audioplayer is below:
load handel;
player = audioplayer(y, Fs);
play(player);
but there's no such thing as 'beep'. still idk how to extract info from 'beep'.
a a, 'beep' is a matlab command that produces the "ding" sounds when you have an error. From the command window, just type the word "beep" (no quotes) and you'll hear it if your speakers are on.
If you have a specific question, please follow-up.
I know how beep sounds like. I want to use it in my program as there's no other better sound to use. Commands 'beep' and 'sound(beep)' produce different sounds. On my pc, the latter sounds better and shorter, while the former is for error messages. My question was: what about 'sound(beep)'? is it 'playing' or 'loading' very time i run it? anyway of using the y, Fs approach to make it faster?
When I run sound(beep) on matlab 2018a, it produces an error. Do you have a variable named "beep" that overrides matlab's beep command?
sound(beep)
Error using sound (line 33)
Audio data must be real and floating point.
The sound() command receives a vector of data so you must have a variable named 'beep' that stores the sound data. I'm not sure how you're loading it. You'll need to share the relevant code for me to understand what you're doing.
no, i have nothing else to help run the code. simply:
beep or sound(beep)
that's the whole code. there's no previous variable beep. i'm using matlab 2012.
Then you should definitely avoid using sound(beep) because it will break on more recent releases of Matlab.
then is there any short system sound in matlab that i can put in my little game for collision actions?
beep
I'm suggesting you don't use sound(beep). "beep" alone is what works. However, it's the same sound as an error which might be confusing to matlab users.
To bring this topic full circle, my recommendation is what I originally typed in my answer. Find a short, small file sized sound byte - there's lot of them on the internet. Load it into persistent memory when you start your game, and then just use audioread() or some other sound playing function whenever you want to play the sound.
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.
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 Just for fun in Help Center and File Exchange

Tags

Asked:

a a
on 3 Dec 2018

Edited:

on 26 Jul 2019

Community Treasure Hunt

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

Start Hunting!