Reputation: 419
I'm streaming music from Spotify with C# wrapper ohLibSpotify and play it with NAudio. Now I'm trying to create a spectrum visualization for the data i receive.
When i get data from libspotify, following callback gets called:
public void MusicDeliveryCallback(SpotifySession session, AudioFormat format, IntPtr frames, int num_frames)
{
//handle received music data from spotify for streaming
//format: audio format for streaming
//frames: pointer to the byte-data in storage
var size = num_frames * format.channels * 2;
if (size != 0)
{
_copiedFrames = new byte[size];
Marshal.Copy(frames, _copiedFrames, 0, size); //Copy Pointer Bytes to _copiedFrames
_bufferedWaveProvider.AddSamples(_copiedFrames, 0, size); //adding bytes from _copiedFrames as samples
}
}
Is it possible to analyze the data i pass to the BufferedWaveProvider to create a realtime visualization? And can somebody explain how?
Upvotes: 1
Views: 1155
Reputation: 4992
The standard tool for transforming time-domain signals like audio samples into a frequency domain information is the Fourier transform.
Grab the fast Fourier transform library of your choice and throw it at your data; you will get a decomposition of the signal into its constituent frequencies. You can then take that data and visualize however you like. Spectrograms are particularly easy; you just need to plot the magnitude of each frequency component versus the frequency and time.
Upvotes: 1