private static void CheckError(LibSpeexDSP.RESAMPLER_ERR e) { switch (e) { case LibSpeexDSP.RESAMPLER_ERR.SUCCESS: return; case LibSpeexDSP.RESAMPLER_ERR.ALLOC_FAILED: throw new InsufficientMemoryException($"{nameof(LibSpeexDSP)}: Alloc failed"); case LibSpeexDSP.RESAMPLER_ERR.BAD_STATE: throw new Exception($"{nameof(LibSpeexDSP)}: Bad state"); case LibSpeexDSP.RESAMPLER_ERR.INVALID_ARG: throw new ArgumentException($"{nameof(LibSpeexDSP)}: Bad Argument"); case LibSpeexDSP.RESAMPLER_ERR.PTR_OVERLAP: throw new Exception($"{nameof(LibSpeexDSP)}: Buffers cannot overlap"); } }
/// <summary> /// /// </summary> /// <param name="quality">0 to 10</param> /// <param name="rationum">numerator of srate change ratio (inrate / outrate)</param> /// <param name="ratioden">demonenator of srate change ratio (inrate / outrate)</param> /// <param name="sratein">sampling rate in, rounded to nearest hz</param> /// <param name="srateout">sampling rate out, rounded to nearest hz</param> /// <param name="drainer">function which accepts output as produced. if null, act as an ISyncSoundProvider</param> /// <param name="input">source to take input from when output is requested. if null, no autofetching</param> public SpeexResampler(int quality, uint rationum, uint ratioden, uint sratein, uint srateout, Action <short[], int> drainer = null, ISoundProvider input = null) { if (drainer != null && input != null) { throw new ArgumentException("Can't autofetch without being an ISyncSoundProvider?"); } LibSpeexDSP.RESAMPLER_ERR err = LibSpeexDSP.RESAMPLER_ERR.SUCCESS; st = LibSpeexDSP.speex_resampler_init_frac(2, rationum, ratioden, sratein, srateout, quality, ref err); if (st == IntPtr.Zero) { throw new Exception("LibSpeexDSP returned null!"); } CheckError(err); this.drainer = drainer ?? InternalDrain; this.input = input; outbuf = new short[inbuf.Length * ratioden / rationum / 2 * 2 + 128]; }
/// <summary> /// Initializes a new instance of the <see cref="SpeexResampler"/> class /// </summary> /// <param name="quality">0 to 10</param> /// <param name="rationum">numerator of sample rate change ratio (inrate / outrate)</param> /// <param name="ratioden">denominator of sample rate change ratio (inrate / outrate)</param> /// <param name="sratein">sampling rate in, rounded to nearest hz</param> /// <param name="srateout">sampling rate out, rounded to nearest hz</param> /// <param name="drainer">function which accepts output as produced. if null, act as an <seealso cref="ISoundProvider"/></param> /// <param name="input">source to take input from when output is requested. if null, no auto-fetching</param> public SpeexResampler(Quality quality, uint rationum, uint ratioden, uint sratein, uint srateout, Action <short[], int> drainer = null, ISoundProvider input = null) { if (drainer != null && input != null) { throw new ArgumentException($"Can't autofetch without being an {nameof(ISoundProvider)}?"); } LibSpeexDSP.RESAMPLER_ERR err = LibSpeexDSP.RESAMPLER_ERR.SUCCESS; _st = LibSpeexDSP.speex_resampler_init_frac(2, rationum, ratioden, sratein, srateout, quality, ref err); if (_st == IntPtr.Zero) { throw new Exception($"{nameof(LibSpeexDSP)} returned null!"); } CheckError(err); _drainer = drainer ?? InternalDrain; _input = input; _outbuf = new short[(_inbuf.Length * ratioden / rationum / 2 * 2) + 128]; }