Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play sound as soon as possible?

There are about 60 sound samples in my application. I have to play it with strong accuracy in time. 5ms will mater. Sound must play in response to user actions. So I do not know what sound'll play next. I thinked aboud precreating of SoundEffectInstance for all sounds. Does creating of SoundEffectInstance take any time and memory? Is it full copy of SoundEffect in memory or just some pointer information. Can I improve my application if I'll precreate SoundEffectInstances? Is there any other way to play sample as soon as possible in XNA?

like image 976
xander27 Avatar asked Mar 06 '12 16:03

xander27


People also ask

How do I trigger audio in unity?

Unity has a built in method for triggering events when a button is pressed and this includes an option for playing an Audio Clip. Just create an On Click event trigger, drag a Game Object to the object field and select PlayOneShot(AudioClip) from the Audio Source section of the drop down menu.

How do I download sound effects for Powerpoint?

On the Effect tab, under Enhancements, click the arrow in the Sound list, and then do one of the following: To add a sound from the list, click a sound. To add a sound from a file, click Other Sound, and then locate the sound file that you want to use.


2 Answers

A SoundEffect contains the actual waveform data. It uses up a large amount of memory (as much as the waveform data requires).

A SoundEffectInstance is a small object, containing data about playback (position, volume, etc) and a handle to the "voice" that has been allocated to do the playback.

When you use SoundEffect.Play, internally it creates a SoundEffectInstance. It can also pool these instances - so it does not need to recreate them on subsequent calls.

Creating a SoundEffectInstance takes some time and memory. For sound effects in a game running at 30FPS (33ms per frame) the latency involved should be imperceptible.


If you're trying to play back sequenced music by mixing together different sound effects, where even 5ms matters, you need to implement your own audio mixer using DynamicSoundEffectInstance. This will ensure that sounds are synchronised down to the sample.


If you're trying to create some kind of real-time playable "instrument", and you're trying to get your input-to-output latency down to 5ms - then give up now. 5ms is absurdly good and normally requires specialist equipment and software.

Still, if you need to reduce your input-to-output latency as best you can, you can poll input more frequently than once per frame. And you should still probably use DSEI. I am not sure if DSEI has a configurable playback buffer size, but if it does you should make it as small as possible without introducing audio glitching.

like image 128
Andrew Russell Avatar answered Sep 20 '22 10:09

Andrew Russell


When a user chooses a set, you should load those sound effects into a dictionary, so accessing them will be faster later on.

However, you shouldn't load all the sound effects, especially those that you don't need in a certain screen or set. Additionnally, you should unload the sound effects you won't need anymore.

It does take time to load those sound effects, depending on the size and quantity you want to load. I'd recommend you make a basic loading screen for loading/unloading content in your game (most games do it that way). You will be loading the soundeffect (wav format I guess?) in memory, not just a pointer, so that's why sound effects need to be kept short.

An already loaded sound effect will play very fast (the delay is imperceptible). Preloading will increase the application's performance, at the cost of memory usage.

like image 30
Msonic Avatar answered Sep 20 '22 10:09

Msonic