[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 buy windows vps on 24 de março de 2017 - 5:38 pm

    You made some respectable points there. I seemed on the web for the difficulty and located most people will go together with together with your website.

  2. #2 by http://news.cartierreplica.win/cartierreplicawin/136.asp on 24 de março de 2017 - 5:41 pm

    I had gotten it present towards my mother towards Xmas as she is actually one jewelry freak. The actual something she does not put on far concerning is, bracelets. I purchased her your charm bracelet then after she launched that yesterday she absolutley enjoyed that it! Right now the problem was, the lady obtaining that in and also off simply by by herself. Haha… on the whole awesome product or service, it delivered to arrived very very early furthermore my personal mother is experiencing this. Thank a person.

  3. #3 by http://news.cheapmoncler.xyz/cheapmonclerxyz/142.asp on 24 de março de 2017 - 5:41 pm

    it comes down within a stunning package styles ideal although its slightly small actually of our mothers wrist but it looks great just really want that it had been further

  4. #4 by http://news.monclerjacketsoutlet.xyz/monclerjacketsoutletxyz/132.asp on 24 de março de 2017 - 5:42 pm

    it comes down in a beautiful container seems very good and yet it really is a bit mini even for the the parents wrist however it appearance very good just want information technology had been much longer

  5. #5 by http://news.cartierbraceletsreplica.win/cartierbraceletsreplicawin/151.asp on 24 de março de 2017 - 5:42 pm

    it comes down within a beautiful box appears awesome and yet their slightly little additionally for the the mothers wrist but it looks very good just desire this had been longer

  6. #6 by cheap shoes christian louboutin on 24 de março de 2017 - 6:25 pm

    My boy provided me personally the towards moms day. He realizes im not really up to price however everything comes from that center. I can’t know it has the favourite colors plus actually suits my personal wrist. I never ever could choose bracelets to fit headphonesmy little wrist. Really pleased to have recieved it being a gifts.

  7. #7 by louis vuitton used on 24 de março de 2017 - 7:40 pm

  8. #8 by warisan on 24 de março de 2017 - 7:47 pm

    I liked as much as you will obtain performed right here. The sketch is attractive, your authored subject matter stylish. nevertheless, you command get bought an nervousness over that you wish be delivering the following. unwell undoubtedly come further earlier again since precisely the same just about a lot incessantly inside of case you shield this increase.

  9. #9 by neck & back pain on 24 de março de 2017 - 8:31 pm

    such an ideal method of writing? I ave a presentation next

  10. #10 by hermes belts on 24 de março de 2017 - 8:38 pm

    My boy provided me the for the parents day. He realizes im never about price tag although exactly what comes from the center. I cannot believe this has our favorite color and also really fits our wrist. I never could see bracelets to fit headphonesthe limited wrist. Most pleased to come with recieved this particular as a gifts.

  11. #11 by http://news.cartierreplica.top/cartierreplicatop/48.asp on 24 de março de 2017 - 8:50 pm

    This particular product is at that very good cost I by no means idea the actual grade would be and so exceptional. It’s breathtaking. It mom does appreciate information technology concerning Christmas time early morning after she opens up gift therefore appearances as though I devoted even more, although pricing had been just great!!

  12. #12 by http://news.monclerjacketsoutlet.pw/monclerjacketsoutletpw/125.asp on 24 de março de 2017 - 8:50 pm

    I have bought that brand name of bracelet various period. Every one is super sweet, prepared very well, cannot tarnish and/or significant depending on that an you buy as well as just who people provide this to.

1 492 493 494
(não será publicado)