using System; namespace Photon.Voice { public class RawCodec { public class Encoder : IEncoderDirect { public string Error { get; private set; } public Action, FrameFlags> Output { set; get; } int sizeofT = System.Runtime.InteropServices.Marshal.SizeOf(default(T)); byte[] byteBuf = new byte[0]; private static readonly ArraySegment EmptyBuffer = new ArraySegment(new byte[] { }); public ArraySegment DequeueOutput(out FrameFlags flags) { flags = 0; return EmptyBuffer; } public void EndOfStream() { } public I GetPlatformAPI() where I : class { return null; } public void Dispose() { } public void Input(T[] buf) { if (Error != null) { return; } if (Output == null) { Error = "RawCodec.Encoder: Output action is not set"; return; } if (buf == null) { return; } if (buf.Length == 0) { return; } var s = buf.Length * sizeofT; if (byteBuf.Length < s) { byteBuf = new byte[s]; } Buffer.BlockCopy(buf, 0, byteBuf, 0, s); Output(new ArraySegment(byteBuf, 0, s), 0); } } public class Decoder : IDecoder { public string Error { get; private set; } public Decoder(Action> output) { this.output = output; } public void Open(VoiceInfo info) { } T[] buf = new T[0]; int sizeofT = System.Runtime.InteropServices.Marshal.SizeOf(default(T)); public void Input(ref FrameBuffer byteBuf) { if (byteBuf.Array == null) { return; } if (byteBuf.Length == 0) { return; } var s = byteBuf.Length / sizeofT; if (buf.Length < s) { buf = new T[s]; } Buffer.BlockCopy(byteBuf.Array, byteBuf.Offset, buf, 0, byteBuf.Length); output(new FrameOut((T[])(object)buf, false)); } public void Dispose() { } private Action> output; } // Adapts FrameOut output to FrameOut decoder // new RawCodec.Decoder(new RawCodec.ShortToFloat(output as Action>).Output); public class ShortToFloat { public ShortToFloat(Action> output) { this.output = output; } public void Output(FrameOut shortBuf) { if (buf.Length < shortBuf.Buf.Length) { buf = new float[shortBuf.Buf.Length]; } AudioUtil.Convert(shortBuf.Buf, buf, shortBuf.Buf.Length); output(new FrameOut((float[])(object)buf, false)); } Action> output; float[] buf = new float[0]; } } }