/* Copyright (C) 2014 DaikonForge */
namespace DaikonForge.VoIP
{
using UnityEngine;
///
/// Implements an audio player which writes to a streaming audio clip
///
[AddComponentMenu("DFVoice/Unity Audio Player")]
[RequireComponent(typeof(AudioSource))]
public class UnityAudioPlayer : MonoBehaviour, IAudioPlayer
{
public AudioClip testClip;
public bool PlayingSound
{
get
{
return GetComponent().isPlaying;
}
}
public bool IsThreeDimensional = false;
public bool Equalize = false;
public float EqualizeSpeed = 1f;
public float TargetEqualizeVolume = 0.75f;
public float MaxEqualization = 5f;
private int frequency = 16000;
private int writeHead = 0;
private int totalWritten = 0;
private AudioClip playClip;
//private int delayForFrames = 0;
//private int lastTime = 0;
//private int played = 0;
private float currentGain = 1f;
private float targetGain = 1f;
void Start()
{
playClip = AudioClip.Create( "vc", frequency * LocalVoiceController.RecordMaxLength, 1, frequency, false);
// backwards compatibility
if (GetComponent() == null)
gameObject.AddComponent();
GetComponent().clip = playClip;
GetComponent().Stop();
GetComponent().loop = false;
if (testClip != null)
{
GetComponent().PlayOneShot(testClip);
}
GetComponent().spatialBlend = IsThreeDimensional ? 1f : 0f;
}
//PS:unity4.x用边解码边播放会出现不连贯的问题,unity5不会,注意使用
//void Update()
//{
// if (GetComponent().isPlaying)
// {
// if (lastTime > GetComponent().timeSamples)
// {
// played += GetComponent().clip.samples;
// }
// lastTime = GetComponent().timeSamples;
// currentGain = Mathf.MoveTowards(currentGain, targetGain, Time.deltaTime * EqualizeSpeed);
// if (played + GetComponent().timeSamples >= totalWritten)
// {
// GetComponent().Pause();
// delayForFrames = 2;
// }
// }
//}
void OnDestroy()
{
Destroy(GetComponent().clip);
}
public void SetSampleRate(int sampleRate, int seconds)
{
//if( GetComponent() == null ) return;
//if( GetComponent().clip != null && GetComponent().clip.frequency == sampleRate ) return;
this.frequency = sampleRate;
if (GetComponent().clip != null)
Destroy(GetComponent().clip);
playClip = AudioClip.Create( "vc", frequency * seconds, 1, frequency, false);
GetComponent().clip = playClip;
GetComponent().Stop();
GetComponent().loop = false;
writeHead = 0;
totalWritten = 0;
//delayForFrames = 0;
//lastTime = 0;
//played = 0;
}
public void BufferAudio(BigArray audioData)
{
//if (GetComponent() == null) return;
float[] temp = TempArray.Obtain(audioData.Length);
audioData.CopyTo(0, temp, 0, audioData.Length * 4);
if (Equalize)
{
float maxAmp = AudioUtils.GetMaxAmplitude(temp);
targetGain = TargetEqualizeVolume / maxAmp;
//currentGain = Mathf.MoveTowards(currentGain, targetGain, Time.deltaTime * EqualizeSpeed);
if (targetGain > MaxEqualization)
targetGain = MaxEqualization;
if (targetGain < currentGain)
{
currentGain = targetGain;
}
AudioUtils.ApplyGain(temp, targetGain);
}
playClip.SetData(temp, writeHead);
TempArray.Release(temp);
writeHead += audioData.Length;
totalWritten += audioData.Length;
writeHead %= playClip.samples;
//PS:unity4.x用边解码边播放会出现不连贯的问题,unity5不会,注意使用
//Debug.LogWarning("---" + writeHead);
//if (!GetComponent().isPlaying)
//{
// delayForFrames--;
// if (delayForFrames <= 0)
// {
// GetComponent().Play();
// }
//}
}
public void Play(string filePath)
{
//StartCoroutine(PlayBack(filePath));
GetComponent().Play();
}
public AudioClip GetAudioClip()
{
//return GetComponent().clip;
return playClip;
}
//System.Collections.IEnumerator PlayBack(string filePath)
//{
// WWW www = new WWW("file://" + filePath);
// yield return www;
// AudioClip clip = www.GetAudioClipCompressed(false,AudioType.OGGVORBIS);
// AudioSource source = GetComponent();
// source.PlayOneShot(clip);
//}
}
}