106 lines
3.4 KiB
C#
106 lines
3.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using Voice;
|
|
using System.IO;
|
|
|
|
namespace SafeMobileLib
|
|
{
|
|
public class VolumeUpdatedEventArgs : EventArgs
|
|
{
|
|
public int Volume { get; set; }
|
|
}
|
|
|
|
public class JitterBufferWaveProvider : WaveStream
|
|
{
|
|
//private readonly NSpeex.Decoder decoder ;
|
|
//private readonly SpeexJitterBuffer jitterBuffer;
|
|
private readonly WaveFormat waveFormat;
|
|
private object readWriteLock = new object();
|
|
|
|
/*public JitterBufferWaveProvider(Stream S)
|
|
{
|
|
waveFormat = new WaveFormat(8192, 16, 1);
|
|
//jitterBuffer = new NativeSpeexJitterBuffer(decoder);
|
|
}*/
|
|
public JitterBufferWaveProvider(Stream s)
|
|
{
|
|
waveFormat = new WaveFormat(8192, 16, 1);
|
|
}
|
|
|
|
public override int Read(byte[] buffer, int offset, int count)
|
|
{
|
|
int peakVolume = 0;
|
|
int bytesRead = 0;
|
|
lock (readWriteLock)
|
|
{
|
|
while (bytesRead < count)
|
|
{
|
|
if (exceedingBytes.Count != 0)
|
|
{
|
|
buffer[bytesRead++] = exceedingBytes.Dequeue();
|
|
}
|
|
else
|
|
{
|
|
short[] decodedBuffer = new short[200];
|
|
//Console.WriteLine("decoder.FrameSize" + decoder.FrameSize);
|
|
//jitterBuffer.Get(decodedBuffer);
|
|
for (int i = 0; i < decodedBuffer.Length; ++i)
|
|
{
|
|
if (bytesRead < count)
|
|
{
|
|
short currentSample = decodedBuffer[i];
|
|
peakVolume = currentSample > peakVolume ? currentSample : peakVolume;
|
|
BitConverter.GetBytes(currentSample).CopyTo(buffer, offset + bytesRead);
|
|
bytesRead += 2;
|
|
}
|
|
else
|
|
{
|
|
var bytes = BitConverter.GetBytes(decodedBuffer[i]);
|
|
exceedingBytes.Enqueue(bytes[0]);
|
|
exceedingBytes.Enqueue(bytes[1]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
OnVolumeUpdated(peakVolume);
|
|
|
|
return bytesRead;
|
|
}
|
|
|
|
public override void Write(byte[] buffer, int offset, int count)
|
|
{
|
|
lock (readWriteLock)
|
|
{
|
|
//jitterBuffer.Put(buffer);
|
|
}
|
|
}
|
|
|
|
public override long Length
|
|
{
|
|
get { return 20; }//jitterBuffer.Length; }
|
|
}
|
|
|
|
public override long Position
|
|
{
|
|
get { return 0; }
|
|
set { throw new NotImplementedException(); }
|
|
}
|
|
|
|
public EventHandler<VolumeUpdatedEventArgs> VolumeUpdated;
|
|
|
|
private void OnVolumeUpdated(int volume)
|
|
{
|
|
var eventHandler = VolumeUpdated;
|
|
if (eventHandler != null)
|
|
{
|
|
eventHandler.BeginInvoke(this, new VolumeUpdatedEventArgs { Volume = volume }, null, null);
|
|
}
|
|
}
|
|
|
|
private readonly Queue<byte> exceedingBytes = new Queue<byte>();
|
|
}
|
|
}
|