public EncoderPipeline([NotNull] WaveFormat inputFormat, [NotNull] IVoiceEncoder encoder, [NotNull] ICommsNetwork net) { if (inputFormat == null) { throw new ArgumentNullException("inputFormat"); } if (encoder == null) { throw new ArgumentNullException("encoder"); } if (net == null) { throw new ArgumentNullException("net"); } _net = net; _inputFormat = inputFormat; _encoder = new ReadonlyLockedValue <IVoiceEncoder>(encoder); //Create buffers to store the encoder input (1 frame of floats) and output (twice equivalent amount of bytes) _plainSamples = new float[encoder.FrameSize]; _encodedBytes = new byte[encoder.FrameSize * sizeof(float) * 2]; //Input buffer to store raw data from microphone _input = new BufferedSampleProvider(_inputFormat, encoder.FrameSize * 2); //Resample data from microphone rate -> encoder rate _resampler = new Resampler(_input, encoder.SampleRate); //Provides encoder sized and encoder rate frames of data _output = new SampleToFrameProvider(_resampler, (uint)encoder.FrameSize); }
private int EncodeFrames([NotNull] IVoiceEncoder encoder, int maxCount) { var count = 0; //Read frames of resampled samples (as many as we can, we want to keep this buffer empty and latency low) var encoderInput = new ArraySegment <float>(_plainSamples, 0, encoder.FrameSize); while (_output.Read(encoderInput) && count < maxCount) { //Encode it var encoded = encoder.Encode(encoderInput, new ArraySegment <byte>(_encodedBytes)); //Transmit it _net.SendVoice(encoded); count++; } return(count); }
public EncoderPipeline(IMicrophoneCapture mic, IVoiceEncoder encoder, ICommsNetwork net, Func <int> channelCount) { _mic = mic; _encoder = encoder; _net = net; _channelCount = channelCount; _encodedBytes = new byte[encoder.FrameSize * sizeof(float)]; _plainSamples = new float[encoder.FrameSize]; _inputFormat = mic.Format; //Create an input buffer with plenty of spare space _input = new BufferedSampleProvider(_inputFormat, Math.Max(_encoder.FrameSize * 2, mic.FrameSize * 2)); _resampler = new Resampler(_input, _encoder.SampleRate); //Whatever we did above, we need to read in frame size chunks _output = new SampleToFrameProvider(_resampler, (uint)encoder.FrameSize); }