Old 04-18-2009, 08:37 PM   #1 (permalink)
Zuner
 
badmoodguy88's Avatar
 
Join Date: Jun 2008
Posts: 72
badmoodguy88 is on a distinguished road
Default Demo of pitch, volume, pan shifting

I was working with Musician .3. It is a very short program and so ideal for demoing SoundEffect and SoundEffectInstance play methods. I have no plans on personally doing any thing more with Musician.

Any way I made clip sound clip three loop fairly smoothly though the first part of its 'note' and hold that loop until released. Where it then plays the rest of the note. They are actually cut into two different sound clips to do this.

as you can see here they are two different waves
Code:
            End3 = Content.Load<SoundEffect>(@"Audio/End3");
            Loop3 = Content.Load<SoundEffect>(@"Audio/Loop3");
here is the code that plays them as one note with sustain and the decay as the key(downbutton) is released.
Code:
if (GamePad.GetState(PlayerIndex.One).DPad.Down == ButtonState.Pressed)
            {
                if (pressdownonce == true)
                {
                    pressdownonce = false;
                    playdownend = true;
                    Three = Loop3.Play(normalvolume, normalpitch, normalpan, true);
                }
            }
if (GamePad.GetState(PlayerIndex.One).DPad.Down == ButtonState.Released)
            {
                pressdownonce = true;

                if (playdownend == true)
                {
                        playdownend = false;
                        Three.Stop();
                        Three = End3.Play(normalvolume, normalpitch, normalpan, true);
                }
               
            }
Then I figured out how to change the volume of the SoundEffect as it is playing so that it fades in and out slowly (repleted volume changes like this are called tremolo and are usually faster).
The key is to make the SoundEffectInstance update the Variable for volume with out calling play again. Three.Volume =

For the heck of it I then made Pitch and Pan change at the same time as volume.

Code:
            if (GamePad.GetState(PlayerIndex.One).DPad.Down == ButtonState.Pressed)
            {
                if (pressdownonce == true)
                {
                    pressdownonce = false;
                    playdownend = true;
                    Three = Loop3.Play(normalvolume, (normalvolume - 1.001f), (normalvolume - .75f), true);

                }
                Three.Volume = normalvolume;
                Three.Pitch = (normalvolume - 1.001f);
                Three.Pan = (normalvolume - .75f);
                if (normalvolume > 1f)
                {
                    volumeup = false;
                }
                if (normalvolume <= 0.5f)
                {
                    volumeup = true;
                }
                if (volumeup == true)
                {
                    normalvolume += 0.005f;
                }
                if (volumeup == false)
                {
                    normalvolume -= 0.005f;
                }
            }
            if (GamePad.GetState(PlayerIndex.One).DPad.Down == ButtonState.Released)
            {
                pressdownonce = true;

                if (playdownend == true)
                {
                       normalvolume -= 0.1f;
                       Three.Volume = normalvolume;
                       Three.Pitch = (normalvolume - 1.001f);
                       normalvolume -= 0.1f;
                       Three.Volume = normalvolume;
                       Three.Pitch = (normalvolume - 1.001f);
                       normalvolume -= 0.1f;
                       Three.Volume = normalvolume;
                       Three.Pitch = (normalvolume - 1.001f);
                       normalvolume -= 0.1f;
                       Three.Volume = normalvolume;
                       Three.Pitch = (normalvolume - 1.001f);
                       normalvolume -= 0.1f;
                       Three.Volume = normalvolume;
                       Three.Pitch = (normalvolume - 1.001f);
                        playdownend = false;
                        Three.Stop();
                        normalvolume += 0.5f;
                        Three = End3.Play(normalvolume, (normalvolume - 1.001f), (normalvolume - .75f), false);
                }
               
            }
to help clear things up more...
Code:
        //new variables
        //-1f to 1f. Playing a soundclip wile going smoothly from 1f to -1f seems to give some clicks around 0f.
        float normalpitch = 0f;

        //0f to 1f
        float normalvolume = 1.0f;

        //-1f(left)to 1f(right)
        float normalpan = 0f;

        SoundEffect End3;
        SoundEffect Loop3;
        SoundEffectInstance Three;
        bool pressdownonce = true;
        bool playdownend = false;

        //SoundEffectInstance must exist before you can .Stop(); them or change any peramiter of their play method
        bool callintoexistence = true;
        float mutevolume = 0f;

        bool volumeup = true;
        //end of new
You may need to silently play a SoundEffectInstance once so that it exists. If you try to use .stop .pitch .pan .volume before ever calling .play it will crash the game. This plays a sound at the first opportunity and then does nothing for the rest of the game.
Code:
        protected override void Update(GameTime gameTime)
        {
            //SoundEffectInstance must exist before you can .Stop(); them or change any peramiter of their play method
            //so this plays silent sounds once
            if (callintoexistence == true)
            {
                callintoexistence = false;
                Three = End3.Play(mutevolume);
            }
These code fragments are meant to give you an idea of how to add some more advanced sound effects to sound clips but the implementation shown above and in the WIP below are under the GNU GENERAL PUBLIC LICENSE both because that is what the game Musician 0.3 is already under and because that is the license I personally would put my code under.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Download here
Musician WIP DEMO(sustain-decay & pitch, volume, pan shifting).rar
Musician WIP DEMO(sustain-decay & pitch, volume, pan shifting).rar
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

Last edited by badmoodguy88; 04-18-2009 at 08:42 PM.



badmoodguy88 is offline   Reply With Quote

Advertisement [Remove Advertisement]
Old 04-19-2009, 09:13 PM   #2 (permalink)
Experienced Member
 
Tiptup300's Avatar
 
Join Date: Apr 2008
Posts: 808
Tiptup300 has disabled reputation
Default

sin(gameTime.TotalElapsedSeconds)



Tiptup300 is offline   Reply With Quote
Old 04-20-2009, 08:42 PM   #3 (permalink)
Member
 
BroiledVictory's Avatar
 
Join Date: Dec 2008
Location: Puyallup, Washington State
Posts: 526
BroiledVictory will become famous soon enough
Send a message via Skype™ to BroiledVictory
Awards Showcase
Biggest Shouter 
Total Awards: 1
Default

@TipTup Hush300, sinner!
@BadMoonGuy88Neato.

I know, this is a helpful and constructive comment
I'm a addict.
__________________
Formally SmileDog.
Note: I prefer to have conversations over Skype, not over PM.
You will be bottom priority if I happen to be talking to a friend/relative.

thx Jorvette!




BroiledVictory is offline   Reply With Quote
Reply

Bookmarks

Thread Tools