This documentation is archived and is not being maintained.

MediaPlayer.GetVisualizationData Method

Retrieves visualization (frequency and sample) data for the currently-playing song.

Namespace: Microsoft.Xna.Framework.Media
Assembly: Microsoft.Xna.Framework (in microsoft.xna.framework.dll)

public static void GetVisualizationData (
         VisualizationData visualizationData
)

Parameters

visualizationData
Visualization (frequency and sample) data for the currently playing song.

Visualization data can be used to create visualizers, which are graphical representations of sound. For example, a simple visualizer might be an equalizer spectrum display that shows the current power level for each frequency band.

The frequency data is a collection of float values. Each element corresponds to a frequency band, ranging from 20Hz to 20KHz. In the collection, the distribution of bands from 20Hz to 20KHz is logarithmic, not linear. This means that elements at the lower end of the spectrum represent a smaller frequency range than those at the upper end of the spectrum.

Each value in the frequency collection is a normalized float value from 0.0f to 1.0f, and is the logarithmic scaled power level for that frequency band. Frequency is perceived as pitch.

Information returned from the GetVisualizationData method can determine if a sound is more of a higher pitch, or more of a lower pitch. This information can be useful when determining how the sounds might drive game play.

The following example demonstrates using frequency and sample data to display the frequency power spectrum and waveform of the currently playing song. The program display is in landscape mode.

       if (MediaPlayer.State == MediaState.Playing)
       {
           MediaPlayer.GetVisualizationData(visData);
       }
...
           spritebatch.Begin();
           for (int y = 0; y < arraySize; y++)
           {
               // Draw frequency spectrum display.
               spritebatch.Draw(
                   line,
                   new Vector2((float)((-1.0 + visData.Frequencies[y]) * width + 1), y),
                   Color.White);

               // Draw waveform from samples.
               spritebatch.Draw(
                   line,
                   new Vector2((float)(visData.Samples[y] * width / 4 + width * 3 / 4), y),
                   sampleLine,
                   Color.White);
           }
           spriteBatch.End();

Xbox 360, Windows XP SP2, Windows Vista, Zune
Show: