You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
matlab sounds in game design
3 views (last 30 days)
Show older comments
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
on 3 Dec 2018
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.
John D'Errico
on 3 Dec 2018
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.
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.
Accepted Answer
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
a a
on 4 Dec 2018
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?
Adam Danz
on 4 Dec 2018
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.
a a
on 4 Dec 2018
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).
Adam Danz
on 4 Dec 2018
I see. I hope these are just example names and that you don't have a file named 'function.m'

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.
Walter Roberson
on 7 Dec 2018
Any time I see someone asking what is wrong with global variables, I can make several predictions with high certainty:
- 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
- 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
- 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 .
a a
on 8 Dec 2018
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.
Walter Roberson
on 9 Dec 2018
use audioplayer to set up an object ready to play .Then use the play() or playblocking() method .
Walter Roberson
on 9 Dec 2018
documentation for audioplayer has an example .
Adam Danz
on 10 Dec 2018
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.
a a
on 10 Dec 2018
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?
Adam Danz
on 10 Dec 2018
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.
Adam Danz
on 10 Dec 2018
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
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
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'))
More Answers (0)
See Also
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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)