Exemple #1
0
        public float[] getLevelAt(double seconds, double duration)
        {
            if (levelStream == 0 && File != "")
            {
                levelStream = Bass.BASS_StreamCreateFile(File, 0, 0, BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_SAMPLE_FLOAT | BASSFlag.BASS_STREAM_PRESCAN);
                if (levelStream == 0)
                {
                    return(new float[2]);
                }
            }

            Bass.BASS_ChannelSetPosition(levelStream, seconds);
            float[] levels = new float[2];
            return(Bass.BASS_ChannelGetLevels(levelStream, (float)duration, BASSLevel.BASS_LEVEL_STEREO));
        }
        private void waveformGenerateWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            WaveformGenerationParams waveformParams = e.Argument as WaveformGenerationParams;
            int  stream         = Bass.BASS_StreamCreateFile(waveformParams.Path, 0, 0, BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_SAMPLE_FLOAT | BASSFlag.BASS_STREAM_PRESCAN);
            int  frameLength    = (int)Bass.BASS_ChannelSeconds2Bytes(stream, 0.02);
            long streamLength   = Bass.BASS_ChannelGetLength(stream, 0);
            int  frameCount     = (int)((double)streamLength / (double)frameLength);
            int  waveformLength = frameCount * 2;

            float[] waveformData = new float[waveformLength];
            float[] levels       = new float[2];

            int actualPoints = Math.Min(waveformParams.Points, frameCount);

            int compressedPointCount = actualPoints * 2;

            float[]    waveformCompressedPoints = new float[compressedPointCount];
            List <int> waveMaxPointIndexes      = new List <int>();

            for (int i = 1; i <= actualPoints; i++)
            {
                waveMaxPointIndexes.Add((int)Math.Round(waveformLength * ((double)i / (double)actualPoints), 0));
            }

            float maxLeftPointLevel  = float.MinValue;
            float maxRightPointLevel = float.MinValue;
            int   currentPointIndex  = 0;

            for (int i = 0; i < waveformLength; i += 2)
            {
                levels              = Bass.BASS_ChannelGetLevels(stream);
                waveformData[i]     = levels[0];
                waveformData[i + 1] = levels[1];

                if (levels[0] > maxLeftPointLevel)
                {
                    maxLeftPointLevel = levels[0];
                }
                if (levels[1] > maxRightPointLevel)
                {
                    maxRightPointLevel = levels[1];
                }

                if (i > waveMaxPointIndexes[currentPointIndex])
                {
                    waveformCompressedPoints[(currentPointIndex * 2)]     = maxLeftPointLevel;
                    waveformCompressedPoints[(currentPointIndex * 2) + 1] = maxRightPointLevel;
                    maxLeftPointLevel  = float.MinValue;
                    maxRightPointLevel = float.MinValue;
                    currentPointIndex++;
                }
                if (i % 3000 == 0)
                {
                    float[] clonedData = (float[])waveformCompressedPoints.Clone();
                    Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        WaveformData = clonedData;
                    }));
                }

                if (waveformGenerateWorker.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                    ;
                }
            }
            float[] finalClonedData = (float[])waveformCompressedPoints.Clone();
            Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                fullLevelData = waveformData;
                WaveformData  = finalClonedData;
            }));
            Bass.BASS_StreamFree(stream);
        }