示例#1
0
    public int ReadAudio(float[] data, int dstIdx, int readlength, int targetChannels, SourceChannel channelType, bool modulate = true)
    {
        int length           = 0;
        int DataLength       = readlength;
        int srcChannelsCount = 2;

        int channelIndex = 0;
        int stepSize     = 1;

        while (length < DataLength && startIndex < samples.Length)
        {
            srcChannelsCount = channels;
            if (channels == 2 && channelType != SourceChannel.Both)
            {
                srcChannelsCount = 1;
                stepSize         = 2;
                channelIndex     = (channelType == SourceChannel.Left ? 0 : 1);
            }

            //calculate the left amount of data in this packet
            int sz = samples.Length - startIndex;
            //determine the amount of data we going to use of this packet
            int count = (int)Mathf.Clamp(sz, 0,
                                         readlength - length);//Remaining data to be filled

            GstNetworkAudioPlayer.ProcessAudioPackets(samples, startIndex, channelIndex, count, stepSize, srcChannelsCount, data, (dstIdx + length), channels, modulate);

            startIndex += count;
            length     += count;
        }
        return(length);
    }
示例#2
0
    void ReadAudio(float[] data, int channels, bool modulate)
    {
        if (!Player.IsStarted() ||
            _paused)
        {
            return;
        }
        uint  length           = 0;
        int   timeout          = 0;
        float average          = 0;
        uint  DataLength       = (uint)data.Length;
        uint  srcChannelsCount = 2;
        uint  targetLength     = DataLength;

        uint channelIndex = 0;
        uint stepSize     = 1;

        while (length < DataLength)
        {
            AudioSamples p;

            lock (_dataMutex) {
                p = GetExistingPacket();
            }
            if (p == null)
            {
                if (!_Process())
                {
                    WaitCount++;
                    ++timeout;
                    if (timeout > 20)
                    {
                        break;
                    }
                }
                else
                {
                    timeout = 0;
                }
                continue;
            }

            srcChannelsCount = (uint)p.channels;
            if (srcChannelsCount == 2 && this.Channel != SourceChannel.Both)
            {
                srcChannelsCount = 1;
                stepSize         = 2;
                channelIndex     = (uint)(this.Channel == SourceChannel.Left ? 0 : 1);
            }

            //calculate the left amount of data in this packet
            uint sz = (uint)Mathf.Max(0, p.samples.Length - p.startIndex);
            //determine the amount of data we going to use of this packet
            uint count = (uint)Mathf.Min(sz,
                                         Mathf.Max(0, data.Length - length) /*Remaining data to be filled*/);

            average += GstNetworkAudioPlayer.ProcessAudioPackets(p.samples, (int)p.startIndex, (int)channelIndex, (int)count, (int)stepSize, (int)srcChannelsCount, data, (int)length, (int)channels, modulate);

            lock (_dataMutex) {
                if (count + p.startIndex < p.samples.Length)
                {
                    p.startIndex = (int)(count + p.startIndex);
                    _packets.Insert(0, p);
                }
                else
                {
                    RemovePacket(p);
                }
            }
            length += count;
        }

        average /= (float)targetLength;
        _movingAverage.Add(average, 0.5f);
        averageAudio = Mathf.Sqrt(_movingAverage.Value());         //20*Mathf.Log10(Mathf.Sqrt(_movingAverage.Value ()));// (_movingAverage.Value ());
        //if(averageAudio<-100)averageAudio=-100;
    }
示例#3
0
        public void ReadAudio(float[] data, int channels, bool block)
        {
            uint  length           = 0;
            int   timeout          = 0;
            float average          = 0;
            uint  DataLength       = (uint)data.Length;
            uint  srcChannelsCount = 2;
            uint  targetLength     = DataLength;

            uint channelIndex = 0;
            uint stepSize     = 1;

            while (length < DataLength)
            {
                AudioSamples p;

                lock (this) {
                    p = GetExistingPacket();
                }
                if (p == null)
                {
                    if (block && timeout < 20)
                    {
                        ++timeout;
                        continue;
                    }
                    else
                    {
                        break;
                    }
                }

                srcChannelsCount = p.channels;
                if (srcChannelsCount == 2 && this.channel != SourceChannel.Both)
                {
                    srcChannelsCount = 1;
                    stepSize         = 2;
                    channelIndex     = (uint)(this.channel == SourceChannel.Left ? 0 : 1);
                }

                //calculate the left amount of data in this packet
                uint sz = (uint)Mathf.Max(0, p.samples.Length - p.startIndex);
                //determine the amount of data we going to use of this packet
                uint count = (uint)Mathf.Min(sz,
                                             Mathf.Max(0, data.Length - length) /*Remaining data to be filled*/);

                GstNetworkAudioPlayer.ProcessAudioPackets(p.samples, (int)p.startIndex, (int)channelIndex, (int)count, (int)stepSize, (int)srcChannelsCount, data, (int)length, (int)channels);

                lock (this) {
                    if (count + p.startIndex < p.samples.Length)
                    {
                        p.startIndex = count + p.startIndex;
                        Insert(0, p);
                    }
                    else
                    {
                        Owner.RemovePacket(p);
                    }
                }
                length += count;
            }
        }
