-
Notifications
You must be signed in to change notification settings - Fork 44
Updated Pull request targetting [Base] #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
15d4e0e
1a2ea45
cc5a249
8942163
1f1fd8b
de92242
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,89 @@ | ||
--- | ||
title: How to adjust Pitch and Volume | ||
description: Demonstrates how to manipulate the pitch and volume of sound effects as they play. | ||
requireMSLicense: true | ||
--- | ||
|
||
# Adjusting Pitch and Volume | ||
The **[SoundEffect.Play](xref:Microsoft.Xna.Framework.Audio.SoundEffect.Play)** method allows you to specify the pitch and volume of a sound to play. However, after you call **[Play](xref:Microsoft.Xna.Framework.Audio.SoundEffect.Play)**, you cannot modify the sound. Using **[SoundEffectInstance](xref:Microsoft.Xna.Framework.Audio.SoundEffectInstance)** for a given **[SoundEffect](xref:Microsoft.Xna.Framework.Audio.SoundEffect)** allows you to change the **pitch** and **volume** of a sound at any time during playback. | ||
|
||
The [SoundEffect.Play](xref:Microsoft.Xna.Framework.Audio.SoundEffect.Play) method allows you to specify the pitch and volume of a sound to play. However, after you call [Play](xref:Microsoft.Xna.Framework.Audio.SoundEffect.Play), you cannot modify the sound. Using [SoundEffectInstance](xref:Microsoft.Xna.Framework.Audio.SoundEffectInstance) for a given [SoundEffect](xref:Microsoft.Xna.Framework.Audio.SoundEffect) allows you to change the pitch and volume of a sound at any time during playback. | ||
> [!NOTE] | ||
> The pitch of a sound changes the frequency of the sound, which in turn changes the speed of the sound. The volume of a sound changes the amplitude of the sound, which in turn changes the loudness of the sound. | ||
|
||
## Change Pitch and Volume of Sound | ||
|
||
1. Declare [SoundEffect](xref:Microsoft.Xna.Framework.Audio.SoundEffect) and [Stream](http://msdn.microsoft.com/en-us/library/system.io.stream.aspx) by using the method shown in [Playing a Sound](HowTo_PlayASound.md). In addition to the method described in [Playing a Sound](HowTo_PlayASound.md), declare [SoundEffectInstance](xref:Microsoft.Xna.Framework.Audio.SoundEffectInstance). | ||
1. Declare a **[SoundEffect](xref:Microsoft.Xna.Framework.Audio.SoundEffect)** and a [Stream](http://msdn.microsoft.com/en-us/library/system.io.stream.aspx) file by using the method shown in [Playing a Sound](HowTo_PlayASound.md). In addition to the method described in [Playing a Sound](HowTo_PlayASound.md), declare a **[SoundEffectInstance](xref:Microsoft.Xna.Framework.Audio.SoundEffectInstance)** and a **Sound Effect** field member. We also create two float fields for **pitch** and **volume** to store the pitch and volume of the sound effect and assign initial values to them. | ||
|
||
```csharp | ||
SoundEffectInstance soundInstance; | ||
// place these usings at the top of the file | ||
using System.IO; | ||
using Microsoft.Xna.Framework; | ||
using Microsoft.Xna.Framework.Audio; | ||
using Microsoft.Xna.Framework.Graphics; | ||
using Microsoft.Xna.Framework.Input; | ||
|
||
// place these fields at the top of the class | ||
private SoundEffect soundEffect; | ||
private SoundEffectInstance soundEffectInstance; | ||
private float pitch = 0.75f; | ||
private float volume = 0.5f; | ||
``` | ||
|
||
2. In the [Game.LoadContent](xref:Microsoft.Xna.Framework.Game.LoadContent) method, set the SoundEffectInstance object to the return value of [SoundEffect.CreateInstance](xref:Microsoft.Xna.Framework.Audio.SoundEffect.CreateInstance). | ||
> [!NOTE] | ||
> Usings are declared at the top of the file to ensure that the necessary namespaces are available to the class. The fields are declared at the top of the class to ensure that they are accessible to all methods in the class. | ||
|
||
2. In the [Game.LoadContent](xref:Microsoft.Xna.Framework.Game.LoadContent) method, set the **SoundEffectInstance** object to the return value of [SoundEffect.CreateInstance](xref:Microsoft.Xna.Framework.Audio.SoundEffect.CreateInstance). | ||
|
||
3. In the **[Game.LoadContent](xref:Microsoft.Xna.Framework.Game.LoadContent)** method, set the **SoundEffectInstance** object to the return value of **[SoundEffect.CreateInstance](xref:Microsoft.Xna.Framework.Audio.SoundEffect.CreateInstance)**. We also optionally define a variable **soundFile** to store the location of the sound file being used with the **[TitleContainer.OpenStream](xref:Microsoft.Xna.Framework.TitleContainer#Microsoft_Xna_Framework_TitleContainer_OpenStream_System_String_)** method, which is accessed with the **using** keyword, and include a field member variable called **soundEffect**, to hold the stream. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The text does not match the code below, consider revising |
||
|
||
```csharp | ||
soundfile = TitleContainer.OpenStream(@"Content\tx0_fire1.wav"); | ||
using Stream soundfile = TitleContainer.OpenStream(@"Content\Sound__FileName.wav"); | ||
soundEffect = SoundEffect.FromStream(soundfile); | ||
soundInstance = soundEffect.CreateInstance(); | ||
``` | ||
|
||
3. Adjust the sound to the desired level using the [SoundEffectInstance.Pitch](xref:Microsoft.Xna.Framework.Audio.SoundEffectInstance.Pitch) and [SoundEffectInstance.Volume](xref:Microsoft.Xna.Framework.Audio.SoundEffectInstance.Volume) properties. | ||
4. Adjust the sound to the desired level using the [SoundEffectInstance.Pitch](xref:Microsoft.Xna.Framework.Audio.SoundEffectInstance.Pitch) and [SoundEffectInstance.Volume](xref:Microsoft.Xna.Framework.Audio.SoundEffectInstance.Volume) properties. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Text does not match the code, I suggest the code needs to change here. |
||
|
||
```csharp | ||
// Play Sound | ||
soundInstance.Play(); | ||
soundEffectInstance.Play(); | ||
``` | ||
|
||
4. Play the sound using [SoundEffectInstance.Play](xref:Microsoft.Xna.Framework.Audio.SoundEffectInstance.Play). | ||
> [!NOTE] | ||
> An instance will play once, to loop the sound, you can use the **[SoundEffectInstance.IsLooped](xref:Microsoft.Xna.Framework.Audio.SoundEffectInstance.IsLooped)** property to set the sound to loop. Also note that the sound will not repeat until the sound has finished playing. You can utilise the **[SoundEffectInstance.State](xref:Microsoft.Xna.Framework.Audio.SoundEffectInstance.State)** property to check if the sound is playing, paused or stopped. Use the **[SoundEffectInstance.Stop](xref:Microsoft.Xna.Framework.Audio.SoundEffectInstance.Stop)** method to stop the sound. | ||
|
||
## An Extended Example | ||
|
||
1. Below the **[Game.Draw](xref:Microsoft.Xna.Framework.Game#Microsoft_Xna_Framework_Game_Draw_Microsoft_Xna_Framework_GameTime_)** method, create a new method called **IsKeyPressed**, which will check if a specified key is pressed and return a boolean value of true if it has been pressed. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I fixed the API references, as they work differently to the original XNA interpreter. Note the change from
To
Browse the API docs on the website to get the Method links There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which site? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need a quick hand at how to make the # extension not done web anchors in 15 years... and was unable to find that anchor... |
||
|
||
```csharp | ||
// Pitch takes values from -1 to 1 | ||
soundInstance.Pitch = pitch; | ||
|
||
// Volume only takes values from 0 to 1 | ||
soundInstance.Volume = volume; | ||
``` | ||
private bool IsKeyPressed(Keys key) | ||
{ | ||
return Keyboard.GetState().IsKeyDown(key); | ||
} | ||
|
||
``` | ||
|
||
2. In the **[Game.Update](xref:Microsoft.Xna.Framework.Game#Microsoft_Xna_Framework_Game_Update_Microsoft_Xna_Framework_GameTime_)** method, check if the **Space** key is pressed and adjust the pitch and volume of the sound effect accordingly. The pitch and volume values are adjusted by +0.1f each time the **Space key** is pressed. The pitch values are clamped to a minimum value of -1.0f and a maximum value of 1.0f, and the volume values are then clamped to a minimum value of 0f and a maximum value of 1.0f. This is done to ensure that the pitch and volume values are within valid ranges. | ||
|
||
```csharp | ||
// Check if the SpaceKey is pressed and play the instance | ||
if (IsKeyPressed(Keys.Space)) | ||
{ | ||
pitch += 0.1f; | ||
volume += 0.1f; | ||
pitch = MathHelper.Clamp(pitch, -1.0f, 1.0f); | ||
volume = MathHelper.Clamp(volume, 0f, 1.0f); | ||
soundEffectInstance.Pitch = pitch; | ||
soundEffectInstance.Volume = volume; | ||
soundEffectInstance.Play(); | ||
} | ||
``` | ||
|
||
> [!NOTE] | ||
> The **MathHelper.Clamp** method is used to ensure that the pitch and volume values are within the valid range. The pitch value is clamped between -1 and 1, while the volume value is clamped between 0 and 1. | ||
|
||
> [!NOTE] | ||
> The check for the keypress does not prevent the call to the method repeating so any value entered may peak the value in a singe key press. To prevent this, you can add a delay to the key press check, or use a boolean value to check if the key has been pressed and released. | ||
|
||
## Concepts | ||
|
||
|
@@ -63,9 +108,3 @@ Provides a loaded sound resource. | |
[SoundEffectInstance Class](xref:Microsoft.Xna.Framework.Audio.SoundEffectInstance) | ||
|
||
Provides a single playing, paused, or stopped instance of a [SoundEffect](xref:Microsoft.Xna.Framework.Audio.SoundEffect) sound. | ||
|
||
--- | ||
|
||
© 2012 Microsoft Corporation. All rights reserved. | ||
|
||
© 2023 The MonoGame Foundation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated SoundEffectInstance to SoundEffectInstance to match the styling used elsewhere in the doc. Please ensure to use consistent styling when referring to elements or classes.