public byte[] Compress(AudioChunk input) { int frameSize = GetFrameSize(); if (input != null) { short[] newData = input.Data; _incomingSamples.Write(newData); } else { // If input is null, assume we are at end of stream and pad the output with zeroes int paddingNeeded = _incomingSamples.Available() % frameSize; if (paddingNeeded > 0) { _incomingSamples.Write(new short[paddingNeeded]); } } int outCursor = 0; if (_incomingSamples.Available() >= frameSize) { unsafe { fixed(byte *benc = scratchBuffer) { short[] nextFrameData = _incomingSamples.Read(frameSize); byte[] nextFrameBytes = AudioMath.ShortsToBytes(nextFrameData); IntPtr encodedPtr = new IntPtr(benc); _timer.Reset(); _timer.Start(); int thisPacketSize = opus_encode(_encoder, nextFrameBytes, frameSize, encodedPtr, scratchBuffer.Length); outCursor += thisPacketSize; _timer.Stop(); } } } if (outCursor > 0) { _statistics.EncodeSpeed = _frameSize / ((double)_timer.ElapsedTicks / Stopwatch.Frequency * 1000); } byte[] finalOutput = new byte[outCursor]; Array.Copy(scratchBuffer, 0, finalOutput, 0, outCursor); return(finalOutput); }
/// <summary> /// Creates a new audio sample from a .WAV file name /// </summary> /// <param name="fileName"></param> //public AudioChunk(string fileName) // : this(new FileStream(fileName, FileMode.Open)) //{ //} public byte[] GetDataAsBytes() { return(AudioMath.ShortsToBytes(Data)); }