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)

Syntax

public static void GetVisualizationData (
         VisualizationData visualizationData
)

Parameters

  • visualizationData
    Type: VisualizationData
    Visualization (frequency and sample) data for the currently playing song.

Remarks

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.

Dd254788.note(en-us,XNAGameStudio.41).gifWindows Phone Specific Information

Visualization data is not available on Windows Phone. Any attempt to get visualization data on the phone will populate the VisualizationData class with 0s.

The Complete Sample

The code in the topic shows you the technique. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

Download Spectrum.zip.

Example

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();

See Also

Reference

MediaPlayer Class
MediaPlayer Members
Microsoft.Xna.Framework.Media Namespace

Platforms

Xbox 360, Windows 7, Windows Vista, Windows XP, Windows Phone