Author Topic: Spectrum amplitudes per time, in a 16 bands diagram  (Read 189 times)

manteltraeger

  • Posts: 1
Hi,

actually itís my first post here. For my purposes I need to get the spectrum of an mp3-file. I want to load the spectrum it in the cache, to process it later.
My Problem is, that I have 2 mp3ís which are almost the same, just one file has a little bit more silence in the beginning the other one has some more silence in the end, on else they are the same. The length of the songs differs about 0.2 seconds. When I process the cached data I get completely different spectrums. How do I get the right spectrum amplitudes per time, in a 16 bands diagram?
Thatís my code:

Code: [Select]
Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero);

            int bassStream = Bass.BASS_StreamCreateFile(filename, 0L, 0L, BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_SAMPLE_FLOAT);

            if (bassStream != 0)
            {
                int BUFFER_SIZE = 1024;
                int BANDS = 16;
                int SPECHEIGHT = 255;

                IList<int>[] spectrumData = new IList<int>[BANDS];
                var fft = new float[BUFFER_SIZE];

                while (Bass.BASS_ChannelIsActive(bassStream) == BASSActive.BASS_ACTIVE_PLAYING)
                {
                    int band, y;
                    Bass.BASS_ChannelGetData(bassStream, fft, (int)(BASSData.BASS_DATA_FFT2048));

                    int b0 = 0;

                    for (band = 0; band < BANDS; band++)
                    {
                        float peak = 0;
                        int b1 = (int)Math.Round(Math.Pow(2, band * 10.0 / (BANDS - 1)), 0);
                        if (b1 > 1023)
                        {
                            b1 = 1023;
                        }

                        if (b1 <= b0)
                        {
                            b1 = b0 + 1; // make sure it uses at least 1 FFT bin
                        }

                        for (; b0 < b1; b0++)
                        {
                            if (peak < fft[1 + b0])
                            {
                                peak = fft[1 + b0];
                            }
                        }

                        y = (int)Math.Round(Math.Sqrt(peak) * 3 * SPECHEIGHT, 0);

                        if (y < 0) y = 0;
                        if (y > SPECHEIGHT) y = SPECHEIGHT; // cap it

                        if (spectrumData[band] == null)
                            spectrumData[band] = new List<int>();

                        spectrumData[band].Add(y);
                    }
                }

                Bass.BASS_ChannelStop(bassStream);
                Bass.BASS_StreamFree(bassStream);

Any help appreciated, thx in advance, manteltraeger!