private bool OnNewWaveformData(WaveformStreamer streamer, float[] data, int remaining)
        {
            StreamedAudioClipPreview.StreamingContext streamingContext = this.m_Contexts[streamer];
            int num = streamingContext.index / this.m_Clip.channels;

            for (int i = 0; i < this.m_StreamedSegments.Length; i++)
            {
                if (this.m_StreamedSegments[i].streamer == streamer && num >= this.m_StreamedSegments[i].streamingIndexOffset && this.m_StreamedSegments[i].segmentLength > num - this.m_StreamedSegments[i].streamingIndexOffset)
                {
                    this.SetMMWaveData((this.m_StreamedSegments[i].textureOffset - this.m_StreamedSegments[i].streamingIndexOffset) * this.m_Clip.channels + streamingContext.index, data);
                }
            }
            streamingContext.index += data.Length / 2;
            return(remaining != 0);
        }
        bool OnNewWaveformData(WaveformStreamer streamer, float[] data, int remaining)
        {
            StreamingContext c = m_Contexts[streamer];

            int pixelPos = c.index / m_Clip.channels;

            for (var i = 0; i < m_StreamedSegments.Length; i++)
            {
                if (m_StreamedSegments[i].streamer == streamer && pixelPos >= m_StreamedSegments[i].streamingIndexOffset && m_StreamedSegments[i].segmentLength > (pixelPos - m_StreamedSegments[i].streamingIndexOffset))
                {
                    SetMMWaveData((m_StreamedSegments[i].textureOffset - m_StreamedSegments[i].streamingIndexOffset) * m_Clip.channels + c.index, data);
                }
            }

            c.index += data.Length / 2;

            return(remaining != 0);
        }
