C# - Получение FFT из MP3 файла - GetFFTData

public List<float[]> GetFFTData(string filename, FFTSize size, float fps, int maxwidth, int startscan=0)
        {
            Un4seen.Bass.BASSData FFTType = Un4seen.Bass.BASSData.BASS_DATA_FFT256;
            int arlen = 0;
            if (size == FFTSize.FFT128) FFTType = Un4seen.Bass.BASSData.BASS_DATA_FFT256;
            if (size == FFTSize.FFT256) FFTType = Un4seen.Bass.BASSData.BASS_DATA_FFT512;
            if (size == FFTSize.FFT512) FFTType = Un4seen.Bass.BASSData.BASS_DATA_FFT1024;
            if (size == FFTSize.FFT1024) FFTType = Un4seen.Bass.BASSData.BASS_DATA_FFT2048;
            if (size == FFTSize.FFT2048) FFTType = Un4seen.Bass.BASSData.BASS_DATA_FFT4096;
            if (size == FFTSize.FFT4096) FFTType = Un4seen.Bass.BASSData.BASS_DATA_FFT8192;
            if (size == FFTSize.FFT8192) FFTType = Un4seen.Bass.BASSData.BASS_DATA_FFT16384;
            arlen=(int)size;
            int len = (int)FFTType;
 
            GC.Collect();
            time = 0;
            Data.Clear();
            Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_NOSPEAKER, IntPtr.Zero);
            // create the stream
            int chan = Bass.BASS_StreamCreateFile(filename, 0, 0,
                              BASSFlag.BASS_SAMPLE_FLOAT  | BASSFlag.BASS_STREAM_DECODE);
            Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_UPDATEPERIOD, 0);
            var pos = Bass.BASS_ChannelGetLength(chan);
            var p = Bass.BASS_ChannelBytes2Seconds (chan, pos);
            //Console.Write("ScanFile");
            //Console.Clear();
            long byte_pos=0;
            int n = 0;
            while (byte_pos<pos)
            {
                n++;
 
                if (Data.Count > maxwidth)
                {
                    break;
                }
                else if(n>startscan)
                {
                    byte_pos = Bass.BASS_ChannelSeconds2Bytes(chan, time);
                    Bass.BASS_ChannelSetPosition(chan, byte_pos, BASSMode.BASS_POS_BYTES);
                    float[] fft = new float[arlen];
                    Bass.BASS_ChannelGetData(chan, fft, len);
                    Data.Add(fft);
                }
                time += 1F / fps;
            }
            Bass.BASS_ChannelPause(chan);
            Bass.BASS_Stop();
            Bass.BASS_StreamFree(chan);
            GC.Collect();
            return Data;
        }
Отправить комментарий

Популярные сообщения