Exemple #1
0
    void loadData()
    {
        if (filepath != string.Empty && File.Exists(filepath))
        {
#if BASS_AUDIO
            long        trackLengthInBytes = Bass.BASS_ChannelGetLength(handle);
            const float FRAME_TIME         = 0.002f;
            long        frameLengthInBytes = Bass.BASS_ChannelSeconds2Bytes(handle, FRAME_TIME);
            int         NumFrames          = (int)System.Math.Round(1f * trackLengthInBytes / frameLengthInBytes);

            _data = new float[NumFrames * 2];

            float[] levels = new float[2];
            for (int i = 0; i < _data.Length && !stop; i += 2)
            {
                Bass.BASS_ChannelGetLevel(handle, levels, FRAME_TIME, BASSLevel.BASS_LEVEL_STEREO);
                float average = (levels[0] + levels[1]) / 2.0f;
                _data[i]     = -average;
                _data[i + 1] = average;
            }
#else
            byte[]  bytes      = File.ReadAllBytes(filepath);
            float[] sampleData = new float[0];
            switch (Path.GetExtension(filepath))
            {
            case (".ogg"):
                NVorbis.VorbisReader vorbis = new NVorbis.VorbisReader(filepath);
                vorbis.ClipSamples = false;

                _data = new float[vorbis.TotalSamples * vorbis.Channels];

                //vorbis.ReadSamples(_data, 0, _data.Length);

                int count = 0;
                while ((count += vorbis.ReadSamples(_data, count, 16000)) > 0 && !stop)
                {
                }

                break;

            case (".wav"):
                WAV wav = new WAV(bytes);
                sampleData = NAudioPlayer.InterleaveChannels(wav);
                break;

            case (".mp3"):
                NAudioPlayer.WAVFromMp3Data(bytes, out sampleData);
                break;

            default:
                return;
            }
#endif
            if (!stop)
            {
#if !BASS_AUDIO
                _data = sampleData;
#endif

                Debug.Log("Sample length: " + _data.Length);
            }
            else
            {
                _data = new float[0];
            }
        }
    }