public OpusCodec(int samplerate, int channels, VoiceApplication application) { if (!AllowedSampleRates.Contains(samplerate)) { throw new ArgumentOutOfRangeException(nameof(samplerate), string.Concat("Sample rate must be one of ", string.Join(", ", AllowedSampleRates))); } if (!AllowedChannelCounts.Contains(channels)) { throw new ArgumentOutOfRangeException(nameof(channels), string.Concat("Channel count must be one of ", string.Join(", ", AllowedChannelCounts))); } this.SampleRate = samplerate; this.Channels = channels; this.Application = application; var err = OpusError.OPUS_OK; this.Encoder = CreateEncoder(this.SampleRate, this.Channels, (int)this.Application, out err); this.Errors = err; if (this.Errors != OpusError.OPUS_OK) { throw new Exception(this.Errors.ToString()); } }
public OpusCodec(int sampleRate, int channels, VoiceApplication application) { if (!AllowedSampleRates.Contains(sampleRate)) { throw new ArgumentOutOfRangeException(nameof(sampleRate), string.Concat("Sample rate must be one of ", string.Join(", ", AllowedSampleRates))); } if (!AllowedChannelCounts.Contains(channels)) { throw new ArgumentOutOfRangeException(nameof(channels), string.Concat("Channel count must be one of ", string.Join(", ", AllowedChannelCounts))); } this.SampleRate = sampleRate; this.Channels = channels; this.Application = application; this.Encoder = CreateEncoder(this.SampleRate, this.Channels, (int)this.Application, out var err); this.CheckForError(err); var sig = OpusSignal.Auto; switch (application) { case VoiceApplication.Music: sig = OpusSignal.Music; break; case VoiceApplication.Voice: sig = OpusSignal.Voice; break; } err = EncoderControl(this.Encoder, OpusControl.SetSignal, (int)sig); this.CheckForError(err); err = EncoderControl(this.Encoder, OpusControl.SetPacketLossPercent, 15); this.CheckForError(err); err = EncoderControl(this.Encoder, OpusControl.SetInBandFec, 1); this.CheckForError(err); err = EncoderControl(this.Encoder, OpusControl.SetBitrate, 131072); this.CheckForError(err); #if !NETSTANDARD1_1 this.Decoder = CreateDecoder(this.SampleRate, this.Channels, out err); this.CheckForError(err); #endif }
/// <summary> /// Creates a new audio format for use with Opus encoder. /// </summary> /// <param name="sampleRate">Audio sampling rate in Hz.</param> /// <param name="channelCount">Number of audio channels in the data.</param> /// <param name="voiceApplication">Encoder preset to use.</param> public AudioFormat(int sampleRate = 48000, int channelCount = 2, VoiceApplication voiceApplication = VoiceApplication.Music) { if (!AllowedSampleRates.Contains(sampleRate)) { throw new ArgumentOutOfRangeException(nameof(sampleRate), "Invalid sample rate specified."); } if (!AllowedChannelCounts.Contains(channelCount)) { throw new ArgumentOutOfRangeException(nameof(channelCount), "Invalid channel count specified."); } if (voiceApplication != VoiceApplication.Music && voiceApplication != VoiceApplication.Voice && voiceApplication != VoiceApplication.LowLatency) { throw new ArgumentOutOfRangeException(nameof(voiceApplication), "Invalid voice application specified."); } this.SampleRate = sampleRate; this.ChannelCount = channelCount; this.VoiceApplication = voiceApplication; }
internal bool IsValid() => AllowedSampleRates.Contains(this.SampleRate) && AllowedChannelCounts.Contains(this.ChannelCount) && (this.VoiceApplication == VoiceApplication.Music || this.VoiceApplication == VoiceApplication.Voice || this.VoiceApplication == VoiceApplication.LowLatency);