/// <summary>
        /// Initializes a new <see cref="OpusEncoder"/> instance, with the specified intended application, sample rate and channels.
        /// </summary>
        /// <param name="application">The intended application.</param>
        /// <param name="sampleRate">The sample rate in the input audio, 48000, 24000, 16000, 12000 or 8000 Hz.</param>
        /// <param name="channels">The channels in the input audio, mono or stereo.</param>
        public OpusEncoder(Application application, int sampleRate, int channels)
        {
            if (!Enum.IsDefined(typeof(Application), application))
            {
                throw new ArgumentException("Value is not defined in the enumeration.", nameof(application));
            }

            switch (sampleRate)
            {
            case 8000:
            case 12000:
            case 16000:
            case 24000:
            case 48000:
                break;

            default:
                throw new ArgumentException("Value must be one of the following: 8000, 12000, 16000, 24000 or 48000.", nameof(sampleRate));
            }

            if (channels < 1 || channels > 2)
            {
                throw new ArgumentOutOfRangeException(nameof(channels), "Value must be between 1 and 2.");
            }

            Application = application;
            SampleRate  = sampleRate;
            Channels    = channels;
            Bitrate     = 128000;

            _handle = API.opus_encoder_create(sampleRate, channels, (int)application, out int error);
            API.ThrowIfError(error);

            // Setting to -1 (OPUS_BITRATE_MAX) enables bitrate to be regulated by the output buffer length.
            int result = API.opus_encoder_ctl(_handle, (int)Control.SetBitrate, -1);

            API.ThrowIfError(result);
        }
Beispiel #2
0
 public static extern int opus_encoder_ctl(SafeEncoderHandle st, int request, int value);
Beispiel #3
0
 public static extern int opus_encode(SafeEncoderHandle st, IntPtr pcm, int frame_size, IntPtr data, int max_data_bytes);