Adjusting Pitch and Volume

The SoundEffect.Play method allows you to specify the pitch and volume of a sound to play. 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.

Complete Sample

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

Download ChangePitchAndVolumeWithoutXACT_Sample.zip

Change Pitch and Volume of Sound

To adjust the pitch and volume of a sound

  1. Declare SoundEffect and Stream by using the method shown in Playing a Sound. In addition to the method described in Playing a Sound, declare SoundEffectInstance.

    SoundEffectInstance soundInstance;
    
  2. In the Game.LoadContent method, set the SoundEffectInstance object to the return value of SoundEffect.CreateInstance.

    soundfile = TitleContainer.OpenStream(@"Content\tx0_fire1.wav");
    soundEffect = SoundEffect.FromStream(soundfile);
    soundInstance = soundEffect.CreateInstance();
    
  3. Adjust the sound to the desired level using the SoundEffectInstance.Pitch and SoundEffectInstance.Volume properties.

    // Play Sound
    soundInstance.Play();
    
  4. Play the sound using SoundEffectInstance.Play.

    // Pitch takes values from -1 to 1
    soundInstance.Pitch = pitch;
    
    // Volume only takes values from 0 to 1
    soundInstance.Volume = volume;
    

Concepts

Reference