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