Skip to content

Commit b3fdb30

Browse files
Updated audio section
1 parent 8fbbeb8 commit b3fdb30

10 files changed

+483
-5
lines changed

articles/getting_to_know/howto/audio/HowTo_ChangePitchAndVolume.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Using **[SoundEffectInstance](xref:Microsoft.Xna.Framework.Audio.SoundEffectInst
1919
2020
## Requirements
2121

22-
This sample uses a sound file named [tx0_fire1.wav](./files/tx0_fire1.wav), which you can download from [this link here](./files/tx0_fire1.wav). (save-as)
22+
This sample uses a sound file named [tx0_fire1.wav](files/tx0_fire1.wav), which you can download from [this link here](files/tx0_fire1.wav). (save-as)
2323
But you can use your own if you wish.
2424

2525
## Change Pitch and Volume of Sound
@@ -87,7 +87,6 @@ But you can use your own if you wish.
8787
{
8888
return Keyboard.GetState().IsKeyDown(key);
8989
}
90-
9190
```
9291

9392
2. In the **[Game.Update](xref:Microsoft.Xna.Framework.Game#Microsoft_Xna_Framework_Game_Update_Microsoft_Xna_Framework_GameTime_)** method, we will add a check to see 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.
@@ -112,7 +111,7 @@ But you can use your own if you wish.
112111
> [!IMPORTANT]
113112
> The check for the keypress does not prevent the call to the method repeating so any value entered may peak the value in a single 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.
114113

115-
## Concepts
114+
## See Also
116115

117116
* [Playing a Sound](HowTo_PlayASound.md)
118117

articles/getting_to_know/howto/audio/HowTo_LoopASound.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Not much extra code is needed to continuously loop a sound file in your game. Si
4444
> [!NOTE]
4545
> Obviously you would not normally call `play` in the `LoadContent` method, we just use here as an example.
4646

47-
## Concepts
47+
## See Also
4848

4949
* [Playing a Sound](HowTo_PlayASound.md)
5050

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
title: How to work with Microphones
3+
description: This topic provides basic information about microphone usage in games.
4+
requireMSLicense: true
5+
---
6+
7+
## Supported Microphone Devices
8+
9+
The Microphone API is only implemented on OpenAL based platforms at this time. This includes iOS/Android and Desktop projects using DesktopGL.
10+
11+
## Capabilities of the Microphone API
12+
13+
The MonoGame Microphone API has the following functionality:
14+
15+
* Captures the audio stream from a microphone.
16+
* Submits and controls a stream of audio buffers for playback using the [DynamicSoundEffectInstance](xref:Microsoft.Xna.Framework.Audio.DynamicSoundEffectInstance) object.
17+
* Plays back audio.
18+
19+
## Microphone API Process Workflow
20+
21+
The Microphone API behaves like a simple audio recorder with a configurable capture buffer. It has been designed with the following development process workflow:
22+
23+
1. Select the microphone connected to the device.
24+
2. Configure the microphone's capture buffer size.
25+
3. Control the recording using standard transport controls (Start and Stop).
26+
4. Retrieve the captured audio using the [GetData](xref:Microsoft.Xna.Framework.Audio.Microphone) method.
27+
28+
Also, you can use the **BufferReady** event handler of the [Microphone](xref:Microsoft.Xna.Framework.Audio.Microphone) class if you want to be notified when the audio capture buffer is ready to be processed.
29+
30+
## See Also
31+
32+
* [Creating and Playing Sounds](../../whatis/WhatIs_Audio.md)
33+
34+
Provides overviews about audio technology, and presents predefined scenarios to demonstrate how to use audio.
35+
36+
## Reference
37+
38+
* [DynamicSoundEffectInstance](xref:Microsoft.Xna.Framework.Audio.DynamicSoundEffectInstance)
39+
40+
Provides properties, methods, and events for play back of the audio buffer.
41+
42+
* [Microphone](xref:Microsoft.Xna.Framework.Audio.Microphone)
43+
44+
Provides properties, methods, and fields and events for capturing audio data with microphones.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
title: How to play a Song / music
3+
description: Demonstrates how to play a song from a media library.
4+
requireMSLicense: true
5+
---
6+
7+
## Overview
8+
9+
In this example we will demonstrate the ability to load a media file such as an MP3, OGG or WMA file.
10+
11+
The [MediaPlayer](xref:Microsoft.Xna.Framework.Media.MediaPlayer) class manages long running music or media. Will start audio playback when using the [Play](xref:Microsoft.Xna.Framework.Media.MediaPlayer#Microsoft_Xna_Framework_Media_MediaPlayer_Play_Microsoft_Xna_Framework_Media_Song_) method. If the user currently is playing a different song, the [Stop](xref:Microsoft.Xna.Framework.Media.MediaPlayer#Microsoft_Xna_Framework_Media_MediaPlayer_Stop) method can be used to stop the current song.
12+
13+
> [!NOTE]
14+
> Only one song can be played at any time using the MediaPlayer.
15+
16+
## Requirements
17+
18+
This sample uses a mp3 file named [rock_loop_stereo.mp3](https://github.com/MonoGame/MonoGame/raw/develop/Tests/Assets/Audio/rock_loop_stereo.mp3), which you can download from [this link here](https://github.com/MonoGame/MonoGame/raw/develop/Tests/Assets/Audio/rock_loop_stereo.mp3). (save-as)
19+
But you can use your own if you wish.
20+
21+
## To add a music file to your project
22+
23+
1. Open your MonoGame game in your editor of choice.
24+
25+
2. Open the `Content.mgcb` file by either double-clicking on it, or right-clicking and selecting `Open in MGCB editor". (depending on your editor)
26+
27+
3. Click **Add** in the menu bar, and then click **Existing Item**. (alternatively, use the icon for the same task in the editor)
28+
29+
4. Navigate to the music (mp3) file you want to add, and then select it.
30+
31+
> The selected audio file is inserted into your project. By default, it is processed by the Content Pipeline as a [Song](xref:Microsoft.Xna.Framework.Media.Song)
32+
33+
## To play a song
34+
35+
The following demonstrates how to play a song from the media file in the content project.
36+
37+
1. Declare a [Song](xref:Microsoft.Xna.Framework.Media.Song) parameter as a reference for the media file to load.
38+
39+
```csharp
40+
private Song song;
41+
```
42+
43+
2. Load the media file using [Content.Load](xref:Microsoft.Xna.Framework.Content.ContentManager) in the 'LoadContent' method.
44+
45+
```csharp
46+
song = Content.Load<Song>("rock_loop_stereo");
47+
```
48+
49+
3. Check if the [MediaPlayer](xref:Microsoft.Xna.Framework.Media.MediaPlayer) is currently playing (or has a paused track), and then call "Stop" if needed. As soon as the [MediaPlayer](xref:Microsoft.Xna.Framework.Media.MediaPlayer) is ready, play the music.
50+
51+
```csharp
52+
// check the current state of the MediaPlayer.
53+
if(MediaPlayer.State != MediaState.Stopped)
54+
{
55+
MediaPlayer.Stop(); // stop current audio playback if playing or paused.
56+
}
57+
58+
// Play the selected song reference.
59+
MediaPlayer.Play(song);
60+
```
61+
62+
## See Also
63+
64+
* [Playing a Song from a URI](HowTo_PlaySongfromURI.md)
65+
66+
Demonstrates how to use the [MediaPlayer](xref:Microsoft.Xna.Framework.Media.MediaPlayer) to play a song from a Uniform Resource Identifier (URI).
67+
68+
* [Media Overview](../../whatis/WhatIs_Audio.md)
69+
70+
Provides a high-level overview about the capabilitiessuch as playing music and video and accessing picturesof the Media API in MonoGame.
71+
72+
## Reference
73+
74+
* [MediaPlayer Class](xref:Microsoft.Xna.Framework.Media.MediaPlayer)
75+
76+
Provides methods and properties to play, pause, resume, and stop songs. **MediaPlayer** also exposes shuffle, repeat, volume, play position, and visualization capabilities.

articles/getting_to_know/howto/audio/HowTo_PlayASound.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ But you can use your own if you wish.
5050
> [!NOTE]
5151
> Obviously you would not normally call `play` in the `LoadContent` method, we just use here as an example.
5252

53-
## Concepts
53+
## See Also
5454

5555
* [Looping a Sound](HowTo_LoopASound.md)
5656

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
---
2+
title: How to record Sound with a Microphone
3+
description: This topic demonstrates the basics of recording audio using a microphone.
4+
requireMSLicense: true
5+
---
6+
7+
## Overview
8+
9+
The following procedure shows the bare basics of using the microphone to record audio. In practice, you will want to use UI elements (such as buttons) to start and stop audio recording.
10+
11+
As handling microphone audio can become quite complex, this tutorial is split into two halves:
12+
13+
* Part 1 walks through the required code in order to record audio from the default microphone and then stop recording.
14+
* Part 2 is a more in-depth sample which also processes the recorded audio and plays it back to you.
15+
16+
## Part 1, Guide to recording audio from the microphone
17+
18+
1. Get the device's default microphone by using [Microphone.Default](xref:Microsoft.Xna.Framework.Audio.Microphone).
19+
20+
```csharp
21+
Microphone activeMicrophone;
22+
23+
activeMicrophone = Microphone.Default;
24+
if (activeMicrophone != null)
25+
{
26+
27+
}
28+
else
29+
{
30+
// No microphone is attached to the device
31+
}
32+
```
33+
34+
2. Set the size (duration) of the Microphone buffer and wire up the event to handle the data as it becomes available. THen declare a buffer array to hold the audio data from these settings.
35+
36+
```csharp
37+
// Set the capture buffer size for low latency.
38+
// Microphone will call the game back when it has captured at least that much audio data.
39+
activeMicrophone.BufferDuration = TimeSpan.FromMilliseconds(100);
40+
41+
// Subscribe to the event that's raised when the capture buffer is filled.
42+
activeMicrophone.BufferReady += BufferReady;
43+
44+
// We will put the mic samples in this buffer. We only want to allocate it once.
45+
micSamples = new byte[activeMicrophone.GetSampleSizeInBytes(activeMicrophone.BufferDuration)];
46+
```
47+
48+
3. The `Buffer Ready` handler can then process the prepared microphone data for storage, transmission or playback, getting the sample data from the Microphone and storing it in the prepared buffer array (micSamples).
49+
50+
```csharp
51+
/// <summary>
52+
/// This is called each time a microphone buffer has been filled.
53+
/// </summary>
54+
void BufferReady(object sender, EventArgs e)
55+
{
56+
try
57+
{
58+
// Copy the captured audio data into the pre-allocated array.
59+
int sampleDataSize = activeMicrophone.GetData(micSamples);
60+
}
61+
catch (NoMicrophoneConnectedException)
62+
{
63+
// Microphone was disconnected - let the user know.
64+
}
65+
}
66+
```
67+
68+
4. Begin recording audio, calling method [Microphone.Start](xref:Microsoft.Xna.Framework.Audio.Microphone).
69+
70+
```csharp
71+
private void StartRecording()
72+
{
73+
// Can't start a microphone that doesn't exist.
74+
if (activeMicrophone == null) { return; }
75+
76+
try
77+
{
78+
activeMicrophone.Start();
79+
}
80+
catch (NoMicrophoneConnectedException)
81+
{
82+
// Microphone was disconnected - let the user know.
83+
}
84+
}
85+
```
86+
87+
5. While the microphone is recording, [Microphone.GetData (Byte\[\], Int32, Int32)](xref:Microsoft.Xna.Framework.Audio.Microphone) is called in the aforementioned `BufferReady` handler as the audio data becomes available.
88+
89+
```csharp
90+
int sampleDataSize = activeMicrophone.GetData(micSamples);
91+
```
92+
93+
6. When you are finished recording, set the microphone state to stopped by calling method [Stop](xref:Microsoft.Xna.Framework.Audio.Microphone).
94+
95+
```csharp
96+
private void StopRecording()
97+
{
98+
// Can't stop a microphone that doesn't exist.
99+
if (activeMicrophone == null) { return; }
100+
101+
try
102+
{
103+
// Stop the microphone
104+
activeMicrophone.Stop();
105+
}
106+
catch (NoMicrophoneConnectedException)
107+
{
108+
UpdateMicrophoneStatus();
109+
}
110+
}
111+
```
112+
113+
## Part 2, Full example of outputting the microphone to audio
114+
115+
As promised, the following is a fully working sample that:
116+
117+
* Starts recording and outputting audio from the active microphone when "space is pressed".
118+
* Stops recording when "P" is pressed.
119+
* Adds an "Extension method" to determine if a Microphone is connected.
120+
* A "PickFirstConnectedMicrophone" method to choose a specific microphone. Can be modified so you can switch mic's
121+
* An "InitializeMicrophone" method to initialize and setup the microphone, updated to work in "Update" so it can continually monitor the Mic connection.
122+
123+
Enjoy, all code commented where applicable for reference.
124+
125+
[!code-csharp[](files/microphonesample.cs)]
126+
127+
## See Also
128+
129+
* [Working with Microphones](HowTo_Microphone.md)
130+
131+
Provides basic information about microphone usage in games.
132+
133+
* [HowTo_StreamDataFromWav](HowTo_StreamDataFromWav.md)
134+
135+
a sample demonstrating the uses of the [DynamicSoundEffectInstance](xref:Microsoft.Xna.Framework.Audio.DynamicSoundEffectInstance)
136+
137+
## Reference
138+
139+
* [Microphone](xref:Microsoft.Xna.Framework.Audio.Microphone)
140+
141+
Provides properties, methods, and fields and events for capturing audio data with microphones.
142+
143+
* [DynamicSoundEffectInstance](xref:Microsoft.Xna.Framework.Audio.DynamicSoundEffectInstance)
144+
145+
Provides properties, methods, and events for play back of the audio buffer.

0 commit comments

Comments
 (0)