| @@ -18,7 +18,7 @@ namespace Discord.Audio | |||||
| OpusError error; | OpusError error; | ||||
| _ptr = CreateDecoder(samplingRate, channels, out error); | _ptr = CreateDecoder(samplingRate, channels, out error); | ||||
| if (error != OpusError.OK) | if (error != OpusError.OK) | ||||
| throw new InvalidOperationException($"Error occured while creating decoder: {error}"); | |||||
| throw new Exception($"Opus Error: {error}"); | |||||
| } | } | ||||
| /// <summary> Produces PCM samples from Opus-encoded audio. </summary> | /// <summary> Produces PCM samples from Opus-encoded audio. </summary> | ||||
| @@ -33,7 +33,7 @@ namespace Discord.Audio | |||||
| result = Decode(_ptr, inPtr + inputOffset, inputCount, outPtr + outputOffset, (output.Length - outputOffset) / SampleSize / MaxChannels, 0); | result = Decode(_ptr, inPtr + inputOffset, inputCount, outPtr + outputOffset, (output.Length - outputOffset) / SampleSize / MaxChannels, 0); | ||||
| if (result < 0) | if (result < 0) | ||||
| throw new Exception(((OpusError)result).ToString()); | |||||
| throw new Exception($"Opus Error: {(OpusError)result}"); | |||||
| return result; | return result; | ||||
| } | } | ||||
| @@ -25,13 +25,12 @@ namespace Discord.Audio | |||||
| OpusError error; | OpusError error; | ||||
| _ptr = CreateEncoder(samplingRate, channels, (int)application, out error); | _ptr = CreateEncoder(samplingRate, channels, (int)application, out error); | ||||
| if (error != OpusError.OK) | if (error != OpusError.OK) | ||||
| throw new InvalidOperationException($"Error occured while creating encoder: {error}"); | |||||
| throw new Exception($"Opus Error: {error}"); | |||||
| } | } | ||||
| /// <summary> Produces Opus encoded audio from PCM samples. </summary> | /// <summary> Produces Opus encoded audio from PCM samples. </summary> | ||||
| /// <param name="input">PCM samples to encode.</param> | /// <param name="input">PCM samples to encode.</param> | ||||
| /// <param name="inputOffset">Offset of the frame in pcmSamples.</param> | |||||
| /// <param name="output">Buffer to store the encoded frame.</param> | /// <param name="output">Buffer to store the encoded frame.</param> | ||||
| /// <returns>Length of the frame contained in outputBuffer.</returns> | /// <returns>Length of the frame contained in outputBuffer.</returns> | ||||
| public unsafe int EncodeFrame(byte[] input, int inputOffset, int inputCount, byte[] output, int outputOffset) | public unsafe int EncodeFrame(byte[] input, int inputOffset, int inputCount, byte[] output, int outputOffset) | ||||
| @@ -42,7 +41,7 @@ namespace Discord.Audio | |||||
| result = Encode(_ptr, inPtr + inputOffset, inputCount / SampleSize, outPtr + outputOffset, output.Length - outputOffset); | result = Encode(_ptr, inPtr + inputOffset, inputCount / SampleSize, outPtr + outputOffset, output.Length - outputOffset); | ||||
| if (result < 0) | if (result < 0) | ||||
| throw new Exception(((OpusError)result).ToString()); | |||||
| throw new Exception($"Opus Error: {(OpusError)result}"); | |||||
| return result; | return result; | ||||
| } | } | ||||
| @@ -51,7 +50,7 @@ namespace Discord.Audio | |||||
| { | { | ||||
| var result = EncoderCtl(_ptr, OpusCtl.SetInbandFECRequest, value ? 1 : 0); | var result = EncoderCtl(_ptr, OpusCtl.SetInbandFECRequest, value ? 1 : 0); | ||||
| if (result < 0) | if (result < 0) | ||||
| throw new Exception(((OpusError)result).ToString()); | |||||
| throw new Exception($"Opus Error: {(OpusError)result}"); | |||||
| } | } | ||||
| /// <summary> Gets or sets whether Forward Error Correction is enabled. </summary> | /// <summary> Gets or sets whether Forward Error Correction is enabled. </summary> | ||||
| @@ -62,7 +61,7 @@ namespace Discord.Audio | |||||
| var result = EncoderCtl(_ptr, OpusCtl.SetBitrateRequest, value * 1000); | var result = EncoderCtl(_ptr, OpusCtl.SetBitrateRequest, value * 1000); | ||||
| if (result < 0) | if (result < 0) | ||||
| throw new Exception(((OpusError)result).ToString()); | |||||
| throw new Exception($"Opus Error: {(OpusError)result}"); | |||||
| } | } | ||||
| protected override void Dispose(bool disposing) | protected override void Dispose(bool disposing) | ||||
| @@ -1,4 +1,5 @@ | |||||
| using System.Runtime.InteropServices; | |||||
| using System; | |||||
| using System.Runtime.InteropServices; | |||||
| namespace Discord.Audio | namespace Discord.Audio | ||||
| { | { | ||||
| @@ -9,17 +10,27 @@ namespace Discord.Audio | |||||
| [DllImport("libsodium", EntryPoint = "crypto_secretbox_open_easy", CallingConvention = CallingConvention.Cdecl)] | [DllImport("libsodium", EntryPoint = "crypto_secretbox_open_easy", CallingConvention = CallingConvention.Cdecl)] | ||||
| private static extern int SecretBoxOpenEasy(byte* output, byte* input, long inputLength, byte[] nonce, byte[] secret); | private static extern int SecretBoxOpenEasy(byte* output, byte* input, long inputLength, byte[] nonce, byte[] secret); | ||||
| public static int Encrypt(byte[] input, int inputOffset, long inputLength, byte[] output, int outputOffset, byte[] nonce, byte[] secret) | |||||
| public static int Encrypt(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, byte[] nonce, byte[] secret) | |||||
| { | { | ||||
| fixed (byte* inPtr = input) | fixed (byte* inPtr = input) | ||||
| fixed (byte* outPtr = output) | fixed (byte* outPtr = output) | ||||
| return SecretBoxEasy(outPtr + outputOffset, inPtr + inputOffset, inputLength, nonce, secret); | |||||
| { | |||||
| int error = SecretBoxEasy(outPtr + outputOffset, inPtr + inputOffset, inputLength, nonce, secret); | |||||
| if (error != 0) | |||||
| throw new Exception($"Sodium Error: {error}"); | |||||
| return inputLength + 16; | |||||
| } | |||||
| } | } | ||||
| public static int Decrypt(byte[] input, int inputOffset, long inputLength, byte[] output, int outputOffset, byte[] nonce, byte[] secret) | |||||
| public static int Decrypt(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, byte[] nonce, byte[] secret) | |||||
| { | { | ||||
| fixed (byte* inPtr = input) | fixed (byte* inPtr = input) | ||||
| fixed (byte* outPtr = output) | fixed (byte* outPtr = output) | ||||
| return SecretBoxOpenEasy(outPtr + outputOffset, inPtr + inputOffset, inputLength, nonce, secret); | |||||
| { | |||||
| int error = SecretBoxOpenEasy(outPtr + outputOffset, inPtr + inputOffset, inputLength, nonce, secret); | |||||
| if (error != 0) | |||||
| throw new Exception($"Sodium Error: {error}"); | |||||
| return inputLength - 16; | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||