public void Reset() { if (_average == null) { return; } _average.Reset(); this.value.Invoke(_average.Value()); }
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; }
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; }