示例#3
0
 internal static extern IntPtr Internal_WaveformStreamerCreate(WaveformStreamer instance, [NotNull] AudioClip clip, double start, double duration,
                                                               int numOutputSamples, [NotNull] Func <WaveformStreamer, float[], int, bool> onNewWaveformData);
        private StreamedAudioClipPreview.Segment[] CalculateAndStartStreamers(double localStart, double localLength)
        {
            double num = localStart;

            localStart %= (double)this.m_Clip.length;
            double num2 = localLength / (double)base.Size.x;

            StreamedAudioClipPreview.Segment[] result;
            if (!base.looping)
            {
                if (num > (double)this.m_Clip.length)
                {
                    result = null;
                }
                else
                {
                    double num3 = Math.Min((double)this.m_Clip.length - num, localLength);
                    int    num4 = (int)Math.Min((double)base.Size.x, (double)base.Size.x * Math.Max(0.0, num3 / localLength));
                    if (num4 < 1)
                    {
                        result = null;
                    }
                    else
                    {
                        StreamedAudioClipPreview.Segment[] array = new StreamedAudioClipPreview.Segment[1];
                        array[0].streamer             = new WaveformStreamer(this.m_Clip, num, num3, num4, new Func <WaveformStreamer, float[], int, bool>(this.OnNewWaveformData));
                        array[0].segmentLength        = (int)base.Size.x;
                        array[0].textureOffset        = 0;
                        array[0].streamingIndexOffset = 0;
                        result = array;
                    }
                }
            }
            else
            {
                StreamedAudioClipPreview.Segment[] array;
                if (localStart + localLength - num2 > (double)this.m_Clip.length)
                {
                    double num5 = (double)base.Size.x / localLength;
                    if (localLength >= (double)this.m_Clip.length)
                    {
                        double           num6     = localLength / (double)this.m_Clip.length;
                        WaveformStreamer streamer = new WaveformStreamer(this.m_Clip, 0.0, (double)this.m_Clip.length, (int)((double)base.Size.x / num6), new Func <WaveformStreamer, float[], int, bool>(this.OnNewWaveformData));
                        double           num7     = (double)this.m_Clip.length - localStart;
                        double           num8     = 0.0;
                        array = new StreamedAudioClipPreview.Segment[Mathf.CeilToInt((float)((localStart + localLength) / (double)this.m_Clip.length))];
                        for (int i = 0; i < array.Length; i++)
                        {
                            double num9 = Math.Min(num7 + num8, localLength) - num8;
                            array[i].streamer             = streamer;
                            array[i].segmentLength        = (int)(num9 * num5);
                            array[i].textureOffset        = (int)(num8 * num5);
                            array[i].streamingIndexOffset = (int)(((double)this.m_Clip.length - num7) * num5);
                            num8 += num7;
                            num7  = (double)this.m_Clip.length;
                        }
                    }
                    else
                    {
                        double num10 = (double)this.m_Clip.length - localStart;
                        double num11 = localLength - num10;
                        array                         = new StreamedAudioClipPreview.Segment[2];
                        array[0].streamer             = new WaveformStreamer(this.m_Clip, localStart, num10, (int)(num10 * num5), new Func <WaveformStreamer, float[], int, bool>(this.OnNewWaveformData));
                        array[0].segmentLength        = (int)(num10 * num5);
                        array[0].textureOffset        = 0;
                        array[0].streamingIndexOffset = 0;
                        array[1].streamer             = new WaveformStreamer(this.m_Clip, 0.0, num11, (int)(num11 * num5), new Func <WaveformStreamer, float[], int, bool>(this.OnNewWaveformData));
                        array[1].segmentLength        = (int)(num11 * num5);
                        array[1].textureOffset        = (int)(num10 * num5);
                        array[1].streamingIndexOffset = 0;
                    }
                }
                else
                {
                    array                         = new StreamedAudioClipPreview.Segment[1];
                    array[0].streamer             = new WaveformStreamer(this.m_Clip, localStart, localLength, (int)base.Size.x, new Func <WaveformStreamer, float[], int, bool>(this.OnNewWaveformData));
                    array[0].segmentLength        = (int)base.Size.x;
                    array[0].textureOffset        = 0;
                    array[0].streamingIndexOffset = 0;
                }
                result = array;
            }
            return(result);
        }
        Segment[] CalculateAndStartStreamers(double localStart, double localLength)
        {
            Segment[] segments      = null;
            var       originalStart = localStart;

            // we don't care about the global position, only the locally visible offset into the clip
            localStart %= m_Clip.length;

            var secondsPerPixel = localLength / Size.x;

            if (!looping)
            {
                // holding (= !looping) is a special case handled before everything
                // else, because it's very simple to implement since it defines a
                // section capped by length of the clip

                if (originalStart > m_Clip.length)
                {
                    return(null);
                }

                var clampedLength         = Math.Min(m_Clip.length - originalStart, localLength);
                var previewPixelsToRender = (int)Math.Min(Size.x, Size.x * Math.Max(0, clampedLength / localLength));

                if (previewPixelsToRender < 1)
                {
                    return(null);
                }

                segments = new Segment[1];

                segments[0].streamer             = new WaveformStreamer(m_Clip, originalStart, clampedLength, previewPixelsToRender, OnNewWaveformData);
                segments[0].segmentLength        = (int)Size.x;
                segments[0].textureOffset        = 0;
                segments[0].streamingIndexOffset = 0;

                return(segments);
            }

            // epsilon added to discriminate between invisible floating point rounding errors
            // and actual loops (i.e. more than a single pixel larger than then length)
            if (localStart + localLength - secondsPerPixel > m_Clip.length)
            {
                var secondsToPixels = Size.x / localLength;

                // special case, the first part is clipped but at least one full length of the clip is available
                // we can then use one streamer to fill in all visible segments
                if (localLength >= m_Clip.length)
                {
                    var numberOfLoops = localLength / m_Clip.length;
                    var streamer      = new WaveformStreamer(m_Clip, 0, m_Clip.length, (int)(Size.x / numberOfLoops), OnNewWaveformData);

                    var currentClipSegmentPart = m_Clip.length - localStart;

                    var localPosition = 0.0;

                    segments = new Segment[Mathf.CeilToInt((float)((localStart + localLength) / m_Clip.length))];

                    for (int i = 0; i < segments.Length; ++i)
                    {
                        var cappedLength = Math.Min(currentClipSegmentPart + localPosition, localLength) - localPosition;
                        segments[i].streamer             = streamer;
                        segments[i].segmentLength        = (int)(cappedLength * secondsToPixels);
                        segments[i].textureOffset        = (int)(localPosition * secondsToPixels);
                        segments[i].streamingIndexOffset = (int)((m_Clip.length - currentClipSegmentPart) * secondsToPixels);

                        localPosition         += currentClipSegmentPart;
                        currentClipSegmentPart = m_Clip.length;
                    }
                }
                else
                {
                    // two disjoint regions, since streaming is time-continuous we have to split it up in two portions
                    var firstPart  = m_Clip.length - localStart;
                    var secondPart = localLength - firstPart;

                    segments = new Segment[2];

                    segments[0].streamer             = new WaveformStreamer(m_Clip, localStart, firstPart, (int)(firstPart * secondsToPixels), OnNewWaveformData);
                    segments[0].segmentLength        = (int)(firstPart * secondsToPixels);
                    segments[0].textureOffset        = 0;
                    segments[0].streamingIndexOffset = 0;

                    segments[1].streamer             = new WaveformStreamer(m_Clip, 0, secondPart, (int)(secondPart * secondsToPixels), OnNewWaveformData);
                    segments[1].segmentLength        = (int)(secondPart * secondsToPixels);
                    segments[1].textureOffset        = (int)(firstPart * secondsToPixels);
                    segments[1].streamingIndexOffset = 0;
                }
            }
            else
            {
                // handle single visible part of clip, that does not extend beyond the end
                // with a length less than a clip - equaling one streamer.
                segments = new Segment[1];

                segments[0].streamer             = new WaveformStreamer(m_Clip, localStart, localLength, (int)Size.x, OnNewWaveformData);
                segments[0].segmentLength        = (int)Size.x;
                segments[0].textureOffset        = 0;
                segments[0].streamingIndexOffset = 0;
            }

            return(segments);
        }
示例#6
0
 public void Stop()
 {
     WaveformStreamer.Internal_WaveformStreamerStop(this.m_Data);
 }
示例#7
0
 private WaveformStreamer(AudioClip clip, double start, double duration, int numOutputSamples, Func <object, float[], int, bool> onNewWaveformData)
 {
     this.m_Data = WaveformStreamer.Internal_WaveformStreamerCreateUntyped(this, clip, start, duration, numOutputSamples, onNewWaveformData);
 }