### Install OpusSharp.Natives via dotnet CLI Source: https://github.com/avionblock/opussharp/blob/master/docs/quick-start/index.md Installs only the OpusSharp.Natives package, which contains precompiled binaries. ```bash dotnet add package OpusSharp.Natives --version x.y.z ``` -------------------------------- ### Install OpusSharp via dotnet CLI Source: https://github.com/avionblock/opussharp/blob/master/docs/quick-start/index.md Installs the OpusSharp metapackage, including core and native components. ```bash dotnet add package OpusSharp --version x.y.z ``` -------------------------------- ### Install OpusSharp.Core via dotnet CLI Source: https://github.com/avionblock/opussharp/blob/master/docs/quick-start/index.md Installs only the OpusSharp.Core package, allowing for manual management of native binaries. ```bash dotnet add package OpusSharp.Core --version x.y.z ``` -------------------------------- ### Unity Integration Example Source: https://github.com/avionblock/opussharp/blob/master/docs/quick-start/index.md Example demonstrating how to use Static or Dynamic OpusEncoder/Decoder for Unity integration to prevent IL2CPP errors. ```csharp using OpusSharp.Core; IOpusEncoder encoder; IOpusDecoder decoder; //Decoder #if UNITY_IOS && !UNITY_EDITOR encoder = new Static.OpusEncoder(...); decoder = new Static.OpusDecoder(...); #else encoder = new Dynamic.OpusEncoder(...); decoder = new Dynamic.OpusDecoder(...); ``` -------------------------------- ### Create OpusDecoder Source: https://github.com/avionblock/opussharp/blob/master/docs/quick-start/index.md Example of creating an OpusDecoder and decoding audio data. ```csharp using OpusSharp.Core; var decoder = new OpusDecoder(48000, 2); //20ms at 48khz is 960 samples byte[] encoded = ...; byte[] decoded = new byte[3840]; //Works out to around 3840 bytes for 20ms audio byte[] decodedSamples = decoder.Decode(encoded, encoded.Length, decoded, 960, false); //encoded.Length should not be used unless the size of the encoded audio is the exact same size. Console.WriteLine(decodedSamples); ``` -------------------------------- ### Create OpusEncoder Source: https://github.com/avionblock/opussharp/blob/master/docs/quick-start/index.md Example of creating an OpusEncoder and encoding audio data. ```csharp using OpusSharp.Core; var encoder = new OpusEncoder(48000, 2, OpusPredefinedValues.OPUS_APPLICATION_VOIP); //20ms at 48khz is 960 samples byte[] audio = ...; byte[] encoded = new byte[1000]; int encodedBytes = encoder.Encode(audio, 960, encoded, encoded.Length); Console.WriteLine(encodedBytes); ``` -------------------------------- ### Get version Source: https://github.com/avionblock/opussharp/blob/master/docs/examples/OpusInfo.md Retrieves and prints the Opus library version. ```csharp using OpusSharp.Core; Console.WriteLine(OpusInfo.Version()); ``` -------------------------------- ### Calling a CTL get Source: https://github.com/avionblock/opussharp/blob/master/docs/examples/OpusEncoder.md Demonstrates how to retrieve the sample rate of the Opus encoder using the CTL get method. ```csharp using OpusSharp.Core; //Additionally. You can set use_static to true to make the library use a statically linked version of libopus. (usefuly for iOS distributions). OpusEncoder encoder = new OpusEncoder(48000, 2, OpusPredefinedValues.OPUS_APPLICATION_VOIP); int sampleRate = 0; encoder.Ctl(GenericCTL.OPUS_GET_SAMPLE_RATE, ref sampleRate); //OpusSharp already checks if an error occurred with the CTL request and will throw an OpusException if there is an error, otherwise OpusErrorCodes.OPUS_OK. Console.WriteLine(sampleRate); ``` -------------------------------- ### Encoder Example Source: https://github.com/avionblock/opussharp/blob/master/docs/examples/Home.md Demonstrates how to encode audio data using OpusSharp. ```cs using OpusSharp.Core; byte[] someAudioData = ...; var frameSizeMs = 20; var sampleRate = 48000; var channels = 1; var samplesPerFrame = sampleRate / (1000 / frameSizeMs) * channels; //960 samples per frame. var encoder = new OpusEncoder(sampleRate, channels, OpusPredefinedValues.OPUS_APPLICATION_VOIP); byte[] encodedAudio = new byte[1000] //1000 bytes for an encoded buffer should be enough according to the opus documentation. var encodedBytes = encoder.Encode(someAudioData, samplesPerFrame, encodedAudio, encodedAudio.Length); //We can use encodedBytes to trim any excess data from the encodedAudio buffer before sending over the network or writing to a file. ``` -------------------------------- ### Basic NAudio Example Source: https://github.com/avionblock/opussharp/blob/master/README.md Basic NAudio Example ```csharp using NAudio.Wave; using OpusSharp.Core; var format = new WaveFormat(48000, 2); var buffer = new BufferedWaveProvider(format) { ReadFully = true }; var encoder = new OpusEncoder(format.SampleRate, format.Channels, OpusPredefinedValues.OPUS_APPLICATION_VOIP); var decoder = new OpusDecoder(format.SampleRate, format.Channels); var recorder = new WaveInEvent() { BufferMilliseconds = 20, WaveFormat = format }; var player = new WaveOutEvent(); recorder.DataAvailable += Recorder_DataAvailable; recorder.StartRecording(); player.Init(buffer); player.Play(); void Recorder_DataAvailable(object? sender, WaveInEventArgs e) { var encoded = new byte[1000]; var encodedBytes = encoder.Encode(e.Buffer, 960, encoded, encoded.Length); Console.WriteLine(encodedBytes); var decoded = new byte[3840]; var decodedSamples = decoder.Decode(encoded, encodedBytes, decoded, 960, false); Console.WriteLine(decodedSamples); buffer.AddSamples(decoded, 0, decoded.Length); } Console.ReadLine(); ``` -------------------------------- ### Decoder Example Source: https://github.com/avionblock/opussharp/blob/master/docs/examples/Home.md Demonstrates how to decode Opus encoded audio data using OpusSharp. ```cs using OpusSharp.Core; byte[] someEncodedAudio = ...; var frameSizeMs = 20; var sampleRate = 48000; var channels = 1; var samplesPerFrame = sampleRate / (1000 / frameSizeMs) * channels; //960 samples per frame. var decoder = new OpusDecoder(sampleRate, channels); var decoded = new byte[1920]; //We get 1920 bytes from doing this calculation because 16/8 (16 bit audio, 1 byte is 8 bits) equals 2 multiplied by samplesPerFrame gets us bytes per frame. 16/(sizeof(byte) * 8) * samplesPerFrame var decodedSamples = decoder.Decode(someEncodedAudio, someEncodedAudio.Length, decoded, samplesPerFrame, false); ``` -------------------------------- ### Calling a CTL get Source: https://github.com/avionblock/opussharp/blob/master/docs/examples/OpusDecoder.md Demonstrates how to retrieve the sample rate of the Opus decoder using the OPUS_GET_SAMPLE_RATE CTL command. ```csharp using OpusSharp.Core; //Additionally. You can set use_static to true to make the library use a statically linked version of libopus. (usefuly for iOS distributions). OpusDecoder decoder = new OpusDecoder(48000, 2); int sampleRate = 0; decoder.Ctl(GenericCTL.OPUS_GET_SAMPLE_RATE, ref sampleRate); //OpusSharp already checks if an error occurred with the CTL request and will throw an OpusException if there is an error, otherwise OpusErrorCodes.OPUS_OK. Console.WriteLine(sampleRate); ``` -------------------------------- ### NAudio Example Source: https://github.com/avionblock/opussharp/blob/master/docs/examples/Home.md Integrates OpusSharp with NAudio for recording, encoding, decoding, and playback. ```csharp using NAudio.Wave; using OpusSharp.Core; //Format to record and playback. var format = new WaveFormat(48000, 2); //Buffer for player to read and recorder to input. var buffer = new BufferedWaveProvider(format) { ReadFully = true }; //Setup the encoder. var encoder = new OpusEncoder(format.SampleRate, format.Channels, OpusPredefinedValues.OPUS_APPLICATION_VOIP); //Setup the decoder. var decoder = new OpusDecoder(format.SampleRate, format.Channels); //Setup the recorder and player. var recorder = new WaveInEvent() { BufferMilliseconds = 20, WaveFormat = format }; var player = new WaveOutEvent(); recorder.DataAvailable += Recorder_DataAvailable; //Start recording and playback. recorder.StartRecording(); player.Init(buffer); player.Play(); void Recorder_DataAvailable(object? sender, WaveInEventArgs e) { var encoded = new byte[1000]; //Allocate buffer to encode into. var encodedBytes = encoder.Encode(e.Buffer, 960, encoded, encoded.Length); Console.WriteLine(encodedBytes); var decoded = new byte[3840]; //Allocate buffer to decode into. var decodedSamples = decoder.Decode(encoded, encodedBytes, decoded, 960, false); Console.WriteLine(decodedSamples); //Add decoded audio into the player's buffer. buffer.AddSamples(decoded, 0, decoded.Length); } Console.ReadLine(); ``` -------------------------------- ### Calling a CTL via OpusSharp.Core.Extensions Source: https://github.com/avionblock/opussharp/blob/master/docs/examples/OpusDecoder.md Demonstrates how to set and get the gain of the Opus decoder using extension methods provided by OpusSharp.Core.Extensions. ```csharp using OpusSharp.Core; using OpusSharp.Core.Extensions; //Additionally. You can set use_static to true to make the library use a statically linked version of libopus. (usefuly for iOS distributions). OpusDecoder decoder = new OpusDecoder(48000, 2); decoder.SetGain(5); //Takes in a ushort. Console.WriteLine(decoder.GetGain()); ``` -------------------------------- ### Decoder Example Source: https://github.com/avionblock/opussharp/blob/master/README.md Simple decoder example for initializing and decoding. You of course can use `short[]` or `float[]` arrays to encode as well. ```cs using OpusSharp.Core; byte[] someEncodedAudio = ...; var frameSizeMs = 20; var sampleRate = 48000; var channels = 1; var samplesPerFrame = sampleRate / (1000 / frameSizeMs) * channels; //960 samples per frame. var decoder = new OpusDecoder(sampleRate, channels); var decoded = new byte[1920]; //We get 1920 bytes from doing this calculation because 16/8 (16 bit audio, 1 byte is 8 bits) equals 2 multiplied by samplesPerFrame gets us bytes per frame. 16/(sizeof(byte) * 8) * samplesPerFrame var decodedSamples = decoder.Decode(someEncodedAudio, someEncodedAudio.Length, decoded, samplesPerFrame, false); ``` -------------------------------- ### CTL Example using OpusSharp.Core.Extensions Source: https://github.com/avionblock/opussharp/blob/master/README.md This CTL example shows the usage of extensions to make setting CTL's easier. ```csharp using OpusSharp.Core; using OpusSharp.Core.Extensions; OpusEncoder encoder = new OpusEncoder(48000, 2, OpusPredefinedValues.OPUS_APPLICATION_AUDIO); encoder.SetComplexity(2); Console.WriteLine(encoder.GetComplexity()); ``` -------------------------------- ### Calling a CTL via OpusSharp.Core.Extensions Source: https://github.com/avionblock/opussharp/blob/master/docs/examples/OpusEncoder.md Demonstrates how to set and get the encoding complexity using extension methods provided by OpusSharp.Core.Extensions. ```csharp using OpusSharp.Core; using OpusSharp.Core.Extensions; OpusEncoder encoder = new OpusEncoder(48000, 2, OpusPredefinedValues.OPUS_APPLICATION_AUDIO); encoder.SetComplexity(2); Console.WriteLine(encoder.GetComplexity()); ``` -------------------------------- ### Get string error Source: https://github.com/avionblock/opussharp/blob/master/docs/examples/OpusInfo.md Retrieves and prints the string representation of an Opus error code. ```csharp using OpusSharp.Core; Console.WriteLine(OpusInfo.StringError(0)); //opus_ok; ``` -------------------------------- ### CTL Example Source: https://github.com/avionblock/opussharp/blob/master/README.md This example shows the raw usage of calling a CTL with no extensions, OpusSharp will directly pass the call to the opus binary. ```cs using OpusSharp.Core; OpusEncoder encoder = new OpusEncoder(48000, 2, OpusPredefinedValues.OPUS_APPLICATION_VOIP); //1 == true, 0 == false encoder.Ctl(EncoderCTL.OPUS_SET_VBR, 1); //OpusSharp already checks if an error occurred with the CTL request and will throw an OpusException if there is an error, otherwise OpusErrorCodes.OPUS_OK. //Most setter CTL's do not require a pointer reference. All Getter CTL's require a pointer reference (for now). ``` -------------------------------- ### Encoder Example Source: https://github.com/avionblock/opussharp/blob/master/README.md Simple encoder example for initializing and encoding. You of course can use `short[]` or `float[]` arrays to encode as well. ```cs using OpusSharp.Core; byte[] someAudioData = ...; var frameSizeMs = 20; var sampleRate = 48000; var channels = 1; var samplesPerFrame = sampleRate / (1000 / frameSizeMs) * channels; //960 samples per frame. var encoder = new OpusEncoder(sampleRate, channels, OpusPredefinedValues.OPUS_APPLICATION_VOIP); byte[] encodedAudio = new byte[1000] //1000 bytes for an encoded buffer should be enough according to the opus documentation. var encodedBytes = encoder.Encode(someAudioData, samplesPerFrame, encodedAudio, encodedAudio.Length); //We can use encodedBytes to trim any excess data from the encodedAudio buffer before sending over the network or writing to a file. ``` -------------------------------- ### Using a native function Source: https://github.com/avionblock/opussharp/blob/master/docs/examples/NativeOpus.md Demonstrates creating and closing an Opus encoder using a native function call. ```csharp using OpusSharp.Core.SafeHandlers; using OpusSharp.Core; unsafe { int error = 0; OpusEncoderSafeHandle safeHandle = NativeOpus.opus_encoder_create(48000, 2, (int)OpusPredefinedValues.OPUS_APPLICATION_AUDIO, &error); if(error < 0) throw new OpusException(((OpusErrorCodes)error).ToString()); //Successfully created an OpusEncoder native object. //Free/Close Object safeHandle.Close(); } ``` -------------------------------- ### Static Usage Example Source: https://github.com/avionblock/opussharp/blob/master/README.md This example shows a forced usage of OpusSharp to use the statically linked opus binary. ```csharp // On iOS and browser WASM OpusSharp switches to StaticNativeOpus automatically. // You can still force the same behavior manually with use_static: true. var encoder = new OpusEncoder(sampleRate, channels, OpusPredefinedValues.OPUS_APPLICATION_VOIP, use_static: true); var encoder = new OpusDecoder(sampleRate, channels, use_static: true); ``` -------------------------------- ### Unity Example Source: https://github.com/avionblock/opussharp/blob/master/README.md For unity integration, you may want to use `Static.OpusDecoder`, `Dynamic.OpusDecoder`, `Static.OpusEncoder` or `Dynamic.OpusEncoder` to prevent IL2CPP errors for example... ```csharp using OpusSharp.Core; IOpusEncoder encoder; IOpusDecoder decoder; //Decoder #if UNITY_IOS && !UNITY_EDITOR encoder = new Static.OpusEncoder(...); decoder = new Static.OpusDecoder(...); #else encoder = new Dynamic.OpusEncoder(...); decoder = new Dynamic.OpusDecoder(...); ``` -------------------------------- ### Calling a CTL set Source: https://github.com/avionblock/opussharp/blob/master/docs/examples/OpusDecoder.md Demonstrates how to set the gain of the Opus decoder using the OPUS_SET_GAIN CTL command. ```csharp using OpusSharp.Core; //Additionally. You can set use_static to true to make the library use a statically linked version of libopus. (usefuly for iOS distributions). OpusDecoder decoder = new OpusDecoder(48000, 2); //Most CTL set functions do not require a reference pointer, so we pass in the variable directly. decoder.Ctl(DecoderCTL.OPUS_SET_GAIN, 0); //OpusSharp already checks if an error occurred with the CTL request and will throw an OpusException if there is an error, otherwise OpusErrorCodes.OPUS_OK. ``` -------------------------------- ### Using a statically linked native function Source: https://github.com/avionblock/opussharp/blob/master/docs/examples/NativeOpus.md Demonstrates creating and closing an Opus encoder using a statically linked native function call. ```csharp using OpusSharp.Core.SafeHandlers; using OpusSharp.Core; unsafe { int error = 0; OpusEncoderSafeHandle safeHandle = StaticNativeOpus.opus_encoder_create(48000, 2, (int)OpusPredefinedValues.OPUS_APPLICATION_AUDIO, &error); if(error < 0) throw new OpusException(((OpusErrorCodes)error).ToString()); //Successfully created an OpusEncoder native object. //Free/Close Object safeHandle.Close(); } ``` -------------------------------- ### Calling a CTL set Source: https://github.com/avionblock/opussharp/blob/master/docs/examples/OpusEncoder.md Demonstrates how to set the VBR (Variable Bitrate) mode for the Opus encoder using the CTL set method. ```csharp using OpusSharp.Core; //Additionally. You can set use_static to true to make the library use a statically linked version of libopus. (usefuly for iOS distributions). OpusEncoder encoder = new OpusEncoder(48000, 2, OpusPredefinedValues.OPUS_APPLICATION_VOIP); encoder.Ctl(EncoderCTL.OPUS_SET_VBR, 1); //OpusSharp already checks if an error occurred with the CTL request and will throw an OpusException if there is an error, otherwise OpusErrorCodes.OPUS_OK. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.