コード例 #1
0
    void LoadData()
    {
        if (!string.IsNullOrEmpty(filepath) && File.Exists(filepath))
        {
            audioStream = AudioManager.LoadStream(filepath);
            lock (dataLock)
            {
                _length = audioStream.ChannelLengthInSeconds();
            }

            long        trackLengthInBytes = audioStream.ChannelLengthInBytes();
            const float FRAME_TIME         = 0.002f;
            long        frameLengthInBytes = audioStream.ChannelSecondsToBytes(FRAME_TIME);
            int         NumFrames          = (int)System.Math.Round(1f * trackLengthInBytes / frameLengthInBytes);

            lock (dataLock)
            {
                _data = new float[NumFrames * 2];
            }

            float[] levels = new float[2];
            for (int i = 0; i < _data.Length && !stop; i += 2)
            {
                lock (dataLock)
                {
                    audioStream.GetChannelLevels(ref levels, FRAME_TIME);
                    float average = (levels[0] + levels[1]) / 2.0f;
                    _data[i]     = -average;
                    _data[i + 1] = average;
                }
            }

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