XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

[XNA 3.0 CTP] GetVisualizationData()

Last post 6/4/2008 10:06 PM by Embassy Alpha. 4 replies.
  • 5/27/2008 10:59 PM

    [XNA 3.0 CTP] GetVisualizationData()

    I have a question about the GetVisualizationData function in the MediaPlayer class. This function returns two 256 float arrays. One representing the frequency and one for Samples of the Wave.

    Here is an example of an attempt to scan the frequencies of an entire song.

    bool bScanning = true;

                TimeSpan total_span = TimeSpan.Zero;

                MediaPlayer.Stop(); 

     

                while (bScanning)

                {

                   

                    // Get visualization data for this frame.

                    MediaPlayer.GetVisualizationData(frequencies, samples);

                   

                    // Using the output window to watch frequency buffer

                    // (using 5 as the index for lookup into frequency range).

                    System.Diagnostics.Debug.Write(frequencies[5]);

                    System.Diagnostics.Debug.Write("  ");

     

                    // add 26 milliseconds. I have heard that a frame in an MPEG-1 file lasts for 26ms.

                    total_span += delta;

     

                    // I have to resume the song in order to get the frequency data,

                    // If I don't I get zeros in the frequency buffer.

                    MediaPlayer.Resume();

                    // Advance song forward one frame.

                    MediaPlayer.PlayPosition = total_span;

                    // Pause the song right away so that we are still in the frame

                    // for the next GetVisualizationData() call.

                    MediaPlayer.Pause();

     

                    // Break out if the song is done scanning.

                    if (MediaPlayer.PlayPosition >= song.Duration)

                        bScanning = false;

                   

                }

     I am currently using 26 milliseconds to skip a head every time I want to read the frequencies. I do this because I heard that a frame in an MPEG-1 file lasts for 26ms. If 26 milliseconds is the correct time that a single frame of an mp3 lasts, then if I use a 13 millisecond delta shouldn't I start getting duplicate frequencies in my output window, since I will be sampling the frequency buffer twice per mp3 frame? I checked this, and this is not the case. I end up getting different frequencies when I sample with 13 milliseconds instead of 26 milliseconds. My end goal is to be able to sample the frequencies for every single frame (or step) of a song. Does anybody know a way that this can be done?

     

  • 5/28/2008 12:01 AM In reply to

    Re: [XNA 3.0 CTP] GetVisualizationData()

    The visualization data is read while the music is playing, as part of the same process that actually decodes and submits the audio to the output sound hardware. I don't think this will work if you are jumping back and forth through the file, as it won't be getting a steady stream of incoming audio to process. You need to give the audio some time to play in order for the processor to see the necessary data, so I don't think it will be possible to analyze an entire file faster than realtime like you are trying to do here.
    XNA Framework Developer - blog - homepage
  • 6/4/2008 8:38 PM In reply to

    Re: [XNA 3.0 CTP] GetVisualizationData()

    Would it be possible to pre-process an entire soundfile by any other means. I guess it might be possible to write a custom Content Processor to do this. However that would mean that only music converted into .xnb and included with the project would work. I would like to be able to use the music already on someone's Zune.
  • 6/4/2008 8:50 PM In reply to

    Re: [XNA 3.0 CTP] GetVisualizationData()

    You could probably write either a second game or a second part of your game that would do this processing. Then simply save out all the data to a file on the Zune. Then your game can simply reference those files for the data. Maybe make a "game" that simply runs over all (or some probably to save space) and churns out these files. Then just instruct users to run that game first to generate the data files for the songs they want.
  • 6/4/2008 10:06 PM In reply to

    Re: [XNA 3.0 CTP] GetVisualizationData()

    I was thinking of doing something along those lines, as long as the user is patient enough to wait an entire song's length. I was hoping to be able to do something like Audiosurf which calculates the song data quite quickly.

    I was playing around with skipping through the song and sampling only 256 times. Which only took 15 seconds to calculate. I found that if I called GetVisualizationData() right after Mediaplayer.PlayPosition I would get junk data that would be random each time. However if gave it a little bit of time between when I set the Playposition and when I called GetVisualizationData() I would get consistent data each time. However I don't know if the information was very useable, since I was just skipping though the song the AudioBuffer was probably getting filled differently than if I was reading it in Real-Time.

    The Ipod game Phase was able to read a user's songs. However the way they did it was that you had to add the songs to a specific playlist in iTunes and then the calculations were done on the computer side before being moved onto the IPod.

Page 1 of 1 (5 items) Previous Next