示例#4
0
    void ReadAudio(float[] data, int channels)
    {
        if (!Player.IsLoaded() || !Player.IsPlaying() ||
            _paused)
        {
            return;
        }
        int   length           = 0;
        int   timeout          = 0;
        float average          = 0;
        int   DataLength       = data.Length;
        int   srcChannelsCount = 2;
        int   targetLength     = DataLength;

        int channelIndex = 0;
        int stepSize     = 1;

        while (length < DataLength)
        {
            AudioPacket p;

            lock (_dataMutex) {
                p = GetExistingPacket();
            }
            if (p == null)
            {
                if (!_Process())
                {
                    WaitCount++;
                    ++timeout;
                    if (timeout > 20)
                    {
                        break;
                    }
                }
                else
                {
                    timeout = 0;
                }
                continue;
            }

            srcChannelsCount = p.channelsCount;
            if (srcChannelsCount == 2 && this.Channel != SourceChannel.Both)
            {
                srcChannelsCount = 1;
                stepSize         = 2;
                channelIndex     = (this.Channel == SourceChannel.Left ? 0 : 1);
            }

            /*
             * if (channels == 2 && srcChannelsCount == 1 )
             *      targetLength = DataLength / 2;
             * else
             *      targetLength = DataLength;*/

            //calculate the left amount of data in this packet
            int sz = Mathf.Max(0, p.data.Length - p.startIndex);
            //determine the amount of data we going to use of this packet
            int count = Mathf.Min(sz,
                                  Mathf.Max(0, data.Length - length) /*Remaining data to be filled*/);

            /*
             * if (channels == srcChannelsCount) {
             *      for (int i = 0,j=0; i < count; i+=stepSize,++j) {
             *              data [j + length] *= p.data [p.startIndex + i+channelIndex]*Volume;
             *              average += p.data [p.startIndex + i+channelIndex]*p.data [p.startIndex + i+channelIndex];
             *      }
             * } else if (channels == 2 && srcChannelsCount == 1) {
             *      for (int i = 0,j=0; i < count;) {
             *              data [2*j + length] *= p.data [p.startIndex + i+channelIndex]*Volume;
             *              data [2*j+ length + 1] *= p.data [p.startIndex + i+channelIndex]*Volume;
             *              average += p.data [p.startIndex + i+channelIndex]*p.data [p.startIndex + i+channelIndex];
             *              i += stepSize;
             *              j++;
             *      }
             * } else if (channels == 1 && srcChannelsCount == 2) {
             *      for (int i = 0; i < count; i++) {
             *              data [i + length] *= p.data [p.startIndex + 2 * i]*Volume;
             *              average += p.data [p.startIndex + 2 *i]*p.data [p.startIndex + 2 *i];
             *      }
             * }*/

            average += GstNetworkAudioPlayer.ProcessAudioPackets(p.data, p.startIndex, channelIndex, count, stepSize, srcChannelsCount, data, length, channels);

            lock (_dataMutex) {
                if (count + p.startIndex < p.data.Length)
                {
                    p.startIndex = count + p.startIndex;
                    _packets.Insert(0, p);
                }
                else
                {
                    RemovePacket(p);
                }
            }
            length += count;
        }

        average /= (float)targetLength;
        _movingAverage.Add(average, 0.5f);
        averageAudio = Mathf.Sqrt(_movingAverage.Value());         //20*Mathf.Log10(Mathf.Sqrt(_movingAverage.Value ()));// (_movingAverage.Value ());
        //if(averageAudio<-100)averageAudio=-100;
    }