Export (0) Print
Expand All

How To: Change the Pitch or Volume of a Sound

Demonstrates how to change pitch and volume of a playing sound.

The Complete Sample

The code in this topic shows you the technique. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

The SoundEffect.Play method allows you to specify the pitch and volume of a sound for playing the sound. However, after you call Play, you cannot modify the sound. Using SoundEffectInstance for a given SoundEffect allows you to change the pitch and volume of a sound at any time during playback.

Simple Sound Manipulation

  1. Declare a SoundEffect using the method shown in How To: Play a Sound.

  2. Declare a SoundEffectInstance, and set it to the return value of SoundEffect.CreateInstance.

    SoundEffect soundEffect;
    SoundEffectInstance soundInstance;
    protected override void LoadContent()
    {
        soundEffect = Content.Load<SoundEffect>("tx0_fire1");
        soundInstance = soundEffect.CreateInstance();
    }
    
  3. Call SoundEffectInstance.Play to play the sound.

    if (playerOne.Buttons.A == ButtonState.Pressed)
    {
        // Play Sound
        soundInstance.Play();
    }
    
  4. Set Pitch and Volume to the desired values.

    // Pitch takes values from -1 to 1
    float pitch = playerOne.ThumbSticks.Left.X;
    // Volume only takes values from 0 to 1
    float volume = (playerOne.ThumbSticks.Right.X+1)/2;
    
    soundInstance.Pitch = pitch;
    soundInstance.Volume = volume;
    

Community Additions

ADD
Show:
© 2014 Microsoft