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); }
public Wrapper(GstNetworkAudioPlayer o) { _owner = o; }
void _initAudioPlayers() { _audioCreated = true; _configReceived = false; //Create audio playback if (AudioStream) { string audioPorts = ""; GstAudioPlayer.SourceChannel[] channels; if (!_isSpatialAudio) { channels = new GstAudioPlayer.SourceChannel[1] { GstAudioPlayer.SourceChannel.Both }; for (int i = AudioLocation.Count; i < _audioSourceCount; ++i) { AudioLocation.Add(Vector3.zero); } } else { //check number of audio locations for (int i = AudioLocation.Count; i < 2 * _audioSourceCount; ++i) { AudioLocation.Add(Vector3.zero); } channels = new GstAudioPlayer.SourceChannel[2] { GstAudioPlayer.SourceChannel.Right, GstAudioPlayer.SourceChannel.Left }; /* * // AudioLocation = new Vector3[_audioSourceCount * 2]; * float angle = 0; * float step = Mathf.Deg2Rad* 360.0f / (float)AudioLocation.Count; * float r = 0.1f; * for (int i = 0; i < AudioLocation.Count; ++i) { * AudioLocation [i] = new Vector3 (Mathf.Cos (angle)*r, 0, Mathf.Sin (angle)*r); * angle += step; * }*/ } int idx = 0; for (int i = 0; i < _audioSourceCount; ++i) { GstNetworkAudioPlayer aplayer; GstAudioPacketGrabber grabber; aplayer = new GstNetworkAudioPlayer(); aplayer.SetSampleRate(AudioSettings.outputSampleRate); aplayer.SetUseCustomOutput(true); int audioPort = Settings.Instance.GetPortValue("AudioPort", 0); string ip = Settings.Instance.GetValue("Ports", "ReceiveHost", _ifo.IP); aplayer.SetIP(ip, audioPort, false); aplayer.CreateStream(); aplayer.Play(); _audioPort = aplayer.GetAudioPort(); Debug.Log("Playing audio from port:" + _audioPort.ToString()); audioPorts += _audioPort.ToString(); if (i != _audioSourceCount - 1) { audioPorts += ","; } // next create the audio grabber to encapsulate the audio player grabber = new GstAudioPacketGrabber(); grabber.Player = aplayer.AudioWrapper; _audioGrabbers.Add(grabber); //finally create sound object(s)-- for (int j = 0; j < channels.Length; ++j) { //Create Sound Object GameObject audioObj = new GameObject("AudioObject" + i.ToString() + "_" + TargetNode.name + "_" + channels[j].ToString()); audioObj.transform.parent = TargetNode.transform; audioObj.transform.position = AudioLocation[idx]; AudioSource asrc = audioObj.AddComponent <AudioSource> (); asrc.loop = true; _player = audioObj.AddComponent <GstAudioPlayer> (); _player.Player = aplayer.AudioWrapper; _player.TargetSrc = asrc; _player.grabber = grabber; _player.SupportSpatialAudio = _isSpatialAudio; _player.Channel = channels [j]; grabber.AttachedPlayers.Add(_player); AudioObjects.Add(asrc); ++idx; } _audioPlayer.Add(aplayer); } RobotConnector.Connector.SendData(TxKitEars.ServiceName, "AudioPort", audioPorts, true); } }
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; }
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; } }
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; }