void GetRelativeVolume(VoiceChatPacket packet, out float maxAmplitude, out int highVolumeFrequency, out int minFrequency, out int maxFrequency) { float[] sample = null; VoiceChatUtils.Decompress(new NSpeex.SpeexDecoder(NSpeex.BandMode.Narrow), packet, out sample); // clear fftBuffer for (int i = 0; i < fftBuffer.Length; ++i) { fftBuffer[i] = 0; } Array.Copy(sample, 0, fftBuffer, 0, sample.Length); Exocortex.DSP.Fourier.FFT(fftBuffer, fftBuffer.Length / 2, Exocortex.DSP.FourierDirection.Forward); highVolumeFrequency = -1; minFrequency = -1; maxFrequency = -1; maxAmplitude = -1; for (int i = 0; i < fftBuffer.Length; ++i) { if (fftBuffer[i] > maxAmplitude) { maxAmplitude = fftBuffer[i]; highVolumeFrequency = i; } if (minFrequency == -1 && fftBuffer[i] > 0) { minFrequency = i; } if (fftBuffer[i] > 0) { maxFrequency = i; } } }
//播放录音 public void PlayRecording(int voiceLen) { if (source == null) { return; } iPhoneSpeaker.ForceToSpeaker(); int size = VoiceChatSettings.Instance.SampleSize; voiceLen = voiceLen / compressLen * size; VoiceChatPacket packet = new VoiceChatPacket(); packet.Compression = VoiceChatSettings.Instance.Compression; packet.Data = VoiceChatBytePool.Instance.Get(); packet.Length = compressLen; for (int i = 0; i < voiceLen; i += size) { float[] sample = null; Array.Copy(cacheData, i, packet.Data, 0, size); int length = VoiceChatUtils.Decompress(speexDec, packet, out sample); Array.Copy(sample, 0, data, i, length); VoiceChatFloatPool.Instance.Return(sample); } source.clip.SetData(data, 0); source.Play(); // isPlaying = true; VoiceChatBytePool.Instance.Return(packet.Data); }
public void OnNewSample(VoiceChatPacket newPacket) { // Set last time we got something lastRecvTime = Time.time; packetsToPlay.Add(newPacket.PacketId, newPacket); if (packetsToPlay.Count < 10) { return; } var pair = packetsToPlay.First(); var packet = pair.Value; packetsToPlay.Remove(pair.Key); // Decompress float[] sample = null; int length = VoiceChatUtils.Decompress(speexDec, packet, out sample); // Add more time to received received += VoiceChatSettings.Instance.SampleTime; // Push data to buffer Array.Copy(sample, 0, data, index, length); // Increase index index += length; // Handle wrap-around if (index >= audioSource.clip.samples) { index = 0; } // Set data audioSource.clip.SetData(data, 0); //if (!audioSource.isPlaying) // audioSource.Play(); // If we're not playing if (!audioSource.isPlaying) { // Set that we should be playing shouldPlay = true; // And if we have no delay set, set it. if (playDelay <= 0) { playDelay = (float)VoiceChatSettings.Instance.SampleTime * playbackDelay; } } VoiceChatFloatPool.Instance.Return(sample); }
public void OnNewSample(VoiceChatPacket packet) { if (!initialized) { Start(); } // Store last packet // Set last time we got something lastRecvTime = Time.time; // Decompress float[] sample = null; int length = VoiceChatUtils.Decompress(speexDec, packet, out sample); // Add more time to received received += VoiceChatSettings.Instance.SampleTime; // Push data to buffer Array.Copy(sample, 0, data, index, length); // Increase index index += length; // Handle wrap-around if (index >= GetComponent <AudioSource>().clip.samples) { index = 0; } // Set data GetComponent <AudioSource>().clip.SetData(data, 0); // If we're not playing if (!GetComponent <AudioSource>().isPlaying) { // Set that we should be playing shouldPlay = true; // And if we have no delay set, set it. if (playDelay <= 0) { playDelay = (float)VoiceChatSettings.Instance.SampleTime * playbackDelay; } } VoiceChatFloatPool.Instance.Return(sample); }