added chipmunk catchup

upm
kjjohnsen 2022-11-14 19:32:53 -05:00
parent 862e8a9dd6
commit f550e56814
2 changed files with 23 additions and 9 deletions

View File

@ -45,9 +45,9 @@ namespace VelNet
double lastMicSample; //holds the last mic sample, in case we need to interpolate it double lastMicSample; //holds the last mic sample, in case we need to interpolate it
double sampleTimer = 0; //increments with every mic sample, but when over the encodeTime, causes a sample and subtracts that encode time double sampleTimer = 0; //increments with every mic sample, but when over the encodeTime, causes a sample and subtracts that encode time
EventWaitHandle waiter; EventWaitHandle waiter;
float silenceThreshold = .02f; //average volume of packet public float silenceThreshold = .01f; //average volume of packet
int numSilent = 0; //number of silent packets detected int numSilent = 0; //number of silent packets detected
int minSilencePacketsToStop = 5; public int minSilencePacketsToStop = 5;
double averageVolume = 0; double averageVolume = 0;
Thread t; Thread t;
public Action<FixedArray> encodedFrameAvailable = delegate { }; public Action<FixedArray> encodedFrameAvailable = delegate { };

View File

@ -10,13 +10,14 @@ namespace VelNet
public VelVoice voiceSystem; //must be set for the player only public VelVoice voiceSystem; //must be set for the player only
public AudioSource source; //must be set for the clone only public AudioSource source; //must be set for the clone only
AudioClip myClip; AudioClip myClip;
int bufferedAmount = 0; public int bufferedAmount = 0;
int playedAmount = 0; public int playedAmount = 0;
int lastTime = 0; int lastTime = 0;
float[] empty = new float[1000]; //a buffer of 0s to force silence, because playing doesn't stop on demand float[] empty = new float[1000]; //a buffer of 0s to force silence, because playing doesn't stop on demand
float delayStartTime; float delayStartTime;
public override void ReceiveBytes(byte[] message) public override void ReceiveBytes(byte[] message)
{ {
float[] temp = voiceSystem.decodeOpusData(message, message.Length); float[] temp = voiceSystem.decodeOpusData(message, message.Length);
myClip.SetData(temp, bufferedAmount % source.clip.samples); myClip.SetData(temp, bufferedAmount % source.clip.samples);
bufferedAmount += temp.Length; bufferedAmount += temp.Length;
@ -44,7 +45,7 @@ namespace VelNet
{ {
//float[] temp = new float[frame.count]; //float[] temp = new float[frame.count];
//System.Array.Copy(frame.array, temp, frame.count); //System.Array.Copy(frame.array, temp, frame.count);
MemoryStream mem = new MemoryStream(); MemoryStream mem = new MemoryStream();
BinaryWriter writer = new BinaryWriter(mem); BinaryWriter writer = new BinaryWriter(mem);
writer.Write(frame.array, 0, frame.count); writer.Write(frame.array, 0, frame.count);
this.SendBytes(mem.ToArray(), false); this.SendBytes(mem.ToArray(), false);
@ -54,22 +55,35 @@ namespace VelNet
}; };
} }
myClip = AudioClip.Create(this.name, 16000 * 7, 1, 16000, false); myClip = AudioClip.Create(this.name, 16000 * 10, 1, 16000, false);
source.clip = myClip; source.clip = myClip;
source.loop = true; source.loop = true;
source.Pause(); source.Pause();
} }
// Update is called once per frame // Update is called once per frame
void Update() void Update()
{ {
if (bufferedAmount > playedAmount && !source.isPlaying) if (bufferedAmount > playedAmount)
{ {
if ((bufferedAmount - playedAmount > 1000) || (Time.time - delayStartTime) > .1f) //this seems to make the quality better
var offset = bufferedAmount - playedAmount;
if ((offset > 1000) || (Time.time - delayStartTime) > .1f) //this seems to make the quality better
{ {
source.Play(); var temp = Mathf.Max(0, offset - 2000);
source.pitch = 1 + temp / 18000.0f; //okay to behind by 2000, but speed up real quick if by 170000
if (!source.isPlaying)
{
source.Play();
}
} }
else else
{ {