public unsafe void Feed(byte[] bytes, int offset, int count) { if (_isDisposed) { return; } lock (_componentsLock) { if (_isDisposed) { return; } var position = offset; fixed(byte *bytesPtr = bytes) while (position != count + offset) { var segmentLength = BitConverter.ToInt32(bytes, position); position += 4; int bufferLength; var decodedBuffer = _opusDecoder.Decode(bytesPtr + position, segmentLength, out bufferLength); _writeableBufferingSource.Write(decodedBuffer, 0, bufferLength); position += segmentLength; } } }
private static async Task encoderThread() { //fstream = File.Open("test.ac3", FileMode.Create); while (true) { lock (encoderLock) { Monitor.Wait(encoderLock); } while (sampleQueue.Count > 0) { float[] samples = sampleQueue.Dequeue(); enc.Encode(samples, samples.Length / 6, (b, o, c) => wBuffSrc.Write(b, o, c)); } //if (i++ % 10 == 0) fstream.Flush(); //await Task.Delay(TimeSpan.FromMilliseconds(20)); } }
private void RunClient() { // client mode using (var soundOut = new WasapiOut { Latency = (int)(_configuration.MaxAudioLatency * 1000) }) { var wavFormat = new WaveFormat(48000, 32, 2, AudioEncoding.IeeeFloat); using (var wb = new WriteableBufferingSource(wavFormat, (int)(wavFormat.BytesPerSecond * _configuration.MaxAudioLatency)) { FillWithZeros = true }) { _udpSocket = new UDPSocket(); _udpSocket.Client(_configuration.IpAddress, _configuration.Port, (point, bytes) => { wb.Write(bytes, 0, bytes.Length); }); _udpSocket.Send(new byte[] { 0 }, 1); Console.WriteLine("Sent start request to server!"); soundOut.Initialize(wb); soundOut.Play(); var paused = false; while (true) { var key = Console.ReadKey(); switch (key.Key) { case ConsoleKey.Q: soundOut.Stop(); _udpSocket.Send(new byte[] { 1 }, 1); _stopClient.Close(); return; case ConsoleKey.C: _udpSocket.Send(new byte[] { 0 }, 1); Console.WriteLine("Sending connection request to the server..."); break; case ConsoleKey.P: _udpSocket.Send(!paused ? new byte[] { 1 } : new byte[] { 0 }, 1); paused = !paused; break; case ConsoleKey.R: _udpSocket.Send(new byte[] { 2 }, 1); _udpSocket.Send(new byte[] { 1 }, 1); Console.WriteLine("Resetting server and client..."); return; case ConsoleKey.X: _udpSocket.Send(new byte[] { 3 }, 1); Console.WriteLine("Stopping server and client..."); soundOut.Stop(); _stopClient.Close(); return; } } } } }