[PloobsEngine] Tutorial 9 – Playing Sounds


This tutorial will talk about how to play all kind of sounds using PloobsEngine. (tutorials series here)

The PloobsEngine has basicaly three types of sounds: The 3D sound effects, the 2D sound effects an a Music Player.

Before understanding the details of each type, we need to know the global options that affects the way we play sounds.

The PloobsEngine offers a way to configure the global settings using a description object. The following code shows how to recover and set these properties:

      protected override void InitScreen(GraphicInfo GraphicInfo, EngineStuff engine)
        {
            base.InitScreen(GraphicInfo, engine);

            ///Controls some master option of the engine sound player
            SoundMasterOptionDescription sod = engine.GetSoundMasterOptionDescription();
            ///between 0 and 1
            sod.MasterVolume = 0.9f;
            ///varies according to your game scale
            ///Used in 3D sounds
            sod.DistanceScale = 200;
            engine.SetSoundMasterOptionDescription(ref sod);
        }

It is pretty simple, we just recover the Sound Options object from the engine (can be done anywhere, not only in the InitScreen as i did in this sample), change the options and set it in the engine.
For now, we have the following options we can set:

  • DistanceScale (use in 3D sound,  it scales the attenuation, value can be in any range — depends of the distance between your objects in your game)
  • DoplerScale (controls how intense the dopler effect should be, value must be in the 0-1 range)
  • MasterVolume (between 0 and 1 — controls the master volume)

SimpleSoundEffect

This is the easiest way to produce a sound.  The following code shows how to use it:

///Create (second parameter is the path of the sound, we support all xna sound types)
SimpleSoundEffect se = new SimpleSoundEffect(contentManager, "Songs/alarm");
///Use the following methods to play,plause and stop the effect
se.Play();
se.Pause();
se.Stop();

The problem with this aproach is that the sounds used are not reutilized (internaly, every time you create a sound instance, a new internal structure is created), so it can consume lots of memory (principaly in WP7).
The good point is that the sounds are not shared, so you can do wherever you want with it without fear of destroying something else.

SoundAudioPlayer
This way of playing sound is a bit diferent, it creates a repository of sounds and share the instances with everyone who wants.
The following code shows how to create the SoundAudioPlayer, how to add sounds to it and how to play its contents.

            ///Create and add a sound to the SoundAudioPlayer
            SoundAudioPlayer ap = new SoundAudioPlayer(contentManager);
            ///Add a sound to the repo (the second parameter is a name used to refer to this song in the future)
            ap.AddSoundToRepository("Songs/bye", "bye");
            ///Play, Stop ....
            ap.PlaySoundEffect("bye");
            ap.StopSoundEffect("bye");


The good and the bad point is that the memory is shared (good for WP7, but hard to manage in some circunstances)=P

The best option is to mix both approaches.

 LocalMediaAudioPlayer

This class is used to play musics stored in your hard drive (Media Player library). You show lists of albums, songs … and let the player choose what he wants to listen. The following code shows how to use it:

///Load the Sounds that you hear in your Microsoft Media Player
///Just loading the first album found =P
LocalMediaAudioPlayer lm = new LocalMediaAudioPlayer();
AlbumCollection ac = lm.MediaLibrary.Albums;
lm.PlayAlbum(ac[0]);

It is pretty easy to use, but it  is not very “interesting” (most users dont use Media Player Libraries =P)

Static3DSound

The Static3DSound is responsible for playing 3D static sounds (that have a fixed position in the space and follow the doppler rule). Different from the other sounds, this one must be added to the IWorld, the following code shows hot to create and play a 3D static sound.

            ///Static 3D sound, the last parameter is the position
            Static3DSound sound = new Static3DSound(contentManager, "Songs/pianosong", Vector3.Zero);
            this.World.AddSoundEmitter(sound, true);
            ///Play/Stop
            sound.Stop(true);
            sound.Play();

You can also create sounds that follow objects (if you want you can make any kind of 3D sounds, just extending the existing ones). The following sample shows a 3D sound effect that follows an IObject. (sound position is updated each frame according to the IObject position)

        {
                SimpleModel sm2 = new SimpleModel(factory, "Model\\ball");
                sm2.SetTexture(factory.CreateTexture2DColor(1, 1, Color.Red, false), TextureType.DIFFUSE);
                DeferredNormalShader nd = new DeferredNormalShader();
                IMaterial m = new DeferredMaterial(nd);
                SphereObject pi2 = new SphereObject(new Vector3(100,50,0), 1, 10, 10, MaterialDescription.DefaultBepuMaterial());
                IObject o = new IObject(m, sm2, pi2);
                this.World.AddObject(o);

                sound2 = new ObjectFollower3DSound(contentManager, "Songs/pianosong", o);
                this.World.AddSoundEmitter(sound2,true);
            }

One commom problem iwith 3D is forgetting to set the flobal Distance Scale (if you dont and your game scale is very diferent from the default one you wont not hear the 3D sound effect).

This is all for today =P.

Any doubts, problems … Just contact to us =P

You can find all of the discussed code in the AdvancedDemos Package.

Links

, , , , ,

  1. #1 by http://www.ja-trax.com/fr/soledshoes.asp on 19 de agosto de 2017 - 6:58 pm

    I had gotten the headphonesof the mother concerning moms time, as well as she absolutely adored information technology! It really is actually adorable headphonesas well as the suggesting on the card just that will come in container is very sentimental!! As well as the good of the beads was very good!

  2. #2 by Auto Care and Service on 19 de agosto de 2017 - 7:54 pm

    Hello.This post was really remarkable, especially because I was investigating for thoughts on this matter last Friday.

  3. #3 by http://finedocs.com/loubouton.asp on 19 de agosto de 2017 - 7:58 pm

    I got the headphonesto my mother towards mothers day, furthermore she completely adored they! It is completely adorable headphonesas well as the saying regarding the card in which goes into the container is very emotional!! And the top quality of the beads was ideal!

  4. #4 by Auto Care and Service on 19 de agosto de 2017 - 8:22 pm

    I am not sure where you are getting your information, but good topic. I needs to spend some time learning much more or understanding more. Thanks for fantastic info I was looking for this info for my mission.

  5. #5 by clck here on 19 de agosto de 2017 - 8:51 pm

    You have brought up a very great details , appreciate it for the post.

1 746 747 748
(não será publicado)