Skip to content

Commit

Permalink
Merge pull request #20 from MrValentine7777/documentation-reviews
Browse files Browse the repository at this point in the history
Updated Pull request targetting [Base]
  • Loading branch information
SimonDarksideJ authored May 30, 2024
2 parents bb7ab08 + de92242 commit 1a29b79
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 55 deletions.
81 changes: 60 additions & 21 deletions articles/monogame/howto/audio/HowTo_ChangePitchAndVolume.md
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.

```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.

```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.

```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

Expand All @@ -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.
4 changes: 1 addition & 3 deletions articles/monogame/howto/content_pipeline/HowTo_Add_XML.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ description: Describes how to add custom game data as an XML file through the Co
requireMSLicense: true
---

## Adding XML files to game content

Custom game data that is expressed in an XML format can be easily integrated into your game through the MonoGame Content Pipeline.

This example demonstrates the procedure for integrating custom XML data into the content project of a simple game for the Windows platform.
Expand All @@ -17,7 +15,7 @@ Within the MonoGame solution, you create a new Windows Game Library project.
1. Right-click the solution node, point to `Add`, click `New Project`, and then select the `MonoGame Game Library` template.

> [!TIP]
> A `MonoGame Game Library` project is created instead of a Content Pipeline Extension Library project so that the class we will define can be used by both the [Content Importer](xref:Microsoft.Xna.Framework.Content.Pipeline.ContentImporter-1) that runs at build-time and the [Content Loader](xref:Microsoft.Xna.Framework.Content.ContentManager#Microsoft_Xna_Framework_Content_ContentManager_Load__1_System_String_) at game runtime.
> A `MonoGame Game Library` project is created instead of a Content Pipeline Extension Library project so that the class we will define can be used by both the [Content Importer](https://docs.monogame.net/api/Microsoft.Xna.Framework.Content.Pipeline.ContentImporter-1.html) that runs at build-time and the [Content Loader](xref:Microsoft.Xna.Framework.Content.ContentManager#Microsoft_Xna_Framework_Content_ContentManager_Load__1_System_String_) at game runtime.
2. In the `Name` box, type `MyDataTypes`, and then click `OK`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The example is very basic but the principles are the same, when drawing to a Ren
5. Draw your game as normal.
6. Draw the Render Texture to the screen in the position we desire (e.g. in the lower corner for a mini-map), most likely on top of your game graphics.

> TIP
> [!TIP]
> The technique is very useful, especially if you are doing split-screen gaming and need to draw multiple camera views.
## Requirements
Expand Down
15 changes: 4 additions & 11 deletions articles/monogame/howto/graphics/HowTo_RenderModel.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
---
title: How to render a Model using a Basic Effect
description: Demonstrates how to rotate a group of sprites around a single point.
description: Demonstrates how to load and render a model using the MonoGame Content Pipeline.
requireMSLicense: true
---

# Rendering a Model with a Basic Effect

Demonstrates how to load and render a model using the MonoGame Content Pipeline. It is assumed that an existing Windows game project is loaded in MonoGame. In this example, the project is called "CPModel."
> It is assumed that an existing Windows game project is loaded in MonoGame. In this example, the project is called "CPModel."
This example has three main parts: importing and processing the model, drawing the resultant managed object as a model with full lighting effect in the game, and enabling movement of the model with a game pad.

Expand Down Expand Up @@ -150,11 +149,5 @@ Development is complete so you are ready to build and run the game. Control the

## See Also

[Adding Content to a Game](../Content_Pipeline/HowTo_LoadContent.md)
[Adding Content to a Game](../Content_Pipeline/HowTo_GameContent_Add.md)
[Using Input](../input/index.md)

---

© 2012 Microsoft Corporation. All rights reserved.

© 2023 The MonoGame Foundation.
15 changes: 3 additions & 12 deletions articles/monogame/whatis/content_pipeline/index.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
---
title: What is the Content pipeline?
description: The basics of the content pipeline for MonoGame!
description: The topics in this section describe how to add and load content such as textures, meshes, sounds, and data in your game.
requireMSLicense: true
---

# Adding Content to a Game

The topics in this section describe how to add and load content such as textures, meshes, sounds, and data in your game.

## In This Section

[What Is Content?](CP_Overview.md)

Describes the purpose of the MonoGame Content Pipeline and how it helps you add art and data assets to your game.

[Loading Content](../../howto/Content_Pipeline/HowTo_LoadContent.md)
[Loading Content](../../howto/Content_Pipeline/HowTo_GameContent_Add.md)

Demonstrates how to load content such as models, textures, sounds, and effects.

Expand Down Expand Up @@ -54,9 +51,3 @@ Describes the valid tags and values for Sprite-Font (.spritefont) XML files used
[What are the XML Elements for XMLImporter?](CP_XML_Elements.md)

The base elements that are recognized by XmlImporter Class.

---

© 2012 Microsoft Corporation. All rights reserved.

© 2023 The MonoGame Foundation.
9 changes: 2 additions & 7 deletions articles/monogame/whatis/index.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
---
title: What is
title: "What Is" Articles for MonoGame
description: A series of articles to answer common questions related to MonoGame operation!
requireMSLicense: true
---

# "What Is" Articles for MonoGame

These articles provide a brief introduction into graphics pipeline functionality.

## In This Section

[WhatIs Audio?](WhatIs_Audio.md)
Expand Down Expand Up @@ -47,6 +44,4 @@ An overview of the MonoGame Class Library reference, containing all the API call

---

© 2012 Microsoft Corporation. All rights reserved.

© 2023 The MonoGame Foundation.

0 comments on commit 1a29b79

Please sign in to comment.